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