|
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 #include <numeric> |
|
20 #include <iterator> |
|
21 #include <functional> |
|
22 |
|
23 #include "iota.h" |
|
24 #include "cppunit/cppunit_proxy.h" |
|
25 |
|
26 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) |
|
27 using namespace std; |
|
28 #endif |
|
29 |
|
30 // |
|
31 // TestCase class |
|
32 // |
|
33 class PermTest : public CPPUNIT_NS::TestCase |
|
34 { |
|
35 CPPUNIT_TEST_SUITE(PermTest); |
|
36 CPPUNIT_TEST(nextprm0); |
|
37 CPPUNIT_TEST(nextprm1); |
|
38 CPPUNIT_TEST(nextprm2); |
|
39 CPPUNIT_TEST(prevprm0); |
|
40 CPPUNIT_TEST(prevprm1); |
|
41 CPPUNIT_TEST(prevprm2); |
|
42 CPPUNIT_TEST_SUITE_END(); |
|
43 |
|
44 protected: |
|
45 void nextprm0(); |
|
46 void nextprm1(); |
|
47 void nextprm2(); |
|
48 void prevprm0(); |
|
49 void prevprm1(); |
|
50 void prevprm2(); |
|
51 }; |
|
52 |
|
53 CPPUNIT_TEST_SUITE_REGISTRATION(PermTest); |
|
54 |
|
55 // |
|
56 // tests implementation |
|
57 // |
|
58 void PermTest::prevprm0() |
|
59 { |
|
60 int v1[3] = { 0, 1, 2 }; |
|
61 prev_permutation(v1, v1 + 3); |
|
62 |
|
63 CPPUNIT_ASSERT(v1[0]==2); |
|
64 CPPUNIT_ASSERT(v1[1]==1); |
|
65 CPPUNIT_ASSERT(v1[2]==0); |
|
66 } |
|
67 void PermTest::prevprm1() |
|
68 { |
|
69 vector <int> v1(3); |
|
70 __iota(v1.begin(), v1.end(), 0); |
|
71 |
|
72 prev_permutation(v1.begin(), v1.end()); |
|
73 CPPUNIT_ASSERT(v1[0]==2); |
|
74 CPPUNIT_ASSERT(v1[1]==1); |
|
75 CPPUNIT_ASSERT(v1[2]==0); |
|
76 prev_permutation(v1.begin(), v1.end()); |
|
77 CPPUNIT_ASSERT(v1[0]==2); |
|
78 CPPUNIT_ASSERT(v1[1]==0); |
|
79 CPPUNIT_ASSERT(v1[2]==1); |
|
80 prev_permutation(v1.begin(), v1.end()); |
|
81 CPPUNIT_ASSERT(v1[0]==1); |
|
82 CPPUNIT_ASSERT(v1[1]==2); |
|
83 CPPUNIT_ASSERT(v1[2]==0); |
|
84 prev_permutation(v1.begin(), v1.end()); |
|
85 CPPUNIT_ASSERT(v1[0]==1); |
|
86 CPPUNIT_ASSERT(v1[1]==0); |
|
87 CPPUNIT_ASSERT(v1[2]==2); |
|
88 prev_permutation(v1.begin(), v1.end()); |
|
89 CPPUNIT_ASSERT(v1[0]==0); |
|
90 CPPUNIT_ASSERT(v1[1]==2);// |
|
91 CPPUNIT_ASSERT(v1[2]==1); |
|
92 prev_permutation(v1.begin(), v1.end()); |
|
93 CPPUNIT_ASSERT(v1[0]==0); |
|
94 CPPUNIT_ASSERT(v1[1]==1); |
|
95 CPPUNIT_ASSERT(v1[2]==2); |
|
96 prev_permutation(v1.begin(), v1.end()); |
|
97 CPPUNIT_ASSERT(v1[0]==2); |
|
98 CPPUNIT_ASSERT(v1[1]==1); |
|
99 CPPUNIT_ASSERT(v1[2]==0); |
|
100 prev_permutation(v1.begin(), v1.end()); |
|
101 CPPUNIT_ASSERT(v1[0]==2); |
|
102 CPPUNIT_ASSERT(v1[1]==0); |
|
103 CPPUNIT_ASSERT(v1[2]==1); |
|
104 prev_permutation(v1.begin(), v1.end()); |
|
105 CPPUNIT_ASSERT(v1[0]==1); |
|
106 CPPUNIT_ASSERT(v1[1]==2); |
|
107 CPPUNIT_ASSERT(v1[2]==0); |
|
108 } |
|
109 void PermTest::prevprm2() |
|
110 { |
|
111 vector <int> v1(3); |
|
112 __iota(v1.begin(), v1.end(), 0); |
|
113 |
|
114 prev_permutation(v1.begin(), v1.end(), greater<int>()); |
|
115 CPPUNIT_ASSERT(v1[0]==0); |
|
116 CPPUNIT_ASSERT(v1[1]==2); |
|
117 CPPUNIT_ASSERT(v1[2]==1); |
|
118 prev_permutation(v1.begin(), v1.end(), greater<int>()); |
|
119 CPPUNIT_ASSERT(v1[0]==1); |
|
120 CPPUNIT_ASSERT(v1[1]==0); |
|
121 CPPUNIT_ASSERT(v1[2]==2); |
|
122 prev_permutation(v1.begin(), v1.end(), greater<int>()); |
|
123 CPPUNIT_ASSERT(v1[0]==1); |
|
124 CPPUNIT_ASSERT(v1[1]==2); |
|
125 CPPUNIT_ASSERT(v1[2]==0); |
|
126 prev_permutation(v1.begin(), v1.end(), greater<int>()); |
|
127 CPPUNIT_ASSERT(v1[0]==2); |
|
128 CPPUNIT_ASSERT(v1[1]==0); |
|
129 CPPUNIT_ASSERT(v1[2]==1); |
|
130 prev_permutation(v1.begin(), v1.end(), greater<int>()); |
|
131 CPPUNIT_ASSERT(v1[0]==2); |
|
132 CPPUNIT_ASSERT(v1[1]==1); |
|
133 CPPUNIT_ASSERT(v1[2]==0); |
|
134 prev_permutation(v1.begin(), v1.end(), greater<int>()); |
|
135 CPPUNIT_ASSERT(v1[0]==0); |
|
136 CPPUNIT_ASSERT(v1[1]==1); |
|
137 CPPUNIT_ASSERT(v1[2]==2); |
|
138 prev_permutation(v1.begin(), v1.end(), greater<int>()); |
|
139 CPPUNIT_ASSERT(v1[0]==0); |
|
140 CPPUNIT_ASSERT(v1[1]==2); |
|
141 CPPUNIT_ASSERT(v1[2]==1); |
|
142 prev_permutation(v1.begin(), v1.end(), greater<int>()); |
|
143 CPPUNIT_ASSERT(v1[0]==1); |
|
144 CPPUNIT_ASSERT(v1[1]==0); |
|
145 CPPUNIT_ASSERT(v1[2]==2); |
|
146 prev_permutation(v1.begin(), v1.end(), greater<int>()); |
|
147 CPPUNIT_ASSERT(v1[0]==1); |
|
148 CPPUNIT_ASSERT(v1[1]==2); |
|
149 CPPUNIT_ASSERT(v1[2]==0); |
|
150 } |
|
151 void PermTest::nextprm0() |
|
152 { |
|
153 int v1[3] = { 0, 1, 2 }; |
|
154 next_permutation(v1, v1 + 3); |
|
155 |
|
156 CPPUNIT_ASSERT(v1[0]==0); |
|
157 CPPUNIT_ASSERT(v1[1]==2); |
|
158 CPPUNIT_ASSERT(v1[2]==1); |
|
159 } |
|
160 void PermTest::nextprm1() |
|
161 { |
|
162 vector <int> v1(3); |
|
163 __iota(v1.begin(), v1.end(), 0); |
|
164 |
|
165 next_permutation(v1.begin(), v1.end()); |
|
166 CPPUNIT_ASSERT(v1[0]==0); |
|
167 CPPUNIT_ASSERT(v1[1]==2); |
|
168 CPPUNIT_ASSERT(v1[2]==1); |
|
169 next_permutation(v1.begin(), v1.end()); |
|
170 CPPUNIT_ASSERT(v1[0]==1); |
|
171 CPPUNIT_ASSERT(v1[1]==0); |
|
172 CPPUNIT_ASSERT(v1[2]==2); |
|
173 next_permutation(v1.begin(), v1.end()); |
|
174 CPPUNIT_ASSERT(v1[0]==1); |
|
175 CPPUNIT_ASSERT(v1[1]==2); |
|
176 CPPUNIT_ASSERT(v1[2]==0); |
|
177 next_permutation(v1.begin(), v1.end()); |
|
178 CPPUNIT_ASSERT(v1[0]==2); |
|
179 CPPUNIT_ASSERT(v1[1]==0); |
|
180 CPPUNIT_ASSERT(v1[2]==1); |
|
181 next_permutation(v1.begin(), v1.end()); |
|
182 CPPUNIT_ASSERT(v1[0]==2); |
|
183 CPPUNIT_ASSERT(v1[1]==1); |
|
184 CPPUNIT_ASSERT(v1[2]==0); |
|
185 next_permutation(v1.begin(), v1.end()); |
|
186 CPPUNIT_ASSERT(v1[0]==0); |
|
187 CPPUNIT_ASSERT(v1[1]==1); |
|
188 CPPUNIT_ASSERT(v1[2]==2); |
|
189 next_permutation(v1.begin(), v1.end()); |
|
190 CPPUNIT_ASSERT(v1[0]==0); |
|
191 CPPUNIT_ASSERT(v1[1]==2); |
|
192 CPPUNIT_ASSERT(v1[2]==1); |
|
193 next_permutation(v1.begin(), v1.end()); |
|
194 CPPUNIT_ASSERT(v1[0]==1); |
|
195 CPPUNIT_ASSERT(v1[1]==0); |
|
196 CPPUNIT_ASSERT(v1[2]==2); |
|
197 next_permutation(v1.begin(), v1.end()); |
|
198 CPPUNIT_ASSERT(v1[0]==1); |
|
199 CPPUNIT_ASSERT(v1[1]==2); |
|
200 CPPUNIT_ASSERT(v1[2]==0); |
|
201 } |
|
202 void PermTest::nextprm2() |
|
203 { |
|
204 vector <char> v1(3); |
|
205 __iota(v1.begin(), v1.end(), 'A'); |
|
206 |
|
207 next_permutation(v1.begin(), v1.end(), less<char>()); |
|
208 CPPUNIT_ASSERT(v1[0]=='A'); |
|
209 CPPUNIT_ASSERT(v1[1]=='C'); |
|
210 CPPUNIT_ASSERT(v1[2]=='B'); |
|
211 next_permutation(v1.begin(), v1.end(), less<char>()); |
|
212 CPPUNIT_ASSERT(v1[0]=='B'); |
|
213 CPPUNIT_ASSERT(v1[1]=='A'); |
|
214 CPPUNIT_ASSERT(v1[2]=='C'); |
|
215 next_permutation(v1.begin(), v1.end(), less<char>()); |
|
216 CPPUNIT_ASSERT(v1[0]=='B'); |
|
217 CPPUNIT_ASSERT(v1[1]=='C'); |
|
218 CPPUNIT_ASSERT(v1[2]=='A'); |
|
219 next_permutation(v1.begin(), v1.end(), less<char>()); |
|
220 CPPUNIT_ASSERT(v1[0]=='C'); |
|
221 CPPUNIT_ASSERT(v1[1]=='A'); |
|
222 CPPUNIT_ASSERT(v1[2]=='B'); |
|
223 next_permutation(v1.begin(), v1.end(), less<char>()); |
|
224 CPPUNIT_ASSERT(v1[0]=='C'); |
|
225 CPPUNIT_ASSERT(v1[1]=='B'); |
|
226 CPPUNIT_ASSERT(v1[2]=='A'); |
|
227 next_permutation(v1.begin(), v1.end(), less<char>()); |
|
228 CPPUNIT_ASSERT(v1[0]=='A'); |
|
229 CPPUNIT_ASSERT(v1[1]=='B'); |
|
230 CPPUNIT_ASSERT(v1[2]=='C'); |
|
231 next_permutation(v1.begin(), v1.end(), less<char>()); |
|
232 CPPUNIT_ASSERT(v1[0]=='A'); |
|
233 CPPUNIT_ASSERT(v1[1]=='C'); |
|
234 CPPUNIT_ASSERT(v1[2]=='B'); |
|
235 next_permutation(v1.begin(), v1.end(), less<char>()); |
|
236 CPPUNIT_ASSERT(v1[0]=='B'); |
|
237 CPPUNIT_ASSERT(v1[1]=='A'); |
|
238 CPPUNIT_ASSERT(v1[2]=='C'); |
|
239 next_permutation(v1.begin(), v1.end(), less<char>()); |
|
240 CPPUNIT_ASSERT(v1[0]=='B'); |
|
241 CPPUNIT_ASSERT(v1[1]=='C'); |
|
242 CPPUNIT_ASSERT(v1[2]=='A'); |
|
243 |
|
244 } |