Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
25.includes.cpp
Go to the documentation of this file.
1
29#include <algorithm> // for includes
30#include <cstring> // for strlen, 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
43bool
44includes (InputIter<lt_comp<base<no_ctor> > >,
48
49template
50bool
51includes (InputIter<lt_comp<base<no_ctor> > >,
56
57#endif // _RWSTD_NO_EXPLICIT_INSTANTIATION
58
59} // namespace std
60
61
63template <class T>
64struct Less
65{
66 static std::size_t funcalls_;
67
68 // dummy arguments provided to prevent the class from being
69 // default constructible and implicit conversion from int
70 Less (int /* dummy */, int /* dummy */) {
71 funcalls_ = 0;
72 }
73
74 // return a type other than bool but one that is implicitly
75 // convertible to bool to detect incorrect assumptions
76 conv_to_bool operator() (const T &x, const T &y) /* non-const */ {
77 ++funcalls_;
78 return conv_to_bool::make (x.data_.val_ < y.data_.val_);
79 }
80
81 static const char* name () { return "Less"; }
82
83private:
84 void operator= (Less&); // not assignable
85};
86
87template<class T> std::size_t Less<T>::funcalls_;
88
89
91template
92<class T, class InputIterator1, class InputIterator2, class Predicate>
94 const char *src1,
95 const char *src2,
96 const bool res,
97 const InputIterator1 &it1,
98 const InputIterator2 &it2,
99 const T*,
100 const Predicate *ppred)
101{
102 const char* const it1name = type_name (it1, (T*)0);
103 const char* const it2name = type_name (it2, (T*)0);
104 const char* const fname = "includes";
105 const char* const predname = Predicate::name();
106
107 const std::size_t nsrc1 = std::strlen (src1);
108 const std::size_t nsrc2 = std::strlen (src2);
109
110 T* const xsrc1 = T::from_char (src1, nsrc1);
111 T* const xsrc2 = T::from_char (src2, nsrc2);
112
113 T* const xsrc1_end = xsrc1 + nsrc1;
114 T* const xsrc2_end = xsrc2 + nsrc2;
115
116 const InputIterator1 first1 = make_iter (xsrc1, xsrc1, xsrc1_end, it1);
117 const InputIterator1 last1 = make_iter (xsrc1_end, xsrc1, xsrc1_end, it1);
118 const InputIterator2 first2 = make_iter (xsrc2, xsrc2, xsrc2_end, it2);
119 const InputIterator2 last2 = make_iter (xsrc2_end, xsrc2, xsrc2_end, it2);
120
121 const Predicate pred (0, 0);
122
123 const std::size_t last_n_op_lt = T::n_total_op_lt_;
124
125 const bool result = ppred ?
126 std::includes (first1, last1, first2, last2, pred)
127 : std::includes (first1, last1, first2, last2);
128
129 // check the returned value
130 rw_assert (res == result, 0, line,
131 "line %d: %s<%s, %s%{?}, %s%{;}>(\"%s\", \"%s\", ...) "
132 "== %b, got %b",
133 __LINE__, fname, it1name, it2name, ppred, predname, src1, src2,
134 res, result);
135
136 std::size_t n_ops_lt = ppred ?
137 Predicate::funcalls_ : T::n_total_op_lt_ - last_n_op_lt;
138
139 // check the complexity
140 std::size_t n_exp_ops = nsrc1 + nsrc2 > 0 ? 2 * (nsrc1 + nsrc2) - 1 : 0;
141 rw_assert (n_ops_lt <= n_exp_ops, 0, line,
142 "line %d: %s<%s, %s%{?}, %s%{;}>(\"%s\", \"%s\", ...) "
143 "complexity: got %zu, expected <= %zu",
144 __LINE__, fname, it1name, it2name, ppred, predname, src1, src2,
145 n_ops_lt, n_exp_ops);
146
147 delete[] xsrc1;
148 delete[] xsrc2;
149}
150
151
152
154template
155<class T, class InputIterator1, class InputIterator2, class Predicate>
156void test_includes (const InputIterator1 &it1,
157 const InputIterator2 &it2,
158 const T*,
159 const Predicate *ppred)
160{
161 const char* const it1name = type_name (it1, (T*)0);
162 const char* const it2name = type_name (it2, (T*)0);
163 const char* const fname = "includes";
164 const char* const predname = Predicate::name ();
165
166 rw_info (0, 0, 0,
167 "std::%s(%s, %2$s, %s, %3$s%{?}, %s%{;})",
168 fname, it1name, it2name, ppred, predname);
169
170#define TEST(src1, src2, res) \
171 test_includes (__LINE__, src1, src2, res, it1, it2, (T*)0, ppred)
172
173 TEST ("", "", true);
174 TEST ("a", "", true);
175 TEST ("", "a", false);
176
177 TEST ("a", "a", true);
178 TEST ("a", "b", false);
179 TEST ("b", "a", false);
180
181 TEST ("aa", "aa", true);
182 TEST ("aa", "ab", false);
183 TEST ("ab", "aa", false);
184 TEST ("ab", "ab", true);
185
186 TEST ("abcdef", "b", true);
187 TEST ("abcdef", "bd", true);
188 TEST ("abcdef", "i", false);
189 TEST ("abcdef", "ei", false);
190
191 TEST ("abcde", "abcde", true);
192 TEST ("abbde", "abbde", true);
193 TEST ("abcce", "abcce", true);
194 TEST ("abcdd", "abcdd", true);
195
196 TEST ("ab", "aaabbbb", false);
197 TEST ("ab", "aaabbbc", false);
198 TEST ("aaabbbb", "ab", true);
199 TEST ("aaabbbc", "ac", true);
200
201 TEST ("a", "aaaaaaaa", false);
202 TEST ("a", "aaaaaaab", false);
203
204 TEST ("aaaaaaaa", "aaaaaaaa", true);
205 TEST ("aaaaaaab", "aaaaaaab", true);
206}
207
208
210/* extern */ int rw_opt_no_predicate; // --no-predicate
211/* extern */ int rw_opt_no_input_iter; // --no-InputIterator
212/* extern */ int rw_opt_no_fwd_iter; // --no-ForwardIterator
213/* extern */ int rw_opt_no_bidir_iter; // --no-BidirectionalIterator
214/* extern */ int rw_opt_no_rnd_iter; // --no-RandomAccessIterator
215
216
218template <class T, class InputIterator1, class Predicate>
219void test_includes (const InputIterator1 &it1,
220 const T*,
221 const Predicate *ppred)
222{
223 static const InputIter<T> input_iter (0, 0, 0);
224 static const FwdIter<T> fwd_iter (0, 0, 0);
225 static const BidirIter<T> bidir_iter (0, 0, 0);
226 static const RandomAccessIter<T> rand_iter (0, 0, 0);
227
229 rw_note (0, __FILE__, __LINE__, "InputIterator test disabled");
230 }
231 else {
232 test_includes (it1, input_iter, (T*)0, ppred);
233 }
234
235 if (rw_opt_no_fwd_iter) {
236 rw_note (0, __FILE__, __LINE__, "ForwardIterator test disabled");
237 }
238 else {
239 test_includes (it1, fwd_iter, (T*)0, ppred);
240 }
241
243 rw_note (0, __FILE__, __LINE__, "BidirectionalIterator test disabled");
244 }
245 else {
246 test_includes (it1, bidir_iter, (T*)0, ppred);
247 }
248
249 if (rw_opt_no_rnd_iter) {
250 rw_note (0, __FILE__, __LINE__, "RandomAccessIterator test disabled");
251 }
252 else {
253 test_includes (it1, rand_iter, (T*)0, ppred);
254 }
255}
256
257
259template <class T, class Predicate>
260void test_includes (const T*,
261 const Predicate *ppred)
262{
263 static const InputIter<T> input_iter (0, 0, 0);
264 static const FwdIter<T> fwd_iter (0, 0, 0);
265 static const BidirIter<T> bidir_iter (0, 0, 0);
266 static const RandomAccessIter<T> rand_iter (0, 0, 0);
267
268 rw_info (0, 0, 0,
269 "template <class %s, class %s%{?}, class %s%{;}> "
270 "bool std::includes (%1$s, %1$s, %2$s, %2$s%{?}, %s%{;})",
271 "InputIterator1", "InputIterator2", ppred, "Compare",
272 ppred, "Compare");
273
275 rw_note (0, __FILE__, __LINE__, "InputIterator test disabled");
276 }
277 else {
278 test_includes (input_iter, (T*)0, ppred);
279 }
280
281 if (rw_opt_no_fwd_iter) {
282 rw_note (0, __FILE__, __LINE__, "ForwardIterator test disabled");
283 }
284 else {
285 test_includes (fwd_iter, (T*)0, ppred);
286 }
287
289 rw_note (0, __FILE__, __LINE__, "BidirectionalIterator test disabled");
290 }
291 else {
292 test_includes (bidir_iter, (T*)0, ppred);
293 }
294
295 if (rw_opt_no_rnd_iter) {
296 rw_note (0, __FILE__, __LINE__, "RandomAccessIterator test disabled");
297 }
298 else {
299 test_includes (rand_iter, (T*)0, ppred);
300 }
301}
302
303
305static int run_test (int, char*[])
306{
308
310 rw_note (0, __FILE__, __LINE__,
311 "std::includes predicate test disabled");
312 }
313 else {
315 }
316
317 return 0;
318}
319
320
321
323int main (int argc, char *argv[])
324{
325 return rw_test (argc, argv, __FILE__,
326 "lib.includes",
327 0 /* no comment */,
328 run_test,
329 "|-no-predicate#"
330 "|-no-InputIterator# "
331 "|-no-ForwardIterator# "
332 "|-no-BidirectionalIterator# "
333 "|-no-RandomAccessIterator#",
339}
#define T
Definition 17.names.cpp:163
#define y
Definition 17.names.cpp:141
#define line
Definition 17.names.cpp:490
#define res
Definition 17.names.cpp:531
#define x
Definition 17.names.cpp:140
volatile int result
void run_test(const ForwardIterator &dummy, T *, Predicate *pred)
void test_includes(int line, const char *src1, const char *src2, const bool res, const InputIterator1 &it1, const InputIterator2 &it2, const T *, const Predicate *ppred)
int rw_opt_no_fwd_iter
#define TEST(src1, src2, res)
int rw_opt_no_rnd_iter
int rw_opt_no_bidir_iter
int rw_opt_no_input_iter
int rw_opt_no_predicate
const char * type_name()
#define main
#define _RWSTD_NAMESPACE(name)
Definition _defs.h:567
Definition json.h:5363
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 const char * name()
Less(int, int)
static conv_to_bool make(bool val)