summaryrefslogtreecommitdiff
path: root/Tests/AK/TestAnyOf.cpp
diff options
context:
space:
mode:
authorAli Mohammad Pur <ali.mpfard@gmail.com>2021-07-22 19:11:00 +0430
committerAndreas Kling <kling@serenityos.org>2021-07-22 22:56:20 +0200
commitd40d10aae72d1d6d04b818d2173e5c5f5a731384 (patch)
tree765ab8adb42259008759804e30af7c071869d55b /Tests/AK/TestAnyOf.cpp
parentf879e041339ee414ddb9514a0883baa66ce34f6d (diff)
downloadserenity-d40d10aae72d1d6d04b818d2173e5c5f5a731384.zip
AK: Implement {any,all}_of(IterableContainer&&, Predicate)
This is a generally nicer-to-use version of the existing {any,all}_of() that doesn't require the user to explicitly provide two iterators. As a bonus, it also allows arbitrary iterators (as opposed to the hard requirement of providing SimpleIterators in the iterator version).
Diffstat (limited to 'Tests/AK/TestAnyOf.cpp')
-rw-r--r--Tests/AK/TestAnyOf.cpp41
1 files changed, 41 insertions, 0 deletions
diff --git a/Tests/AK/TestAnyOf.cpp b/Tests/AK/TestAnyOf.cpp
index 2fcfaf29e6..5b7400c5d2 100644
--- a/Tests/AK/TestAnyOf.cpp
+++ b/Tests/AK/TestAnyOf.cpp
@@ -21,3 +21,44 @@ TEST_CASE(should_determine_if_predicate_applies_to_any_element_in_container)
EXPECT(any_of(a.begin(), a.end(), [](auto elem) { return elem == 1; }));
EXPECT(!any_of(a.begin(), a.end(), [](auto elem) { return elem == 2; }));
}
+
+TEST_CASE(container_form)
+{
+ constexpr Array a { 10, 20, 30 };
+ static_assert(any_of(a, [](auto elem) { return elem == 10; }));
+ static_assert(any_of(a, [](auto elem) { return elem == 20; }));
+ static_assert(!any_of(a, [](auto elem) { return elem == 40; }));
+
+ EXPECT(any_of(a, [](auto elem) { return elem == 10; }));
+ EXPECT(any_of(a, [](auto elem) { return elem == 20; }));
+ EXPECT(!any_of(a, [](auto elem) { return elem == 40; }));
+
+ Vector b { 10, 20, 30 };
+ EXPECT(any_of(b, [](auto elem) { return elem > 10; }));
+ EXPECT(!any_of(b, [](auto elem) { return elem > 40; }));
+
+ struct ArbitraryIterable {
+ struct ArbitraryIterator {
+ ArbitraryIterator(int v)
+ : value(v)
+ {
+ }
+
+ bool operator==(ArbitraryIterator const&) const = default;
+ int operator*() const { return value; }
+ ArbitraryIterator& operator++()
+ {
+ ++value;
+ return *this;
+ }
+
+ int value;
+ };
+ ArbitraryIterator begin() const { return 0; }
+ ArbitraryIterator end() const { return 20; }
+ };
+
+ ArbitraryIterable c;
+ EXPECT(any_of(c, [](auto elem) { return elem < 20; }));
+ EXPECT(!any_of(c, [](auto elem) { return elem > 31; }));
+}