First public contribution.
6 #if defined (STLPORT) && defined (_STLP_DEBUG) && defined (_STLP_DEBUG_MODE_THROWS)
7 # define _STLP_DO_CHECK_BAD_PREDICATE
12 #include "cppunit/cppunit_proxy.h"
14 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
21 class PartialTest : public CPPUNIT_NS::TestCase
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);
33 CPPUNIT_TEST(partsum0);
34 CPPUNIT_TEST(partsum1);
35 CPPUNIT_TEST(partsum2);
36 CPPUNIT_TEST_SUITE_END();
48 void bad_predicate_detected();
50 static bool str_compare(const char* a_, const char* b_)
52 return strcmp(a_, b_) < 0 ? 1 : 0;
56 CPPUNIT_TEST_SUITE_REGISTRATION(PartialTest);
59 // tests implementation
61 void PartialTest::parsrt0()
63 int numbers[6] = { 5, 2, 4, 3, 1, 6 };
65 partial_sort((int*)numbers, (int*)numbers + 3, (int*)numbers + 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);
76 void PartialTest::parsrt1()
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 };
82 vector <int> v1(numbers, numbers+10);
83 partial_sort(v1.begin(), v1.begin() + v1.size() / 2, v1.end());
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);
97 void PartialTest::parsrt2()
99 char const* names[] = { "aa", "ff", "dd", "ee", "cc", "bb" };
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++)
106 partial_sort(v1.begin(), v1.begin() + nameSize / 2, v1.end(), str_compare);
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] );
123 void PartialTest::parsrtc0()
125 int numbers[6] = { 5, 2, 4, 3, 1, 6 };
128 partial_sort_copy((int*)numbers, (int*)numbers + 6, (int*)result, (int*)result + 3);
130 CPPUNIT_ASSERT(result[0]==1);
131 CPPUNIT_ASSERT(result[1]==2);
132 CPPUNIT_ASSERT(result[2]==3);
135 void PartialTest::parsrtc1()
137 int numbers[10] ={ 3, 0, 4, 3, 2, 8, 2, 7, 7, 5 };
139 //3 0 4 3 2 8 2 7 7 5
142 vector <int> v1(numbers, numbers+10);
143 vector <int> result(5);
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);
153 void PartialTest::parsrtc2()
155 char const* names[] = { "aa", "ff", "dd", "ee", "cc", "bb" };
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++)
161 vector <char const*> result(3);
162 partial_sort_copy(v1.begin(), v1.end(), result.begin(), result.end(), str_compare);
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] );
173 #if defined (_STLP_DO_CHECK_BAD_PREDICATE)
174 void PartialTest::bad_predicate_detected()
176 int numbers[] = { 0, 0, 1, 0, 0, 1, 0, 0 };
177 const size_t s = sizeof(numbers) / sizeof(numbers[0]);
180 partial_sort(numbers, numbers + s / 2, numbers + s, less_equal<int>());
182 //Here is means that no exception has been raised
183 CPPUNIT_ASSERT( false );
185 catch (runtime_error const&)
186 { /*OK bad predicate has been detected.*/ }
189 vector<int> result(s);
190 partial_sort_copy(numbers, numbers + s, result.begin(), result.end(), less_equal<int>());
192 //Here is means that no exception has been raised
193 CPPUNIT_ASSERT( false );
195 catch (runtime_error const&)
196 { /*OK bad predicate has been detected.*/ }
200 void PartialTest::partsum0()
202 int numbers[6] = { 1, 2, 3, 4, 5, 6 };
205 partial_sum((int*)numbers, (int*)numbers + 6, (int*)result);
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);
216 void PartialTest::partsum1()
219 __iota(v1.begin(), v1.end(), 0);
220 vector <int> v2(v1.size());
221 partial_sum(v1.begin(), v1.end(), v2.begin());
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);
236 void PartialTest::partsum2()
239 __iota(v1.begin(), v1.end(), 1);
240 vector <int> v2(v1.size());
241 partial_sum(v1.begin(), v1.end(), v2.begin(), multiplies<int>());
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);