Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
25.equal.cpp
Go to the documentation of this file.
1
29#include <algorithm> // for equal
30#include <functional> // for equal_to
31#include <cstddef> // for size_t
32
33#include <rw_alg_test.h>
34#include <rw_value.h> // for UserClass
35#include <rw_driver.h> // for rw_test()...
36
37
40
41// disable explicit instantiation for compilers (like MSVC)
42// that can't handle it
43#ifndef _RWSTD_NO_EXPLICIT_INSTANTIATION
44
45template
46bool
50
51template
52bool
57
58#endif // _RWSTD_NO_EXPLICIT_INSTANTIATION
59
60} // namespace std
61
62
63// exercises std::equal()
64template <class InputIterator1, class InputIterator2, class T>
65void test_equal (std::size_t N,
66 const InputIterator1 &it1_dummy,
67 const InputIterator2 &it2_dummy,
68 T*,
69 const char *predicate)
70{
71 static const char* const it1name = type_name (it1_dummy, (T*)0);
72 static const char* const it2name = type_name (it2_dummy, (T*)0);
73
74 rw_info (0, 0, 0, "std::equal (%s, %1$s, %s%{?}, %s%{;})",
75 it1name, it2name, 0 != predicate, predicate);
76
77 // generate sequential values for each default constructed UserClass
78 // for both lists
80
81 // use ::operator new() to prevent default initialization
82 const std::size_t size = N * sizeof (UserClass);
83 UserClass *buf1 = _RWSTD_STATIC_CAST (UserClass*, ::operator new (size));
84 UserClass *buf2 = _RWSTD_STATIC_CAST (UserClass*, ::operator new (size));
85
86 const std::size_t mid_inx = N / 2;
87
88 for (std::size_t i = 0; i != N; ++i) {
89
90 // default-construct a new UserClass at the end of the array
91 new (buf1 + i) UserClass ();
92
93 // build a nearly identical array only missing the N/2-th element
94 if (i != mid_inx)
95 new (buf2 + i) UserClass ();
96 }
97
98 new (buf2 + mid_inx) UserClass ();
99
100 for (std::size_t i = 0; i != N; ++i) {
101
102 // exercise 25.1.8 - std::equal()
103 std::size_t last_n_op_eq = UserClass::n_total_op_eq_;
104
105 UserClass* const buf1_end = buf1 + i + 1;
106 UserClass* const buf2_end = buf2 + i + 1;
107
108 const InputIterator1 first1 =
109 make_iter (buf1, buf1, buf1_end, it1_dummy);
110
111 const InputIterator1 last1 =
112 make_iter (buf1_end, buf1_end, buf1_end, it1_dummy);
113
114 const InputIterator2 first2 =
115 make_iter (buf2, buf2, buf2_end, it2_dummy);
116
117 bool res;
118
119 if (predicate) {
120 res = std::equal (first1, last1, first2,
121 std::equal_to<UserClass>());
122 }
123 else {
124 res = std::equal (first1, last1, first2);
125 }
126
127 // verify 25.1.8, p1
128 int success = (res && i < mid_inx) || (!res && mid_inx <= i);
129
130 const bool equal_expected = i < mid_inx;
131
132 rw_assert (success, 0, __LINE__,
133 "%zu. equal(%s, %2$s, %s%{?}, %s%{;}) == %d, got %d",
134 i + 1, it1name, it2name,
135 0 != predicate, predicate,
136 equal_expected, res);
137
138 // verify 25.1.8, p2
139 success = UserClass::n_total_op_eq_ - last_n_op_eq <= (N + 1);
140 rw_assert (success, 0, __LINE__,
141 "%zu. equal(%s, %2$s, %s%s%{?}, %s%{;}) complexity: "
142 "%zu <= %zu",
143 i + 1, it1name, it2name,
144 0 != predicate, predicate,
146
147 if (!success)
148 break;
149 }
150
151 ::operator delete (buf1);
152 ::operator delete (buf2);
153}
154
155
157/* extern */ int rw_opt_no_input_iter; // --no-InputIterator
158/* extern */ int rw_opt_no_fwd_iter; // --no-ForwardIterator
159/* extern */ int rw_opt_no_bidir_iter; // --no-BidirectionalIterator
160/* extern */ int rw_opt_no_rnd_iter; // --no-RandomAccessIterator
161/* extern */ int rw_opt_no_predicate; // --no-Predicate
162
163
164template <class InputIterator, class T>
165void test_equal (const InputIterator &dummy, T*, const char *predicate)
166{
167 static const std::size_t N = 1024;
168
170 rw_note (0, __FILE__, __LINE__, "InputIterator test disabled");
171 }
172 else {
174 (UserClass*)0, (UserClass*)0), (UserClass*)0,
175 predicate);
176 }
177
178 if (rw_opt_no_fwd_iter) {
179 rw_note (0, __FILE__, __LINE__, "ForwardIterator test disabled");
180 }
181 else {
183 }
184
186 rw_note (0, __FILE__, __LINE__, "BidirectionalIterator test disabled");
187 }
188 else {
190 }
191
192 if (rw_opt_no_rnd_iter) {
193 rw_note (0, __FILE__, __LINE__, "RandomAccessIterator test disabled");
194 }
195 else {
197 predicate);
198 }
199}
200
201
202
204static void
205test_equal (const char *predicate)
206{
207 rw_info (0, 0, 0,
208 "template <class %s, class %s> "
209 "bool std::equal (%1$s, %1$s, %2$s%{?}, %s%{;})",
210 "InputIterator1", "InputIterator2",
211 0 != predicate, predicate);
212
214 rw_note (0, __FILE__, __LINE__, "InputIterator test disabled");
215 }
216 else {
218 (UserClass*)0), (UserClass*)0, predicate);
219 }
220
221 if (rw_opt_no_fwd_iter) {
222 rw_note (0, __FILE__, __LINE__, "ForwardIterator test disabled");
223 }
224 else {
226 }
227
229 rw_note (0, __FILE__, __LINE__, "BidirectionalIterator test disabled");
230 }
231 else {
233 }
234
235 if (rw_opt_no_rnd_iter) {
236 rw_note (0, __FILE__, __LINE__, "RandomAccessIterator test disabled");
237 }
238 else {
240 }
241
242}
243
244
246static int
247run_test (int, char*[])
248{
249 test_equal (0);
250
252 rw_note (0, __FILE__, __LINE__, "Predicate test disabled");
253 }
254 else {
255 test_equal ("std::equal_to<UserClass>");
256 }
257
258 return 0;
259}
260
261
263int main (int argc, char *argv[])
264{
265 return rw_test (argc, argv, __FILE__,
266 "lib.alg.equal",
267 0 /* no comment */, run_test,
268 "|-no-ForwardIterator#"
269 "|-no-BidirectionalIterator#"
270 "|-no-RandomAccessIterator#"
271 "|-no-Predicate",
276}
#define N
Definition 17.names.cpp:157
#define i
Definition 17.names.cpp:126
#define T
Definition 17.names.cpp:163
#define res
Definition 17.names.cpp:531
bool equal(const T &x, const U &y)
int rw_opt_no_fwd_iter
Definition 25.equal.cpp:158
int rw_opt_no_rnd_iter
Definition 25.equal.cpp:160
int rw_opt_no_bidir_iter
Definition 25.equal.cpp:159
int rw_opt_no_input_iter
Definition 25.equal.cpp:157
int rw_opt_no_predicate
Definition 25.equal.cpp:161
void test_equal(std::size_t N, const InputIterator1 &it1_dummy, const InputIterator2 &it2_dummy, T *, const char *predicate)
Definition 25.equal.cpp:65
const char * type_name()
#define main
#define _RWSTD_STATIC_CAST(T, x)
Definition _defs.h:534
#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 gen_seq_2lists()
Definition alg_test.cpp:47
_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 _RWSTD_SIZE_T n_total_op_eq_
Definition rw_value.h:132
static int(* gen_)()
Definition rw_value.h:122