aboutsummaryrefslogtreecommitdiff
path: root/test/unit/iter_test.cpp
blob: 52e4348df6ee96dd4282ee3776890004f4ebbd60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <vector>
#include <list>
#include <algorithm>
#include <numeric>

#include "iota.h"
#include "cppunit/cppunit_proxy.h"

#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
using namespace std;
#endif

//
// TestCase class
//
class IterTest : public CPPUNIT_NS::TestCase
{
  CPPUNIT_TEST_SUITE(IterTest);
  CPPUNIT_TEST(iter1);
  CPPUNIT_TEST(iter3);
  CPPUNIT_TEST(iter4);
  CPPUNIT_TEST(iterswp0);
  CPPUNIT_TEST(iterswp1);
  CPPUNIT_TEST(iterswp2);
  CPPUNIT_TEST(iterswp3);
  CPPUNIT_TEST_SUITE_END();

protected:
  void iter1();
  void iter3();
  void iter4();
  void iterswp0();
  void iterswp1();
  void iterswp2();
  void iterswp3();
};

CPPUNIT_TEST_SUITE_REGISTRATION(IterTest);

//
// tests implementation
//
void IterTest::iter1()
{
  vector<const char*> v; // Vector of character strings.
  v.push_back("zippy"); // First element.
  v.push_back("motorboy"); // Second element.
  typedef vector<const char*> vec;
  unsigned counter = 0;
  for (vec::iterator i = v.begin(); i != v.end(); ++i, ++counter) {
    switch (counter) {
      case 0:
        CPPUNIT_ASSERT(!strcmp(*i, "zippy"));
        break;
      case 1:
        CPPUNIT_ASSERT(!strcmp(*i, "motorboy"));
        break;
      default:
        CPPUNIT_FAIL;
    }
  }
}
void IterTest::iter3()
{
  typedef vector<const char*> Vec;
  Vec v; // Vector of character strings.
  v.push_back("zippy"); // First element.
  v.push_back("motorboy"); // Second element.
  Vec::reverse_iterator it;
  unsigned counter = 0;
  for (it = v.rbegin(); it != v.rend(); ++it, ++counter) {
    switch (counter) {
      case 1:
        CPPUNIT_ASSERT(!strcmp(*it, "zippy"));
        break;
      case 0:
        CPPUNIT_ASSERT(!strcmp(*it, "motorboy"));
        break;
      default:
        CPPUNIT_FAIL;
    }
  }
}
void IterTest::iter4()
{
  vector<int> v; // Empty vector of integers.
  v.push_back(1);
  v.push_back(2);
  v.push_back(3);
  // Position immediately after last item.
  vector<int>::iterator i = v.end();
  // Move back one and then access.
  CPPUNIT_ASSERT((*--i)==3);
  i -= 2; // Jump back two items.
  CPPUNIT_ASSERT((*i)==1);
}
void IterTest::iterswp0()
{
  int numbers[6] = { 0, 1, 2, 3, 4, 5 };

  iter_swap(numbers, numbers + 3);

  CPPUNIT_ASSERT(numbers[0]==3);
  CPPUNIT_ASSERT(numbers[1]==1);
  CPPUNIT_ASSERT(numbers[2]==2);
  CPPUNIT_ASSERT(numbers[3]==0);
  CPPUNIT_ASSERT(numbers[4]==4);
  CPPUNIT_ASSERT(numbers[5]==5);

}
void IterTest::iterswp1()
{
  vector<int> v1(6);
  __iota(v1.begin(), v1.end(), 0);
  iter_swap( v1.begin(), v1.begin() + 3 );

  CPPUNIT_ASSERT(v1[0]==3);
  CPPUNIT_ASSERT(v1[1]==1);
  CPPUNIT_ASSERT(v1[2]==2);
  CPPUNIT_ASSERT(v1[3]==0);
  CPPUNIT_ASSERT(v1[4]==4);
  CPPUNIT_ASSERT(v1[5]==5);
}
void IterTest::iterswp2()
{
  vector<bool> boolVector;

  boolVector.push_back( true );
  boolVector.push_back( false );

  vector<bool>::iterator i1 = boolVector.begin();
  vector<bool>::iterator i2 = boolVector.begin();
  ++i2;

  bool v0 = *i1;
  bool v1 = *i2;

  iter_swap( i1, i2 );

  CPPUNIT_ASSERT(( *i1 == v1 && *i2 == v0 ));
}


void IterTest::iterswp3()
{
  vector<int> vvref(10, 10);
  vector<int> lvref(10, 20);

  vector<vector<int> > vvints(4, vvref);
  list<vector<int> > lvints(4, lvref);

  iter_swap(vvints.begin(), lvints.begin());
  CPPUNIT_CHECK( vvints.front() == lvref );
  CPPUNIT_CHECK( lvints.front() == vvref );

  //const vector<vector<int> > &cvvints = vvints;
  //iter_swap(cvvints.begin(), lvints.begin());
  //iter_swap(lvints.begin(), cvvints.begin());

#if defined (STLPORT) && defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
  int *pvvint = &vvints.front().front();
  int *plvint = &lvints.front().front();

  iter_swap(vvints.begin(), lvints.begin());
  //Check that elements have been swaped:
  CPPUNIT_CHECK( pvvint == &lvints.front().front() );
  CPPUNIT_CHECK( plvint == &vvints.front().front() );
#endif
}