31
|
1 |
#include <vector>
|
|
2 |
#include <list>
|
|
3 |
#include <algorithm>
|
|
4 |
#include <numeric>
|
|
5 |
|
|
6 |
#include "iota.h"
|
|
7 |
#include "cppunit/cppunit_proxy.h"
|
|
8 |
|
|
9 |
#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
|
|
10 |
using namespace std;
|
|
11 |
#endif
|
|
12 |
|
|
13 |
//
|
|
14 |
// TestCase class
|
|
15 |
//
|
|
16 |
class IterTest : public CPPUNIT_NS::TestCase
|
|
17 |
{
|
|
18 |
CPPUNIT_TEST_SUITE(IterTest);
|
|
19 |
CPPUNIT_TEST(iter1);
|
|
20 |
CPPUNIT_TEST(iter3);
|
|
21 |
CPPUNIT_TEST(iter4);
|
|
22 |
CPPUNIT_TEST(iterswp0);
|
|
23 |
CPPUNIT_TEST(iterswp1);
|
|
24 |
CPPUNIT_TEST(iterswp2);
|
|
25 |
CPPUNIT_TEST(iterswp3);
|
|
26 |
CPPUNIT_TEST_SUITE_END();
|
|
27 |
|
|
28 |
protected:
|
|
29 |
void iter1();
|
|
30 |
void iter3();
|
|
31 |
void iter4();
|
|
32 |
void iterswp0();
|
|
33 |
void iterswp1();
|
|
34 |
void iterswp2();
|
|
35 |
void iterswp3();
|
|
36 |
};
|
|
37 |
|
|
38 |
CPPUNIT_TEST_SUITE_REGISTRATION(IterTest);
|
|
39 |
|
|
40 |
//
|
|
41 |
// tests implementation
|
|
42 |
//
|
|
43 |
void IterTest::iter1()
|
|
44 |
{
|
|
45 |
vector<const char*> v; // Vector of character strings.
|
|
46 |
v.push_back("zippy"); // First element.
|
|
47 |
v.push_back("motorboy"); // Second element.
|
|
48 |
typedef vector<const char*> vec;
|
|
49 |
unsigned counter = 0;
|
|
50 |
for (vec::iterator i = v.begin(); i != v.end(); ++i, ++counter) {
|
|
51 |
switch (counter) {
|
|
52 |
case 0:
|
|
53 |
CPPUNIT_ASSERT(!strcmp(*i, "zippy"));
|
|
54 |
break;
|
|
55 |
case 1:
|
|
56 |
CPPUNIT_ASSERT(!strcmp(*i, "motorboy"));
|
|
57 |
break;
|
|
58 |
default:
|
|
59 |
CPPUNIT_ASSERT(false);
|
|
60 |
}
|
|
61 |
}
|
|
62 |
}
|
|
63 |
void IterTest::iter3()
|
|
64 |
{
|
|
65 |
typedef vector<const char*> Vec;
|
|
66 |
Vec v; // Vector of character strings.
|
|
67 |
v.push_back("zippy"); // First element.
|
|
68 |
v.push_back("motorboy"); // Second element.
|
|
69 |
Vec::reverse_iterator it;
|
|
70 |
unsigned counter = 0;
|
|
71 |
for (it = v.rbegin(); it != v.rend(); ++it, ++counter) {
|
|
72 |
switch (counter) {
|
|
73 |
case 1:
|
|
74 |
CPPUNIT_ASSERT(!strcmp(*it, "zippy"));
|
|
75 |
break;
|
|
76 |
case 0:
|
|
77 |
CPPUNIT_ASSERT(!strcmp(*it, "motorboy"));
|
|
78 |
break;
|
|
79 |
default:
|
|
80 |
CPPUNIT_ASSERT(false);
|
|
81 |
}
|
|
82 |
}
|
|
83 |
|
|
84 |
CPPUNIT_ASSERT(true);
|
|
85 |
}
|
|
86 |
void IterTest::iter4()
|
|
87 |
{
|
|
88 |
vector<int> v; // Empty vector of integers.
|
|
89 |
v.push_back(1);
|
|
90 |
v.push_back(2);
|
|
91 |
v.push_back(3);
|
|
92 |
// Position immediately after last item.
|
|
93 |
vector<int>::iterator i = v.end();
|
|
94 |
// Move back one and then access.
|
|
95 |
CPPUNIT_ASSERT((*--i)==3);
|
|
96 |
i -= 2; // Jump back two items.
|
|
97 |
CPPUNIT_ASSERT((*i)==1);
|
|
98 |
}
|
|
99 |
void IterTest::iterswp0()
|
|
100 |
{
|
|
101 |
int numbers[6] = { 0, 1, 2, 3, 4, 5 };
|
|
102 |
|
|
103 |
iter_swap(numbers, numbers + 3);
|
|
104 |
|
|
105 |
CPPUNIT_ASSERT(numbers[0]==3);
|
|
106 |
CPPUNIT_ASSERT(numbers[1]==1);
|
|
107 |
CPPUNIT_ASSERT(numbers[2]==2);
|
|
108 |
CPPUNIT_ASSERT(numbers[3]==0);
|
|
109 |
CPPUNIT_ASSERT(numbers[4]==4);
|
|
110 |
CPPUNIT_ASSERT(numbers[5]==5);
|
|
111 |
|
|
112 |
}
|
|
113 |
void IterTest::iterswp1()
|
|
114 |
{
|
|
115 |
vector<int> v1(6);
|
|
116 |
__iota(v1.begin(), v1.end(), 0);
|
|
117 |
iter_swap( v1.begin(), v1.begin() + 3 );
|
|
118 |
|
|
119 |
CPPUNIT_ASSERT(v1[0]==3);
|
|
120 |
CPPUNIT_ASSERT(v1[1]==1);
|
|
121 |
CPPUNIT_ASSERT(v1[2]==2);
|
|
122 |
CPPUNIT_ASSERT(v1[3]==0);
|
|
123 |
CPPUNIT_ASSERT(v1[4]==4);
|
|
124 |
CPPUNIT_ASSERT(v1[5]==5);
|
|
125 |
}
|
|
126 |
void IterTest::iterswp2()
|
|
127 |
{
|
|
128 |
vector<bool> boolVector;
|
|
129 |
|
|
130 |
boolVector.push_back( true );
|
|
131 |
boolVector.push_back( false );
|
|
132 |
|
|
133 |
vector<bool>::iterator i1 = boolVector.begin();
|
|
134 |
vector<bool>::iterator i2 = boolVector.begin();
|
|
135 |
++i2;
|
|
136 |
|
|
137 |
bool v0 = *i1;
|
|
138 |
bool v1 = *i2;
|
|
139 |
|
|
140 |
iter_swap( i1, i2 );
|
|
141 |
|
|
142 |
CPPUNIT_ASSERT(( *i1 == v1 && *i2 == v0 ));
|
|
143 |
}
|
|
144 |
|
|
145 |
|
|
146 |
void IterTest::iterswp3()
|
|
147 |
{
|
|
148 |
vector<int> vvref(10, 10);
|
|
149 |
vector<int> lvref(10, 20);
|
|
150 |
|
|
151 |
vector<vector<int> > vvints(4, vvref);
|
|
152 |
list<vector<int> > lvints(4, lvref);
|
|
153 |
|
|
154 |
iter_swap(vvints.begin(), lvints.begin());
|
|
155 |
CPPUNIT_CHECK( vvints.front() == lvref );
|
|
156 |
CPPUNIT_CHECK( lvints.front() == vvref );
|
|
157 |
|
|
158 |
#if defined (STLPORT) && defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
|
|
159 |
int *pvvint = &vvints.front().front();
|
|
160 |
int *plvint = &lvints.front().front();
|
|
161 |
|
|
162 |
iter_swap(vvints.begin(), lvints.begin());
|
|
163 |
//Check that elements have been swaped:
|
|
164 |
CPPUNIT_CHECK( pvvint == &lvints.front().front() );
|
|
165 |
CPPUNIT_CHECK( plvint == &vvints.front().front() );
|
|
166 |
#endif
|
|
167 |
}
|