#include "gtest/gtest.h" #include "common/utils/ContainerUtils.hh" #include #include #include #include #include #include #include bool is_even(int i) { return i % 2 == 0; } TEST(EraseIf, Map) { std::map m = { {1, "one"}, {2, "two"}, {3, "three"}, {4, "four"} }; auto m2 = m; // copy std::map expected = {{1, "one"}, {3, "three"}}; eos::common::erase_if(m, [](const auto & p) { return is_even(p.first); }); ASSERT_EQ(expected, m); eos::common::erase_if(m2, [](const auto & p) { return !is_even(p.first); }); std::map expected2 = {{2, "two"}, {4, "four"}}; ASSERT_EQ(expected2, m2); } TEST(EraseIf, UnorderedMap) { std::unordered_map m = { {1, "one"}, {2, "two"}, {3, "three"}, {4, "four"} }; auto m2 = m; // copy std::unordered_map expected = {{1, "one"}, {3, "three"}}; eos::common::erase_if(m, [](const auto & p) { return is_even(p.first); }); ASSERT_EQ(expected, m); eos::common::erase_if(m2, [](const auto & p) { return !is_even(p.first); }); std::unordered_map expected2 = {{2, "two"}, {4, "four"}}; ASSERT_EQ(expected2, m2); } TEST(EraseIf, Set) { std::set s = {1, 2, 3, 4}; eos::common::erase_if(s, is_even); //s.erase(std::remove_if(s.begin(),s.end(),is_even)); std::set expected = {1, 3}; ASSERT_EQ(expected, s); } TEST(EraseIf, UnorderedSet) { std::unordered_set s = {1, 2, 3, 4}; eos::common::erase_if(s, is_even); //s.erase(std::remove_if(s.begin(),s.end(),is_even)); std::unordered_set expected = {1, 3}; ASSERT_EQ(expected, s); } TEST(ContainerUtils, get_msb) { ASSERT_EQ(eos::common::get_msb(1),0); ASSERT_EQ(eos::common::get_msb(2),1); ASSERT_EQ(eos::common::get_msb(3),1); ASSERT_EQ(eos::common::get_msb(4),2); ASSERT_EQ(eos::common::get_msb(1023),9); ASSERT_EQ(eos::common::get_msb(1024),10); ASSERT_EQ(eos::common::get_msb(0xFFFFFFFFFFFFFFFF),63); ASSERT_EQ(eos::common::get_msb(1UL<<32),32); } TEST(ContainerUtils, clamp_index) { ASSERT_EQ(eos::common::clamp_index(4,10), 4); ASSERT_EQ(eos::common::clamp_index(10,10), 0); ASSERT_EQ(eos::common::clamp_index(24,8), 0); ASSERT_EQ(eos::common::clamp_index(24,16), 8); ASSERT_EQ(eos::common::clamp_index(25,8), 1); ASSERT_EQ(eos::common::clamp_index(1ULL<<32, 0xFFFFFFFF), 1); ASSERT_EQ(eos::common::clamp_index(1ULL<<48, 1ULL<<32), 0); ASSERT_EQ(eos::common::clamp_index((1ULL<<32) + (1UL<<16), 1ULL<<32), 1UL<<16); } using eos::common::pickIndexRR; TEST(pickIndexRR, list) { using Cont = std::list; Cont C {1, 2, 3}; // repeat this 4 times Cont expected { 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3 }; Cont actual; for (int i = 0; i < 12; ++i) { actual.emplace_back(pickIndexRR(C, i)); } ASSERT_EQ(expected, actual); Cont C2{1}; for (int i = 0; i < 12; ++i) { EXPECT_EQ(pickIndexRR(C2, i), 1); } } TEST(pickIndexRR, exception) { std::list C; EXPECT_THROW(pickIndexRR(C, 0), std::out_of_range); std::vector V{}; EXPECT_THROW(pickIndexRR(V, 0), std::out_of_range); std::vector v(1); ASSERT_EQ(v.size(), 1); EXPECT_NO_THROW(pickIndexRR(v, 1)); EXPECT_EQ(pickIndexRR(v, 2), 0); } TEST(pickIndexRR, Set) { using Cont = std::set; Cont C {1, 2, 3}; // repeat this 4 times std::vector expected { 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3 }; std::vector actual; for (int i = 0; i < 12; ++i) { actual.emplace_back(pickIndexRR(C, i)); } ASSERT_EQ(expected, actual); } TEST(pickIndexRR, UnorderedSet) { using Cont = std::unordered_set; Cont C {1, 2, 3}; std::vector base; base.insert(base.end(), C.begin(), C.end()); ASSERT_EQ(base.size(), 3); ASSERT_EQ(C.size(), 3); // Copy contents back to another hash set to check that we are equal ie // base == C Cont base_set; for (const auto& item : base) { base_set.emplace(item); } ASSERT_EQ(C, base_set); // repeat this 4 times for our good ol RR std::vector expected; for (int i = 0; i < 4; i++) { expected.insert(std::end(expected), std::begin(base), std::end(base)); } std::vector actual; for (int i = 0; i < 12; ++i) { actual.emplace_back(pickIndexRR(C, i)); } ASSERT_EQ(expected, actual); } // TESTING for remove_if TEST(StdEraseIf, vector) { std::vector v = {1, 2, 3, 4}; //eos::common::erase_if(v, is_even); will not compile v.erase(std::remove_if(v.begin(), v.end(), is_even)); std::vector expected = {1, 3, 4}; // remove_if only does [first, last); ASSERT_EQ(expected, v); } TEST(splice, simple_vector_move_append) { std::vector v {1, 2, 3, 4}; std::vector v2 {5, 6, 7}; eos::common::splice(v, std::move(v2)); std::vector expected {1, 2, 3, 4, 5, 6, 7}; ASSERT_EQ(expected, v); } TEST(ContainerUtils, next_power2) { ASSERT_EQ(eos::common::next_power2(0), 1); ASSERT_EQ(eos::common::next_power2(1), 1); ASSERT_EQ(eos::common::next_power2(2), 2); ASSERT_EQ(eos::common::next_power2(3), 4); ASSERT_EQ(eos::common::next_power2(4), 4); ASSERT_EQ(eos::common::next_power2(5), 8); ASSERT_EQ(eos::common::next_power2(9), 16); ASSERT_EQ(eos::common::next_power2(1025), 2048); ASSERT_EQ(eos::common::next_power2(0xFFFFFFFF), 0x100000000); }