|
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 <numeric> |
|
18 #include <vector> |
|
19 #include <algorithm> |
|
20 #include <functional> |
|
21 |
|
22 #if defined (STLPORT) && defined (_STLP_DEBUG) && defined (_STLP_DEBUG_MODE_THROWS) |
|
23 # define _STLP_DO_CHECK_BAD_PREDICATE |
|
24 # include <stdexcept> |
|
25 #endif |
|
26 |
|
27 #include "iota.h" |
|
28 #include "cppunit/cppunit_proxy.h" |
|
29 |
|
30 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) |
|
31 using namespace std; |
|
32 #endif |
|
33 |
|
34 // |
|
35 // TestCase class |
|
36 // |
|
37 class PartialTest : public CPPUNIT_NS::TestCase |
|
38 { |
|
39 CPPUNIT_TEST_SUITE(PartialTest); |
|
40 CPPUNIT_TEST(parsrt0); |
|
41 CPPUNIT_TEST(parsrt1); |
|
42 CPPUNIT_TEST(parsrt2); |
|
43 CPPUNIT_TEST(parsrtc0); |
|
44 CPPUNIT_TEST(parsrtc1); |
|
45 CPPUNIT_TEST(parsrtc2); |
|
46 #if defined (_STLP_DO_CHECK_BAD_PREDICATE) |
|
47 CPPUNIT_TEST(bad_predicate_detected); |
|
48 #endif |
|
49 CPPUNIT_TEST(partsum0); |
|
50 CPPUNIT_TEST(partsum1); |
|
51 CPPUNIT_TEST(partsum2); |
|
52 CPPUNIT_TEST_SUITE_END(); |
|
53 |
|
54 protected: |
|
55 void parsrt0(); |
|
56 void parsrt1(); |
|
57 void parsrt2(); |
|
58 void parsrtc0(); |
|
59 void parsrtc1(); |
|
60 void parsrtc2(); |
|
61 void partsum0(); |
|
62 void partsum1(); |
|
63 void partsum2(); |
|
64 void bad_predicate_detected(); |
|
65 |
|
66 static bool str_compare(const char* a_, const char* b_) |
|
67 { |
|
68 return strcmp(a_, b_) < 0 ? 1 : 0; |
|
69 } |
|
70 }; |
|
71 |
|
72 CPPUNIT_TEST_SUITE_REGISTRATION(PartialTest); |
|
73 |
|
74 // |
|
75 // tests implementation |
|
76 // |
|
77 void PartialTest::parsrt0() |
|
78 { |
|
79 int numbers[6] = { 5, 2, 4, 3, 1, 6 }; |
|
80 |
|
81 partial_sort((int*)numbers, (int*)numbers + 3, (int*)numbers + 6); |
|
82 |
|
83 // 1 2 3 5 4 6 |
|
84 CPPUNIT_ASSERT(numbers[0]==1); |
|
85 CPPUNIT_ASSERT(numbers[1]==2); |
|
86 CPPUNIT_ASSERT(numbers[2]==3); |
|
87 CPPUNIT_ASSERT(numbers[3]==5); |
|
88 CPPUNIT_ASSERT(numbers[4]==4); |
|
89 CPPUNIT_ASSERT(numbers[5]==6); |
|
90 } |
|
91 |
|
92 void PartialTest::parsrt1() |
|
93 { |
|
94 // 8 8 5 3 7 6 5 3 2 4 |
|
95 // 2 3 3 4 5 8 8 7 6 5 |
|
96 int numbers[10] ={ 8, 8, 5, 3, 7, 6, 5, 3, 2, 4 }; |
|
97 |
|
98 vector <int> v1(numbers, numbers+10); |
|
99 partial_sort(v1.begin(), v1.begin() + v1.size() / 2, v1.end()); |
|
100 |
|
101 CPPUNIT_ASSERT(v1[0]==2); |
|
102 CPPUNIT_ASSERT(v1[1]==3); |
|
103 CPPUNIT_ASSERT(v1[2]==3); |
|
104 CPPUNIT_ASSERT(v1[3]==4); |
|
105 CPPUNIT_ASSERT(v1[4]==5); |
|
106 CPPUNIT_ASSERT(v1[5]==8); |
|
107 CPPUNIT_ASSERT(v1[6]==8); |
|
108 CPPUNIT_ASSERT(v1[7]==7); |
|
109 CPPUNIT_ASSERT(v1[8]==6); |
|
110 CPPUNIT_ASSERT(v1[9]==5); |
|
111 } |
|
112 |
|
113 void PartialTest::parsrt2() |
|
114 { |
|
115 char const* names[] = { "aa", "ff", "dd", "ee", "cc", "bb" }; |
|
116 |
|
117 const unsigned nameSize = sizeof(names) / sizeof(names[0]); |
|
118 vector <char const*> v1(nameSize); |
|
119 for(size_t i = 0; i < v1.size(); i++) |
|
120 v1[i] = names[i]; |
|
121 |
|
122 partial_sort(v1.begin(), v1.begin() + nameSize / 2, v1.end(), str_compare); |
|
123 |
|
124 // aa bb cc ff ee dd |
|
125 CPPUNIT_ASSERT( strcmp(v1[0], "aa") == 0 ); |
|
126 CPPUNIT_ASSERT( v1[0] == names[0] ); |
|
127 CPPUNIT_ASSERT( strcmp(v1[1], "bb") == 0 ); |
|
128 CPPUNIT_ASSERT( v1[1] == names[5] ); |
|
129 CPPUNIT_ASSERT( strcmp(v1[2], "cc") == 0 ); |
|
130 CPPUNIT_ASSERT( v1[2] == names[4] ); |
|
131 CPPUNIT_ASSERT( strcmp(v1[3], "ff") == 0 ); |
|
132 CPPUNIT_ASSERT( v1[3] == names[1] ); |
|
133 CPPUNIT_ASSERT( strcmp(v1[4], "ee") == 0 ); |
|
134 CPPUNIT_ASSERT( v1[4] == names[3] ); |
|
135 CPPUNIT_ASSERT( strcmp(v1[5], "dd") == 0 ); |
|
136 CPPUNIT_ASSERT( v1[5] == names[2] ); |
|
137 } |
|
138 |
|
139 void PartialTest::parsrtc0() |
|
140 { |
|
141 int numbers[6] = { 5, 2, 4, 3, 1, 6 }; |
|
142 |
|
143 int result[3]; |
|
144 partial_sort_copy((int*)numbers, (int*)numbers + 6, (int*)result, (int*)result + 3); |
|
145 //1 2 3 |
|
146 CPPUNIT_ASSERT(result[0]==1); |
|
147 CPPUNIT_ASSERT(result[1]==2); |
|
148 CPPUNIT_ASSERT(result[2]==3); |
|
149 } |
|
150 |
|
151 void PartialTest::parsrtc1() |
|
152 { |
|
153 int numbers[10] ={ 3, 0, 4, 3, 2, 8, 2, 7, 7, 5 }; |
|
154 |
|
155 //3 0 4 3 2 8 2 7 7 5 |
|
156 //0 2 2 3 3 |
|
157 |
|
158 vector <int> v1(numbers, numbers+10); |
|
159 vector <int> result(5); |
|
160 |
|
161 partial_sort_copy(v1.begin(), v1.end(), result.begin(), result.end()); |
|
162 CPPUNIT_ASSERT(result[0]==0); |
|
163 CPPUNIT_ASSERT(result[1]==2); |
|
164 CPPUNIT_ASSERT(result[2]==2); |
|
165 CPPUNIT_ASSERT(result[3]==3); |
|
166 CPPUNIT_ASSERT(result[4]==3); |
|
167 } |
|
168 |
|
169 void PartialTest::parsrtc2() |
|
170 { |
|
171 char const* names[] = { "aa", "ff", "dd", "ee", "cc", "bb" }; |
|
172 |
|
173 const unsigned nameSize = sizeof(names) / sizeof(names[0]); |
|
174 vector <char const*> v1(nameSize); |
|
175 for(size_t i = 0; i < v1.size(); i++) |
|
176 v1[i] = names[i]; |
|
177 vector <char const*> result(3); |
|
178 partial_sort_copy(v1.begin(), v1.end(), result.begin(), result.end(), str_compare); |
|
179 |
|
180 // aa bb cc |
|
181 CPPUNIT_ASSERT( strcmp( result[0], "aa" ) == 0 ); |
|
182 CPPUNIT_ASSERT( result[0] == names[0] ); |
|
183 CPPUNIT_ASSERT( strcmp( result[1], "bb" ) == 0 ); |
|
184 CPPUNIT_ASSERT( result[1] == names[5] ); |
|
185 CPPUNIT_ASSERT( strcmp( result[2], "cc" ) == 0 ); |
|
186 CPPUNIT_ASSERT( result[2] == names[4] ); |
|
187 } |
|
188 |
|
189 #if defined (_STLP_DO_CHECK_BAD_PREDICATE) |
|
190 void PartialTest::bad_predicate_detected() |
|
191 { |
|
192 int numbers[] = { 0, 0, 1, 0, 0, 1, 0, 0 }; |
|
193 const size_t s = sizeof(numbers) / sizeof(numbers[0]); |
|
194 |
|
195 try { |
|
196 partial_sort(numbers, numbers + s / 2, numbers + s, less_equal<int>()); |
|
197 |
|
198 //Here is means that no exception has been raised |
|
199 CPPUNIT_ASSERT( false ); |
|
200 } |
|
201 catch (runtime_error const&) |
|
202 { /*OK bad predicate has been detected.*/ } |
|
203 |
|
204 try { |
|
205 vector<int> result(s); |
|
206 partial_sort_copy(numbers, numbers + s, result.begin(), result.end(), less_equal<int>()); |
|
207 |
|
208 //Here is means that no exception has been raised |
|
209 CPPUNIT_ASSERT( false ); |
|
210 } |
|
211 catch (runtime_error const&) |
|
212 { /*OK bad predicate has been detected.*/ } |
|
213 } |
|
214 #endif |
|
215 |
|
216 void PartialTest::partsum0() |
|
217 { |
|
218 int numbers[6] = { 1, 2, 3, 4, 5, 6 }; |
|
219 |
|
220 int result[6]; |
|
221 partial_sum((int*)numbers, (int*)numbers + 6, (int*)result); |
|
222 |
|
223 // 1 3 6 10 15 21 |
|
224 CPPUNIT_ASSERT(result[0]==1); |
|
225 CPPUNIT_ASSERT(result[1]==3); |
|
226 CPPUNIT_ASSERT(result[2]==6); |
|
227 CPPUNIT_ASSERT(result[3]==10); |
|
228 CPPUNIT_ASSERT(result[4]==15); |
|
229 CPPUNIT_ASSERT(result[5]==21); |
|
230 } |
|
231 |
|
232 void PartialTest::partsum1() |
|
233 { |
|
234 vector <int> v1(10); |
|
235 __iota(v1.begin(), v1.end(), 0); |
|
236 vector <int> v2(v1.size()); |
|
237 partial_sum(v1.begin(), v1.end(), v2.begin()); |
|
238 |
|
239 // 0 1 3 6 10 15 21 28 36 45 |
|
240 CPPUNIT_ASSERT(v2[0]==0); |
|
241 CPPUNIT_ASSERT(v2[1]==1); |
|
242 CPPUNIT_ASSERT(v2[2]==3); |
|
243 CPPUNIT_ASSERT(v2[3]==6); |
|
244 CPPUNIT_ASSERT(v2[4]==10); |
|
245 CPPUNIT_ASSERT(v2[5]==15); |
|
246 CPPUNIT_ASSERT(v2[6]==21); |
|
247 CPPUNIT_ASSERT(v2[7]==28); |
|
248 CPPUNIT_ASSERT(v2[8]==36); |
|
249 CPPUNIT_ASSERT(v2[9]==45); |
|
250 } |
|
251 |
|
252 void PartialTest::partsum2() |
|
253 { |
|
254 vector <int> v1(5); |
|
255 __iota(v1.begin(), v1.end(), 1); |
|
256 vector <int> v2(v1.size()); |
|
257 partial_sum(v1.begin(), v1.end(), v2.begin(), multiplies<int>()); |
|
258 // 1 2 6 24 120 |
|
259 CPPUNIT_ASSERT(v2[0]==1); |
|
260 CPPUNIT_ASSERT(v2[1]==2); |
|
261 CPPUNIT_ASSERT(v2[2]==6); |
|
262 CPPUNIT_ASSERT(v2[3]==24); |
|
263 CPPUNIT_ASSERT(v2[4]==120); |
|
264 } |