26class EnumerateUnitTest :
public UnitTest
29 EnumerateUnitTest() :
UnitTest (
"Enumerate", UnitTestCategories::containers) {}
33 beginTest (
"enumeration works for bidirectional iterators");
35 const std::list<int> elements { 10, 20, 30, 40, 50 };
36 std::vector<int> counts;
38 for (
const auto pair : enumerate (elements))
39 counts.push_back ((
int) pair.index);
41 expect (counts == std::vector<int> { 0, 1, 2, 3, 4 });
44 beginTest (
"enumeration works for random-access iterators");
46 const std::vector<std::string> strings {
"a",
"bb",
"ccc",
"dddd",
"eeeee" };
48 std::vector<int> counts;
50 for (
const auto [count, element] : enumerate (strings))
51 counts.push_back ((
int) ((
size_t) count + element.size()));
53 expect (counts == std::vector<int> { 1, 3, 5, 7, 9 });
56 beginTest (
"enumeration works for mutable ranges");
58 std::vector<std::string> strings {
"",
"",
"",
"",
"" };
60 for (
const auto [count, element] : enumerate (strings))
61 element = std::to_string (count);
63 expect (strings == std::vector<std::string> {
"0",
"1",
"2",
"3",
"4" });
66 beginTest (
"iterator can be incremented by more than one");
68 std::vector<int> ints (6);
70 const auto enumerated = enumerate (ints);
72 std::vector<int> counts;
74 for (
auto b = enumerated.begin(), e = enumerated.end(); b != e; b += 2)
75 counts.push_back ((
int) (*b).index);
77 expect (counts == std::vector<int> { 0, 2, 4 });
80 beginTest (
"iterator can be started at a non-zero value");
82 const std::vector<int> ints (6);
84 std::vector<int> counts;
86 for (
const auto enumerated : enumerate (ints, 5))
87 counts.push_back ((
int) enumerated.index);
89 expect (counts == std::vector<int> { 5, 6, 7, 8, 9, 10 });
92 beginTest (
"subtracting two EnumerateIterators returns the difference between the base iterators");
94 const std::vector<int> ints (6);
95 const auto enumerated = enumerate (ints);
96 expect ((
int) (enumerated.end() - enumerated.begin()) == (
int) ints.size());
99 beginTest (
"EnumerateIterator can be decremented");
101 const std::vector<int> ints (5);
102 std::vector<int> counts;
104 const auto enumerated = enumerate (std::as_const (ints));
106 for (
auto i = enumerated.end(), b = enumerated.begin(); i != b; --i)
107 counts.push_back ((
int) (*(i - 1)).index);
109 expect (counts == std::vector<int> { -1, -2, -3, -4, -5 });
112 beginTest (
"EnumerateIterator can be compared");
114 const std::vector<int> ints (6);
115 const auto enumerated = enumerate (ints);
116 expect (enumerated.begin() < enumerated.end());
117 expect (enumerated.begin() <= enumerated.end());
118 expect (enumerated.end() > enumerated.begin());
119 expect (enumerated.end() >= enumerated.begin());
124static EnumerateUnitTest enumerateUnitTest;
UnitTest(const String &name, const String &category=String())
void beginTest(const String &testName)
void expect(bool testResult, const String &failureMessage=String())