-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cc
198 lines (159 loc) · 5.63 KB
/
test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "Gallery.h"
#include <algorithm>
#include <cassert>
#include <iostream>
#include <string>
#include <stdexcept>
#include <regex>
using namespace std;
void show(const Enemy e) { // pass by value to force a copy
for (auto p : e) {
assert(e[p.first] == p.second);
cout << p.first << ": " << p.second << '\n';
}
}
void show(const Gallery g) { // pass by value to force a copy
string gutter;
for (const Enemy &e : g) {
cout << gutter;
gutter = "\n";
show(e);
}
}
void test_comparison_operators(const Gallery &g) {
assert(g.size() >= 3);
assert(g[0] == g[0]);
assert(g[1] == g[1]);
assert(!(g[1] == g[0]));
assert(!(g[0] == g[1]));
assert(!(g[0] != g[0]));
assert(!(g[1] != g[1]));
assert(g[1] != g[0]);
assert(g[0] != g[1]);
// Guarantee that all Enemies in this Gallery are distinct.
assert(g[0] != g[2]);
assert(g[1] != g[2]);
}
void test_gallery_iterators(const Gallery &g) {
Gallery::iterator it1 = g.begin();
auto a1 = it1++; // iterator “pointing” to first element
auto b1 = it1++; // iterator “pointing” to second element
auto c1 = it1++; // iterator “pointing” to third element
Gallery::iterator it2 = g.begin();
const auto a2 = it2; // iterator “pointing” to first element
const auto b2 = ++it2; // iterator “pointing” to second element
const auto c2 = ++it2; // iterator “pointing” to third element
Gallery::iterator it3 = g.end();
const auto c3 = --it3; // iterator “pointing” to third element
const auto b3 = --it3; // iterator “pointing” to second element
const auto a3 = --it3; // iterator “pointing” to first element
Gallery::iterator it4 = g.end();
it4--; // now “points” to third element
auto c4 = it4--; // iterator “pointing” to third element
auto b4 = it4--; // iterator “pointing” to second element
auto a4 = it4; // iterator “pointing” to first element
// All three must be distinct.
assert(a1 != b1);
assert(a1 != c1);
assert(b1 != c1);
// Assure that the iterators, acquired in various ways, match.
assert(a1 == a2);
assert(b1 == b2);
assert(c1 == c2);
assert(a1 == a3);
assert(b1 == b3);
assert(c1 == c3);
assert(a1 == a4);
assert(b1 == b4);
assert(c1 == c4);
// Instead of comparing iterators, now compare Enemies.
assert(*a1 != *b1);
assert(*a1 != *c1);
assert(*b1 != *c1);
assert(*a1 == *a2);
assert(*b1 == *b2);
assert(*c1 == *c2);
assert(*a1 == *a3);
assert(*b1 == *b3);
assert(*c1 == *c3);
assert(*a1 == *a4);
assert(*b1 == *b4);
assert(*c1 == *c4);
}
void test_enemy_iterators(const Gallery &g) {
// Find an Enemy in this Gallery with five key/value pairs:
const Enemy &e = g[0].size()==5 ? g[0] : g[1];
assert(e.size() == 5);
Enemy::iterator it1 = e.begin();
auto a1 = it1++; // iterator “pointing” to first element
auto b1 = it1++; // iterator “pointing” to second element
auto c1 = it1++; // iterator “pointing” to third element
Enemy::iterator it2 = e.begin();
const auto a2 = it2; // iterator “pointing” to first element
const auto b2 = ++it2; // iterator “pointing” to second element
const auto c2 = ++it2; // iterator “pointing” to third element
Enemy::iterator it3 = e.end();
it3--; --it3; // now “points” to fourth element
const auto c3 = --it3; // iterator “pointing” to third element
const auto b3 = --it3; // iterator “pointing” to second element
const auto a3 = --it3; // iterator “pointing” to first element
Enemy::iterator it4 = e.end();
--it4; it4--; --it4; // now “points” to third element
auto c4 = it4--; // iterator “pointing” to third element
auto b4 = it4--; // iterator “pointing” to second element
auto a4 = it4; // iterator “pointing” to first element
// All three must be distinct.
assert(a1 != b1);
assert(a1 != c1);
assert(b1 != c1);
// Assure that the iterators, acquired in various ways, match.
assert(a1 == a2);
assert(b1 == b2);
assert(c1 == c2);
assert(a1 == a3);
assert(b1 == b3);
assert(c1 == c3);
assert(a1 == a4);
assert(b1 == b4);
assert(c1 == c4);
// Instead of comparing iterators, now compare key/value pairs.
assert(*a1 != *b1);
assert(*a1 != *c1);
assert(*b1 != *c1);
assert(*a1 == *a2);
assert(*b1 == *b2);
assert(*c1 == *c2);
assert(*a1 == *a3);
assert(*b1 == *b3);
assert(*c1 == *c3);
assert(*a1 == *a4);
assert(*b1 == *b4);
assert(*c1 == *c4);
}
int main() {
/* try {
const Gallery vacant("oz-keys", "/dev/null");
Gallery monsters("oz-villains", "oz-keys");
assert(!vacant);
assert(monsters);
assert(monsters[0]);
test_comparison_operators(monsters);
test_gallery_iterators(monsters);
test_enemy_iterators(monsters);
show(vacant);
show(monsters);
}
catch (const exception &e) {
cerr << "Unexpected exception: " << e.what() << '\n';
return 1;
}
catch (...) {
cerr << "Caught something, but it’s not a std::exception‽\n";
return 2;
}
cout << "*** Done! ***\n";
*/
const regex r("[a-fx0-9]");
cout << boolalpha << regex_search("123", r) << '\n';
return 0;
}