Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
25.transform.cpp
Go to the documentation of this file.
1
29#include <algorithm> // for transform
30#include <cstddef> // for size_t
31
32#include <rw_alg_test.h>
33#include <rw_value.h> // for UserClass
34#include <rw_driver.h> // for rw_test()
35
36
38
39// disable explicit instantiation for compilers (like MSVC)
40// that can't handle it
41#ifndef _RWSTD_NO_EXPLICIT_INSTANTIATION
42
43template
45transform (InputIter<assign<base<> > >,
48 func<assign<base<> > >);
49
50template
52transform (InputIter<assign<base<> > >,
57
58#endif // _RWSTD_NO_EXPLICIT_INSTANTIATION
59
60} // namespace std
61
62
64template <class T>
65struct incT
66{
67 // not a binary function
68 enum { binary = 0 };
69
70 // function name
71 static const char* name () { return "unary_function"; }
72
73 // dummy arguments to prevent the class from being default
74 // constructible or construtible by conversion from an int
75 incT (int /* dummy */, int /* dummy */) {
76 funcalls_ = 0;
77 }
78
79 // non-const in order to detect unwarranted assumptions
80 // in the algorithm(s)
81 T operator() (const T &x) /* non-const */ {
82 ++funcalls_;
83 T y (x);
84 y.data_.val_ += 1;
85 return y;
86 }
87
88 static std::size_t funcalls_;
89};
90
91
92template <class T>
93struct plusT
94{
95 // binary function
96 enum { binary = 1 };
97
98 // function name
99 static const char* name () { return "binary_function"; }
100
101 // dummy arguments to prevent the class from being default
102 // constructible or construtible by conversion from an int
103 plusT (int, int) {
104 funcalls_ = 0;
105 }
106
107 // non-const in order to detect unwarranted assumptions
108 // in the algorithm(s)
109 T operator() (const T &a, const T &b) /* non-const */ {
110 ++funcalls_;
111 T x (a);
112 x.data_.val_ = a.data_.val_ + b.data_.val_;
113 return x;
114 }
115
116 static std::size_t funcalls_;
117};
118
119template <class T> std::size_t incT<T>::funcalls_;
120template <class T> std::size_t plusT<T>::funcalls_;
121
122
124/* extern */ int rw_opt_nloops = 32; // --nloops=#
125/* extern */ int rw_opt_no_unary_function; // --no-unary_function
126/* extern */ int rw_opt_no_binary_function; // --no-binary_function
127/* extern */ int rw_opt_no_input_iter; // --no-InputIterator
128/* extern */ int rw_opt_no_output_iter; // --no-OutputIterator
129/* extern */ int rw_opt_no_fwd_iter; // --no-ForwardIterator
130/* extern */ int rw_opt_no_bidir_iter; // --no-BidirectionalIterator
131/* extern */ int rw_opt_no_rnd_iter; // --no-RandomAccessIterator
132
133
135template <class InputIterator, class Unused, class OutputIterator>
138 Unused /* for compatibility with the overload below */,
140{
141 return std::transform (first, last, dest, fun);
142}
143
144template <class InputIterator1, class InputIterator2, class OutputIterator>
146invoke_transform (InputIterator1 first1, InputIterator1 last1,
147 InputIterator2 first2,
149{
150 return std::transform (first1, last1, first2, dest, fun);
151}
152
153
154template <class T, class InputIterator1, class InputIterator2,
155 class OutputIterator, class Function>
156void test_transform (const T* ptr,
157 const InputIterator1 &it1,
158 const InputIterator2 &it2,
159 const OutputIterator &out,
160 const Function*,
161 int same_seq)
162{
163 static const char* const it1name = type_name (it1, ptr);
164 static const char* const it2name = type_name (it2, ptr);
165 static const char* const outname = type_name (out, ptr);
166 static const char* const algname = "transform";
167 static const char* const funname = Function::name ();
168 static const bool binary = Function::binary;
169
170 const std::size_t nloops = std::size_t (rw_opt_nloops);
171
172 rw_info (0, 0, 0,
173 "std::%s (%s, %2$s%{?}, %s%{;}, %s, %s)%{?}, %s%d == %s%{;}",
174 algname, it1name, binary, it2name, outname, funname,
175 same_seq, "first", same_seq, "dest");
176
177 // generate sequential values for each default constructed T
178 T::gen_ = gen_seq;
179
180 // make sure buf{1,2}[0] is dereferenceable even when (nloops == 0)
181 T* const buf1 = new T [nloops + 1];
182 T* const buf2 = new T [nloops + 1];
183 T* const buf3 = same_seq ? (same_seq == 1 ? buf1 : buf2) : new T [nloops];
184
185 const int start1_val =
186 same_seq == 2 ? buf2 [0].data_.val_ : buf1 [0].data_.val_;
187 const int start2_val =
188 same_seq == 2 ? buf1 [0].data_.val_ : buf2 [0].data_.val_;
189
190 for (std::size_t i = 0; i < nloops; ++i) {
191
192 T* const buf1_end = buf1 + i;
193 const T* const buf2_end = buf2 + i;
194 const T* const buf3_end = buf3 + i;
195
196 const InputIterator1 first1 = make_iter (buf1, buf1, buf1_end, it1);
197 const InputIterator1 last1 = make_iter (buf1_end, buf1, buf1_end, it1);
198 const InputIterator2 first2 = make_iter (buf2, buf2, buf2_end, it2);
199 const OutputIterator dest = make_iter (buf3, buf3, buf3_end, out);
200
201 const Function fun (0, 0); // dummy arguments
202
203 // invoke the overload of std::transform() appropriate
204 // for this Function
205 const OutputIterator result =
206 invoke_transform (first1, last1, first2, dest, fun);
207
208 // check the returned iterator
209 bool success = result.cur_ == buf3_end;
210 rw_assert (success, 0, __LINE__,
211 "%zu. %s (%s, %2$s%{?}, %s%{;}, %s, %s)%{?}, "
212 "%s%d == %s%{;} : return val: "
213 "dest + %td, expected dest + %zu",
214 i, algname, it1name, binary, it2name, outname, funname,
215 same_seq, "first", same_seq, "dest",
216 result.cur_ - buf3, i);
217
218 if (!success)
219 break;
220
221 // check the transformation results
222 int exp_val = 0;
223 std::size_t j = 0;
224 for ( ; j < i; j++) {
225 if (0 == same_seq) {
226 exp_val = binary ?
227 start1_val + int (j) + start2_val + int (j)
228 : start1_val + int (j) + 1;
229 }
230 else {
231 exp_val = binary ?
232 start1_val + int (j)
233 + (start2_val + int (j)) * int (i - j)
234 : start1_val + int (i);
235 }
236
237 success = buf3 [j].data_.val_ == exp_val;
238 if (!success)
239 break;
240 }
241
242 rw_assert (success, 0, __LINE__,
243 "%zu. %s (%s, %2$s%{?}, %s%{;}, %s, %s)%{?}, "
244 "%s%d == %s%{;} : error: incorrect value %d, "
245 "expected %d, position %zu",
246 i, algname, it1name, binary, it2name, outname, funname,
247 same_seq, "first", same_seq, "dest",
248 buf3 [j].data_.val_, exp_val, j + 1);
249
250 if (!success)
251 break;
252
253 // check compexity, 25.2.3 p4
254 rw_assert (Function::funcalls_ == i, 0, __LINE__,
255 "%zu. %s (%s, %2$s%{?}, %s%{;}, %s, %s)%{?}, "
256 "%s%d == %s%{;} : complexity : %zu "
257 "applications of %5$s, expected %zu",
258 i, algname, it1name, binary, it2name, outname, funname,
259 same_seq, "first", same_seq, "dest",
260 Function::funcalls_, i);
261 }
262
263 delete[] buf1;
264 delete[] buf2;
265
266 if (0 == same_seq)
267 delete[] buf3;
268}
269
270
272template <class T, class InputIterator1, class InputIterator2,
273 class OutputIterator, class Function>
274void gen_test (const T* ptr,
275 const InputIterator1 &it1,
276 const InputIterator2 &it2,
277 const OutputIterator &out,
278 const Function* pfun,
279 int tag1,
280 int tag2,
281 int tag3,
282 int same_seq)
283{
284 const InputIter<UserClass> input_iter (0, 0, 0);
285 const OutputIter<T> output_iter (0, 0, 0);
286 const FwdIter<T> fwd_iter (0, 0, 0);
287 const BidirIter<T> bidir_iter (0, 0, 0);
288 const RandomAccessIter<UserClass> rand_iter (0, 0, 0);
289
290 // tag1, tag2 and tag3 indicates that an iterator needs to be generated
291 // at the corresponding position by a recursive call to gen_test
292 // and other positions there the iterator will be used
293 // for all tags:
294 // 0 means that no iterator is needed here
295 // (maybe it was already generated or just not needed)
296 // 1 means that the iterator is needed at this position
297 // (first for tag1, second for tag2, etc)
298 // 2 means that the iterator is needed at this position and the
299 // same type of iterator should be used at third position
300
303 if (1 == tag1 || (0 == tag1 && 1 == tag2))
304 rw_note (0, __FILE__, __LINE__, "InputIterator test disabled");
305 }
306 else {
307 if (1 == tag1) {
308 gen_test (ptr, input_iter, it2, out,
309 pfun, 0, tag2, tag3, same_seq);
310 }
311 else if (0 == tag1 && 1 == tag2) {
312 gen_test (ptr, it1, input_iter, out,
313 pfun, tag1, 0, tag3, same_seq);
314 }
315 }
316
318 if (rw_opt_no_fwd_iter) {
319 if (tag1 || (!tag1 && tag2) || (!tag1 && !tag2 && tag3))
320 rw_note (0, __FILE__, __LINE__, "ForwardIterator test disabled");
321 }
322 else {
323 if (1 == tag1) {
324 gen_test (ptr, fwd_iter, it2, out,
325 pfun, 0, tag2, tag3, same_seq);
326 }
327 else if (2 == tag1) {
328 gen_test (ptr, fwd_iter, it2, fwd_iter,
329 pfun, 0, tag2, 0, same_seq);
330 }
331 else if (0 == tag1 && 1 == tag2) {
332 gen_test (ptr, it1, fwd_iter, out,
333 pfun, tag1, 0, tag3, same_seq);
334 }
335 else if (0 == tag1 && 2 == tag2) {
336 gen_test (ptr, it1, fwd_iter, fwd_iter,
337 pfun, tag1, 0, 0, same_seq);
338 }
339 else if (0 == tag1 && 0 == tag2 && 1 == tag3) {
340 gen_test (ptr, it1, it2, fwd_iter,
341 pfun, tag1, tag2, 0, same_seq);
342 }
343 }
344
347 if (tag1 || (!tag1 && tag2) || (!tag1 && !tag2 && tag3))
348 rw_note (0, __FILE__, __LINE__,
349 "BidirectionalIterator test disabled");
350 }
351 else {
352 if (1 == tag1) {
353 gen_test (ptr, bidir_iter, it2, out,
354 pfun, 0, tag2, tag3, same_seq);
355 }
356 else if (2 == tag1) {
357 gen_test (ptr, bidir_iter, it2, bidir_iter,
358 pfun, 0, tag2, 0, same_seq);
359 }
360 else if (0 == tag1 && 1 == tag2) {
361 gen_test (ptr, it1, bidir_iter, out,
362 pfun, tag1, 0, tag3, same_seq);
363 }
364 else if (0 == tag1 && 2 == tag2) {
365 gen_test (ptr, it1, bidir_iter, bidir_iter,
366 pfun, tag1, 0, 0, same_seq);
367 }
368 else if (0 == tag1 && 0 == tag2 && 1 == tag3) {
369 gen_test (ptr, it1, it2, bidir_iter,
370 pfun, tag1, tag2, 0, same_seq);
371 }
372 }
373
375 if (rw_opt_no_rnd_iter) {
376 if (tag1 || (!tag1 && tag2) || (!tag1 && !tag2 && tag3))
377 rw_note (0, __FILE__, __LINE__,
378 "RandomAccessIterator test disabled");
379 }
380 else {
381 if (1 == tag1) {
382 gen_test (ptr, rand_iter, it2, out,
383 pfun, 0, tag2, tag3, same_seq);
384 }
385 else if (2 == tag1) {
386 gen_test (ptr, rand_iter, it2, rand_iter,
387 pfun, 0, tag2, 0, same_seq);
388 }
389 else if (0 == tag1 && 1 == tag2) {
390 gen_test (ptr, it1, rand_iter, out,
391 pfun, tag1, 0, tag3, same_seq);
392 }
393 else if (0 == tag1 && 2 == tag2) {
394 gen_test (ptr, it1, rand_iter, rand_iter,
395 pfun, tag1, 0, 0, same_seq);
396 }
397 else if (0 == tag1 && 0 == tag2 && 1 == tag3) {
398 gen_test (ptr, it1, it2, rand_iter,
399 pfun, tag1, tag2, 0, same_seq);
400 }
401 }
402
405 if (0 == tag1 && 0 == tag2 && 1 == tag3)
406 rw_note (0, __FILE__, __LINE__, "OutputIterator test disabled");
407 }
408 else {
409 if (0 == tag1 && 0 == tag2 && 1 == tag3) {
410 gen_test (ptr, it1, it2, output_iter,
411 pfun, tag1, tag2, 0, same_seq);
412 }
413 }
414
415 if (0 == tag1 && 0 == tag2 && 0 == tag3) {
416 test_transform (ptr, it1, it2, out, pfun, same_seq);
417 }
418}
419
420
422static int
423run_test (int, char*[])
424{
425 const InputIter<UserClass> input_iter (0, 0, 0);
426 const RandomAccessIter<UserClass> rand_iter;
427 const UserClass* const ptr = 0;
428
429 // test transform with a unary function
430 // according to 25.2.3 p5 'result may be equal to first'
431 // so it is neccessary to test this case separately
433 rw_note (0, __FILE__, __LINE__,
434 "std::transform unary function test disabled");
435 }
436 else {
437 const incT<UserClass>* const pfun = 0;
438
439 gen_test (ptr, input_iter, rand_iter, rand_iter,
440 pfun, 1, 0, 1, 0 /* result distinct from first */);
441
442 // test 25.2.3 p5 - result is equal to first
443 // set tag1 = 2 and tag3 = 0 to test all iterators at the first
444 // position and avoid generating iterators at the third pos
445 gen_test (ptr, input_iter, rand_iter, rand_iter,
446 pfun, 2, 0, 0, 1 /* result same as first */);
447 }
448
449 // test transform with a binary function
450 // according to 25.2.3 p5 'result may be equal to first1 or first2'
451 // so it is neccessary to test these cases separately
453 rw_note (0, __FILE__, __LINE__,
454 "std::transform binary function test disabled");
455 }
456 else {
457 const plusT<UserClass>* const pfun = 0;
458
459 gen_test (ptr, input_iter, rand_iter, rand_iter,
460 pfun, 1, 1, 1, 0 /* result distinct from first{1,2} */);
461
462 // test 25.2.3 p5 - result is equal to first2
463 // set tag2 = 2 and tag3 = 0 to test all iterators at the second
464 // position and avoid generating iterators at the third pos
465 gen_test (ptr, input_iter, rand_iter, rand_iter,
466 pfun, 1, 2, 0, 2 /* result same as first2 */);
467
468 // test 25.2.3 p5 - result is equal to first1
469 // set tag1 = 2 and tag3 = 0 to test all iterators at the first
470 // position and avoid generating iterators at the third pos
471 // set same_seq to 1 to indicate that first1 and dest should be
472 // the same iterators
473 gen_test (ptr, input_iter, rand_iter, rand_iter,
474 pfun, 2, 1, 0, 1 /* result same as first1 */);
475 }
476
477 return 0;
478}
479
480
481
483int main (int argc, char *argv[])
484{
485 return rw_test (argc, argv, __FILE__,
486 "lib.alg.transform",
487 0 /* no comment */, run_test,
488 "|-nloops#0 " // must be non-negative
489 "|-no-unary_function# "
490 "|-no-binary_function# "
491 "|-no-InputIterator# "
492 "|-no-OutputIterator# "
493 "|-no-ForwardIterator# "
494 "|-no-BidirectionalIterator# "
495 "|-no-RandomAccessIterator",
504}
cbfun_t * pfun
#define i
Definition 17.names.cpp:126
#define T
Definition 17.names.cpp:163
#define y
Definition 17.names.cpp:141
#define fun
Definition 17.names.cpp:471
#define ptr
Definition 17.names.cpp:525
#define last
Definition 17.names.cpp:487
#define x
Definition 17.names.cpp:140
#define a
Definition 17.names.cpp:109
#define j
Definition 17.names.cpp:127
#define b
Definition 17.names.cpp:110
#define Function
Definition 17.names.cpp:472
#define OutputIterator
Definition 17.names.cpp:515
volatile int result
void run_test(const ForwardIterator &dummy, T *, Predicate *pred)
OutputIterator invoke_transform(InputIterator first, InputIterator last, Unused, OutputIterator dest, incT< UserClass > fun)
int rw_opt_no_binary_function
int rw_opt_no_fwd_iter
int rw_opt_no_rnd_iter
void test_transform(const T *ptr, const InputIterator1 &it1, const InputIterator2 &it2, const OutputIterator &out, const Function *, int same_seq)
void gen_test(const T *ptr, const InputIterator1 &it1, const InputIterator2 &it2, const OutputIterator &out, const Function *pfun, int tag1, int tag2, int tag3, int same_seq)
int rw_opt_no_unary_function
int rw_opt_no_bidir_iter
int rw_opt_no_input_iter
int rw_opt_no_output_iter
int rw_opt_nloops
const char * type_name()
const int out
#define main
#define _RWSTD_NAMESPACE(name)
Definition _defs.h:567
Definition json.h:5363
_TEST_EXPORT int gen_seq()
Definition alg_test.cpp:38
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_info(int, const char *, int, const char *,...)
Definition driver.cpp:1681
_TEST_EXPORT int rw_note(int, const char *, int, const char *,...)
Definition driver.cpp:1664
_TEST_EXPORT int rw_assert(int, const char *, int, const char *,...)
Definition driver.cpp:1630
static std::size_t funcalls_
static const char * name()
incT(int, int)
T operator()(const T &x)
static std::size_t funcalls_
plusT(int, int)
static const char * name()
T operator()(const T &a, const T &b)