testEnumIterator.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 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"
11#include "unitTestMain.h"
12
13#include <cppunit/TestAssert.h>
14
16
17enum class TestEnum {
18 enumBegin_ = 0,
19 zero = enumBegin_,
20 one,
21 two,
22 three,
23 four,
24 enumEnd_
25};
26
27enum class UnsignedTestEnum : unsigned char {
28 enumBegin_ = 0,
29 zero = enumBegin_,
30 one,
31 two,
32 three,
33 four,
34 enumEnd_
35};
36
37void
39{
41 CPPUNIT_ASSERT(*i == TestEnum::zero);
42 ++i;
43 CPPUNIT_ASSERT(*i == TestEnum::one);
44 ++i;
45 CPPUNIT_ASSERT(*i == TestEnum::two);
46 ++i;
47 CPPUNIT_ASSERT(*i == TestEnum::three);
48 ++i;
49 CPPUNIT_ASSERT(*i == TestEnum::four);
50 ++i;
51 CPPUNIT_ASSERT(i == WholeEnum<TestEnum>().end());
52}
53
54void
56{
58 CPPUNIT_ASSERT(*i == TestEnum::four);
59 ++i;
60 CPPUNIT_ASSERT(*i == TestEnum::three);
61 ++i;
62 CPPUNIT_ASSERT(*i == TestEnum::two);
63 ++i;
64 CPPUNIT_ASSERT(*i == TestEnum::one);
65 ++i;
66 CPPUNIT_ASSERT(*i == TestEnum::zero);
67 ++i;
68 CPPUNIT_ASSERT(i == WholeEnum<TestEnum>().rend());
69}
70
71void
73{
75 CPPUNIT_ASSERT(*i == TestEnum::zero);
76 ++i;
77 CPPUNIT_ASSERT(*i == TestEnum::one);
78 --i;
79 CPPUNIT_ASSERT(*i == TestEnum::zero);
80
81 auto enumBegin=WholeEnum<TestEnum>().begin();
82 auto enumEnd=WholeEnum<TestEnum>().end();
83 i=enumBegin;
84 int count=0;
85 while (i != enumEnd) {
86 ++i;
87 ++count;
88 if (count > 20) // prevent infinite loops in test
89 break;
90 }
91 while (i != enumBegin) {
92 --i;
93 ++count;
94 if (count > 20) // prevent infinite loops in test
95 break;
96 }
97 CPPUNIT_ASSERT_EQUAL(10, count);
98
99 --i; //intentional out-of-bounds
100 CPPUNIT_ASSERT(i != enumBegin);
101 CPPUNIT_ASSERT(*i != TestEnum::zero);
102}
103
104void
106{
107 int j = 0;
108 for (auto e : WholeEnum<TestEnum>()) {
109 (void)e;
110 ++j;
111 if (j > 20) // prevent infinite loops in test
112 break;
113 }
114 CPPUNIT_ASSERT_EQUAL(5,j);
115}
116
117void
119{
120 int j = 0;
121 // free function-based range
122 for (auto e : EnumRange(TestEnum::two, TestEnum::four)) {
123 (void)e;
124 ++j;
125 if (j > 20) // prevent infinite loops in test
126 break;
127 }
128 CPPUNIT_ASSERT_EQUAL(2,j);
129}
130
131void
133{
134 int j = 0;
135 for (auto e = WholeEnum<TestEnum>().rbegin(); e != WholeEnum<TestEnum>().rend(); ++e ) {
136 (void)e;
137 ++j;
138 if (j > 20) // prevent infinite loops in test
139 break;
140 }
141 CPPUNIT_ASSERT_EQUAL(5,j);
142}
143
EnumRangeT< EnumType > EnumRange(EnumType begin, EnumType one_past_end)
Definition: EnumIterator.h:192
iterator end() const
Definition: EnumIterator.h:168
iterator begin() const
Definition: EnumIterator.h:167
reverse_iterator rbegin() const
Definition: EnumIterator.h:169
reverse_iterator rend() const
Definition: EnumIterator.h:170
CPPUNIT_TEST_SUITE_REGISTRATION(testEnumIterator)
UnsignedTestEnum
TestEnum

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors