Converted binary_search_test to lightweight_test.hpp

This commit is contained in:
Andrey Semashev
2020-05-10 23:24:58 +03:00
parent f26a04df65
commit be1294c8a8

View File

@ -3,18 +3,19 @@
// accompanying file LICENSE_1_0.txt or copy at // accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt) // http://www.boost.org/LICENSE_1_0.txt)
#include <boost/detail/binary_search.hpp>
#include <vector> #include <vector>
#include <string> #include <string>
#include <memory> #include <memory>
#include <climits> #include <climits>
#include <iostream> #include <iostream>
#include <cassert> #include <cstddef>
#include <stdlib.h> // for rand(). Would use cstdlib but VC6.4 doesn't put it in std:: #include <stdlib.h> // for rand(). Would use cstdlib but VC6.4 doesn't put it in std::
#include <list> #include <list>
#include <utility>
#include <algorithm> #include <algorithm>
#include <boost/detail/binary_search.hpp>
#include <boost/detail/workaround.hpp> #include <boost/detail/workaround.hpp>
#include <cstddef> #include <boost/core/lightweight_test.hpp>
#if defined(__SGI_STL_PORT) ? defined(__SGI_STL_OWN_IOSTREAMS) : (!defined(__GNUC__) || __GNUC__ > 2) #if defined(__SGI_STL_PORT) ? defined(__SGI_STL_OWN_IOSTREAMS) : (!defined(__GNUC__) || __GNUC__ > 2)
# define USE_SSTREAM # define USE_SSTREAM
@ -32,7 +33,7 @@ namespace {
struct mystring : std::string struct mystring : std::string
{ {
typedef std::string base; typedef std::string base;
mystring(std::string const& x) mystring(std::string const& x)
: base(x) {} : base(x) {}
}; };
@ -180,13 +181,13 @@ template <class Sequence, class Compare>
void test_loop(Sequence& x, Compare cmp, unsigned long test_count) void test_loop(Sequence& x, Compare cmp, unsigned long test_count)
{ {
typedef typename Sequence::const_iterator const_iterator; typedef typename Sequence::const_iterator const_iterator;
for (unsigned long i = 0; i < test_count; ++i) for (unsigned long i = 0; i < test_count; ++i)
{ {
random_sorted_sequence(x); random_sorted_sequence(x);
const const_iterator start = x.begin(); const const_iterator start = x.begin();
const const_iterator finish = x.end(); const const_iterator finish = x.end();
unsigned key = random_number(); unsigned key = random_number();
const const_iterator l = searches<Compare>::lower_bound(start, finish, key, cmp); const const_iterator l = searches<Compare>::lower_bound(start, finish, key, cmp);
const const_iterator u = searches<Compare>::upper_bound(start, finish, key, cmp); const const_iterator u = searches<Compare>::upper_bound(start, finish, key, cmp);
@ -204,38 +205,40 @@ void test_loop(Sequence& x, Compare cmp, unsigned long test_count)
if (p == u) if (p == u)
{ {
assert(found_l); BOOST_TEST(found_l);
found_u = true; found_u = true;
} }
unsigned value = to_int(*p); unsigned value = to_int(*p);
assert(value >= last_value); BOOST_TEST(value >= last_value);
last_value = value; last_value = value;
if (!found_l) if (!found_l)
{ {
++index; ++index;
assert(to_int(*p) < key); BOOST_TEST(to_int(*p) < key);
} }
else if (!found_u) else if (!found_u)
{ {
++count; ++count;
assert(to_int(*p) == key); BOOST_TEST(to_int(*p) == key);
} }
else else
assert(to_int(*p) > key); {
BOOST_TEST(to_int(*p) > key);
}
} }
assert(found_l || l == finish); BOOST_TEST(found_l || l == finish);
assert(found_u || u == finish); BOOST_TEST(found_u || u == finish);
std::pair<const_iterator, const_iterator> std::pair<const_iterator, const_iterator>
range = searches<Compare>::equal_range(start, finish, key, cmp); range = searches<Compare>::equal_range(start, finish, key, cmp);
assert(range.first == l); BOOST_TEST(range.first == l);
assert(range.second == u); BOOST_TEST(range.second == u);
bool found = searches<Compare>::binary_search(start, finish, key, cmp); bool found = searches<Compare>::binary_search(start, finish, key, cmp);
(void)found; (void)found;
assert(found == (u != l)); BOOST_TEST(found == (u != l));
std::cout << "found " << count << " copies of " << key << " at index " << index << "\n"; std::cout << "found " << count << " copies of " << key << " at index " << index << "\n";
} }
} }
@ -256,5 +259,6 @@ int main()
std::cout << "=== testing bidirectional iterators with compare: ===\n"; std::cout << "=== testing bidirectional iterators with compare: ===\n";
test_loop(y, cmp(), 25); test_loop(y, cmp(), 25);
std::cerr << "******TEST PASSED******\n"; std::cerr << "******TEST PASSED******\n";
return 0;
return boost::report_errors();
} }