From 6c1626e83b0c239470d31b8e3f715dad1f8697b2 Mon Sep 17 00:00:00 2001 From: Martin Janiczek Date: Tue, 26 Sep 2023 20:45:57 +0200 Subject: [PATCH] AK: Add tests for AllOf and AnyOf --- Tests/AK/TestAllOf.cpp | 28 ++++++++++++++++++++++++++++ Tests/AK/TestAnyOf.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/Tests/AK/TestAllOf.cpp b/Tests/AK/TestAllOf.cpp index 1dce469e2d..adfae35fcf 100644 --- a/Tests/AK/TestAllOf.cpp +++ b/Tests/AK/TestAllOf.cpp @@ -10,6 +10,34 @@ #include #include +using namespace Test::Randomized; + +TEST_CASE(vacuous_truth) +{ + constexpr Array a {}; + static_assert(all_of(a.begin(), a.end(), [](auto) { return false; })); + EXPECT(all_of(a.begin(), a.end(), [](auto) { return false; })); +} + +TEST_CASE(all_but_one_false) +{ + constexpr Array a { 0, 1, 2, 3, 4 }; + static_assert(!all_of(a.begin(), a.end(), [](auto n) { return n != 3; })); + EXPECT(!all_of(a.begin(), a.end(), [](auto n) { return n != 3; })); +} + +RANDOMIZED_TEST_CASE(trivial_all_true) +{ + GEN(vec, Gen::vector(0, 10, []() { return Gen::unsigned_int(); })); + EXPECT(all_of(vec.begin(), vec.end(), [](auto) { return true; })); +} + +RANDOMIZED_TEST_CASE(trivial_all_false) +{ + GEN(vec, Gen::vector(1, 10, []() { return Gen::unsigned_int(); })); + EXPECT(!all_of(vec.begin(), vec.end(), [](auto) { return false; })); +} + TEST_CASE(should_determine_if_predicate_applies_to_all_elements_in_container) { constexpr Array a {}; diff --git a/Tests/AK/TestAnyOf.cpp b/Tests/AK/TestAnyOf.cpp index 7be265fc0b..f8a9c04254 100644 --- a/Tests/AK/TestAnyOf.cpp +++ b/Tests/AK/TestAnyOf.cpp @@ -10,6 +10,34 @@ #include #include +using namespace Test::Randomized; + +TEST_CASE(vacuous_truth) +{ + constexpr Array a {}; + static_assert(!any_of(a.begin(), a.end(), [](auto) { return true; })); + EXPECT(!any_of(a.begin(), a.end(), [](auto) { return true; })); +} + +TEST_CASE(all_false) +{ + constexpr Array a { 0, 1, 2, 3, 4 }; + static_assert(!any_of(a.begin(), a.end(), [](auto n) { return n > 10; })); + EXPECT(!any_of(a.begin(), a.end(), [](auto n) { return n > 10; })); +} + +RANDOMIZED_TEST_CASE(trivial_all_true) +{ + GEN(vec, Gen::vector(1, 10, []() { return Gen::unsigned_int(); })); + EXPECT(any_of(vec.begin(), vec.end(), [](auto) { return true; })); +} + +RANDOMIZED_TEST_CASE(trivial_all_false) +{ + GEN(vec, Gen::vector(0, 10, []() { return Gen::unsigned_int(); })); + EXPECT(!any_of(vec.begin(), vec.end(), [](auto) { return false; })); +} + TEST_CASE(should_determine_if_predicate_applies_to_any_element_in_container) { constexpr Array a { 1 };