3 // Copyright (C) 2005, 2006 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 2, or (at your option) any later
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
33 // Permission to use, copy, modify, sell, and distribute this software
34 // is hereby granted without fee, provided that the above copyright
35 // notice appears in all copies, and that both that copyright notice
36 // and this permission notice appear in supporting documentation. None
37 // of the above authors, nor IBM Haifa Research Laboratories, make any
38 // representation about the suitability of this software for any
39 // purpose. It is provided "as is" without express or implied
43 * @file cmp_fn_imps.hpp
44 * Containsert a random regression test for a specific container type.
50 cmp(const Cntnr& r_c, const native_type& r_native_c, const std::string& r_call_fn)
52 m_alloc.set_throw_prob(1);
54 const size_t size = r_c.size();
55 const size_t native_size = r_native_c.size();
57 PB_DS_THROW_IF_FAILED(
59 static_cast<unsigned long>(size) << " " <<
60 static_cast<unsigned long>(native_size),
64 const bool empty = r_c.empty();
65 const bool native_empty = r_native_c.empty();
67 PB_DS_THROW_IF_FAILED(
68 empty == native_empty,
69 empty << " " << native_empty,
75 basic_cmp_(r_c, r_native_c);
77 cmp_(r_c, r_native_c);
81 PB_DS_THROW_IF_FAILED( false, "call-fn: " + r_call_fn, & r_c, & r_native_c);
88 basic_cmp_(const Cntnr& r_c, const native_type& r_native_c)
90 PB_DS_SET_DESTRUCT_PRINT
92 if (static_cast<size_t>(std::distance(r_c.begin(), r_c.end())) !=
94 PB_DS_THROW_IF_FAILED(
96 static_cast<unsigned long>(
97 std::distance(r_c.begin(), r_c.end())) <<
98 " " << static_cast<unsigned long>(r_c.size()),
102 typename native_type::const_iterator it = r_native_c.begin();
104 while (it != r_native_c.end())
106 typename native_type::key_type native_key =
107 test_traits::extract_native_key(*it);
109 m_alloc.set_throw_prob(0);
111 const key_type k = native_key;
113 m_alloc.set_throw_prob(1);
115 typename cntnr::const_point_iterator found_it = r_c.find(k);
117 PB_DS_THROW_IF_FAILED(
118 found_it != r_c.end(),
119 test_traits::native_val_to_string(*it),
123 if (!test_traits::cmp(*found_it, * it))
124 PB_DS_THROW_IF_FAILED( false, "", & r_c, & r_native_c);
129 PB_DS_CANCEL_DESTRUCT_PRINT
135 cmp_(const Cntnr& r_c, const native_type& r_native_c)
140 container_traits::order_preserving,
141 back_order_preserving =
142 container_traits::order_preserving&&
143 !pb_ds::detail::is_same<
144 typename std::iterator_traits<
145 typename cntnr::const_iterator>::iterator_category,
146 std::forward_iterator_tag>::value,
147 reverse_iteration = container_traits::reverse_iteration,
148 order_statistics = test_traits::order_statistics,
149 prefix_search = test_traits::prefix_search,
150 has_mapped = !pb_ds::detail::is_same<
151 typename Cntnr::mapped_type,
152 pb_ds::null_mapped_type>::value
155 order_preserving_cmp_imp(r_c, r_native_c,
156 pb_ds::detail::integral_constant<int,order_preserving>());
158 back_order_preserving_cmp_imp(r_c, r_native_c,
159 pb_ds::detail::integral_constant<int,back_order_preserving>());
161 order_statistics_cmp_imp(r_c, r_native_c,
162 pb_ds::detail::integral_constant<int,order_statistics>());
164 prefix_search_cmp_imp(r_c, r_native_c,
165 pb_ds::detail::integral_constant<int,prefix_search>());
167 reverse_iteration_cmp_imp(r_c, r_native_c,
168 pb_ds::detail::integral_constant<int,reverse_iteration>());
170 lower_bound_cmp_imp(r_c, r_native_c,
171 pb_ds::detail::integral_constant<int,order_preserving>());
173 upper_bound_cmp_imp(r_c, r_native_c,
174 pb_ds::detail::integral_constant<int,order_preserving>());
180 order_preserving_cmp_imp(const Cntnr& /*r_c*/, const native_type& /*r_native_c*/, pb_ds::detail::false_type)
186 order_preserving_cmp_imp(const Cntnr& r_c, const native_type& r_native_c, pb_ds::detail::true_type)
188 PB_DS_SET_DESTRUCT_PRINT
190 typename cntnr::const_iterator b = r_c.begin();
191 typename cntnr::const_iterator e = r_c.end();
193 typename native_type::const_iterator native_b = r_native_c.begin();
194 typename native_type::const_iterator native_e = r_native_c.end();
198 it_cmp_imp(b, e, native_b, native_e);
202 PB_DS_THROW_IF_FAILED( false, "", & r_c, & r_native_c)
207 back_it_cmp_imp(b, e, native_b, native_e);
211 PB_DS_THROW_IF_FAILED( false, "", & r_c, & r_native_c)
214 PB_DS_CANCEL_DESTRUCT_PRINT
220 back_order_preserving_cmp_imp(const Cntnr& /*r_c*/, const native_type& /*r_native_c*/, pb_ds::detail::false_type)
226 back_order_preserving_cmp_imp(const Cntnr& r_c, const native_type& r_native_c, pb_ds::detail::true_type)
228 PB_DS_SET_DESTRUCT_PRINT
230 typename cntnr::const_iterator b = r_c.begin();
231 typename cntnr::const_iterator e = r_c.end();
233 typename native_type::const_iterator native_b = r_native_c.begin();
234 typename native_type::const_iterator native_e = r_native_c.end();
236 it_cmp_imp(b, e, native_b, native_e);
238 PB_DS_CANCEL_DESTRUCT_PRINT
244 reverse_iteration_cmp_imp(const Cntnr& /*r_c*/, const native_type& /*r_native_c*/, pb_ds::detail::false_type)
250 reverse_iteration_cmp_imp(const Cntnr& r_c, const native_type& r_native_c, pb_ds::detail::true_type)
252 PB_DS_SET_DESTRUCT_PRINT
254 typename cntnr::const_reverse_iterator b = r_c.rbegin();
255 typename cntnr::const_reverse_iterator e = r_c.rend();
257 typename native_type::const_reverse_iterator native_b = r_native_c.rbegin();
258 typename native_type::const_reverse_iterator native_e = r_native_c.rend();
262 it_cmp_imp(b, e, native_b, native_e);
266 PB_DS_THROW_IF_FAILED( false, "", & r_c, & r_native_c)
271 back_it_cmp_imp(b, e, native_b, native_e);
275 PB_DS_THROW_IF_FAILED( false, "", & r_c, & r_native_c)
278 PB_DS_CANCEL_DESTRUCT_PRINT
284 order_statistics_cmp_imp(const Cntnr& /*r_c*/, const native_type& /*r_native_c*/, pb_ds::detail::false_type)
290 order_statistics_cmp_imp(const Cntnr& r_c, const native_type& r_native_c, pb_ds::detail::true_type)
293 m_alloc.set_throw_prob(0);
296 test_traits::generate_key(m_g, m_m);
298 m_alloc.set_throw_prob(1);
300 const size_type order = r_c.order_of_key(k);
302 const size_type native_order = std::distance(
304 r_native_c.lower_bound(test_traits::native_key(k)));
306 PB_DS_THROW_IF_FAILED(
307 order == native_order,
308 test_traits::key_to_string(k) << " " <<
309 static_cast<unsigned long>(order) << " " <<
310 static_cast<unsigned long>(native_order),
315 const size_type rand_ord =
316 static_cast<size_t>(m_g.get_unsigned_long(
318 2* static_cast<unsigned long>(m_m)));
320 typename cntnr::const_iterator it =
321 r_c.find_by_order(rand_ord);
323 typename native_type::const_iterator native_it =
326 std::advance(native_it, std::min(rand_ord, r_native_c.size()));
328 if (it == r_c.end()&& native_it != r_native_c.end())
329 PB_DS_THROW_IF_FAILED(
331 static_cast<unsigned long>(rand_ord),
335 if (it != r_c.end()&& native_it == r_native_c.end())
336 PB_DS_THROW_IF_FAILED(
338 static_cast<unsigned long>(rand_ord),
342 if (it != r_c.end()&& native_it != r_native_c.end())
343 PB_DS_THROW_IF_FAILED(
344 test_traits::cmp(*it, * native_it),
345 static_cast<unsigned long>(rand_ord),
353 prefix_search_cmp_imp(const Cntnr& /*r_c*/, const native_type& /*r_native_c*/, pb_ds::detail::false_type)
359 prefix_search_cmp_imp(const Cntnr& r_c, const native_type& r_native_c, pb_ds::detail::true_type)
361 PB_DS_SET_DESTRUCT_PRINT
363 m_alloc.set_throw_prob(0);
366 test_traits::generate_key(m_g, m_m);
368 m_alloc.set_throw_prob(1);
374 typename Cntnr::const_iterator,
375 typename Cntnr::const_iterator>
378 const pref_ret_t pref_ret = r_c.prefix_range(k);
380 typename native_type::const_iterator native_start_it =
383 while (native_start_it != r_native_c.end()&&
384 !test_traits::prefix_match(
386 test_traits::extract_native_key(*native_start_it)))
389 typename native_type::const_iterator native_end_it =
394 if (native_end_it != r_native_c.end())
397 while (native_end_it != r_native_c.end()&&
398 test_traits::prefix_match(
400 test_traits::extract_native_key(*native_end_it)));
402 it_cmp_imp( pref_ret.first, pref_ret.second, native_start_it, native_end_it);
406 PB_DS_THROW_IF_FAILED(
413 PB_DS_CANCEL_DESTRUCT_PRINT
417 template<typename Const_It, class Const_Native_It>
420 it_cmp_imp(Const_It b, Const_It e, Const_Native_It native_b, Const_Native_It native_e)
422 PB_DS_SET_DESTRUCT_PRINT
424 if (std::distance(b, e) != std::distance(native_b, native_e))
426 const size_t dist = std::distance(b, e);
428 const size_t native_dist = std::distance(native_b, native_e);
430 PB_DS_THROW_IF_FAILED(
432 static_cast<unsigned long>(dist) << " "
433 << static_cast<unsigned long>(native_dist),
440 PB_DS_THROW_IF_FAILED( native_b != native_e, "", m_p_c, & m_native_c);
442 if (!test_traits::cmp(*b, * native_b))
443 PB_DS_THROW_IF_FAILED(
445 test_traits::val_to_string(*b) << " " <<
446 test_traits::val_to_string(*native_b),
454 PB_DS_THROW_IF_FAILED( native_b == native_e, "", m_p_c, & m_native_c);
456 PB_DS_CANCEL_DESTRUCT_PRINT
460 template<typename Const_It, class Const_Native_It>
463 back_it_cmp_imp(Const_It b, Const_It e, Const_Native_It native_b, Const_Native_It native_e)
465 PB_DS_SET_DESTRUCT_PRINT
469 PB_DS_THROW_IF_FAILED(
470 native_b != native_e,
471 test_traits::val_to_string(*native_e),
478 PB_DS_THROW_IF_FAILED(
479 test_traits::cmp(*e, * native_e),
480 test_traits::val_to_string(*e) <<
481 test_traits::val_to_string(*native_e),
486 PB_DS_THROW_IF_FAILED(
487 native_b == native_e,
488 test_traits::val_to_string(*native_e),
492 PB_DS_CANCEL_DESTRUCT_PRINT
498 lower_bound_cmp_imp(const Cntnr& /*r_c*/, const native_type& /*r_native_c*/, pb_ds::detail::false_type)
504 lower_bound_cmp_imp(const Cntnr& r_c, const native_type& r_native_c, pb_ds::detail::true_type)
506 PB_DS_SET_DESTRUCT_PRINT
508 m_alloc.set_throw_prob(0);
511 test_traits::generate_key(m_g, m_m);
513 m_alloc.set_throw_prob(1);
515 typename cntnr::const_iterator it = r_c.lower_bound(k);
517 typename native_type::key_type native_k = test_traits::native_key(k);
519 typename native_type::const_iterator native_it =
520 r_native_c.lower_bound(native_k);
522 if (it != r_c.end()&& native_it == r_native_c.end())
523 PB_DS_THROW_IF_FAILED(
525 "it: " + test_traits::val_to_string(*it) + "\n\n",
529 if (it == r_c.end()&& native_it != r_native_c.end())
530 PB_DS_THROW_IF_FAILED(
532 "native_it: " + test_traits::val_to_string(*native_it) + "\n\n",
536 if (it != r_c.end()&& !test_traits::cmp(*it, * native_it))
537 PB_DS_THROW_IF_FAILED(
539 "key: " + test_traits::key_to_string(k) + "\n\n" +
540 "it: " + test_traits::val_to_string(*it) + "\n\n" +
541 "native_it: " + test_traits::val_to_string(*native_it) + "\n\n",
545 PB_DS_CANCEL_DESTRUCT_PRINT
551 upper_bound_cmp_imp(const Cntnr& /*r_c*/, const native_type& /*r_native_c*/, pb_ds::detail::false_type)
557 upper_bound_cmp_imp(const Cntnr& r_c, const native_type& r_native_c, pb_ds::detail::true_type)
559 PB_DS_SET_DESTRUCT_PRINT
561 m_alloc.set_throw_prob(0);
564 test_traits::generate_key(m_g, m_m);
566 m_alloc.set_throw_prob(1);
568 typename cntnr::const_iterator it = r_c.upper_bound(k);
570 typename native_type::key_type native_k = test_traits::native_key(k);
572 typename native_type::const_iterator native_it =
573 r_native_c.upper_bound(native_k);
575 if (it == r_c.end()&& native_it != r_native_c.end())
576 PB_DS_THROW_IF_FAILED(
578 "key: " + test_traits::key_to_string(k) + "\n\n" +
579 "native_it: " + test_traits::val_to_string(*native_it) + "\n\n",
583 if (it != r_c.end()&& native_it == r_native_c.end())
584 PB_DS_THROW_IF_FAILED(
586 "key: " + test_traits::key_to_string(k) + "\n\n" +
587 "it: " + test_traits::val_to_string(*it) + "\n\n",
591 if (it != r_c.end()&& !test_traits::cmp(*it, * native_it))
592 PB_DS_THROW_IF_FAILED(
594 "key: " + test_traits::key_to_string(k) + "\n\n" +
595 "it: " + test_traits::val_to_string(*it) + "\n\n" +
596 "native_it: " + test_traits::val_to_string(*native_it) + "\n\n",
600 PB_DS_CANCEL_DESTRUCT_PRINT