sl@0
|
1 |
// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
//
|
sl@0
|
15 |
|
sl@0
|
16 |
#include <list>
|
sl@0
|
17 |
#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
|
sl@0
|
18 |
# include <slist>
|
sl@0
|
19 |
#endif
|
sl@0
|
20 |
#include <deque>
|
sl@0
|
21 |
#include <vector>
|
sl@0
|
22 |
#include <algorithm>
|
sl@0
|
23 |
#include <functional>
|
sl@0
|
24 |
#include <map>
|
sl@0
|
25 |
#include <string>
|
sl@0
|
26 |
|
sl@0
|
27 |
#include "cppunit/cppunit_proxy.h"
|
sl@0
|
28 |
|
sl@0
|
29 |
#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
|
sl@0
|
30 |
using namespace std;
|
sl@0
|
31 |
#endif
|
sl@0
|
32 |
|
sl@0
|
33 |
//
|
sl@0
|
34 |
// TestCase class
|
sl@0
|
35 |
//
|
sl@0
|
36 |
class AlgTest : public CPPUNIT_NS::TestCase
|
sl@0
|
37 |
{
|
sl@0
|
38 |
CPPUNIT_TEST_SUITE(AlgTest);
|
sl@0
|
39 |
CPPUNIT_TEST(min_max);
|
sl@0
|
40 |
CPPUNIT_TEST(count_test);
|
sl@0
|
41 |
CPPUNIT_TEST(sort_test);
|
sl@0
|
42 |
CPPUNIT_TEST(search_n_test);
|
sl@0
|
43 |
CPPUNIT_TEST(find_first_of_test);
|
sl@0
|
44 |
CPPUNIT_TEST(find_first_of_nsc_test);
|
sl@0
|
45 |
CPPUNIT_TEST(alg_cov);
|
sl@0
|
46 |
CPPUNIT_TEST_SUITE_END();
|
sl@0
|
47 |
|
sl@0
|
48 |
protected:
|
sl@0
|
49 |
void min_max();
|
sl@0
|
50 |
void count_test();
|
sl@0
|
51 |
void sort_test();
|
sl@0
|
52 |
void search_n_test();
|
sl@0
|
53 |
void find_first_of_test();
|
sl@0
|
54 |
void find_first_of_nsc_test();
|
sl@0
|
55 |
void alg_cov();
|
sl@0
|
56 |
};
|
sl@0
|
57 |
|
sl@0
|
58 |
CPPUNIT_TEST_SUITE_REGISTRATION(AlgTest);
|
sl@0
|
59 |
|
sl@0
|
60 |
//
|
sl@0
|
61 |
// tests implementation
|
sl@0
|
62 |
//
|
sl@0
|
63 |
bool mypredicate (int i, int j)
|
sl@0
|
64 |
{
|
sl@0
|
65 |
return (i==j);
|
sl@0
|
66 |
}
|
sl@0
|
67 |
void AlgTest::min_max()
|
sl@0
|
68 |
{
|
sl@0
|
69 |
int i = min(4, 7);
|
sl@0
|
70 |
CPPUNIT_ASSERT( i == 4 );
|
sl@0
|
71 |
char c = max('a', 'z');
|
sl@0
|
72 |
CPPUNIT_ASSERT( c == 'z' );
|
sl@0
|
73 |
|
sl@0
|
74 |
#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
|
sl@0
|
75 |
c = min('a', 'z', greater<char>());
|
sl@0
|
76 |
CPPUNIT_ASSERT( c == 'z' );
|
sl@0
|
77 |
i = max(4, 7, greater<int>());
|
sl@0
|
78 |
CPPUNIT_ASSERT( i == 4 );
|
sl@0
|
79 |
#endif
|
sl@0
|
80 |
}
|
sl@0
|
81 |
|
sl@0
|
82 |
void AlgTest::count_test()
|
sl@0
|
83 |
{
|
sl@0
|
84 |
{
|
sl@0
|
85 |
int i[] = { 1, 4, 2, 8, 2, 2 };
|
sl@0
|
86 |
int n = count(i, i + 6, 2);
|
sl@0
|
87 |
CPPUNIT_ASSERT(n==3);
|
sl@0
|
88 |
#if defined (STLPORT) && !defined (_STLP_NO_ANACHRONISMS)
|
sl@0
|
89 |
n = 0;
|
sl@0
|
90 |
count(i, i + 6, 2, n);
|
sl@0
|
91 |
CPPUNIT_ASSERT(n==3);
|
sl@0
|
92 |
#endif
|
sl@0
|
93 |
}
|
sl@0
|
94 |
{
|
sl@0
|
95 |
vector<int> i;
|
sl@0
|
96 |
i.push_back(1);
|
sl@0
|
97 |
i.push_back(4);
|
sl@0
|
98 |
i.push_back(2);
|
sl@0
|
99 |
i.push_back(8);
|
sl@0
|
100 |
i.push_back(2);
|
sl@0
|
101 |
i.push_back(2);
|
sl@0
|
102 |
int n = count(i.begin(), i.end(), 2);
|
sl@0
|
103 |
CPPUNIT_ASSERT(n==3);
|
sl@0
|
104 |
#if defined (STLPORT) && !defined (_STLP_NO_ANACHRONISMS)
|
sl@0
|
105 |
n = 0;
|
sl@0
|
106 |
count(i.begin(), i.end(), 2, n);
|
sl@0
|
107 |
CPPUNIT_ASSERT(n==3);
|
sl@0
|
108 |
#endif
|
sl@0
|
109 |
}
|
sl@0
|
110 |
}
|
sl@0
|
111 |
|
sl@0
|
112 |
void AlgTest::sort_test()
|
sl@0
|
113 |
{
|
sl@0
|
114 |
{
|
sl@0
|
115 |
vector<int> years;
|
sl@0
|
116 |
years.push_back(1962);
|
sl@0
|
117 |
years.push_back(1992);
|
sl@0
|
118 |
years.push_back(2001);
|
sl@0
|
119 |
years.push_back(1999);
|
sl@0
|
120 |
sort(years.begin(), years.end());
|
sl@0
|
121 |
CPPUNIT_ASSERT(years[0]==1962);
|
sl@0
|
122 |
CPPUNIT_ASSERT(years[1]==1992);
|
sl@0
|
123 |
CPPUNIT_ASSERT(years[2]==1999);
|
sl@0
|
124 |
CPPUNIT_ASSERT(years[3]==2001);
|
sl@0
|
125 |
}
|
sl@0
|
126 |
{
|
sl@0
|
127 |
deque<int> years;
|
sl@0
|
128 |
years.push_back(1962);
|
sl@0
|
129 |
years.push_back(1992);
|
sl@0
|
130 |
years.push_back(2001);
|
sl@0
|
131 |
years.push_back(1999);
|
sl@0
|
132 |
sort(years.begin(), years.end()); // <-- changed!
|
sl@0
|
133 |
CPPUNIT_ASSERT(years[0]==1962);
|
sl@0
|
134 |
CPPUNIT_ASSERT(years[1]==1992);
|
sl@0
|
135 |
CPPUNIT_ASSERT(years[2]==1999);
|
sl@0
|
136 |
CPPUNIT_ASSERT(years[3]==2001);
|
sl@0
|
137 |
}
|
sl@0
|
138 |
}
|
sl@0
|
139 |
|
sl@0
|
140 |
#define ARRAY_SIZE(arr) sizeof(arr) / sizeof(arr[0])
|
sl@0
|
141 |
|
sl@0
|
142 |
void AlgTest::search_n_test()
|
sl@0
|
143 |
{
|
sl@0
|
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};
|
sl@0
|
145 |
|
sl@0
|
146 |
#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
|
sl@0
|
147 |
//search_n
|
sl@0
|
148 |
//Forward iterator
|
sl@0
|
149 |
{
|
sl@0
|
150 |
slist<int> slint(ints, ints + ARRAY_SIZE(ints));
|
sl@0
|
151 |
slist<int>::iterator slit = search_n(slint.begin(), slint.end(), 2, 2);
|
sl@0
|
152 |
CPPUNIT_ASSERT( slit != slint.end() );
|
sl@0
|
153 |
CPPUNIT_ASSERT( *(slit++) == 2 );
|
sl@0
|
154 |
CPPUNIT_ASSERT( *slit == 2 );
|
sl@0
|
155 |
}
|
sl@0
|
156 |
#endif
|
sl@0
|
157 |
|
sl@0
|
158 |
//Bidirectionnal iterator
|
sl@0
|
159 |
{
|
sl@0
|
160 |
list<int> lint(ints, ints + ARRAY_SIZE(ints));
|
sl@0
|
161 |
list<int>::iterator lit = search_n(lint.begin(), lint.end(), 3, 3);
|
sl@0
|
162 |
CPPUNIT_ASSERT( lit != lint.end() );
|
sl@0
|
163 |
CPPUNIT_ASSERT( *(lit++) == 3 );
|
sl@0
|
164 |
CPPUNIT_ASSERT( *(lit++) == 3 );
|
sl@0
|
165 |
CPPUNIT_ASSERT( *lit == 3 );
|
sl@0
|
166 |
}
|
sl@0
|
167 |
|
sl@0
|
168 |
//Random access iterator
|
sl@0
|
169 |
{
|
sl@0
|
170 |
deque<int> dint(ints, ints + ARRAY_SIZE(ints));
|
sl@0
|
171 |
deque<int>::iterator dit = search_n(dint.begin(), dint.end(), 4, 4);
|
sl@0
|
172 |
CPPUNIT_ASSERT( dit != dint.end() );
|
sl@0
|
173 |
CPPUNIT_ASSERT( *(dit++) == 4 );
|
sl@0
|
174 |
CPPUNIT_ASSERT( *(dit++) == 4 );
|
sl@0
|
175 |
CPPUNIT_ASSERT( *(dit++) == 4 );
|
sl@0
|
176 |
CPPUNIT_ASSERT( *dit == 4 );
|
sl@0
|
177 |
}
|
sl@0
|
178 |
|
sl@0
|
179 |
#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
|
sl@0
|
180 |
//search_n with predicate
|
sl@0
|
181 |
//Forward iterator
|
sl@0
|
182 |
{
|
sl@0
|
183 |
slist<int> slint(ints, ints + ARRAY_SIZE(ints));
|
sl@0
|
184 |
slist<int>::iterator slit = search_n(slint.begin(), slint.end(), 2, 1, greater<int>());
|
sl@0
|
185 |
CPPUNIT_ASSERT( slit != slint.end() );
|
sl@0
|
186 |
CPPUNIT_ASSERT( *(slit++) > 1 );
|
sl@0
|
187 |
CPPUNIT_ASSERT( *slit > 2 );
|
sl@0
|
188 |
}
|
sl@0
|
189 |
#endif
|
sl@0
|
190 |
|
sl@0
|
191 |
//Bidirectionnal iterator
|
sl@0
|
192 |
{
|
sl@0
|
193 |
list<int> lint(ints, ints + ARRAY_SIZE(ints));
|
sl@0
|
194 |
list<int>::iterator lit = search_n(lint.begin(), lint.end(), 3, 2, greater<int>());
|
sl@0
|
195 |
CPPUNIT_ASSERT( lit != lint.end() );
|
sl@0
|
196 |
CPPUNIT_ASSERT( *(lit++) > 2 );
|
sl@0
|
197 |
CPPUNIT_ASSERT( *(lit++) > 2 );
|
sl@0
|
198 |
CPPUNIT_ASSERT( *lit > 2 );
|
sl@0
|
199 |
}
|
sl@0
|
200 |
|
sl@0
|
201 |
//Random access iterator
|
sl@0
|
202 |
{
|
sl@0
|
203 |
deque<int> dint(ints, ints + ARRAY_SIZE(ints));
|
sl@0
|
204 |
deque<int>::iterator dit = search_n(dint.begin(), dint.end(), 4, 3, greater<int>());
|
sl@0
|
205 |
CPPUNIT_ASSERT( dit != dint.end() );
|
sl@0
|
206 |
CPPUNIT_ASSERT( *(dit++) > 3 );
|
sl@0
|
207 |
CPPUNIT_ASSERT( *(dit++) > 3 );
|
sl@0
|
208 |
CPPUNIT_ASSERT( *(dit++) > 3 );
|
sl@0
|
209 |
CPPUNIT_ASSERT( *dit > 3 );
|
sl@0
|
210 |
}
|
sl@0
|
211 |
|
sl@0
|
212 |
// test for bug reported by Jim Xochellis
|
sl@0
|
213 |
{
|
sl@0
|
214 |
int array[] = {0, 0, 1, 0, 1, 1};
|
sl@0
|
215 |
int* array_end = array + sizeof(array) / sizeof(*array);
|
sl@0
|
216 |
CPPUNIT_ASSERT(search_n(array, array_end, 3, 1) == array_end);
|
sl@0
|
217 |
}
|
sl@0
|
218 |
|
sl@0
|
219 |
// test for bug with counter == 1, reported by Timmie Smith
|
sl@0
|
220 |
{
|
sl@0
|
221 |
int array[] = {0, 1, 2, 3, 4, 5};
|
sl@0
|
222 |
int* array_end = array + sizeof(array) / sizeof(*array);
|
sl@0
|
223 |
CPPUNIT_ASSERT( search_n(array, array_end, 1, 1, equal_to<int>() ) == &array[1] );
|
sl@0
|
224 |
}
|
sl@0
|
225 |
}
|
sl@0
|
226 |
|
sl@0
|
227 |
void AlgTest::find_first_of_test()
|
sl@0
|
228 |
{
|
sl@0
|
229 |
#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
|
sl@0
|
230 |
slist<int> intsl;
|
sl@0
|
231 |
intsl.push_front(1);
|
sl@0
|
232 |
intsl.push_front(2);
|
sl@0
|
233 |
|
sl@0
|
234 |
{
|
sl@0
|
235 |
vector<int> intv;
|
sl@0
|
236 |
intv.push_back(0);
|
sl@0
|
237 |
intv.push_back(1);
|
sl@0
|
238 |
intv.push_back(2);
|
sl@0
|
239 |
intv.push_back(3);
|
sl@0
|
240 |
|
sl@0
|
241 |
vector<int>::iterator first;
|
sl@0
|
242 |
first = find_first_of(intv.begin(), intv.end(), intsl.begin(), intsl.end());
|
sl@0
|
243 |
CPPUNIT_ASSERT( first != intv.end() );
|
sl@0
|
244 |
CPPUNIT_ASSERT( *first == 1 );
|
sl@0
|
245 |
}
|
sl@0
|
246 |
{
|
sl@0
|
247 |
vector<int> intv;
|
sl@0
|
248 |
intv.push_back(3);
|
sl@0
|
249 |
intv.push_back(2);
|
sl@0
|
250 |
intv.push_back(1);
|
sl@0
|
251 |
intv.push_back(0);
|
sl@0
|
252 |
|
sl@0
|
253 |
vector<int>::iterator first;
|
sl@0
|
254 |
first = find_first_of(intv.begin(), intv.end(), intsl.begin(), intsl.end());
|
sl@0
|
255 |
CPPUNIT_ASSERT( first != intv.end() );
|
sl@0
|
256 |
CPPUNIT_ASSERT( *first == 2 );
|
sl@0
|
257 |
}
|
sl@0
|
258 |
#endif
|
sl@0
|
259 |
|
sl@0
|
260 |
list<int> intl;
|
sl@0
|
261 |
intl.push_front(1);
|
sl@0
|
262 |
intl.push_front(2);
|
sl@0
|
263 |
|
sl@0
|
264 |
{
|
sl@0
|
265 |
vector<int> intv;
|
sl@0
|
266 |
intv.push_back(0);
|
sl@0
|
267 |
intv.push_back(1);
|
sl@0
|
268 |
intv.push_back(2);
|
sl@0
|
269 |
intv.push_back(3);
|
sl@0
|
270 |
|
sl@0
|
271 |
vector<int>::iterator first;
|
sl@0
|
272 |
first = find_first_of(intv.begin(), intv.end(), intl.begin(), intl.end());
|
sl@0
|
273 |
CPPUNIT_ASSERT( first != intv.end() );
|
sl@0
|
274 |
CPPUNIT_ASSERT( *first == 1 );
|
sl@0
|
275 |
}
|
sl@0
|
276 |
{
|
sl@0
|
277 |
vector<int> intv;
|
sl@0
|
278 |
intv.push_back(3);
|
sl@0
|
279 |
intv.push_back(2);
|
sl@0
|
280 |
intv.push_back(1);
|
sl@0
|
281 |
intv.push_back(0);
|
sl@0
|
282 |
|
sl@0
|
283 |
vector<int>::iterator first;
|
sl@0
|
284 |
first = find_first_of(intv.begin(), intv.end(), intl.begin(), intl.end());
|
sl@0
|
285 |
CPPUNIT_ASSERT( first != intv.end() );
|
sl@0
|
286 |
CPPUNIT_ASSERT( *first == 2 );
|
sl@0
|
287 |
}
|
sl@0
|
288 |
}
|
sl@0
|
289 |
|
sl@0
|
290 |
typedef pair<int, string> Pair;
|
sl@0
|
291 |
|
sl@0
|
292 |
struct ValueFinder :
|
sl@0
|
293 |
public binary_function<const Pair&, const string&, bool>
|
sl@0
|
294 |
{
|
sl@0
|
295 |
bool operator () ( const Pair &p, const string& value ) const
|
sl@0
|
296 |
{ return p.second == value; }
|
sl@0
|
297 |
};
|
sl@0
|
298 |
|
sl@0
|
299 |
void AlgTest::find_first_of_nsc_test()
|
sl@0
|
300 |
{
|
sl@0
|
301 |
// Non-symmetrical comparator
|
sl@0
|
302 |
|
sl@0
|
303 |
map<int, string> m;
|
sl@0
|
304 |
vector<string> values;
|
sl@0
|
305 |
|
sl@0
|
306 |
m[1] = "one";
|
sl@0
|
307 |
m[4] = "four";
|
sl@0
|
308 |
m[10] = "ten";
|
sl@0
|
309 |
m[20] = "twenty";
|
sl@0
|
310 |
|
sl@0
|
311 |
values.push_back( "four" );
|
sl@0
|
312 |
values.push_back( "ten" );
|
sl@0
|
313 |
|
sl@0
|
314 |
map<int, string>::iterator i = find_first_of(m.begin(), m.end(), values.begin(), values.end(), ValueFinder());
|
sl@0
|
315 |
|
sl@0
|
316 |
CPPUNIT_ASSERT( i != m.end() );
|
sl@0
|
317 |
CPPUNIT_ASSERT( i->first == 4 || i->first == 10 );
|
sl@0
|
318 |
CPPUNIT_ASSERT( i->second == "four" || i->second == "ten" );
|
sl@0
|
319 |
}
|
sl@0
|
320 |
void AlgTest::alg_cov()
|
sl@0
|
321 |
{
|
sl@0
|
322 |
{
|
sl@0
|
323 |
int myints[]={10,20,30,30,20,10,10,20};
|
sl@0
|
324 |
int fwditer;
|
sl@0
|
325 |
|
sl@0
|
326 |
vector<int> myvector (myints,myints+8);
|
sl@0
|
327 |
|
sl@0
|
328 |
vector<int>::iterator it;
|
sl@0
|
329 |
it = search_n (myvector.begin(), myvector.end(), 2, 30);
|
sl@0
|
330 |
fwditer = int(it-myvector.begin()) ;
|
sl@0
|
331 |
CPPUNIT_ASSERT( fwditer == 2 );
|
sl@0
|
332 |
|
sl@0
|
333 |
it = search_n (myvector.begin(), myvector.end(), 2, 10, mypredicate);
|
sl@0
|
334 |
fwditer = int(it-myvector.begin()) ;
|
sl@0
|
335 |
CPPUNIT_ASSERT( fwditer == 5 );
|
sl@0
|
336 |
|
sl@0
|
337 |
// end()-10 goes back to the 2 positions before begin() as the vector contains only
|
sl@0
|
338 |
// 8 elements so fwditer contains the difference of locations i.e., -2
|
sl@0
|
339 |
it = search_n (myvector.begin(), myvector.end()-10, 2, 30);
|
sl@0
|
340 |
fwditer = int(it-myvector.begin()) ;
|
sl@0
|
341 |
CPPUNIT_ASSERT( fwditer == -2 );
|
sl@0
|
342 |
}
|
sl@0
|
343 |
{
|
sl@0
|
344 |
int N = 10;
|
sl@0
|
345 |
int n=4;
|
sl@0
|
346 |
int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
sl@0
|
347 |
int B[4];
|
sl@0
|
348 |
|
sl@0
|
349 |
random_sample(A, A+N, B, B+n); // generates the random numbers
|
sl@0
|
350 |
|
sl@0
|
351 |
}
|
sl@0
|
352 |
{
|
sl@0
|
353 |
int A[] = {1, 4, 2, 8, 5, 7};
|
sl@0
|
354 |
const int N = sizeof(A) / sizeof(int);
|
sl@0
|
355 |
|
sl@0
|
356 |
CPPUNIT_ASSERT(!is_sorted(A, A + N));
|
sl@0
|
357 |
sort(A, A + N);
|
sl@0
|
358 |
CPPUNIT_ASSERT(is_sorted(A, A + N));
|
sl@0
|
359 |
}
|
sl@0
|
360 |
}
|