diff --git a/tests/coveo/linq/linq_tests.cpp b/tests/coveo/linq/linq_tests.cpp index 26d3697..439a568 100644 --- a/tests/coveo/linq/linq_tests.cpp +++ b/tests/coveo/linq/linq_tests.cpp @@ -246,6 +246,21 @@ void linq_tests() COVEO_ASSERT(all.has_fast_size()); COVEO_ASSERT(all.size() == ab.size()); } + { + std::vector v1 = { 42, 23 }; + std::vector v2 = { 66, 67 }; + const std::vector expected = { 43, 24, 67, 68, 12, 8 }; + + using namespace coveo::linq; + auto all = from(v1) + | concat(v2) + | concat(std::vector{ 11, 7 }); + for (int& i : all) { + ++i; + } + COVEO_ASSERT(detail::equal(std::begin(all), std::end(all), + std::begin(expected), std::end(expected))); + } // contains { @@ -310,6 +325,19 @@ void linq_tests() COVEO_ASSERT(!no_dup_2.has_fast_size()); COVEO_ASSERT(no_dup_2.size() == v_no_dup.size()); } + { + std::vector v = { 42, 23, 66, 42, 67, 66, 23, 11 }; + const std::vector expected = { 84, 46, 132, 42, 134, 66, 23, 22 }; + + using namespace coveo::linq; + auto no_dup = from(v) + | distinct(); + for (int& i : no_dup) { + i *= 2; + } + COVEO_ASSERT(detail::equal(std::begin(v), std::end(v), + std::begin(expected), std::end(expected))); + } // element_at { @@ -319,6 +347,13 @@ void linq_tests() COVEO_ASSERT((from(v) | element_at(1)) == 23); COVEO_ASSERT_THROW(from(v) | element_at(3)); } + { + std::vector v = { 42, 23, 66 }; + + using namespace coveo::linq; + (from(v) | element_at(1)) *= 2; + COVEO_ASSERT(v[1] == 46); + } // element_at_or_default { @@ -343,6 +378,20 @@ void linq_tests() COVEO_ASSERT(!lres.has_fast_size()); COVEO_ASSERT(lres.size() == res.size()); } + { + std::vector v = { 42, 23, 66, 42, 23, 67, 11, 66, 7 }; + const std::vector not_v = { 66, 23, 11 }; + const std::vector expected = { 84, 23, 66, 84, 23, 134, 11, 66, 14 }; + + using namespace coveo::linq; + auto lres = from(v) + | except(not_v); + for (int& i : lres) { + i *= 2; + } + COVEO_ASSERT(detail::equal(std::begin(v), std::end(v), + std::begin(expected), std::end(expected))); + } // first { @@ -354,6 +403,16 @@ void linq_tests() COVEO_ASSERT((from(v) | first([](int i) { return i % 2 != 0; })) == 23); } + { + std::vector v = { 42, 23, 66 }; + const std::vector expected = { 43, 22, 66 }; + + using namespace coveo::linq; + (from(v) | first([](int i) { return i % 2 != 0; })) -= 1; + (from(v) | first()) += 1; + COVEO_ASSERT(detail::equal(std::begin(v), std::end(v), + std::begin(expected), std::end(expected))); + } // first_or_default { @@ -635,6 +694,20 @@ void linq_tests() COVEO_ASSERT(!intersection.has_fast_size()); COVEO_ASSERT(intersection.size() == expected.size()); } + { + std::vector v1 = { 42, 23, 66, 11 }; + const std::vector v2 = { 11, 7, 67, 42, 22 }; + const std::vector expected = { 84, 23, 66, 22 }; + + using namespace coveo::linq; + auto intersection = from(v1) + | intersect(v2); + for (int& i : intersection) { + i *= 2; + } + COVEO_ASSERT(detail::equal(std::begin(v1), std::end(v1), + std::begin(expected), std::end(expected))); + } // join { @@ -706,6 +779,16 @@ void linq_tests() COVEO_ASSERT((from(v) | last([](int i) { return i % 2 != 0; })) == 11); } + { + std::vector v = { 42, 23, 66, 11, 24 }; + const std::vector expected = { 42, 23, 66, 10, 25 }; + + using namespace coveo::linq; + (from(v) | last([](int i) { return i % 2 != 0; })) -= 1; + (from(v) | last()) += 1; + COVEO_ASSERT(detail::equal(std::begin(v), std::end(v), + std::begin(expected), std::end(expected))); + } // last_or_default { @@ -813,7 +896,7 @@ void linq_tests() using namespace coveo::linq; auto seq = from(v) | order_by([](const std::string& a) { return a.size(); }) - | then_by([](const std::string& a) { return a; }); // #clp TODO validate that this calls the proper constructor of order_by_impl + | then_by([](const std::string& a) { return a; }); COVEO_ASSERT(detail::equal(std::begin(seq), std::end(seq), std::begin(expected), std::end(expected))); COVEO_ASSERT(seq.has_fast_size()); @@ -994,6 +1077,25 @@ void linq_tests() COVEO_ASSERT((from(one_42_v) | single(equal_to_42)) == 42); COVEO_ASSERT_THROW(from(two_42_v) | single(equal_to_42)); } + { + std::vector v = { 42 }; + const std::vector expected = { 43 }; + + using namespace coveo::linq; + (from(v) | single()) += 1; + COVEO_ASSERT(detail::equal(std::begin(v), std::end(v), + std::begin(expected), std::end(expected))); + } + { + std::vector v = { 23, 42, 66 }; + const std::vector expected = { 23, 84, 66 }; + auto equal_to_42 = std::bind(std::equal_to(), std::placeholders::_1, 42); + + using namespace coveo::linq; + (from(v) | single(equal_to_42)) *= 2; + COVEO_ASSERT(detail::equal(std::begin(v), std::end(v), + std::begin(expected), std::end(expected))); + } // single_or_default { @@ -1040,8 +1142,21 @@ void linq_tests() COVEO_ASSERT(e_skip_3.size() == last_two.size()); COVEO_ASSERT(e_skip_9.size() == none.size()); } + { + std::vector v = { 42, 23, 66, 11, 24 }; + const std::vector expected = { 42, 23, 66, 22, 48 }; + + using namespace coveo::linq; + auto e_skip_3 = from(v) + | skip(3); + for (int& i : e_skip_3) { + i *= 2; + } + COVEO_ASSERT(detail::equal(std::begin(v), std::end(v), + std::begin(expected), std::end(expected))); + } - // skip_while/skip_while_with_index + // skip_while { const std::vector v = { 42, 23, 66, 11, 24 }; const std::vector v_66_and_up = { 66, 11, 24 }; @@ -1061,6 +1176,21 @@ void linq_tests() COVEO_ASSERT(e_after_60.size() == v_66_and_up.size()); COVEO_ASSERT(e_after_90.size() == none.size()); } + { + std::vector v = { 42, 23, 66, 11, 24 }; + const std::vector expected = { 42, 23, 132, 22, 48 }; + + using namespace coveo::linq; + auto e_after_60 = from(v) + | skip_while([](int i) { return i < 60; }); + for (int& i : e_after_60) { + i *= 2; + } + COVEO_ASSERT(detail::equal(std::begin(v), std::end(v), + std::begin(expected), std::end(expected))); + } + + // skip_while_with_index { const detail::noref_vector v = { 42, 23, 66, 11, 24 }; const std::vector v_66_and_up = { 66, 11, 24 }; @@ -1080,6 +1210,19 @@ void linq_tests() COVEO_ASSERT(e_after_60.size() == v_66_and_up.size()); COVEO_ASSERT(e_after_90.size() == v_24_and_up.size()); } + { + std::vector v = { 42, 23, 66, 11, 24 }; + const std::vector expected = { 42, 23, 66, 11, 48 }; + + using namespace coveo::linq; + auto e_after_60 = from(v) + | skip_while_with_index([](int, std::size_t n) { return n < 4; }); + for (int& i : e_after_60) { + i *= 2; + } + COVEO_ASSERT(detail::equal(std::begin(v), std::end(v), + std::begin(expected), std::end(expected))); + } // sum { @@ -1122,8 +1265,21 @@ void linq_tests() COVEO_ASSERT(e_take_3.size() == first_three.size()); COVEO_ASSERT(e_take_0.size() == none.size()); } + { + std::vector v = { 42, 23, 66, 11, 24 }; + const std::vector expected = { 84, 46, 132, 11, 24 }; + + using namespace coveo::linq; + auto e_take_3 = from(v) + | take(3); + for (int& i : e_take_3) { + i *= 2; + } + COVEO_ASSERT(detail::equal(std::begin(v), std::end(v), + std::begin(expected), std::end(expected))); + } - // take_while/take_while_with_index + // take_while { const std::vector v = { 42, 23, 66, 11, 24 }; const std::vector v_before_66 = { 42, 23 }; @@ -1142,6 +1298,21 @@ void linq_tests() COVEO_ASSERT(e_before_60.size() == v_before_66.size()); COVEO_ASSERT(e_before_90.size() == v.size()); } + { + std::vector v = { 42, 23, 66, 11, 24 }; + const std::vector expected = { 84, 46, 66, 11, 24 }; + + using namespace coveo::linq; + auto e_before_60 = from(v) + | take_while([](int i) { return i < 60; }); + for (int& i : e_before_60) { + i *= 2; + } + COVEO_ASSERT(detail::equal(std::begin(v), std::end(v), + std::begin(expected), std::end(expected))); + } + + // take_while_with_index { const detail::noref_vector v = { 42, 23, 66, 11, 24 }; const std::vector v_before_66 = { 42, 23 }; @@ -1161,6 +1332,19 @@ void linq_tests() COVEO_ASSERT(e_before_60.size() == v_before_66.size()); COVEO_ASSERT(e_before_90.size() == v_before_5th.size()); } + { + std::vector v = { 42, 23, 66, 11, 24 }; + const std::vector expected = { 84, 46, 132, 22, 24 }; + + using namespace coveo::linq; + auto e_before_60 = from(v) + | take_while_with_index([](int, std::size_t n) { return n < 4; }); + for (int& i : e_before_60) { + i *= 2; + } + COVEO_ASSERT(detail::equal(std::begin(v), std::end(v), + std::begin(expected), std::end(expected))); + } // to/to_vector/to_associative/to_map { @@ -1269,8 +1453,25 @@ void linq_tests() COVEO_ASSERT(!union2.has_fast_size()); COVEO_ASSERT(union2.size() == v_union.size()); } + { + std::vector v1 = { 42, 23, 66 }; + std::vector v2 = { 11, 7, 23, 42, 67 }; + const std::vector expected1 = { 84, 46, 132 }; + const std::vector expected2 = { 22, 14, 23, 42, 134 }; - // where/where_with_index + using namespace coveo::linq; + auto union1 = from(v1) + | union_with(v2); + for (int& i : union1) { + i *= 2; + } + COVEO_ASSERT(detail::equal(std::begin(v1), std::end(v1), + std::begin(expected1), std::end(expected1))); + COVEO_ASSERT(detail::equal(std::begin(v2), std::end(v2), + std::begin(expected2), std::end(expected2))); + } + + // where { const detail::noref_vector v = { 42, 23, 66, 11, 7, 24 }; const std::vector expected_odd = { 23, 11, 7 }; @@ -1293,6 +1494,22 @@ void linq_tests() COVEO_ASSERT(!e2.has_fast_size()); COVEO_ASSERT(e2.size() == expected_div_3.size()); } + { + std::vector v = { 42, 23, 66, 11, 7, 24 }; + const std::vector expected = { 42, 46, 66, 22, 14, 24 }; + auto is_odd = [](int i) { return i % 2 != 0; }; + + using namespace coveo::linq; + auto e = from(v) + | where(is_odd); + for (int& i : e) { + i *= 2; + } + COVEO_ASSERT(detail::equal(std::begin(v), std::end(v), + std::begin(expected), std::end(expected))); + } + + // where_with_index { const std::vector v = { 42, 23, 66, 11, 7, 24 }; const std::vector expected_odd_idx = { 23, 11, 24 }; @@ -1306,6 +1523,20 @@ void linq_tests() COVEO_ASSERT(!e.has_fast_size()); COVEO_ASSERT(e.size() == expected_odd_idx.size()); } + { + std::vector v = { 42, 23, 66, 11, 7, 24 }; + const std::vector expected = { 42, 46, 66, 22, 7, 48 }; + auto is_odd_idx = [](int, std::size_t idx) { return idx % 2 != 0; }; + + using namespace coveo::linq; + auto e = from(v) + | where_with_index(is_odd_idx); + for (int& i : e) { + i *= 2; + } + COVEO_ASSERT(detail::equal(std::begin(v), std::end(v), + std::begin(expected), std::end(expected))); + } // zip {