Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
25.fill.cpp
Go to the documentation of this file.
1
29#include <algorithm> // for fill
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
39
40#ifndef _RWSTD_NO_EXPLICIT_INSTANTIATION
41
42template
43void
45 const assign<base<> >&);
46
47template
48void
50 const assign<base<> >&);
51
52#endif // _RWSTD_NO_EXPLICIT_INSTANTIATION
53
54} // namespace std
55
56
58// exercises std::fill()
59template <class ForwardIterator, class T>
60void test_fill (std::size_t N,
61 const ForwardIterator &dummy_iter,
62 const T*)
63{
64 static const char* const itname = type_name (dummy_iter, (T*)0);
65 static const char* const tname = "UserClass";
66
67 rw_info (0, 0, 0, "void std::fill (%s, %1$s, const %s&)", itname, tname);
68
69 // generate sequential values for each default constructed T
70 T::gen_ = gen_seq;
71
72 T* const buf = new T [N];
73
74 for (std::size_t i = 0; i < N; ++i) {
75
76 T* const buf_end = buf + i;
77
78 const ForwardIterator begin =
79 make_iter (buf, buf, buf_end, dummy_iter);
80
81 const ForwardIterator end =
82 make_iter (buf_end, buf_end, buf_end, dummy_iter);
83
84 const T value;
85
86 // the number of invocations of the assignment operator
87 std::size_t n_op_assign = T::n_total_op_assign_;
88
89 std::fill (begin, end, value);
90
91 // compute the number of invocations of the assignment operator
92 // by the algorithm
93 n_op_assign = T::n_total_op_assign_ - n_op_assign;
94
95 bool success = true;
96
97 // verify 25.2.5, p2
98 std::size_t j = 0;
99 for ( ; success && j != i; ++j) {
100 success = buf [j].data_.val_ == value.data_.val_;
101 }
102
103 if (!rw_assert (success, 0, __LINE__,
104 "%zu. fill (%s, %2$s, const %s&): buf [%zu]: %d != %d",
105 i, itname, tname, j, buf [j].data_.val_,
106 value.data_.val_))
107 break;
108
109 // verify the complexity requirements:
110 // Exactly last - first assignments.
111 if (!rw_assert (n_op_assign == i, 0, __LINE__,
112 "%zu. fill (%s, %2$s, const %s&) expected "
113 "%zu invocations of %2$s::operator=(); got %zu",
114 i, itname, tname, i, n_op_assign))
115 break;
116 }
117
118 delete[] buf;
119}
120
121
123// exercises std::fill_n()
124template <class OutputIterator, class Size, class T>
125void test_fill_n (std::size_t N,
126 const OutputIterator &dummy_iter,
127 const Size*,
128 const T*)
129{
130 static const char* const itname = type_name (dummy_iter, (T*)0);
131 static const char* const szname = "Size<int>";
132 static const char* const tname = "UserClass";
133
134 rw_info (0, 0, 0, "void std::fill_n (%s, %s, const %s&)",
135 itname, szname, tname);
136
137 // generate sequential values for each default constructed T
138 T::gen_ = gen_seq;
139
140 T* const buf = new T [N];
141
142 for (std::size_t i = 0; i < N; ++i) {
143
144 T* const buf_end = buf + i;
145
146 const OutputIterator begin =
147 make_iter (buf, buf, buf_end, dummy_iter);
148
149 const Size n (typename Size::IntegralType (i), 0 /* dummy */);
150 const T value;
151
152 // the number of invocations of the assignment operator
153 std::size_t n_op_assign = T::n_total_op_assign_;
154
155 std::fill_n (begin, n, value);
156
157 // compute the number of invocations of the assignment operator
158 n_op_assign = T::n_total_op_assign_ - n_op_assign;
159
160 bool success = true;
161
162 // verify 25.2.5, p2
163 std::size_t j = 0;
164 for ( ; success && j != i; ++j) {
165 success = buf [j].data_.val_ == value.data_.val_;
166 }
167
168 rw_assert (success, 0, __LINE__,
169 "%zu. fill_n (%s, %s, const %s&): buf [%zu]: %d != %d",
170 i, itname, szname, tname, j, buf [j].data_.val_,
171 value.data_.val_);
172
173 if (!success)
174 break;
175
176 success = n_op_assign == i;
177 rw_assert (success, 0, __LINE__,
178 "%zu. fill_n (%s, %s, const %s&) complexity: "
179 "%zu != %zu", i, itname, szname, tname,
180 n_op_assign, i);
181
182 if (!success)
183 break;
184 }
185
186 delete[] buf;
187}
188
189
190
192/* extern */ int rw_opt_nloops = 32; // --nloops
193/* extern */ int rw_opt_no_fill; // --no-fill
194/* extern */ int rw_opt_no_fill_n; // --no-fill_n
195/* extern */ int rw_opt_no_output_iter; // --no-OutputIterator
196/* extern */ int rw_opt_no_fwd_iter; // --no-ForwardIterator
197/* extern */ int rw_opt_no_bidir_iter; // --no-BidirectionalIterator
198/* extern */ int rw_opt_no_rnd_iter; // --no-RandomAccessIterator
199
200static void
201test_fill (const std::size_t N)
202{
203 rw_info (0, 0, 0,
204 "template <class %s, class %s> "
205 "void std::fill (%1$s, %1$s, const %2$s&)",
206 "ForwardIterator", "T");
207
208 if (rw_opt_no_fwd_iter) {
209 rw_note (0, __FILE__, __LINE__, "ForwardIterator test disabled");
210 }
211 else {
213 }
214
216 rw_note (0, __FILE__, __LINE__, "BidirectionalIterator test disabled");
217 }
218 else {
220 }
221
222 if (rw_opt_no_rnd_iter) {
223 rw_note (0, __FILE__, __LINE__, "RandomAccessIterator test disabled");
224 }
225 else {
227 }
228}
229
230
232static void
233test_fill_n (const std::size_t N)
234{
235 rw_info (0, 0, 0,
236 "template <class %s, class %s, class %s> "
237 "void std::fill_n (%1$s, %2$s, const %3$s&)",
238 "OutputIterator", "Size", "T");
239
241 rw_note (0, __FILE__, __LINE__, "OutputIterator test disabled");
242 }
243 else {
245 (UserClass*)0);
246 }
247
248 if (rw_opt_no_fwd_iter) {
249 rw_note (0, __FILE__, __LINE__, "ForwardIterator test disabled");
250 }
251 else {
253 }
254
256 rw_note (0, __FILE__, __LINE__, "BidirectionalIterator test disabled");
257 }
258 else {
260 }
261
262 if (rw_opt_no_rnd_iter) {
263 rw_note (0, __FILE__, __LINE__, "RandomAccessIterator test disabled");
264 }
265 else {
267 (UserClass*)0);
268 }
269}
270
271
273static int
274run_test (int, char*[])
275{
276 // check that the number of loops is non-negative
277 rw_fatal (-1 < rw_opt_nloops, 0, 0,
278 "number of loops must be non-negative, got %d",
280
281 const std::size_t N = std::size_t (rw_opt_nloops);
282
283 if (rw_opt_no_fill) {
284 rw_note (0, __FILE__, __LINE__, "std::fill test disabled");
285 }
286 else {
287 test_fill (N);
288 }
289
290 if (rw_opt_no_fill_n) {
291 rw_note (0, __FILE__, __LINE__, "std::fill_n test disabled");
292 }
293 else {
294 test_fill_n (N);
295 }
296
297 return 0;
298}
299
300
302int main (int argc, char *argv[])
303{
304 return rw_test (argc, argv, __FILE__,
305 "lib.alg.fill",
306 0 /* no comment */, run_test,
307 "|-nloops# "
308 "|-no-fill# "
309 "|-no-fill_n# "
310 "|-no-OutputIterator# "
311 "|-no-ForwardIterator# "
312 "|-no-BidirectionalIterator# "
313 "|-no-RandomAccessIterator#",
321}
#define N
Definition 17.names.cpp:157
#define i
Definition 17.names.cpp:126
#define T
Definition 17.names.cpp:163
#define buf
Definition 17.names.cpp:452
#define j
Definition 17.names.cpp:127
#define n
Definition 17.names.cpp:130
#define ForwardIterator
Definition 17.names.cpp:469
#define OutputIterator
Definition 17.names.cpp:515
int rw_opt_no_fwd_iter
Definition 25.fill.cpp:196
void test_fill_n(std::size_t N, const OutputIterator &dummy_iter, const Size *, const T *)
Definition 25.fill.cpp:125
int rw_opt_no_fill_n
Definition 25.fill.cpp:194
int rw_opt_no_rnd_iter
Definition 25.fill.cpp:198
int rw_opt_no_bidir_iter
Definition 25.fill.cpp:197
void test_fill(std::size_t N, const ForwardIterator &dummy_iter, const T *)
Definition 25.fill.cpp:60
int rw_opt_no_output_iter
Definition 25.fill.cpp:195
int rw_opt_no_fill
Definition 25.fill.cpp:193
int rw_opt_nloops
Definition 25.fill.cpp:192
T value(const T &val)
const char * type_name()
const int end
#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_fatal(int expr, const char *filename, int line, const char *fmtspec,...)
Definition driver.cpp:1596
_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