ReactOS 0.4.15-dev-7127-g2dd0c6c
partial_test.cpp
Go to the documentation of this file.
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"
13
14#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
15using namespace std;
16#endif
17
18//
19// TestCase class
20//
21class PartialTest : public CPPUNIT_NS::TestCase
22{
30#if defined (_STLP_DO_CHECK_BAD_PREDICATE)
32#endif
37
38protected:
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();
49
50 static bool str_compare(const char* a_, const char* b_)
51 {
52 return strcmp(a_, b_) < 0 ? 1 : 0;
53 }
54};
55
57
58//
59// tests implementation
60//
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
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
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
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
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
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)
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 {
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
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
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
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}
_STLP_MOVE_TO_STD_NAMESPACE void partial_sort(_RandomAccessIter __first, _RandomAccessIter __middle, _RandomAccessIter __last)
Definition: _algo.c:1236
_STLP_MOVE_TO_STD_NAMESPACE _RandomAccessIter partial_sort_copy(_InputIter __first, _InputIter __last, _RandomAccessIter __result_first, _RandomAccessIter __result_last)
Definition: _algo.c:1287
_STLP_MOVE_TO_STD_NAMESPACE _OutputIterator partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
Definition: _numeric.h:94
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
CPPUNIT_TEST(parsrtc0)
void parsrt2()
CPPUNIT_TEST_SUITE(PartialTest)
CPPUNIT_TEST(partsum1)
CPPUNIT_TEST(parsrtc1)
CPPUNIT_TEST(partsum2)
CPPUNIT_TEST(parsrt0)
CPPUNIT_TEST_SUITE_END()
void parsrt0()
static bool str_compare(const char *a_, const char *b_)
CPPUNIT_TEST(partsum0)
void bad_predicate_detected()
CPPUNIT_TEST(parsrt1)
CPPUNIT_TEST(parsrt2)
CPPUNIT_TEST(parsrtc2)
void parsrt1()
#define CPPUNIT_TEST(X)
Definition: cppunit_mini.h:182
#define CPPUNIT_TEST_SUITE_REGISTRATION(X)
Definition: cppunit_mini.h:193
#define CPPUNIT_ASSERT(X)
Definition: cppunit_mini.h:200
GLdouble s
Definition: gl.h:2039
GLuint GLuint * names
Definition: glext.h:11545
GLfloat GLfloat v1
Definition: glext.h:6062
GLfloat GLfloat GLfloat v2
Definition: glext.h:6063
GLuint64EXT * result
Definition: glext.h:11304
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
void __iota(_It __first, _It __last, _Tp __val)
Definition: iota.h:8
Definition: features.h:417