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