2 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
12 #include "cppunit/cppunit_proxy.h"
14 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
21 class AlgTest
: public CPPUNIT_NS::TestCase
23 CPPUNIT_TEST_SUITE(AlgTest
);
24 CPPUNIT_TEST(min_max
);
25 CPPUNIT_TEST(count_test
);
26 CPPUNIT_TEST(sort_test
);
27 CPPUNIT_TEST(search_n_test
);
28 CPPUNIT_TEST(find_first_of_test
);
29 CPPUNIT_TEST(find_first_of_nsc_test
);
30 CPPUNIT_TEST_SUITE_END();
37 void find_first_of_test();
38 void find_first_of_nsc_test();
41 CPPUNIT_TEST_SUITE_REGISTRATION(AlgTest
);
44 // tests implementation
46 void AlgTest::min_max()
49 CPPUNIT_ASSERT( i
== 4 );
50 char c
= max('a', 'z');
51 CPPUNIT_ASSERT( c
== 'z' );
53 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
54 c
= min('a', 'z', greater
<char>());
55 CPPUNIT_ASSERT( c
== 'z' );
56 i
= max(4, 7, greater
<int>());
57 CPPUNIT_ASSERT( i
== 4 );
61 void AlgTest::count_test()
64 int i
[] = { 1, 4, 2, 8, 2, 2 };
65 int n
= count(i
, i
+ 6, 2);
67 #if defined (STLPORT) && !defined (_STLP_NO_ANACHRONISMS)
69 count(i
, i
+ 6, 2, n
);
81 int n
= count(i
.begin(), i
.end(), 2);
83 #if defined (STLPORT) && !defined (_STLP_NO_ANACHRONISMS)
85 count(i
.begin(), i
.end(), 2, n
);
91 void AlgTest::sort_test()
95 years
.push_back(1962);
96 years
.push_back(1992);
97 years
.push_back(2001);
98 years
.push_back(1999);
99 sort(years
.begin(), years
.end());
100 CPPUNIT_ASSERT(years
[0]==1962);
101 CPPUNIT_ASSERT(years
[1]==1992);
102 CPPUNIT_ASSERT(years
[2]==1999);
103 CPPUNIT_ASSERT(years
[3]==2001);
107 years
.push_back(1962);
108 years
.push_back(1992);
109 years
.push_back(2001);
110 years
.push_back(1999);
111 sort(years
.begin(), years
.end()); // <-- changed!
112 CPPUNIT_ASSERT(years
[0]==1962);
113 CPPUNIT_ASSERT(years
[1]==1992);
114 CPPUNIT_ASSERT(years
[2]==1999);
115 CPPUNIT_ASSERT(years
[3]==2001);
119 #define ARRAY_SIZE(arr) sizeof(arr) / sizeof(arr[0])
121 void AlgTest::search_n_test()
123 int ints
[] = {0, 1, 2, 3, 3, 4, 4, 4, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5};
125 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
129 slist
<int> slint(ints
, ints
+ ARRAY_SIZE(ints
));
130 slist
<int>::iterator slit
= search_n(slint
.begin(), slint
.end(), 2, 2);
131 CPPUNIT_ASSERT( slit
!= slint
.end() );
132 CPPUNIT_ASSERT( *(slit
++) == 2 );
133 CPPUNIT_ASSERT( *slit
== 2 );
137 //Bidirectionnal iterator
139 list
<int> lint(ints
, ints
+ ARRAY_SIZE(ints
));
140 list
<int>::iterator lit
= search_n(lint
.begin(), lint
.end(), 3, 3);
141 CPPUNIT_ASSERT( lit
!= lint
.end() );
142 CPPUNIT_ASSERT( *(lit
++) == 3 );
143 CPPUNIT_ASSERT( *(lit
++) == 3 );
144 CPPUNIT_ASSERT( *lit
== 3 );
147 //Random access iterator
149 deque
<int> dint(ints
, ints
+ ARRAY_SIZE(ints
));
150 deque
<int>::iterator dit
= search_n(dint
.begin(), dint
.end(), 4, 4);
151 CPPUNIT_ASSERT( dit
!= dint
.end() );
152 CPPUNIT_ASSERT( *(dit
++) == 4 );
153 CPPUNIT_ASSERT( *(dit
++) == 4 );
154 CPPUNIT_ASSERT( *(dit
++) == 4 );
155 CPPUNIT_ASSERT( *dit
== 4 );
158 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
159 //search_n with predicate
162 slist
<int> slint(ints
, ints
+ ARRAY_SIZE(ints
));
163 slist
<int>::iterator slit
= search_n(slint
.begin(), slint
.end(), 2, 1, greater
<int>());
164 CPPUNIT_ASSERT( slit
!= slint
.end() );
165 CPPUNIT_ASSERT( *(slit
++) > 1 );
166 CPPUNIT_ASSERT( *slit
> 2 );
170 //Bidirectionnal iterator
172 list
<int> lint(ints
, ints
+ ARRAY_SIZE(ints
));
173 list
<int>::iterator lit
= search_n(lint
.begin(), lint
.end(), 3, 2, greater
<int>());
174 CPPUNIT_ASSERT( lit
!= lint
.end() );
175 CPPUNIT_ASSERT( *(lit
++) > 2 );
176 CPPUNIT_ASSERT( *(lit
++) > 2 );
177 CPPUNIT_ASSERT( *lit
> 2 );
180 //Random access iterator
182 deque
<int> dint(ints
, ints
+ ARRAY_SIZE(ints
));
183 deque
<int>::iterator dit
= search_n(dint
.begin(), dint
.end(), 4, 3, greater
<int>());
184 CPPUNIT_ASSERT( dit
!= dint
.end() );
185 CPPUNIT_ASSERT( *(dit
++) > 3 );
186 CPPUNIT_ASSERT( *(dit
++) > 3 );
187 CPPUNIT_ASSERT( *(dit
++) > 3 );
188 CPPUNIT_ASSERT( *dit
> 3 );
191 // test for bug reported by Jim Xochellis
193 int array
[] = {0, 0, 1, 0, 1, 1};
194 int* array_end
= array
+ sizeof(array
) / sizeof(*array
);
195 CPPUNIT_ASSERT(search_n(array
, array_end
, 3, 1) == array_end
);
198 // test for bug with counter == 1, reported by Timmie Smith
200 int array
[] = {0, 1, 2, 3, 4, 5};
201 int* array_end
= array
+ sizeof(array
) / sizeof(*array
);
202 CPPUNIT_ASSERT( search_n(array
, array_end
, 1, 1, equal_to
<int>() ) == &array
[1] );
206 struct MyIntComparable
{
207 MyIntComparable(int val
) : _val(val
) {}
208 bool operator == (const MyIntComparable
& other
) const
209 { return _val
== other
._val
; }
215 void AlgTest::find_first_of_test()
217 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
229 vector
<int>::iterator first
;
230 first
= find_first_of(intv
.begin(), intv
.end(), intsl
.begin(), intsl
.end());
231 CPPUNIT_ASSERT( first
!= intv
.end() );
232 CPPUNIT_ASSERT( *first
== 1 );
241 vector
<int>::iterator first
;
242 first
= find_first_of(intv
.begin(), intv
.end(), intsl
.begin(), intsl
.end());
243 CPPUNIT_ASSERT( first
!= intv
.end() );
244 CPPUNIT_ASSERT( *first
== 2 );
259 vector
<int>::iterator first
;
260 first
= find_first_of(intv
.begin(), intv
.end(), intl
.begin(), intl
.end());
261 CPPUNIT_ASSERT( first
!= intv
.end() );
262 CPPUNIT_ASSERT( *first
== 1 );
271 vector
<int>::iterator first
;
272 first
= find_first_of(intv
.begin(), intv
.end(), intl
.begin(), intl
.end());
273 CPPUNIT_ASSERT( first
!= intv
.end() );
274 CPPUNIT_ASSERT( *first
== 2 );
277 char chars
[] = {1, 2};
285 vector
<int>::iterator first
;
286 first
= find_first_of(intv
.begin(), intv
.end(), chars
, chars
+ sizeof(chars
));
287 CPPUNIT_ASSERT( first
!= intv
.end() );
288 CPPUNIT_ASSERT( *first
== 1 );
291 unsigned char chars
[] = {1, 2, 255};
295 intv
.push_back(1029);
299 vector
<int>::iterator first
;
300 first
= find_first_of(intv
.begin(), intv
.end(), chars
, chars
+ sizeof(chars
));
301 CPPUNIT_ASSERT( first
!= intv
.end() );
302 CPPUNIT_ASSERT( *first
== 255 );
305 signed char chars
[] = {93, 2, -101, 13};
309 intv
.push_back(1029);
310 intv
.push_back(-2035);
311 intv
.push_back(-101);
314 vector
<int>::iterator first
;
315 first
= find_first_of(intv
.begin(), intv
.end(), chars
, chars
+ sizeof(chars
));
316 CPPUNIT_ASSERT( first
!= intv
.end() );
317 CPPUNIT_ASSERT( *first
== -101 );
320 char chars
[] = {1, 2};
322 vector
<MyIntComparable
> intv
;
328 vector
<MyIntComparable
>::iterator first
;
329 first
= find_first_of(intv
.begin(), intv
.end(), chars
, chars
+ sizeof(chars
));
330 CPPUNIT_ASSERT( first
!= intv
.end() );
331 CPPUNIT_ASSERT( *first
== 1 );
335 typedef pair
<int, string
> Pair
;
338 public binary_function
<const Pair
&, const string
&, bool>
340 bool operator () ( const Pair
&p
, const string
& value
) const
341 { return p
.second
== value
; }
344 void AlgTest::find_first_of_nsc_test()
346 // Non-symmetrical comparator
349 vector
<string
> values
;
356 values
.push_back( "four" );
357 values
.push_back( "ten" );
359 map
<int, string
>::iterator i
= find_first_of(m
.begin(), m
.end(), values
.begin(), values
.end(), ValueFinder());
361 CPPUNIT_ASSERT( i
!= m
.end() );
362 CPPUNIT_ASSERT( i
->first
== 4 || i
->first
== 10 );
363 CPPUNIT_ASSERT( i
->second
== "four" || i
->second
== "ten" );