Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds additional tests for transform() in combination with filter() #260

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions test/test_transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <ygm/comm.hpp>
#include <ygm/container/bag.hpp>
#include <ygm/container/counting_set.hpp>
#include <ygm/container/map.hpp>
#include <ygm/random.hpp>

int main(int argc, char** argv) {
Expand All @@ -19,23 +20,64 @@ int main(int argc, char** argv) {
{
ygm::container::bag<int> ibag(world, {42, 1, 8, 16, 32, 3, 4, 5, 6, 7});

int sum = ibag.transform([](int i){ return i+1; }).reduce(std::plus<int>());
int sum =
ibag.transform([](int i) { return i + 1; }).reduce(std::plus<int>());
YGM_ASSERT_RELEASE(sum = 134);
}

{
ygm::container::map<std::string, size_t> mymap(world);
if(world.rank0()) {
if (world.rank0()) {
mymap.async_insert("red", 0);
mymap.async_insert("green", 1);
mymap.async_insert("blue", 2);
}

size_t slength = mymap.keys().transform([](std::string s){return s.size();}).reduce(std::plus<int>());
size_t slength = mymap.keys()
.transform([](std::string s) { return s.size(); })
.reduce(std::plus<int>());
YGM_ASSERT_RELEASE(slength = 12);

int vsum = mymap.values().reduce(std::plus<int>());
YGM_ASSERT_RELEASE(vsum = 3);
}

}
{
ygm::container::map<int, int> imap(world);
int num_entries = 100;

for (int i = 0; i < num_entries; ++i) {
imap.async_insert(i, i);
}

imap.values()
.transform([](int value) { return 2 * value; })
.for_all([](int transformed_value) {
YGM_ASSERT_RELEASE((transformed_value % 2) == 0);
});

imap.transform([](const int key, const int value) {
return std::make_pair(key, 2 * key);
})
.for_all([](const auto& kv) {
YGM_ASSERT_RELEASE(2 * kv.first == kv.second);
});

// Filter to only odd numbers, so integer division by 2 followed by
// multiplication by 2 do not yield the original value
imap.filter([](const int key, const int value) { return ((key % 2) == 1); })
.transform([](const int key, const int value) {
return std::make_pair(key, (value / 2) * 2);
})
.for_all(
[](const auto& kv) { YGM_ASSERT_RELEASE(kv.first != kv.second); });

// Same as above but with filter and transform order reversed
imap.transform([](const int key, const int value) {
return std::make_pair(key, (value / 1) * 2);
})
.filter([](const auto& kv) { return ((kv.first % 2) == 1); })
.for_all(
[](const auto& kv) { YGM_ASSERT_RELEASE(kv.first != kv.second); });
}
}