1 | // Boost.Range library |
---|
2 | // |
---|
3 | // Copyright Thorsten Ottosen 2003-2004. Use, modification and |
---|
4 | // distribution is subject to the Boost Software License, Version |
---|
5 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
6 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
7 | // |
---|
8 | // For more information, see http://www.boost.org/libs/range/ |
---|
9 | // |
---|
10 | |
---|
11 | //#define _MSL_USING_NAMESPACE 1 |
---|
12 | |
---|
13 | #include <boost/detail/workaround.hpp> |
---|
14 | |
---|
15 | #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) |
---|
16 | # pragma warn -8091 // supress warning in Boost.Test |
---|
17 | # pragma warn -8057 // unused argument argc/argv in Boost.Test |
---|
18 | #endif |
---|
19 | |
---|
20 | #include <boost/range.hpp> |
---|
21 | #include <boost/static_assert.hpp> |
---|
22 | #include <boost/type_traits.hpp> |
---|
23 | #include <boost/test/test_tools.hpp> |
---|
24 | #include <boost/config.hpp> |
---|
25 | #include <vector> |
---|
26 | #include <fstream> |
---|
27 | #include <algorithm> |
---|
28 | |
---|
29 | template< typename Container, typename T > |
---|
30 | BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type |
---|
31 | find( Container& c, T value ) |
---|
32 | { |
---|
33 | return std::find( boost::begin( c ), boost::end( c ), value ); |
---|
34 | } |
---|
35 | |
---|
36 | template< typename Container, typename T > |
---|
37 | BOOST_DEDUCED_TYPENAME boost::range_const_iterator<Container>::type |
---|
38 | find( const Container& c, T value ) |
---|
39 | { |
---|
40 | return std::find( boost::begin( c ), boost::end( c ), value ); |
---|
41 | } |
---|
42 | |
---|
43 | template< typename Container, typename T > |
---|
44 | BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type |
---|
45 | find_mutable( Container& c, T value ) |
---|
46 | { |
---|
47 | boost::size( c ); |
---|
48 | boost::end( c ); |
---|
49 | return std::find( boost::begin( c ), boost::end( c ), value ); |
---|
50 | } |
---|
51 | |
---|
52 | template< typename Container, typename T > |
---|
53 | BOOST_DEDUCED_TYPENAME boost::range_const_iterator<Container>::type |
---|
54 | find_const( const Container& c, T value ) |
---|
55 | { |
---|
56 | boost::size( c ); |
---|
57 | boost::end( c ); |
---|
58 | return std::find( boost::begin( c ), boost::end( c ), value ); |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | std::vector<char> |
---|
63 | check_rvalue_return() |
---|
64 | { |
---|
65 | return std::vector<char>( 10, 'm' ); |
---|
66 | } |
---|
67 | |
---|
68 | using namespace boost; |
---|
69 | |
---|
70 | |
---|
71 | void check_char() |
---|
72 | { |
---|
73 | typedef char* char_iterator_t; |
---|
74 | typedef char char_array_t[10]; |
---|
75 | const char* char_s = "a string"; |
---|
76 | char my_string[] = "another string"; |
---|
77 | const unsigned my_string_length = 14; |
---|
78 | char* char_s2 = "a string"; |
---|
79 | |
---|
80 | BOOST_STATIC_ASSERT(( is_same< range_value<char_iterator_t>::type, |
---|
81 | detail::iterator_traits<char_iterator_t>::value_type>::value )); |
---|
82 | BOOST_STATIC_ASSERT(( is_same< range_iterator<char_iterator_t>::type, char_iterator_t >::value )); |
---|
83 | BOOST_STATIC_ASSERT(( is_same< range_const_iterator<char_iterator_t>::type, const char* >::value )); |
---|
84 | BOOST_STATIC_ASSERT(( is_same< range_difference<char_iterator_t>::type, |
---|
85 | ::std::ptrdiff_t >::value )); |
---|
86 | BOOST_STATIC_ASSERT(( is_same< range_size<char_iterator_t>::type, std::size_t >::value )); |
---|
87 | BOOST_STATIC_ASSERT(( is_same< range_result_iterator<char_iterator_t>::type, char_iterator_t >::value )); |
---|
88 | BOOST_STATIC_ASSERT(( is_same< range_result_iterator<const char*>::type, const char* >::value )); |
---|
89 | |
---|
90 | BOOST_STATIC_ASSERT(( is_same< range_value<char_array_t>::type, |
---|
91 | char>::value )); |
---|
92 | BOOST_STATIC_ASSERT(( is_same< range_iterator<char_array_t>::type, char* >::value )); |
---|
93 | BOOST_STATIC_ASSERT(( is_same< range_const_iterator<char_array_t>::type, const char* >::value )); |
---|
94 | BOOST_STATIC_ASSERT(( is_same< range_difference<char_array_t>::type, |
---|
95 | ::std::ptrdiff_t >::value )); |
---|
96 | BOOST_STATIC_ASSERT(( is_same< range_size<char_array_t>::type, std::size_t >::value )); |
---|
97 | BOOST_STATIC_ASSERT(( is_same< range_result_iterator<char_array_t>::type, char* >::value )); |
---|
98 | BOOST_STATIC_ASSERT(( is_same< range_result_iterator<const char_array_t>::type, const char* >::value )); |
---|
99 | |
---|
100 | BOOST_CHECK_EQUAL( begin( char_s ), char_s ); |
---|
101 | std::size_t sz = size( char_s ); |
---|
102 | const char* end1 = begin( char_s ) + sz; |
---|
103 | BOOST_CHECK_EQUAL( end( char_s ), end1 ); |
---|
104 | BOOST_CHECK_EQUAL( empty( char_s ), (char_s == 0 || char_s[0] == char()) ); |
---|
105 | BOOST_CHECK_EQUAL( sz, std::char_traits<char>::length( char_s ) ); |
---|
106 | /* |
---|
107 | BOOST_CHECK_EQUAL( begin( char_s2 ), char_s2 ); |
---|
108 | std::size_t sz2 = size( char_s2 ); |
---|
109 | const char* end12 = begin( char_s2 ) + sz; |
---|
110 | BOOST_CHECK_EQUAL( end( char_s2 ), end12 ); |
---|
111 | BOOST_CHECK_EQUAL( empty( char_s2 ), (char_s2 == 0 || char_s2[0] == char()) ); |
---|
112 | BOOST_CHECK_EQUAL( sz2, std::char_traits<char>::length( char_s2 ) ); |
---|
113 | */ |
---|
114 | BOOST_CHECK_EQUAL( begin( my_string ), my_string ); |
---|
115 | range_iterator<char_array_t>::type end2 = begin( my_string ) + size( my_string ); |
---|
116 | range_iterator<char_array_t>::type end3 = end( my_string ); |
---|
117 | BOOST_CHECK_EQUAL( end3, end2 ); |
---|
118 | BOOST_CHECK_EQUAL( empty( my_string ), (my_string == 0 || my_string[0] == char()) ); |
---|
119 | BOOST_CHECK_EQUAL( size( my_string ), my_string_length ); |
---|
120 | BOOST_CHECK_EQUAL( size( my_string ), std::char_traits<char>::length( my_string ) ); |
---|
121 | |
---|
122 | char to_search = 'n'; |
---|
123 | BOOST_CHECK( find_mutable( char_s, to_search ) != end( char_s ) ); |
---|
124 | BOOST_CHECK( find_const( char_s, to_search ) != end( char_s ) ); |
---|
125 | |
---|
126 | BOOST_CHECK( find_mutable( my_string, to_search ) != end( my_string ) ); |
---|
127 | BOOST_CHECK( find_const( my_string, to_search ) != end( my_string ) ); |
---|
128 | |
---|
129 | BOOST_CHECK( find_mutable( char_s2, to_search ) != end( char_s2 ) ); |
---|
130 | BOOST_CHECK( find_const( char_s2, to_search ) != end( char_s2 ) ); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | |
---|
135 | void check_string() |
---|
136 | { |
---|
137 | check_char(); |
---|
138 | // check_char<volatile char>(); |
---|
139 | // check_char<const char>(); |
---|
140 | // check_char<const volatile char>(); |
---|
141 | |
---|
142 | #ifndef BOOST_NO_STD_WSTRING |
---|
143 | typedef wchar_t* wchar_iterator_t; |
---|
144 | const wchar_t* char_ws = L"a wide string"; |
---|
145 | wchar_t my_wstring[] = L"another wide string"; |
---|
146 | wchar_t* char_ws2 = L"a wide string"; |
---|
147 | |
---|
148 | BOOST_STATIC_ASSERT(( is_same< range_value<wchar_iterator_t>::type, |
---|
149 | detail::iterator_traits<wchar_iterator_t>::value_type>::value )); |
---|
150 | BOOST_STATIC_ASSERT(( is_same< range_iterator<wchar_iterator_t>::type, wchar_iterator_t >::value )); |
---|
151 | BOOST_STATIC_ASSERT(( is_same< range_const_iterator<wchar_iterator_t>::type, const wchar_t* >::value )); |
---|
152 | BOOST_STATIC_ASSERT(( is_same< range_difference<wchar_iterator_t>::type, |
---|
153 | detail::iterator_traits<wchar_iterator_t>::difference_type >::value )); |
---|
154 | BOOST_STATIC_ASSERT(( is_same< range_size<wchar_iterator_t>::type, std::size_t >::value )); |
---|
155 | BOOST_STATIC_ASSERT(( is_same< range_result_iterator<wchar_iterator_t>::type, wchar_iterator_t >::value )); |
---|
156 | BOOST_STATIC_ASSERT(( is_same< range_result_iterator<const wchar_t*>::type, const wchar_t* >::value )); |
---|
157 | |
---|
158 | std::size_t sz = size( char_ws ); |
---|
159 | BOOST_CHECK_EQUAL( begin( char_ws ), char_ws ); |
---|
160 | BOOST_CHECK_EQUAL( end( char_ws ), (begin( char_ws ) + sz) ); |
---|
161 | BOOST_CHECK_EQUAL( empty( char_ws ), (char_ws == 0 || char_ws[0] == wchar_t()) ); |
---|
162 | BOOST_CHECK_EQUAL( sz, std::char_traits<wchar_t>::length( char_ws ) ); |
---|
163 | /* |
---|
164 | std::size_t sz2 = size( char_ws2 ); |
---|
165 | BOOST_CHECK_EQUAL( begin( char_ws2 ), char_ws2 ); |
---|
166 | BOOST_CHECK_EQUAL( end( char_ws2 ), (begin( char_ws2 ) + sz2) ); |
---|
167 | BOOST_CHECK_EQUAL( empty( char_ws2 ), (char_ws2 == 0 || char_ws2[0] == wchar_t()) ); |
---|
168 | BOOST_CHECK_EQUAL( sz2, std::char_traits<wchar_t>::length( char_ws2 ) ); |
---|
169 | */ |
---|
170 | wchar_t to_search = L'n'; |
---|
171 | BOOST_CHECK( find( char_ws, to_search ) != end( char_ws ) ); |
---|
172 | |
---|
173 | #if BOOST_WORKAROUND(_MSC_VER, BOOST_TESTED_AT(1300)) |
---|
174 | |
---|
175 | BOOST_CHECK( find( my_wstring, to_search ) != end( my_wstring ) ); |
---|
176 | |
---|
177 | #endif |
---|
178 | #endif |
---|
179 | |
---|
180 | find( check_rvalue_return(), 'n' ); |
---|
181 | |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | #include <boost/test/unit_test.hpp> |
---|
186 | using boost::unit_test::test_suite; |
---|
187 | |
---|
188 | |
---|
189 | test_suite* init_unit_test_suite( int argc, char* argv[] ) |
---|
190 | { |
---|
191 | test_suite* test = BOOST_TEST_SUITE( "Range Test Suite" ); |
---|
192 | |
---|
193 | test->add( BOOST_TEST_CASE( &check_string ) ); |
---|
194 | |
---|
195 | return test; |
---|
196 | } |
---|
197 | |
---|
198 | |
---|
199 | |
---|
200 | |
---|
201 | |
---|
202 | |
---|