Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/utility/binary_search_test.cpp @ 69

Last change on this file since 69 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

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