First public contribution.
7 #include "cppunit/cppunit_proxy.h"
9 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
16 class IterTest : public CPPUNIT_NS::TestCase
18 CPPUNIT_TEST_SUITE(IterTest);
22 CPPUNIT_TEST(iterswp0);
23 CPPUNIT_TEST(iterswp1);
24 CPPUNIT_TEST(iterswp2);
25 CPPUNIT_TEST(iterswp3);
26 CPPUNIT_TEST_SUITE_END();
38 CPPUNIT_TEST_SUITE_REGISTRATION(IterTest);
41 // tests implementation
43 void IterTest::iter1()
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;
50 for (vec::iterator i = v.begin(); i != v.end(); ++i, ++counter) {
53 CPPUNIT_ASSERT(!strcmp(*i, "zippy"));
56 CPPUNIT_ASSERT(!strcmp(*i, "motorboy"));
59 CPPUNIT_ASSERT(false);
63 void IterTest::iter3()
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;
71 for (it = v.rbegin(); it != v.rend(); ++it, ++counter) {
74 CPPUNIT_ASSERT(!strcmp(*it, "zippy"));
77 CPPUNIT_ASSERT(!strcmp(*it, "motorboy"));
80 CPPUNIT_ASSERT(false);
86 void IterTest::iter4()
88 vector<int> v; // Empty vector of integers.
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);
99 void IterTest::iterswp0()
101 int numbers[6] = { 0, 1, 2, 3, 4, 5 };
103 iter_swap(numbers, numbers + 3);
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);
113 void IterTest::iterswp1()
116 __iota(v1.begin(), v1.end(), 0);
117 iter_swap( v1.begin(), v1.begin() + 3 );
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);
126 void IterTest::iterswp2()
128 vector<bool> boolVector;
130 boolVector.push_back( true );
131 boolVector.push_back( false );
133 vector<bool>::iterator i1 = boolVector.begin();
134 vector<bool>::iterator i2 = boolVector.begin();
142 CPPUNIT_ASSERT(( *i1 == v1 && *i2 == v0 ));
146 void IterTest::iterswp3()
148 vector<int> vvref(10, 10);
149 vector<int> lvref(10, 20);
151 vector<vector<int> > vvints(4, vvref);
152 list<vector<int> > lvints(4, lvref);
154 iter_swap(vvints.begin(), lvints.begin());
155 CPPUNIT_CHECK( vvints.front() == lvref );
156 CPPUNIT_CHECK( lvints.front() == vvref );
158 #if defined (STLPORT) && defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
159 int *pvvint = &vvints.front().front();
160 int *plvint = &lvints.front().front();
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() );