42#ifndef _RWSTD_NO_EXPLICIT_INSTANTIATION
96 static const char*
name () {
return "Less"; }
99 void operator= (
Less&);
106template <
class Iterator,
class CopyIterator,
class T,
class Predicate>
110 const std::size_t mid,
112 const CopyIterator &itc,
118 const RandIter rand_it(0, 0, 0);
120 const char*
const itname =
122 const char*
const outname =
"RandomAccessIterator";
123 const char*
const fname = copy ?
"partial_sort_copy" :
"partial_sort";
124 const char*
const funname = ppred ? Predicate::name() :
"operator<()";
129 const std::size_t
nsrc = src ? std::strlen (src) :
N;
131 T*
const xsrc = src ? T::from_char (src,
nsrc) :
new T[
nsrc];
132 T*
const xdst =
new T [mid];
133 T* res_x = copy ? xdst : xsrc;
135 T*
const xsrc_end = xsrc +
nsrc;
136 T*
const xdst_end = xdst + mid;
142 CopyIterator first_c =
make_iter (xsrc, xsrc, xsrc_end, itc);
143 CopyIterator last_c =
make_iter (xsrc_end, xsrc, xsrc_end, itc);
145 RandIter res_first =
make_iter (xdst, xdst, xdst_end, rand_it);
146 RandIter res_last =
make_iter (xdst_end, xdst, xdst_end, rand_it);
149 RandIter
result (0, 0, 0);
151 std::size_t last_n_op_lt = T::n_total_op_lt_;
155 result = std::partial_sort_copy (
156 first_c, last_c, res_first, res_last, pred);
158 result = std::partial_sort_copy (
159 first_c, last_c, res_first, res_last);
163 std::partial_sort (first,
middle,
last, pred);
171 success =
result.cur_ == res_first.cur_ + mid;
173 "line %d: %s<%s%{?}, %s%{;}%{?}, %s%{;}> (): "
174 "returned iterator it is invalid: got result + %td, "
175 "expected result + %zu",
176 __LINE__, fname, itname, copy, outname, ppred, funname,
177 result.cur_ - res_first.cur_, mid);
184 "line %d: %s<%s%{?}, %s%{;}%{?}, %s%{;}> "
185 "(\"%s\", %zu, ...): ==> \"%{X=*.*}\" not sorted",
186 __LINE__, fname, itname, copy, outname, ppred, funname,
187 src, mid,
int (
sizeof (*res_x)),
int (mid), -1, res_x);
191 "line %d: %s<%s%{?}, %s%{;}%{?}, %s%{;}> "
192 "(%zu, %zu, ...): not sorted",
193 __LINE__, fname, itname, copy, outname, ppred, funname,
199 int max_el = res_x[0].data_.val_;
201 for ( ;
j < mid;
j++)
202 max_el = max_el < res_x[
j].data_.val_ ? res_x[
j].data_.val_ : max_el;
207 if (max_el > xsrc[
j].data_.val_)
210 success =
tmp <= mid;
214 success = max_el <= xsrc[
j].data_.val_;
225 "line %d: %s<%s%{?}, %s%{;}%{?}, %s%{;}> "
226 "(\"%s\", %zu, ...): ==> \"%{X=*.*}\" got less element "
227 "%{?}%#c%{;} in the unsorted part", __LINE__, fname,
228 itname, copy, outname, ppred, funname, src, mid,
229 int (
sizeof (*res_x)),
int (copy ? mid :
nsrc), -1, res_x,
230 !copy, xsrc[
j].data_.val_);
234 "line %d: %s<%s%{?}, %s%{;}%{?}, %s%{;}> "
235 "(%zu, %zu, ...): got less element in the unsorted part",
236 __LINE__, fname, itname, copy, outname, ppred, funname,
244 ppred ? Predicate::funcalls_ : T::n_total_op_lt_ - last_n_op_lt;
245 std::size_t exp_ops = 7 *
nsrc *
::ilog2 (mid > 1 ? mid : 2);
246 success = 2 * n_ops <= exp_ops;
248 "line %d: %s<%s%{?}, %s%{;}%{?}, %s%{;}> (): complexity "
249 "for length %zu is %zu, expected no more than %zu",
250 __LINE__, fname, itname, copy, outname, ppred, funname,
251 nsrc, n_ops, exp_ops / 2);
271template <
class Iterator,
class CopyIterator,
class T,
class Predicate>
274 const CopyIterator &itc,
279 const char*
const itname =
281 const char*
const outname =
"RandomAccessIterator";
282 const char*
const fname = copy ?
"partial_sort_copy" :
"partial_sort";
283 const char*
const funname = ppred ? Predicate::name() :
"operator<()";
286 "%{?}%s %{;}std::%s (%s, %4$s, %s%{?}, %2$s%{;}%{?}, %s%{;})",
287 copy, outname, fname, itname, copy ? outname : itname,
288 copy, ppred, funname);
290#define TEST(src, mid) \
291 test_partial_sort (__LINE__, src, 0, mid, it, itc, (T*)0, ppred, copy)
301 TEST (
"hgfedcba", 4);
302 TEST (
"ihgfedcba", 4);
303 TEST (
"jihgfedcba", 5);
311 TEST (
"abcdefgh", 4);
312 TEST (
"abcdefghi", 4);
313 TEST (
"abcdefghij", 5);
318 TEST (
"ddbbccaa", 4);
319 TEST (
"ddeebbccaa", 5);
324 TEST (
"ddbbccaa", 8);
325 TEST (
"ddeebbccaa", 10);
327 TEST (
"aaaaaaaaaa", 5);
328 TEST (
"ababababab", 4);
329 TEST (
"bababababa", 4);
335 "%{?}%s %{;}std::%s (%s, %4$s, %s%{?}, %2$s%{;}%{?}, %s%{;})"
336 ": complexity test disabled",
337 copy, outname, fname, itname, copy ? outname : itname,
338 copy, ppred, funname);
343 "%{?}%s %{;}std::%s (%s, %4$s, %s%{?}, %2$s%{;}%{?}, %s%{;})"
345 copy, outname, fname, itname, copy ? outname : itname,
346 copy, ppred, funname);
348 for (std::size_t
i = 1;
i <
N;
i++)
356template <
class T,
class Predicate>
363 "template <class %s%{?}, class %s%{;}%{?}, class %s%{;}> "
364 "%s std::partial_sort%{?}_copy%{;} (%1$s%{?}, %1$s%{;}, %1$s"
365 "%{?}, %3$s, %3$s%{;}%{?}, %5$s%{;})",
366 copy ?
"InputIterator" :
"RandomAccessIterator",
367 copy,
"RandomAccessIterator", ppred,
"StrictWeakComp",
368 copy ?
"RandomAccessIterator" :
"void",
369 copy, !copy, copy, ppred);
378 (
T*)0, ppred,
false);
383 rw_note (0, __FILE__, __LINE__,
"InputIterator test disabled");
391 rw_note (0, __FILE__, __LINE__,
"ForwardIterator test disabled");
399 rw_note (0, __FILE__, __LINE__,
"BidirectionalIterator test disabled");
407 rw_note (0, __FILE__, __LINE__,
"RandomAccessIterator test disabled");
425 rw_note (0, __FILE__, __LINE__,
426 "std::partial_sort%{?}_copy%{;} predicate test disabled",
443 rw_note (0, __FILE__, __LINE__,
444 "std::partial_sort test disabled");
451 rw_note (0, __FILE__, __LINE__,
452 "std::partial_sort_copy test disabled");
464int main (
int argc,
char *argv[])
466 return rw_test (argc, argv, __FILE__,
471 "|-no-partial_sort# "
472 "|-no-partial_sort_copy# "
473 "|-no-InputIterator# "
474 "|-no-ForwardIterator# "
475 "|-no-BidirectionalIterator# "
476 "|-no-RandomAccessIterator# "
const MyBuffer< char > nsrc[]
void run_test(const ForwardIterator &dummy, T *, Predicate *pred)
void test_partial_sort(int line, const char *src, const std::size_t N, const std::size_t mid, const Iterator &it, const CopyIterator &itc, const T *, const Predicate *ppred, bool copy)
int rw_opt_no_partial_sort
int rw_opt_no_partial_sort_copy
#define _RWSTD_NAMESPACE(name)
T * make_iter(T *cur, const T *, const T *, T *)
_TEST_EXPORT unsigned ilog2(_RWSTD_SIZE_T)
bool is_sorted_lt(InputIterator first, InputIterator last)
_TEST_EXPORT int gen_rnd()
_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 *,...)
_TEST_EXPORT int rw_note(int, const char *, int, const char *,...)
_TEST_EXPORT int rw_assert(int, const char *, int, const char *,...)
static const char * name()
conv_to_bool operator()(const T &x, const T &y)
static std::size_t funcalls_
static conv_to_bool make(bool val)