1 #include <vector>
2 #include <algorithm>
3 #include <functional>
4 #include "cppunit/cppunit_proxy.h"
5
6 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
7 using namespace std;
8 #endif
9
10 //
11 // TestCase class
12 //
13 class EqualTest : public CPPUNIT_NS::TestCase
14 {
15 CPPUNIT_TEST_SUITE(EqualTest);
16 CPPUNIT_TEST(equal_range0);
17 CPPUNIT_TEST(equal_range1);
18 CPPUNIT_TEST(equal_range2);
19 CPPUNIT_TEST(equal0);
20 CPPUNIT_TEST(equal1);
21 CPPUNIT_TEST(equal2);
22 CPPUNIT_TEST(equalto);
23 CPPUNIT_TEST_SUITE_END();
24
25 protected:
26 void equal_range0();
27 void equal_range1();
28 void equal_range2();
29 void equal0();
30 void equal1();
31 void equal2();
32 void equalto();
33 static bool values_squared(int a_, int b_);
34 };
35
36 CPPUNIT_TEST_SUITE_REGISTRATION(EqualTest);
37
38 //
39 // tests implementation
40 //
equal_range0()41 void EqualTest::equal_range0()
42 {
43 int numbers[10] = { 0, 0, 1, 1, 2, 2, 2, 2, 3, 3 };
44 pair<int*, int*> range = equal_range((int*)numbers, (int*)numbers + 10, 2);
45 CPPUNIT_ASSERT( (range.first - numbers) == 4 );
46 CPPUNIT_ASSERT( (range.second - numbers) == 8 );
47 }
48
equal_range1()49 void EqualTest::equal_range1()
50 {
51 typedef vector <int> IntVec;
52 IntVec v(10);
53 for (int i = 0; (size_t)i < v.size(); ++i)
54 v[i] = i / 3;
55
56 pair<IntVec::iterator, IntVec::iterator> range = equal_range(v.begin(), v.end(), 2);
57 CPPUNIT_ASSERT( (range.first - v.begin()) == 6 );
58 CPPUNIT_ASSERT( (range.second - v.begin()) == 9 );
59 for (; range.first != range.second; ++range.first)
60 CPPUNIT_ASSERT( *range.first == 2 );
61
62 range = equal_range(v.begin(), v.end(), 4);
63 CPPUNIT_ASSERT( range.first == range.second );
64 CPPUNIT_ASSERT( range.first == v.end() );
65 }
66
67 struct Test {
68 #if defined (__DMC__)
69 Test();
70 #endif
71
TestTest72 Test(int val) : value(val) {}
73 int value;
74
operator ==Test75 bool operator == (int i) const
76 { return value == i; }
77 };
78
operator <(const Test & v1,int v2)79 bool operator < (const Test& v1, int v2)
80 { return v1.value < v2; }
81
operator <(int v1,const Test & v2)82 bool operator < (int v1, const Test& v2)
83 { return v1 < v2.value; }
84
85 #if defined (_MSC_VER) && !defined (STLPORT)
operator <(const Test & v1,const Test & v2)86 bool operator < (const Test& v1, const Test& v2)
87 { return v1.value < v2.value; }
88 #endif
89
equal_range2()90 void EqualTest::equal_range2()
91 {
92 char chars[] = "aabbccddggghhklllmqqqqssyyzz";
93
94 const unsigned count = sizeof(chars) - 1;
95 pair<char*, char*> range = equal_range((char*)chars, (char*)chars + count, 'q', less<char>());
96 CPPUNIT_ASSERT( (range.first - chars) == 18 );
97 CPPUNIT_ASSERT( (range.second - chars) == 22 );
98 for (; range.first != range.second; ++range.first)
99 CPPUNIT_ASSERT( *range.first == 'q' );
100
101 range = equal_range((char*)chars, (char*)chars + count, 'm', less<char>());
102 CPPUNIT_ASSERT( (range.second - range.first) == 1 );
103 CPPUNIT_ASSERT( *range.first == 'm' );
104
105 vector<Test> tv;
106 vector<Test>::iterator it;
107 pair<vector<Test>::iterator, vector<Test>::iterator> p;
108
109 for (int i = 0; i < 10; ++i) {
110 tv.push_back(i);
111 }
112
113 it = upper_bound(tv.begin(), tv.end(), 5);
114 CPPUNIT_ASSERT( it != tv.end() );
115 CPPUNIT_ASSERT( *it == 6 );
116
117 it = lower_bound(tv.begin(), tv.end(), 5);
118 CPPUNIT_ASSERT( it != tv.end() );
119 CPPUNIT_ASSERT( *it == 5 );
120
121 p = equal_range(tv.begin(), tv.end(), 5);
122 CPPUNIT_ASSERT( p.first != p.second );
123 CPPUNIT_ASSERT( p.first != tv.end() );
124 CPPUNIT_ASSERT( p.second != tv.end() );
125 CPPUNIT_ASSERT( *p.first == 5 );
126 CPPUNIT_ASSERT( *p.second == 6 );
127 }
128
equal0()129 void EqualTest::equal0()
130 {
131 int numbers1[5] = { 1, 2, 3, 4, 5 };
132 int numbers2[5] = { 1, 2, 4, 8, 16 };
133 int numbers3[2] = { 1, 2 };
134
135 CPPUNIT_ASSERT( !equal(numbers1, numbers1 + 5, numbers2) );
136 CPPUNIT_ASSERT( equal(numbers3, numbers3 + 2, numbers1) );
137 }
138
equal1()139 void EqualTest::equal1()
140 {
141 vector <int> v1(10);
142 for (int i = 0; (size_t)i < v1.size(); ++i)
143 v1[i] = i;
144 vector <int> v2(10);
145 CPPUNIT_ASSERT( !equal(v1.begin(), v1.end(), v2.begin()) );
146
147 copy(v1.begin(), v1.end(), v2.begin());
148 CPPUNIT_ASSERT( equal(v1.begin(), v1.end(), v2.begin()) )
149 }
150
equal2()151 void EqualTest::equal2()
152 {
153 vector <int> v1(10);
154 vector <int> v2(10);
155 for (int i = 0; (size_t)i < v1.size(); ++i) {
156 v1[i] = i;
157 v2[i] = i * i;
158 }
159 CPPUNIT_ASSERT( equal(v1.begin(), v1.end(), v2.begin(), values_squared) );
160 }
161
equalto()162 void EqualTest::equalto()
163 {
164 int input1 [4] = { 1, 7, 2, 2 };
165 int input2 [4] = { 1, 6, 2, 3 };
166
167 int output [4];
168 transform((int*)input1, (int*)input1 + 4, (int*)input2, (int*)output, equal_to<int>());
169 CPPUNIT_ASSERT( output[0] == 1 );
170 CPPUNIT_ASSERT( output[1] == 0 );
171 CPPUNIT_ASSERT( output[2] == 1 );
172 CPPUNIT_ASSERT( output[3] == 0 );
173 }
174
values_squared(int a_,int b_)175 bool EqualTest::values_squared(int a_, int b_)
176 {
177 return (a_ * a_ == b_);
178 }
179