Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
0.inputiter.cpp
Go to the documentation of this file.
1
29#include <setjmp.h>
30#include <signal.h>
31#include <stdio.h>
32
33#ifdef _MSC_VER
34#include <crtdbg.h> // for _CrtSetReportMode()
35#endif
36
37#include <rw_alg_test.h> // for InputIter
38#include <rw_value.h> // for UserClass
39#include <rw_driver.h> // for rw_test(), ...
40
41
43int exit_status /* = 0 */;
44
45extern "C" {
46
47int line;
48int fail;
49
50jmp_buf env;
51
52void handle_ABRT (int)
53{
54 fail = 0;
55
56 longjmp (env, 1);
57}
58
59}
60
61#define FAIL(code) \
62 fail = line = __LINE__; \
63 signal (SIGABRT, handle_ABRT); \
64 if (0 == setjmp (env)) { \
65 code; \
66 exit_status = 1; \
67 rw_assert (0, 0, line, "expected assertion"); \
68 } \
69 else \
70 (void)0
71
72#define PASS(code) \
73 fail = -1; line = __LINE__; \
74 signal (SIGABRT, handle_ABRT); \
75 if (0 == setjmp (env)) \
76 code; \
77 else if (fail != line) { \
78 exit_status = 1; \
79 rw_assert (0, 0, line, "unexpected assertion"); \
80 } (void)0
81
82
84
85// FIXME: implement an easy way to print out an array of UserClass
86#define X2STR(ign1, ign2) "***NOT IMPLEMENTED ***"
87
88
90static void
91test_0 ()
92{
94
95 Iterator end0 = make_iter (x + 3, x + 0, x + 3, end0);
96 Iterator end1 = make_iter (x + 3, x + 0, x + 3, end1);
97
98 bool equal;
99
100 // end iterator must compare equal to itself
101 PASS (equal = end0 == end0);
102
103 rw_assert (equal, 0, __LINE__,
104 "InputIter<UserClass> end iterator unexpectedly "
105 "not equal to self: %p == %p", end0.cur_, end0.cur_);
106
107 PASS (equal = end0 != end0);
108
109 rw_assert (!equal, 0, __LINE__,
110 "InputIter<UserClass> end iterator unexpectedly "
111 "not equal to self: %p == %p", end0.cur_, end0.cur_);
112
113 // end iterator must compare equal to another
114 PASS (equal = end0 == end1);
115
116 rw_assert (equal, 0, __LINE__,
117 "InputIter<UserClass> end iterator unexpectedly "
118 "not equal to another: %p == %p", end0.cur_, end1.cur_);
119
120 PASS (equal = end0 != end1);
121
122 rw_assert (!equal, 0, __LINE__,
123 "InputIter<UserClass> end iterator unexpectedly "
124 "not equal to another: %p == %p", end0.cur_, end1.cur_);
125
126 // cannot increment the end iterator
127 FAIL (++end0);
128 FAIL (end0++);
129 FAIL (++end1);
130 FAIL (end1++);
131
132 // cannot dereference the end iterator
133 FAIL (x [0] = *end0);
134 FAIL (x [0] = *end1);
135
136 FAIL (equal = int ('a') == end0->data_.val_);
137 FAIL (equal = int ('a') == end1->data_.val_);
138
139 delete[] x;
140}
141
142
144static void
145test_1 ()
146{
147 UserClass *x = UserClass::from_char ("abcdef");
148 UserClass *y = UserClass::from_char ("ABCDEF");
149
150 const Iterator end = make_iter (x + 6, x + 0, x + 6, end);
151 Iterator it = make_iter (x + 0, x + 0, x + 6, it);
152
153 bool equal;
154
155 // non-end iterator must compare unequal to the end iterator
156 PASS (equal = it == end);
157
158 rw_assert (!equal, 0, __LINE__,
159 "InputIter<UserClass> unexpectedly equal to end: "
160 "%p == %p", it.cur_, end.cur_);
161
162 PASS (y [0] = *it);
163 PASS (++it);
164 PASS (equal = it == end);
165
166 rw_assert (!equal, 0, __LINE__,
167 "InputIter<UserClass> unexpectedly equal to end: "
168 "%p == %p", it.cur_, end.cur_);
169
170 PASS (y [1] = *it);
171 PASS (++it);
172
173 PASS (equal = it == it);
174
175 rw_assert (equal, 0, __LINE__,
176 "InputIter<UserClass> unexpectedly equal to self: "
177 "%p == %p", it.cur_, it.cur_);
178
179 PASS (y [2] = *it);
180 PASS (++it);
181 PASS (y [3] = *it);
182 PASS (++it);
183 PASS (y [4] = *it);
184 PASS (++it);
185 PASS (y [5] = *it);
186 PASS (++it);
187
188 PASS (equal = it == end);
189
190 rw_assert (equal, 0, __LINE__,
191 "InputIter<UserClass> unexpectedly not qual to end: "
192 "%p != %p (diff = %d)",
193 it.cur_, end.cur_, end.cur_ - it.cur_);
194
195 rw_assert (0 == UserClass::compare (x, y, 6), 0, __LINE__,
196 "InputIter<UserClass> data mismatch: %s != %s",
197 X2STR (x, 6), X2STR (y, 6));
198
199 delete[] x;
200 delete[] y;
201}
202
203
205static void
206test_2 ()
207{
208 UserClass *x = UserClass::from_char ("abcdef");
209 UserClass *y = UserClass::from_char ("ABCDEF");
210
211 const Iterator end = make_iter (x + 6, x + 0, x + 6, end);
212 Iterator it = make_iter (x + 0, x + 0, x + 6, it);
213
214 PASS (y [0] = *it);
215 PASS (y [1] = *++it);
216 PASS (y [2] = *it++); // it++ is the same as ++it
217 PASS (y [3] = *it++);
218 PASS (++it);
219 PASS (y [4] = *it);
220 PASS (y [5] = *it++);
221
222 bool equal;
223
224 PASS (equal = it == end);
225
226 rw_assert (!equal, 0, __LINE__,
227 "InputIter<UserClass> unexpectedly equal to end: "
228 "%p != %p (diff = %d)",
229 it.cur_, end.cur_, end.cur_ - it.cur_);
230
231 PASS (++it);
232
233 PASS (equal = it == end);
234
235 rw_assert (equal, 0, __LINE__,
236 "InputIter<UserClass> unexpectedly not equal to end: "
237 "%p != %p (diff = %d)",
238 it.cur_, end.cur_, end.cur_ - it.cur_);
239
240 rw_assert (0 == UserClass::compare (x, y, 6), 0, __LINE__,
241 "InputIter<UserClass> data mismatch: %s != %s",
242 X2STR (x, 6), X2STR (y, 6));
243
244 delete[] x;
245 delete[] y;
246}
247
248
250static void
251test_3 ()
252{
253 UserClass *x = UserClass::from_char ("abcdef");
254 UserClass *y = UserClass::from_char ("ABCDEF");
255
256 const Iterator end = make_iter (x + 6, x + 6, x + 6, end);
257 Iterator it0 = make_iter (x + 0, x + 0, x + 6, it0);
258 Iterator it1 = it0;
259
260 bool equal;
261
262 PASS (equal = it0 == it1);
263
264 rw_assert (equal, 0, __LINE__,
265 "InputIter<UserClass> unexpectedly not equal: "
266 "%p != %p (diff = %d)",
267 it0.cur_, it1.cur_, it0.cur_ - it1.cur_);
268
269 PASS (equal = it0 == end);
270
271 rw_assert (!equal, 0, __LINE__,
272 "InputIter<UserClass> unexpectedly equal to end: "
273 "%p == %p", it1.cur_, end.cur_);
274
275 PASS (y [0] = *it0);
276 PASS (y [1] = *it1);
277
278 rw_assert (y [0].data_.val_ == y [1].data_.val_, 0, __LINE__,
279 "two copies of InputIter<UserClass> unexpectedly yield "
280 "different values: %d != %d",
281 y [0].data_.val_, y [1].data_.val_);
282
283 PASS (it0++);
284 FAIL (it1++); // can't pass through the same iterator twice
285 FAIL (it0 == it1); // it1 not in the domain of operator==()
286
287 PASS (equal = it0 == end);
288
289 rw_assert (!equal, 0, __LINE__,
290 "InputIter<UserClass> unexpectedly equal to end: "
291 "%p == %p", it1.cur_, end.cur_);
292
293 PASS (it1 = it0);
294
295 PASS (equal = it0 == it1);
296
297 rw_assert (equal, 0, __LINE__,
298 "InputIter<UserClass> unexpectedly not equal: "
299 "%p == %p", it0.cur_, it1.cur_);
300
301 PASS (y [0] = *it0);
302 PASS (y [1] = *it1);
303
304 rw_assert (y [0].data_.val_ == y [1].data_.val_, 0, __LINE__,
305 "two copies of InputIter<UserClass> unexpectedly yield "
306 "different values: %d != %d",
307 y [0].data_.val_, y [1].data_.val_);
308
309 rw_assert (y [0].data_.val_ == 'b', 0, __LINE__,
310 "InputIter<UserClass>::operator*() == %d, got %d",
311 y [0].data_.val_, 'b');
312
313 PASS (it1++);
314 FAIL (it0++); // can't pass through the same iterator twice
315 FAIL (it0 == it1); // it0 not in the domain of operator==()
316
317 PASS (equal = it1 == end);
318
319 rw_assert (!equal, 0, __LINE__,
320 "InputIter<UserClass> unexpectedly equal to end: "
321 "%p == %p", it1.cur_, end.cur_);
322
323 FAIL (x [0] = *it0); // cannot dereference
324
325 delete[] x;
326 delete[] y;
327}
328
329
331static int
332run_test (int, char*[])
333{
334#ifndef NDEBUG
335
336 // silence stderr output from invocations of the RW_ASSERT() macro
337 // that are expected to fail by design (i.e., that's what this test
338 // exercises)
339 fclose (stderr);
340
341 test_0 ();
342 test_1 ();
343 test_2 ();
344 test_3 ();
345
346#else // if defined (NDEBUG)
347
348 rw_assert (0, 0, __LINE__, "macro NDEBUG #defined, cannot test");
349
350#endif // NDEBUG
351
352 return exit_status;
353
354}
355
356
358int main (int argc, char *argv[])
359{
360#ifdef _MSC_VER
361 // disable GUI window from abort()
362 _CrtSetReportMode (_CRT_ERROR, _CRTDBG_MODE_DEBUG);
363#endif
364
365 return rw_test (argc, argv, __FILE__,
366 0 /* no clause */,
367 0 /* no comment */, run_test,
368 0 /* co command line options */);
369}
int line
int fail
#define PASS(code)
int exit_status
#define X2STR(ign1, ign2)
#define FAIL(code)
InputIter< UserClass > Iterator
void handle_ABRT(int)
jmp_buf env
#define y
Definition 17.names.cpp:141
#define x
Definition 17.names.cpp:140
bool equal(const T &x, const U &y)
void run_test(const ForwardIterator &dummy, T *, Predicate *pred)
const int end
#define main
#define stderr
T * make_iter(T *cur, const T *, const T *, T *)
_TEST_EXPORT int rw_test(int argc, char *argv[], const char *filename, const char *clause, const char *comment, int(*testfun)(int, char **), const char *optspec,...)
_TEST_EXPORT int rw_assert(int, const char *, int, const char *,...)
Definition driver.cpp:1630
const value_type * cur_
static UserClass from_char(char c)
Definition rw_value.h:221
static int compare(const UserClass *, const char *, _RWSTD_SIZE_T=_RWSTD_SIZE_MAX)