|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 #include <vector> |
|
18 #include <algorithm> |
|
19 |
|
20 #include "cppunit/cppunit_proxy.h" |
|
21 |
|
22 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) |
|
23 using namespace std; |
|
24 #endif |
|
25 |
|
26 // |
|
27 // TestCase class |
|
28 // |
|
29 class FindTest : public CPPUNIT_NS::TestCase |
|
30 { |
|
31 CPPUNIT_TEST_SUITE(FindTest); |
|
32 CPPUNIT_TEST(find0); |
|
33 CPPUNIT_TEST(find1); |
|
34 CPPUNIT_TEST(findif0); |
|
35 CPPUNIT_TEST(findif1); |
|
36 CPPUNIT_TEST(find_char); |
|
37 CPPUNIT_TEST_SUITE_END(); |
|
38 |
|
39 protected: |
|
40 void find0(); |
|
41 void find1(); |
|
42 void findif0(); |
|
43 void findif1(); |
|
44 void find_char(); |
|
45 static bool odd(int a_); |
|
46 static bool div_3(int a_); |
|
47 }; |
|
48 |
|
49 CPPUNIT_TEST_SUITE_REGISTRATION(FindTest); |
|
50 |
|
51 // |
|
52 // tests implementation |
|
53 // |
|
54 void FindTest::find0() |
|
55 { |
|
56 int numbers[10] = { 0, 1, 4, 9, 16, 25, 36, 49, 64 }; |
|
57 |
|
58 int *location = find((int*)numbers, (int*)numbers + 10, 25); |
|
59 |
|
60 CPPUNIT_ASSERT((location - numbers)==5); |
|
61 |
|
62 int *out_range = find((int*)numbers, (int*)numbers + 10, 128); |
|
63 |
|
64 CPPUNIT_ASSERT( out_range == (int *)(numbers + 10) ); |
|
65 } |
|
66 |
|
67 struct Key |
|
68 { |
|
69 int data; |
|
70 |
|
71 /* This operator should rather be global and commutative |
|
72 but implementing it this way show that STLport used to |
|
73 ask too much from the user code. */ |
|
74 bool operator == (int d) const |
|
75 { |
|
76 return data == d; |
|
77 } |
|
78 }; |
|
79 |
|
80 void FindTest::find1() |
|
81 { |
|
82 int years[] = { 1942, 1952, 1962, 1972, 1982, 1992 }; |
|
83 |
|
84 const unsigned yearCount = sizeof(years) / sizeof(years[0]); |
|
85 int* location = find((int*)years, (int*)years + yearCount, 1972); |
|
86 |
|
87 CPPUNIT_ASSERT((location - years)==3); |
|
88 } |
|
89 |
|
90 void FindTest::findif0() |
|
91 { |
|
92 { |
|
93 int numbers[6] = { 2, 4, 8, 15, 32, 64 }; |
|
94 int *location = find_if((int*)numbers, (int*)numbers + 6, odd); |
|
95 |
|
96 CPPUNIT_ASSERT((location - numbers)==3); |
|
97 |
|
98 int numbers_even[6] = { 2, 4, 8, 16, 32, 64 }; |
|
99 |
|
100 int *out_range = find_if((int*)numbers_even, (int*)numbers_even + 6, odd); |
|
101 |
|
102 CPPUNIT_ASSERT( out_range == (int *)(numbers_even + 6) ); |
|
103 } |
|
104 |
|
105 { |
|
106 Key keys[10] = { {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0} }; |
|
107 Key const* k = find(keys + 0, keys + 10, 5); |
|
108 CPPUNIT_ASSERT( k == keys + 10 ); |
|
109 } |
|
110 } |
|
111 |
|
112 void FindTest::findif1() |
|
113 { |
|
114 typedef vector <int> IntVec; |
|
115 IntVec v(10); |
|
116 for(int i = 0; (size_t)i < v.size(); ++i) |
|
117 v[i] =(i + 1) *(i + 1); |
|
118 IntVec::iterator iter; |
|
119 iter = find_if(v.begin(), v.end(), div_3); |
|
120 CPPUNIT_ASSERT((iter - v.begin())==2); |
|
121 } |
|
122 |
|
123 bool FindTest::odd(int a_) |
|
124 { |
|
125 return (a_ % 2) != 0; |
|
126 } |
|
127 |
|
128 bool FindTest::div_3(int a_) |
|
129 { |
|
130 return a_ % 3 ? 0 : 1; |
|
131 } |
|
132 |
|
133 void FindTest::find_char() |
|
134 { |
|
135 char str[] = "abcdefghij"; |
|
136 char *pstr = (char*)str; |
|
137 const char* cpstr = (const char*)str; |
|
138 size_t str_size = sizeof(str) / sizeof(char); |
|
139 |
|
140 char *d = find(pstr, pstr + str_size, 'd'); |
|
141 CPPUNIT_ASSERT( *d == 'd' ); |
|
142 |
|
143 const char *e = find(cpstr, cpstr + str_size, 'e'); |
|
144 CPPUNIT_ASSERT( *e == 'e' ); |
|
145 |
|
146 char *last = find(pstr, pstr + str_size, 'x'); |
|
147 CPPUNIT_ASSERT( last == pstr + str_size ); |
|
148 |
|
149 const char *clast = find(cpstr, cpstr + str_size, 'x'); |
|
150 CPPUNIT_ASSERT( clast == cpstr + str_size ); |
|
151 } |