First public contribution.
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
17 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
27 #include "cppunit/cppunit_proxy.h"
29 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
36 class AlgTest : public CPPUNIT_NS::TestCase
38 CPPUNIT_TEST_SUITE(AlgTest);
39 CPPUNIT_TEST(min_max);
40 CPPUNIT_TEST(count_test);
41 CPPUNIT_TEST(sort_test);
42 CPPUNIT_TEST(search_n_test);
43 CPPUNIT_TEST(find_first_of_test);
44 CPPUNIT_TEST(find_first_of_nsc_test);
45 CPPUNIT_TEST(alg_cov);
46 CPPUNIT_TEST_SUITE_END();
53 void find_first_of_test();
54 void find_first_of_nsc_test();
58 CPPUNIT_TEST_SUITE_REGISTRATION(AlgTest);
61 // tests implementation
63 bool mypredicate (int i, int j)
67 void AlgTest::min_max()
70 CPPUNIT_ASSERT( i == 4 );
71 char c = max('a', 'z');
72 CPPUNIT_ASSERT( c == 'z' );
74 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
75 c = min('a', 'z', greater<char>());
76 CPPUNIT_ASSERT( c == 'z' );
77 i = max(4, 7, greater<int>());
78 CPPUNIT_ASSERT( i == 4 );
82 void AlgTest::count_test()
85 int i[] = { 1, 4, 2, 8, 2, 2 };
86 int n = count(i, i + 6, 2);
88 #if defined (STLPORT) && !defined (_STLP_NO_ANACHRONISMS)
90 count(i, i + 6, 2, n);
102 int n = count(i.begin(), i.end(), 2);
103 CPPUNIT_ASSERT(n==3);
104 #if defined (STLPORT) && !defined (_STLP_NO_ANACHRONISMS)
106 count(i.begin(), i.end(), 2, n);
107 CPPUNIT_ASSERT(n==3);
112 void AlgTest::sort_test()
116 years.push_back(1962);
117 years.push_back(1992);
118 years.push_back(2001);
119 years.push_back(1999);
120 sort(years.begin(), years.end());
121 CPPUNIT_ASSERT(years[0]==1962);
122 CPPUNIT_ASSERT(years[1]==1992);
123 CPPUNIT_ASSERT(years[2]==1999);
124 CPPUNIT_ASSERT(years[3]==2001);
128 years.push_back(1962);
129 years.push_back(1992);
130 years.push_back(2001);
131 years.push_back(1999);
132 sort(years.begin(), years.end()); // <-- changed!
133 CPPUNIT_ASSERT(years[0]==1962);
134 CPPUNIT_ASSERT(years[1]==1992);
135 CPPUNIT_ASSERT(years[2]==1999);
136 CPPUNIT_ASSERT(years[3]==2001);
140 #define ARRAY_SIZE(arr) sizeof(arr) / sizeof(arr[0])
142 void AlgTest::search_n_test()
144 int ints[] = {0, 1, 2, 3, 3, 4, 4, 4, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5};
146 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
150 slist<int> slint(ints, ints + ARRAY_SIZE(ints));
151 slist<int>::iterator slit = search_n(slint.begin(), slint.end(), 2, 2);
152 CPPUNIT_ASSERT( slit != slint.end() );
153 CPPUNIT_ASSERT( *(slit++) == 2 );
154 CPPUNIT_ASSERT( *slit == 2 );
158 //Bidirectionnal iterator
160 list<int> lint(ints, ints + ARRAY_SIZE(ints));
161 list<int>::iterator lit = search_n(lint.begin(), lint.end(), 3, 3);
162 CPPUNIT_ASSERT( lit != lint.end() );
163 CPPUNIT_ASSERT( *(lit++) == 3 );
164 CPPUNIT_ASSERT( *(lit++) == 3 );
165 CPPUNIT_ASSERT( *lit == 3 );
168 //Random access iterator
170 deque<int> dint(ints, ints + ARRAY_SIZE(ints));
171 deque<int>::iterator dit = search_n(dint.begin(), dint.end(), 4, 4);
172 CPPUNIT_ASSERT( dit != dint.end() );
173 CPPUNIT_ASSERT( *(dit++) == 4 );
174 CPPUNIT_ASSERT( *(dit++) == 4 );
175 CPPUNIT_ASSERT( *(dit++) == 4 );
176 CPPUNIT_ASSERT( *dit == 4 );
179 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
180 //search_n with predicate
183 slist<int> slint(ints, ints + ARRAY_SIZE(ints));
184 slist<int>::iterator slit = search_n(slint.begin(), slint.end(), 2, 1, greater<int>());
185 CPPUNIT_ASSERT( slit != slint.end() );
186 CPPUNIT_ASSERT( *(slit++) > 1 );
187 CPPUNIT_ASSERT( *slit > 2 );
191 //Bidirectionnal iterator
193 list<int> lint(ints, ints + ARRAY_SIZE(ints));
194 list<int>::iterator lit = search_n(lint.begin(), lint.end(), 3, 2, greater<int>());
195 CPPUNIT_ASSERT( lit != lint.end() );
196 CPPUNIT_ASSERT( *(lit++) > 2 );
197 CPPUNIT_ASSERT( *(lit++) > 2 );
198 CPPUNIT_ASSERT( *lit > 2 );
201 //Random access iterator
203 deque<int> dint(ints, ints + ARRAY_SIZE(ints));
204 deque<int>::iterator dit = search_n(dint.begin(), dint.end(), 4, 3, greater<int>());
205 CPPUNIT_ASSERT( dit != dint.end() );
206 CPPUNIT_ASSERT( *(dit++) > 3 );
207 CPPUNIT_ASSERT( *(dit++) > 3 );
208 CPPUNIT_ASSERT( *(dit++) > 3 );
209 CPPUNIT_ASSERT( *dit > 3 );
212 // test for bug reported by Jim Xochellis
214 int array[] = {0, 0, 1, 0, 1, 1};
215 int* array_end = array + sizeof(array) / sizeof(*array);
216 CPPUNIT_ASSERT(search_n(array, array_end, 3, 1) == array_end);
219 // test for bug with counter == 1, reported by Timmie Smith
221 int array[] = {0, 1, 2, 3, 4, 5};
222 int* array_end = array + sizeof(array) / sizeof(*array);
223 CPPUNIT_ASSERT( search_n(array, array_end, 1, 1, equal_to<int>() ) == &array[1] );
227 void AlgTest::find_first_of_test()
229 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
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 == 1 );
253 vector<int>::iterator first;
254 first = find_first_of(intv.begin(), intv.end(), intsl.begin(), intsl.end());
255 CPPUNIT_ASSERT( first != intv.end() );
256 CPPUNIT_ASSERT( *first == 2 );
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 == 1 );
283 vector<int>::iterator first;
284 first = find_first_of(intv.begin(), intv.end(), intl.begin(), intl.end());
285 CPPUNIT_ASSERT( first != intv.end() );
286 CPPUNIT_ASSERT( *first == 2 );
290 typedef pair<int, string> Pair;
293 public binary_function<const Pair&, const string&, bool>
295 bool operator () ( const Pair &p, const string& value ) const
296 { return p.second == value; }
299 void AlgTest::find_first_of_nsc_test()
301 // Non-symmetrical comparator
304 vector<string> values;
311 values.push_back( "four" );
312 values.push_back( "ten" );
314 map<int, string>::iterator i = find_first_of(m.begin(), m.end(), values.begin(), values.end(), ValueFinder());
316 CPPUNIT_ASSERT( i != m.end() );
317 CPPUNIT_ASSERT( i->first == 4 || i->first == 10 );
318 CPPUNIT_ASSERT( i->second == "four" || i->second == "ten" );
320 void AlgTest::alg_cov()
323 int myints[]={10,20,30,30,20,10,10,20};
326 vector<int> myvector (myints,myints+8);
328 vector<int>::iterator it;
329 it = search_n (myvector.begin(), myvector.end(), 2, 30);
330 fwditer = int(it-myvector.begin()) ;
331 CPPUNIT_ASSERT( fwditer == 2 );
333 it = search_n (myvector.begin(), myvector.end(), 2, 10, mypredicate);
334 fwditer = int(it-myvector.begin()) ;
335 CPPUNIT_ASSERT( fwditer == 5 );
337 // end()-10 goes back to the 2 positions before begin() as the vector contains only
338 // 8 elements so fwditer contains the difference of locations i.e., -2
339 it = search_n (myvector.begin(), myvector.end()-10, 2, 30);
340 fwditer = int(it-myvector.begin()) ;
341 CPPUNIT_ASSERT( fwditer == -2 );
346 int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
349 random_sample(A, A+N, B, B+n); // generates the random numbers
353 int A[] = {1, 4, 2, 8, 5, 7};
354 const int N = sizeof(A) / sizeof(int);
356 CPPUNIT_ASSERT(!is_sorted(A, A + N));
358 CPPUNIT_ASSERT(is_sorted(A, A + N));