From 2c798f45d90ea55bd9be39e6f7934ba7d18eddab Mon Sep 17 00:00:00 2001 From: Trevor Steil Date: Fri, 13 Sep 2024 09:50:09 -0700 Subject: [PATCH] Adds additional tests for transform() in combination with filter() --- test/test_transform.cpp | 50 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/test/test_transform.cpp b/test/test_transform.cpp index 607916d9..1052c28c 100644 --- a/test/test_transform.cpp +++ b/test/test_transform.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include int main(int argc, char** argv) { @@ -19,23 +20,64 @@ int main(int argc, char** argv) { { ygm::container::bag 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 sum = + ibag.transform([](int i) { return i + 1; }).reduce(std::plus()); YGM_ASSERT_RELEASE(sum = 134); } { ygm::container::map 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()); + size_t slength = mymap.keys() + .transform([](std::string s) { return s.size(); }) + .reduce(std::plus()); YGM_ASSERT_RELEASE(slength = 12); int vsum = mymap.values().reduce(std::plus()); YGM_ASSERT_RELEASE(vsum = 3); } -} \ No newline at end of file + { + ygm::container::map 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); }); + } +}