Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
0.outputiter.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 OutputIter
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
84static void
85test_1 ()
86{
87 UserClass *x = UserClass::from_char ("abcdef");
88 UserClass *y = UserClass::from_char ("ABCDEF");
89
90 // use `out' as the last argument to make_iter() (as opposed
91 // to, say, OutputIter<UserClass>()) since Output Iterators
92 // are not required to be copy-constructible
93 OutputIter<UserClass> out = make_iter (x + 0, x + 0, x + 6, out);
94
95 PASS (*out++ = y [0]);
96 PASS (*out++ = y [1]);
97 PASS (*out++ = y [2]);
98 PASS (*out++ = y [3]);
99 PASS (*out++ = y [4]);
100 PASS (*out++ = y [5]);
101
102 delete[] x;
103 delete[] y;
104}
105
106
108static void
109test_2 ()
110{
111 UserClass *x = UserClass::from_char ("abcdef");
112 UserClass *y = UserClass::from_char ("ABCDEF");
113
114 OutputIter<UserClass> out = make_iter (x + 0, x + 0, x + 6, out);
115
116 PASS (*out = y [0]);
117 PASS (++out);
118 PASS (out = y [1]);
119 PASS (++out);
120 PASS (*out = y [2]);
121 PASS (++out);
122 PASS (out = y [3]);
123 PASS (++out);
124 PASS (*out = y [4]);
125 PASS (++out);
126 PASS (out = y [5]);
127 PASS (++out);
128
129 delete[] x;
130 delete[] y;
131}
132
133
135static void
136test_3 ()
137{
138 UserClass *x = UserClass::from_char ("abcdef");
139 UserClass *y = UserClass::from_char ("ABCDEF");
140
141 OutputIter<UserClass> out = make_iter (x + 0, x + 0, x + 6, out);
142
143 PASS (*out = y [0]);
144 PASS (out++);
145 PASS (out = y [1]);
146 PASS (out++);
147 PASS (*out = y [2]);
148 PASS (out++);
149 PASS (out = y [3]);
150 PASS (out++);
151 PASS (*out = y [4]);
152 PASS (out++);
153 PASS (out = y [5]);
154 PASS (out++);
155
156 delete[] x;
157 delete[] y;
158}
159
160
162static void
163test_4 ()
164{
165 UserClass *x = UserClass::from_char ("abcdef");
166 UserClass *y = UserClass::from_char ("ABCDEF");
167
168 OutputIter<UserClass> out = make_iter (x + 0, x + 0, x + 6, out);
169
170 PASS (*out++ = y [0]);
171 PASS (*out = y [1]);
172 PASS (out++);
173
174 PASS (out++ = y [2]);
175 PASS (out = y [3]);
176 PASS (out++);
177
178 PASS (*out++ = y [4]);
179 PASS (*out = y [5]);
180 PASS (out++);
181
182 delete[] x;
183 delete[] y;
184}
185
186
188static void
189test_5 ()
190{
191 UserClass *x = UserClass::from_char ("abcdef");
192 UserClass *y = UserClass::from_char ("ABCDEF");
193
194 OutputIter<UserClass> out = make_iter (x + 0, x + 0, x + 6, out);
195
196 PASS (*out = y [0]);
197 PASS (++out);
198 PASS (*out++ = y [1]);
199
200 PASS (out = y [2]);
201 PASS (++out);
202 PASS (out++ = y [3]);
203
204 PASS (*out = y [4]);
205 PASS (++out);
206 PASS (*out++ = y [5]);
207
208 delete[] x;
209 delete[] y;
210}
211
212
214static void
215test_6 ()
216{
217 UserClass *x = UserClass::from_char ("abcdef");
218 UserClass *y = UserClass::from_char ("ABCDEF");
219
220 OutputIter<UserClass> out = make_iter (x + 0, x + 0, x + 6, out);
221
222 PASS (*out = y [0]);
223 PASS (*++out = y [1]);
224 PASS (++out = y [2]);
225 PASS (*++out = y [3]);
226 PASS (++out = y [4]);
227 PASS (*++out = y [5]);
228 PASS (out++);
229
230 delete[] x;
231 delete[] y;
232}
233
234
236static void
237test_7 ()
238{
239 UserClass x;
240
242
243 FAIL (++out);
244 FAIL (out++);
245 FAIL (*out);
246 FAIL (*out++);
247 FAIL (*out = x);
248 FAIL (*out++ = x);
249 FAIL (*++out = x);
250 FAIL (out = x);
251 FAIL (out++ = x);
252 FAIL (++out = x);
253
254 out = make_iter (&x, &x, &x + 1, out);
255}
256
257
259static void
260test_8 ()
261{
262 UserClass *x = UserClass::from_char ("abcdef");
263 UserClass *y = UserClass::from_char ("ABCDEF");
264
265 OutputIter<UserClass> out = make_iter (x + 0, x + 0, x + 6, out);
266
267 PASS (*out++ = y [0]);
268 PASS (*out++ = y [1]);
269 PASS (*out++ = y [2]);
270 PASS (*out++ = y [3]);
271 PASS (*out++ = y [4]);
272 PASS (*out++ = y [5]);
273 FAIL (out++);
274
275 delete[] x;
276 delete[] y;
277}
278
279
281static void
282test_9 ()
283{
284 UserClass *x = UserClass::from_char ("abcdef");
285 UserClass *y = UserClass::from_char ("ABCDEF");
286
287 OutputIter<UserClass> out = make_iter (x + 0, x + 0, x + 6, out);
288
289 PASS (*out++ = y [0]);
290 PASS (out++);
291 FAIL (*out++ = y [1]);
292
293 delete[] x;
294 delete[] y;
295}
296
297
299static int
300run_test (int, char*[])
301{
302#ifndef NDEBUG
303
304 // silence stderr output from invocations of the RW_ASSERT() macro
305 // that are expected to fail by design (i.e., that's what this test
306 // exercises)
307 fclose (stderr);
308
309 test_1 ();
310 test_2 ();
311 test_3 ();
312 test_4 ();
313 test_5 ();
314 test_6 ();
315 test_7 ();
316 test_8 ();
317 test_9 ();
318
319#else // if defined (NDEBUG)
320
321 rw_assert (0, 0, __LINE__, "macro NDEBUG #defined, cannot test");
322
323#endif // NDEBUG
324
325 return exit_status;
326
327}
328
329
331int main (int argc, char *argv[])
332{
333#ifdef _MSC_VER
334 // disable GUI window from abort()
335 _CrtSetReportMode (_CRT_ERROR, _CRTDBG_MODE_DEBUG);
336#endif
337
338 return rw_test (argc, argv, __FILE__,
339 0 /* no clause */,
340 0 /* no comment */, run_test,
341 0 /* co command line options */);
342}
int line
int fail
#define PASS(code)
int exit_status
#define FAIL(code)
void handle_ABRT(int)
jmp_buf env
#define y
Definition 17.names.cpp:141
#define x
Definition 17.names.cpp:140
void run_test(const ForwardIterator &dummy, T *, Predicate *pred)
const int out
#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
static UserClass from_char(char c)
Definition rw_value.h:221