testEnumIterator.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9#include "squid.h"
10#include "base/EnumIterator.h"
11#include "compat/cppunit.h"
12#include "unitTestMain.h"
13
14#include <cppunit/TestAssert.h>
15
16class TestEnumIterator : public CPPUNIT_NS::TestFixture
17{
26
27protected:
28 void testForwardIter();
29 void testReverseIter();
31 void testRangeFor();
32 void testRangeForRange();
33 void testUnsignedEnum();
34};
35
37
38enum class TestEnum {
39 enumBegin_ = 0,
40 zero = enumBegin_,
41 one,
42 two,
43 three,
44 four,
45 enumEnd_
46};
47
48enum class UnsignedTestEnum : unsigned char {
49 enumBegin_ = 0,
50 zero = enumBegin_,
51 one,
52 two,
53 three,
54 four,
55 enumEnd_
56};
57
58void
60{
62 CPPUNIT_ASSERT(*i == TestEnum::zero);
63 ++i;
64 CPPUNIT_ASSERT(*i == TestEnum::one);
65 ++i;
66 CPPUNIT_ASSERT(*i == TestEnum::two);
67 ++i;
68 CPPUNIT_ASSERT(*i == TestEnum::three);
69 ++i;
70 CPPUNIT_ASSERT(*i == TestEnum::four);
71 ++i;
72 CPPUNIT_ASSERT(i == WholeEnum<TestEnum>().end());
73}
74
75void
77{
79 CPPUNIT_ASSERT(*i == TestEnum::four);
80 ++i;
81 CPPUNIT_ASSERT(*i == TestEnum::three);
82 ++i;
83 CPPUNIT_ASSERT(*i == TestEnum::two);
84 ++i;
85 CPPUNIT_ASSERT(*i == TestEnum::one);
86 ++i;
87 CPPUNIT_ASSERT(*i == TestEnum::zero);
88 ++i;
89 CPPUNIT_ASSERT(i == WholeEnum<TestEnum>().rend());
90}
91
92void
94{
96 CPPUNIT_ASSERT(*i == TestEnum::zero);
97 ++i;
98 CPPUNIT_ASSERT(*i == TestEnum::one);
99 --i;
100 CPPUNIT_ASSERT(*i == TestEnum::zero);
101
102 auto enumBegin=WholeEnum<TestEnum>().begin();
103 auto enumEnd=WholeEnum<TestEnum>().end();
104 i=enumBegin;
105 int count=0;
106 while (i != enumEnd) {
107 ++i;
108 ++count;
109 if (count > 20) // prevent infinite loops in test
110 break;
111 }
112 while (i != enumBegin) {
113 --i;
114 ++count;
115 if (count > 20) // prevent infinite loops in test
116 break;
117 }
118 CPPUNIT_ASSERT_EQUAL(10, count);
119
120 --i; //intentional out-of-bounds
121 CPPUNIT_ASSERT(i != enumBegin);
122 CPPUNIT_ASSERT(*i != TestEnum::zero);
123}
124
125void
127{
128 int j = 0;
129 for (auto e : WholeEnum<TestEnum>()) {
130 (void)e;
131 ++j;
132 if (j > 20) // prevent infinite loops in test
133 break;
134 }
135 CPPUNIT_ASSERT_EQUAL(5,j);
136}
137
138void
140{
141 int j = 0;
142 // free function-based range
143 for (auto e : EnumRange(TestEnum::two, TestEnum::four)) {
144 (void)e;
145 ++j;
146 if (j > 20) // prevent infinite loops in test
147 break;
148 }
149 CPPUNIT_ASSERT_EQUAL(2,j);
150}
151
152void
154{
155 int j = 0;
156 for (auto e = WholeEnum<TestEnum>().rbegin(); e != WholeEnum<TestEnum>().rend(); ++e ) {
157 (void)e;
158 ++j;
159 if (j > 20) // prevent infinite loops in test
160 break;
161 }
162 CPPUNIT_ASSERT_EQUAL(5,j);
163}
164
165int
166main(int argc, char *argv[])
167{
168 return TestProgram().run(argc, argv);
169}
170
EnumRangeT< EnumType > EnumRange(EnumType begin, EnumType one_past_end)
Definition: EnumIterator.h:188
iterator end() const
Definition: EnumIterator.h:164
iterator begin() const
Definition: EnumIterator.h:163
reverse_iterator rbegin() const
Definition: EnumIterator.h:165
reverse_iterator rend() const
Definition: EnumIterator.h:166
CPPUNIT_TEST(testForwardIter)
CPPUNIT_TEST(testRangeFor)
CPPUNIT_TEST(testUnsignedEnum)
CPPUNIT_TEST(testBidirectionalIter)
CPPUNIT_TEST(testRangeForRange)
CPPUNIT_TEST_SUITE(TestEnumIterator)
CPPUNIT_TEST(testReverseIter)
implements test program's main() function while enabling customization
Definition: unitTestMain.h:26
int run(int argc, char *argv[])
Definition: unitTestMain.h:44
int main(int argc, char *argv[])
UnsignedTestEnum
CPPUNIT_TEST_SUITE_REGISTRATION(TestEnumIterator)
TestEnum

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors