Serenity Operating System
1/*
2 * Copyright (c) 2020, the SerenityOS developers.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibTest/TestCase.h>
8
9#include <AK/AllOf.h>
10#include <AK/Array.h>
11#include <AK/Vector.h>
12
13TEST_CASE(should_determine_if_predicate_applies_to_all_elements_in_container)
14{
15 constexpr Array<int, 10> a {};
16
17 static_assert(all_of(a.begin(), a.end(), [](auto elem) { return elem == 0; }));
18 static_assert(!all_of(a.begin(), a.end(), [](auto elem) { return elem == 1; }));
19
20 EXPECT(all_of(a.begin(), a.end(), [](auto elem) { return elem == 0; }));
21 EXPECT(!all_of(a.begin(), a.end(), [](auto elem) { return elem == 1; }));
22}
23
24TEST_CASE(container_form)
25{
26 constexpr Array a { 10, 20, 30 };
27 static_assert(all_of(a, [](auto elem) { return elem > 0; }));
28 static_assert(!all_of(a, [](auto elem) { return elem > 10; }));
29 EXPECT(all_of(a, [](auto elem) { return elem > 0; }));
30 EXPECT(!all_of(a, [](auto elem) { return elem > 10; }));
31
32 Vector b { 10, 20, 30 };
33 EXPECT(all_of(b, [](auto elem) { return elem > 0; }));
34 EXPECT(!all_of(b, [](auto elem) { return elem > 10; }));
35
36 struct ArbitraryIterable {
37 struct ArbitraryIterator {
38 ArbitraryIterator(int v)
39 : value(v)
40 {
41 }
42
43 bool operator==(ArbitraryIterator const&) const = default;
44 int operator*() const { return value; }
45 ArbitraryIterator& operator++()
46 {
47 ++value;
48 return *this;
49 }
50
51 int value;
52 };
53 ArbitraryIterator begin() const { return 0; }
54 ArbitraryIterator end() const { return 20; }
55 };
56
57 ArbitraryIterable c;
58 EXPECT(all_of(c, [](auto elem) { return elem < 20; }));
59 EXPECT(!all_of(c, [](auto elem) { return elem > 10; }));
60}