binary_search_test.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // (C) Copyright David Abrahams 2000.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <vector>
  6. #include <string>
  7. #include <memory>
  8. #include <climits>
  9. #include <iostream>
  10. #include <cassert>
  11. #include <stdlib.h> // for rand(). Would use cstdlib but VC6.4 doesn't put it in std::
  12. #include <list>
  13. #include <algorithm>
  14. #include <boost/detail/binary_search.hpp>
  15. #include <boost/detail/workaround.hpp>
  16. #include <cstddef>
  17. #if defined(__SGI_STL_PORT) ? defined(__SGI_STL_OWN_IOSTREAMS) : (!defined(__GNUC__) || __GNUC__ > 2)
  18. # define USE_SSTREAM
  19. #endif
  20. #ifdef USE_SSTREAM
  21. # include <sstream>
  22. #else
  23. # include <strstream>
  24. #endif
  25. namespace {
  26. // In order to get ADL to find the comparison operators defined below, they have
  27. struct mystring : std::string
  28. {
  29. typedef std::string base;
  30. mystring(std::string const& x)
  31. : base(x) {}
  32. };
  33. typedef std::vector<mystring> string_vector;
  34. const std::size_t sequence_length = 1000;
  35. unsigned random_number()
  36. {
  37. return static_cast<unsigned>(::rand()) % sequence_length;
  38. }
  39. # ifndef USE_SSTREAM
  40. class unfreezer {
  41. public:
  42. unfreezer(std::ostrstream& s) : m_stream(s) {}
  43. ~unfreezer() { m_stream.freeze(false); }
  44. private:
  45. std::ostrstream& m_stream;
  46. };
  47. # endif
  48. template <class T>
  49. void push_back_random_number_string(T& seq)
  50. {
  51. unsigned value = random_number();
  52. # if defined(__SGI_STL_PORT) ? defined(__SGI_STL_OWN_IOSTREAMS) : (!defined(__GNUC__) || __GNUC__ > 2)
  53. std::ostringstream s;
  54. s << value;
  55. seq.push_back(s.str());
  56. # else
  57. std::ostrstream s;
  58. auto unfreezer unfreeze(s);
  59. s << value << char(0);
  60. seq.push_back(std::string(s.str()));
  61. # endif
  62. }
  63. inline unsigned to_int(unsigned x) { return x; }
  64. inline unsigned to_int(const std::string& x) { return atoi(x.c_str()); }
  65. struct cmp
  66. {
  67. template <class A1, class A2>
  68. inline bool operator()(const A1& a1, const A2& a2) const
  69. {
  70. return to_int(a1) < to_int(a2);
  71. }
  72. };
  73. inline bool operator<(const mystring& x, const unsigned y)
  74. {
  75. return to_int(x) < y;
  76. }
  77. inline bool operator<(const unsigned y, const mystring& x)
  78. {
  79. return y < to_int(x);
  80. }
  81. template <class T>
  82. void sort_by_value(T& x);
  83. template <class T>
  84. void sort_by_value_(T& v, long)
  85. {
  86. std::sort(v.begin(), v.end(), cmp());
  87. }
  88. template <class T>
  89. void random_sorted_sequence(T& seq)
  90. {
  91. seq.clear();
  92. for (std::size_t i = 0; i < sequence_length; ++i)
  93. {
  94. push_back_random_number_string(seq);
  95. }
  96. sort_by_value(seq);
  97. }
  98. template <class T, class A>
  99. void sort_by_value_(std::list<T,A>& l, int)
  100. {
  101. # if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1) && !defined(__SGI_STL_PORT)
  102. // VC6's standard lib doesn't have a template member function for list::sort()
  103. std::vector<T> seq;
  104. seq.reserve(sequence_length);
  105. std::copy(l.begin(), l.end(), std::back_inserter(seq));
  106. sort_by_value(seq);
  107. std::copy(seq.begin(), seq.end(), l.begin());
  108. # else
  109. l.sort(cmp());
  110. # endif
  111. }
  112. template <class T>
  113. void sort_by_value(T& x)
  114. {
  115. (sort_by_value_)(x, 1);
  116. }
  117. // A way to select the comparisons with/without a Compare parameter for testing.
  118. template <class Compare> struct searches
  119. {
  120. template <class Iterator, class Key>
  121. static Iterator lower_bound(Iterator start, Iterator finish, Key key, Compare cmp)
  122. { return boost::detail::lower_bound(start, finish, key, cmp); }
  123. template <class Iterator, class Key>
  124. static Iterator upper_bound(Iterator start, Iterator finish, Key key, Compare cmp)
  125. { return boost::detail::upper_bound(start, finish, key, cmp); }
  126. template <class Iterator, class Key>
  127. static std::pair<Iterator, Iterator> equal_range(Iterator start, Iterator finish, Key key, Compare cmp)
  128. { return boost::detail::equal_range(start, finish, key, cmp); }
  129. template <class Iterator, class Key>
  130. static bool binary_search(Iterator start, Iterator finish, Key key, Compare cmp)
  131. { return boost::detail::binary_search(start, finish, key, cmp); }
  132. };
  133. struct no_compare {};
  134. template <> struct searches<no_compare>
  135. {
  136. template <class Iterator, class Key>
  137. static Iterator lower_bound(Iterator start, Iterator finish, Key key, no_compare)
  138. { return boost::detail::lower_bound(start, finish, key); }
  139. template <class Iterator, class Key>
  140. static Iterator upper_bound(Iterator start, Iterator finish, Key key, no_compare)
  141. { return boost::detail::upper_bound(start, finish, key); }
  142. template <class Iterator, class Key>
  143. static std::pair<Iterator, Iterator> equal_range(Iterator start, Iterator finish, Key key, no_compare)
  144. { return boost::detail::equal_range(start, finish, key); }
  145. template <class Iterator, class Key>
  146. static bool binary_search(Iterator start, Iterator finish, Key key, no_compare)
  147. { return boost::detail::binary_search(start, finish, key); }
  148. };
  149. template <class Sequence, class Compare>
  150. void test_loop(Sequence& x, Compare cmp, unsigned long test_count)
  151. {
  152. typedef typename Sequence::const_iterator const_iterator;
  153. for (unsigned long i = 0; i < test_count; ++i)
  154. {
  155. random_sorted_sequence(x);
  156. const const_iterator start = x.begin();
  157. const const_iterator finish = x.end();
  158. unsigned key = random_number();
  159. const const_iterator l = searches<Compare>::lower_bound(start, finish, key, cmp);
  160. const const_iterator u = searches<Compare>::upper_bound(start, finish, key, cmp);
  161. bool found_l = false;
  162. bool found_u = false;
  163. std::size_t index = 0;
  164. std::size_t count = 0;
  165. unsigned last_value = 0;
  166. (void)last_value;
  167. for (const_iterator p = start; p != finish; ++p)
  168. {
  169. if (p == l)
  170. found_l = true;
  171. if (p == u)
  172. {
  173. assert(found_l);
  174. found_u = true;
  175. }
  176. unsigned value = to_int(*p);
  177. assert(value >= last_value);
  178. last_value = value;
  179. if (!found_l)
  180. {
  181. ++index;
  182. assert(to_int(*p) < key);
  183. }
  184. else if (!found_u)
  185. {
  186. ++count;
  187. assert(to_int(*p) == key);
  188. }
  189. else
  190. assert(to_int(*p) > key);
  191. }
  192. assert(found_l || l == finish);
  193. assert(found_u || u == finish);
  194. std::pair<const_iterator, const_iterator>
  195. range = searches<Compare>::equal_range(start, finish, key, cmp);
  196. assert(range.first == l);
  197. assert(range.second == u);
  198. bool found = searches<Compare>::binary_search(start, finish, key, cmp);
  199. (void)found;
  200. assert(found == (u != l));
  201. std::cout << "found " << count << " copies of " << key << " at index " << index << "\n";
  202. }
  203. }
  204. }
  205. int main()
  206. {
  207. string_vector x;
  208. std::cout << "=== testing random-access iterators with <: ===\n";
  209. test_loop(x, no_compare(), 25);
  210. std::cout << "=== testing random-access iterators with compare: ===\n";
  211. test_loop(x, cmp(), 25);
  212. std::list<mystring> y;
  213. std::cout << "=== testing bidirectional iterators with <: ===\n";
  214. test_loop(y, no_compare(), 25);
  215. std::cout << "=== testing bidirectional iterators with compare: ===\n";
  216. test_loop(y, cmp(), 25);
  217. std::cerr << "******TEST PASSED******\n";
  218. return 0;
  219. }