test-range.cxx (31682d32) test-range.cxx (18cf0442)
1/**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance

--- 8 unchanged lines hidden (view full) ---

17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24#include "preextstl.h"
1/**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance

--- 8 unchanged lines hidden (view full) ---

17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24#include "preextstl.h"
25#include "cppunit/TestAssert.h"
26#include "cppunit/TestFixture.h"
27#include "cppunit/extensions/HelperMacros.h"
25#include "gtest/gtest.h"
28#include "postextstl.h"
29
30#include <o3tl/range.hxx>
31#include <vector>
32#include <deque>
33
34
35
36using o3tl::range;
37using o3tl::make_range;
38using o3tl::range_of;
39using std::size_t;
40
41
26#include "postextstl.h"
27
28#include <o3tl/range.hxx>
29#include <vector>
30#include <deque>
31
32
33
34using o3tl::range;
35using o3tl::make_range;
36using o3tl::range_of;
37using std::size_t;
38
39
42class range_test : public CppUnit::TestFixture
40class range_test : public ::testing::Test
43{
44public:
45
46 void int_test()
47 {
48 range<int>
49 t1(12,88);
50 range<int>
51 t2(33,33);
52
53 // ctor
41{
42public:
43
44 void int_test()
45 {
46 range<int>
47 t1(12,88);
48 range<int>
49 t2(33,33);
50
51 // ctor
54 CPPUNIT_ASSERT_MESSAGE("int ctor1", t1.begin() == 12);
55 CPPUNIT_ASSERT_MESSAGE("int ctor2", t1.end() == 88);
56 CPPUNIT_ASSERT_MESSAGE("int ctor3", t2.begin() == 33);
57 CPPUNIT_ASSERT_MESSAGE("int ctor4", t2.end() == 33);
52 ASSERT_TRUE(t1.begin() == 12) << "int ctor1";
53 ASSERT_TRUE(t1.end() == 88) << "int ctor2";
54 ASSERT_TRUE(t2.begin() == 33) << "int ctor3";
55 ASSERT_TRUE(t2.end() == 33) << "int ctor4";
58
59 // make_range
56
57 // make_range
60 CPPUNIT_ASSERT_MESSAGE("int make_range1", make_range(0,8).begin() == 0);
61 CPPUNIT_ASSERT_MESSAGE("int make_range2", make_range(0,8).end() == 8);
58 ASSERT_TRUE(make_range(0,8).begin() == 0) << "int make_range1";
59 ASSERT_TRUE(make_range(0,8).end() == 8) << "int make_range2";
62
63 // size
60
61 // size
64 CPPUNIT_ASSERT_MESSAGE("int size1", t1.size() == size_t(t1.end() - t1.begin()) );
65 CPPUNIT_ASSERT_MESSAGE("int size2", t2.size() == size_t(0) );
62 ASSERT_TRUE(t1.size() == size_t(t1.end() - t1.begin())) << "int size1";
63 ASSERT_TRUE(t2.size() == size_t(0)) << "int size2";
66
67 // contains
68 range<int> t3(0,10);
69 range<int> t4(7, 15);
70 range<int> t5(12, 12);
71 range<int> t6(13, 77);
72 range<int> t7(87, 87);
73 range<int> t8(87, 88);
74 range<int> t9(88, 88);
75 range<int> t10(33, 120);
76 range<int> t11(90, 100);
77 range<int> t12(200,200);
78
64
65 // contains
66 range<int> t3(0,10);
67 range<int> t4(7, 15);
68 range<int> t5(12, 12);
69 range<int> t6(13, 77);
70 range<int> t7(87, 87);
71 range<int> t8(87, 88);
72 range<int> t9(88, 88);
73 range<int> t10(33, 120);
74 range<int> t11(90, 100);
75 range<int> t12(200,200);
76
79 CPPUNIT_ASSERT_MESSAGE("int contains1", t1.contains(t1));
80 CPPUNIT_ASSERT_MESSAGE("int contains2", t1.contains(t2));
81 CPPUNIT_ASSERT_MESSAGE("int contains3", ! t1.contains(t3));
82 CPPUNIT_ASSERT_MESSAGE("int contains4", ! t1.contains(t4));
83 CPPUNIT_ASSERT_MESSAGE("int contains5", t1.contains(t5));
84 CPPUNIT_ASSERT_MESSAGE("int contains6", t1.contains(t6));
85 CPPUNIT_ASSERT_MESSAGE("int contains7", t1.contains(t7));
86 CPPUNIT_ASSERT_MESSAGE("int contains8", t1.contains(t8));
87 CPPUNIT_ASSERT_MESSAGE("int contains9", ! t1.contains(t9));
88 CPPUNIT_ASSERT_MESSAGE("int contains10", ! t1.contains(t10));
89 CPPUNIT_ASSERT_MESSAGE("int contains11", ! t1.contains(t11));
90 CPPUNIT_ASSERT_MESSAGE("int contains12", ! t1.contains(t12));
77 ASSERT_TRUE(t1.contains(t1)) << "int contains1";
78 ASSERT_TRUE(t1.contains(t2)) << "int contains2";
79 ASSERT_TRUE(! t1.contains(t3)) << "int contains3";
80 ASSERT_TRUE(! t1.contains(t4)) << "int contains4";
81 ASSERT_TRUE(t1.contains(t5)) << "int contains5";
82 ASSERT_TRUE(t1.contains(t6)) << "int contains6";
83 ASSERT_TRUE(t1.contains(t7)) << "int contains7";
84 ASSERT_TRUE(t1.contains(t8)) << "int contains8";
85 ASSERT_TRUE(! t1.contains(t9)) << "int contains9";
86 ASSERT_TRUE(! t1.contains(t10)) << "int contains10";
87 ASSERT_TRUE(! t1.contains(t11)) << "int contains11";
88 ASSERT_TRUE(! t1.contains(t12)) << "int contains12";
91
89
92 CPPUNIT_ASSERT_MESSAGE("int contains n1", t1.contains(50));
93 CPPUNIT_ASSERT_MESSAGE("int contains n2", t1.contains(12));
94 CPPUNIT_ASSERT_MESSAGE("int contains n3", t1.contains(87));
95 CPPUNIT_ASSERT_MESSAGE("int contains n4", ! t1.contains(3));
96 CPPUNIT_ASSERT_MESSAGE("int contains n5", ! t1.contains(11));
97 CPPUNIT_ASSERT_MESSAGE("int contains n6", ! t1.contains(88));
98 CPPUNIT_ASSERT_MESSAGE("int contains n7", ! t1.contains(100));
90 ASSERT_TRUE(t1.contains(50)) << "int contains n1";
91 ASSERT_TRUE(t1.contains(12)) << "int contains n2";
92 ASSERT_TRUE(t1.contains(87)) << "int contains n3";
93 ASSERT_TRUE(! t1.contains(3)) << "int contains n4";
94 ASSERT_TRUE(! t1.contains(11)) << "int contains n5";
95 ASSERT_TRUE(! t1.contains(88)) << "int contains n6";
96 ASSERT_TRUE(! t1.contains(100)) << "int contains n7";
99
100 // overlaps
101 range<int> t13(88,99);
102
97
98 // overlaps
99 range<int> t13(88,99);
100
103 CPPUNIT_ASSERT_MESSAGE("int overlaps1", t1.overlaps(t1));
104 CPPUNIT_ASSERT_MESSAGE("int overlaps2", t1.overlaps(t2));
105 CPPUNIT_ASSERT_MESSAGE("int overlaps3", ! t1.overlaps(t3));
106 CPPUNIT_ASSERT_MESSAGE("int overlaps4", t1.overlaps(t4));
107 CPPUNIT_ASSERT_MESSAGE("int overlaps5", t1.overlaps(t5));
108 CPPUNIT_ASSERT_MESSAGE("int overlaps6", t1.overlaps(t6));
109 CPPUNIT_ASSERT_MESSAGE("int overlaps7", t1.overlaps(t7));
110 CPPUNIT_ASSERT_MESSAGE("int overlaps8", t1.overlaps(t8));
111 CPPUNIT_ASSERT_MESSAGE("int overlaps9", ! t1.overlaps(t9));
112 CPPUNIT_ASSERT_MESSAGE("int overlaps10", t1.overlaps(t10));
113 CPPUNIT_ASSERT_MESSAGE("int overlaps11", ! t1.overlaps(t11));
114 CPPUNIT_ASSERT_MESSAGE("int overlaps12", ! t1.overlaps(t12));
115 CPPUNIT_ASSERT_MESSAGE("int overlaps13", ! t1.overlaps(t13));
101 ASSERT_TRUE(t1.overlaps(t1)) << "int overlaps1";
102 ASSERT_TRUE(t1.overlaps(t2)) << "int overlaps2";
103 ASSERT_TRUE(! t1.overlaps(t3)) << "int overlaps3";
104 ASSERT_TRUE(t1.overlaps(t4)) << "int overlaps4";
105 ASSERT_TRUE(t1.overlaps(t5)) << "int overlaps5";
106 ASSERT_TRUE(t1.overlaps(t6)) << "int overlaps6";
107 ASSERT_TRUE(t1.overlaps(t7)) << "int overlaps7";
108 ASSERT_TRUE(t1.overlaps(t8)) << "int overlaps8";
109 ASSERT_TRUE(! t1.overlaps(t9)) << "int overlaps9";
110 ASSERT_TRUE(t1.overlaps(t10)) << "int overlaps10";
111 ASSERT_TRUE(! t1.overlaps(t11)) << "int overlaps11";
112 ASSERT_TRUE(! t1.overlaps(t12)) << "int overlaps12";
113 ASSERT_TRUE(! t1.overlaps(t13)) << "int overlaps13";
116
117 // distance_to
114
115 // distance_to
118 CPPUNIT_ASSERT_MESSAGE("int distance_to1", t1.distance_to(t13) == 0);
119 CPPUNIT_ASSERT_MESSAGE("int distance_to2", t1.distance_to(t9) == 0);
120 CPPUNIT_ASSERT_MESSAGE("int distance_to3", t1.distance_to(t11) == 2);
121 CPPUNIT_ASSERT_MESSAGE("int distance_to4", t1.distance_to(t8) == -1);
122 CPPUNIT_ASSERT_MESSAGE("int distance_to5", t1.distance_to(t3) == -88);
116 ASSERT_TRUE(t1.distance_to(t13) == 0) << "int distance_to1";
117 ASSERT_TRUE(t1.distance_to(t9) == 0) << "int distance_to2";
118 ASSERT_TRUE(t1.distance_to(t11) == 2) << "int distance_to3";
119 ASSERT_TRUE(t1.distance_to(t8) == -1) << "int distance_to4";
120 ASSERT_TRUE(t1.distance_to(t3) == -88) << "int distance_to5";
123 }
124
125 void iterator_test()
126 {
127 typedef std::vector<char>::const_iterator test_it;
128 const std::vector<char> hv(200,'x');
129
130
131 test_it hit1 = hv.begin() + 12;
132 test_it hit2 = hv.begin() + 88;
133
134 range<test_it>
135 t1(hit1, hit2);
136 range<test_it>
137 t2(hv.begin()+33, hv.begin()+33);
138
139 // ctor
121 }
122
123 void iterator_test()
124 {
125 typedef std::vector<char>::const_iterator test_it;
126 const std::vector<char> hv(200,'x');
127
128
129 test_it hit1 = hv.begin() + 12;
130 test_it hit2 = hv.begin() + 88;
131
132 range<test_it>
133 t1(hit1, hit2);
134 range<test_it>
135 t2(hv.begin()+33, hv.begin()+33);
136
137 // ctor
140 CPPUNIT_ASSERT_MESSAGE("ivec ctor1", t1.begin() == hit1);
141 CPPUNIT_ASSERT_MESSAGE("ivec ctor2", t1.end() == hit2);
142 CPPUNIT_ASSERT_MESSAGE("ivec ctor3", t2.begin() == hv.begin()+33);
143 CPPUNIT_ASSERT_MESSAGE("ivec ctor4", t2.end() == hv.begin()+33);
138 ASSERT_TRUE(t1.begin() == hit1) << "ivec ctor1";
139 ASSERT_TRUE(t1.end() == hit2) << "ivec ctor2";
140 ASSERT_TRUE(t2.begin() == hv.begin()+33) << "ivec ctor3";
141 ASSERT_TRUE(t2.end() == hv.begin()+33) << "ivec ctor4";
144
145 // make_range
142
143 // make_range
146 CPPUNIT_ASSERT_MESSAGE("ivec make_range1", make_range(hv.begin(), hv.begin()+8).begin() == hv.begin());
147 CPPUNIT_ASSERT_MESSAGE("ivec make_range2", make_range(hv.begin(), hv.begin()+8).end() == hv.begin()+8);
144 ASSERT_TRUE(make_range(hv.begin(), hv.begin()+8).begin() == hv.begin()) << "ivec make_range1";
145 ASSERT_TRUE(make_range(hv.begin(), hv.begin()+8).end() == hv.begin()+8) << "ivec make_range2";
148
149 // size
146
147 // size
150 CPPUNIT_ASSERT_MESSAGE("ivec size1", t1.size() == size_t(t1.end() - t1.begin()) );
151 CPPUNIT_ASSERT_MESSAGE("ivec size2", t2.size() == size_t(0) );
148 ASSERT_TRUE(t1.size() == size_t(t1.end() - t1.begin())) << "ivec size1";
149 ASSERT_TRUE(t2.size() == size_t(0)) << "ivec size2";
152
153 // contains
154 range<test_it> t3(hv.begin(), hv.begin() + 10);
155 range<test_it> t4(hv.begin() + 7, hv.begin() + 15);
156 range<test_it> t5(hit1, hit1);
157 range<test_it> t6(hv.begin() + 13, hv.begin() + 77);
158 range<test_it> t7(hv.begin() + 87, hv.begin() + 87);
159 range<test_it> t8(hv.begin() + 87, hit2);
160 range<test_it> t9(hit2, hit2);
161 range<test_it> t10(hv.begin() + 33, hv.begin() + 120);
162 range<test_it> t11(hv.begin() + 90, hv.begin() + 100);
163 range<test_it> t12(hv.begin() + 200,hv.begin() + 200);
164
150
151 // contains
152 range<test_it> t3(hv.begin(), hv.begin() + 10);
153 range<test_it> t4(hv.begin() + 7, hv.begin() + 15);
154 range<test_it> t5(hit1, hit1);
155 range<test_it> t6(hv.begin() + 13, hv.begin() + 77);
156 range<test_it> t7(hv.begin() + 87, hv.begin() + 87);
157 range<test_it> t8(hv.begin() + 87, hit2);
158 range<test_it> t9(hit2, hit2);
159 range<test_it> t10(hv.begin() + 33, hv.begin() + 120);
160 range<test_it> t11(hv.begin() + 90, hv.begin() + 100);
161 range<test_it> t12(hv.begin() + 200,hv.begin() + 200);
162
165 CPPUNIT_ASSERT_MESSAGE("ivec contains1", t1.contains(t1));
166 CPPUNIT_ASSERT_MESSAGE("ivec contains2", t1.contains(t2));
167 CPPUNIT_ASSERT_MESSAGE("ivec contains3", ! t1.contains(t3));
168 CPPUNIT_ASSERT_MESSAGE("ivec contains4", ! t1.contains(t4));
169 CPPUNIT_ASSERT_MESSAGE("ivec contains5", t1.contains(t5));
170 CPPUNIT_ASSERT_MESSAGE("ivec contains6", t1.contains(t6));
171 CPPUNIT_ASSERT_MESSAGE("ivec contains7", t1.contains(t7));
172 CPPUNIT_ASSERT_MESSAGE("ivec contains8", t1.contains(t8));
173 CPPUNIT_ASSERT_MESSAGE("ivec contains9", ! t1.contains(t9));
174 CPPUNIT_ASSERT_MESSAGE("ivec contains10", ! t1.contains(t10));
175 CPPUNIT_ASSERT_MESSAGE("ivec contains11", ! t1.contains(t11));
176 CPPUNIT_ASSERT_MESSAGE("ivec contains12", ! t1.contains(t12));
163 ASSERT_TRUE(t1.contains(t1)) << "ivec contains1";
164 ASSERT_TRUE(t1.contains(t2)) << "ivec contains2";
165 ASSERT_TRUE(! t1.contains(t3)) << "ivec contains3";
166 ASSERT_TRUE(! t1.contains(t4)) << "ivec contains4";
167 ASSERT_TRUE(t1.contains(t5)) << "ivec contains5";
168 ASSERT_TRUE(t1.contains(t6)) << "ivec contains6";
169 ASSERT_TRUE(t1.contains(t7)) << "ivec contains7";
170 ASSERT_TRUE(t1.contains(t8)) << "ivec contains8";
171 ASSERT_TRUE(! t1.contains(t9)) << "ivec contains9";
172 ASSERT_TRUE(! t1.contains(t10)) << "ivec contains10";
173 ASSERT_TRUE(! t1.contains(t11)) << "ivec contains11";
174 ASSERT_TRUE(! t1.contains(t12)) << "ivec contains12";
177
175
178 CPPUNIT_ASSERT_MESSAGE("ivec contains n1", t1.contains(hv.begin() + 50));
179 CPPUNIT_ASSERT_MESSAGE("ivec contains n2", t1.contains(hit1));
180 CPPUNIT_ASSERT_MESSAGE("ivec contains n3", t1.contains(hv.begin() + 87));
181 CPPUNIT_ASSERT_MESSAGE("ivec contains n4", ! t1.contains(hv.begin() + 3));
182 CPPUNIT_ASSERT_MESSAGE("ivec contains n5", ! t1.contains(hv.begin() + 11));
183 CPPUNIT_ASSERT_MESSAGE("ivec contains n6", ! t1.contains(hit2));
184 CPPUNIT_ASSERT_MESSAGE("ivec contains n7", ! t1.contains(hv.begin() + 100));
176 ASSERT_TRUE(t1.contains(hv.begin() + 50)) << "ivec contains n1";
177 ASSERT_TRUE(t1.contains(hit1)) << "ivec contains n2";
178 ASSERT_TRUE(t1.contains(hv.begin() + 87)) << "ivec contains n3";
179 ASSERT_TRUE(! t1.contains(hv.begin() + 3)) << "ivec contains n4";
180 ASSERT_TRUE(! t1.contains(hv.begin() + 11)) << "ivec contains n5";
181 ASSERT_TRUE(! t1.contains(hit2)) << "ivec contains n6";
182 ASSERT_TRUE(! t1.contains(hv.begin() + 100)) << "ivec contains n7";
185
186 // overlaps
187 range<test_it> t13(hit2, hv.begin() + 99);
188
183
184 // overlaps
185 range<test_it> t13(hit2, hv.begin() + 99);
186
189 CPPUNIT_ASSERT_MESSAGE("ivec overlaps1", t1.overlaps(t1));
190 CPPUNIT_ASSERT_MESSAGE("ivec overlaps2", t1.overlaps(t2));
191 CPPUNIT_ASSERT_MESSAGE("ivec overlaps3", ! t1.overlaps(t3));
192 CPPUNIT_ASSERT_MESSAGE("ivec overlaps4", t1.overlaps(t4));
193 CPPUNIT_ASSERT_MESSAGE("ivec overlaps5", t1.overlaps(t5));
194 CPPUNIT_ASSERT_MESSAGE("ivec overlaps6", t1.overlaps(t6));
195 CPPUNIT_ASSERT_MESSAGE("ivec overlaps7", t1.overlaps(t7));
196 CPPUNIT_ASSERT_MESSAGE("ivec overlaps8", t1.overlaps(t8));
197 CPPUNIT_ASSERT_MESSAGE("ivec overlaps9", ! t1.overlaps(t9));
198 CPPUNIT_ASSERT_MESSAGE("ivec overlaps10", t1.overlaps(t10));
199 CPPUNIT_ASSERT_MESSAGE("ivec overlaps11", ! t1.overlaps(t11));
200 CPPUNIT_ASSERT_MESSAGE("ivec overlaps12", ! t1.overlaps(t12));
201 CPPUNIT_ASSERT_MESSAGE("ivec overlaps13", ! t1.overlaps(t13));
187 ASSERT_TRUE(t1.overlaps(t1)) << "ivec overlaps1";
188 ASSERT_TRUE(t1.overlaps(t2)) << "ivec overlaps2";
189 ASSERT_TRUE(! t1.overlaps(t3)) << "ivec overlaps3";
190 ASSERT_TRUE(t1.overlaps(t4)) << "ivec overlaps4";
191 ASSERT_TRUE(t1.overlaps(t5)) << "ivec overlaps5";
192 ASSERT_TRUE(t1.overlaps(t6)) << "ivec overlaps6";
193 ASSERT_TRUE(t1.overlaps(t7)) << "ivec overlaps7";
194 ASSERT_TRUE(t1.overlaps(t8)) << "ivec overlaps8";
195 ASSERT_TRUE(! t1.overlaps(t9)) << "ivec overlaps9";
196 ASSERT_TRUE(t1.overlaps(t10)) << "ivec overlaps10";
197 ASSERT_TRUE(! t1.overlaps(t11)) << "ivec overlaps11";
198 ASSERT_TRUE(! t1.overlaps(t12)) << "ivec overlaps12";
199 ASSERT_TRUE(! t1.overlaps(t13)) << "ivec overlaps13";
202
203 // distance_to
200
201 // distance_to
204 CPPUNIT_ASSERT_MESSAGE("ivec distance_to1", t1.distance_to(t13) == 0);
205 CPPUNIT_ASSERT_MESSAGE("ivec distance_to2", t1.distance_to(t8) == -1);
206 CPPUNIT_ASSERT_MESSAGE("ivec distance_to3", t1.distance_to(t9) == 0);
207 CPPUNIT_ASSERT_MESSAGE("ivec distance_to4", t1.distance_to(t11) == 2);
208 CPPUNIT_ASSERT_MESSAGE("ivec distance_to5", t1.distance_to(t3) == -88);
202 ASSERT_TRUE(t1.distance_to(t13) == 0) << "ivec distance_to1";
203 ASSERT_TRUE(t1.distance_to(t8) == -1) << "ivec distance_to2";
204 ASSERT_TRUE(t1.distance_to(t9) == 0) << "ivec distance_to3";
205 ASSERT_TRUE(t1.distance_to(t11) == 2) << "ivec distance_to4";
206 ASSERT_TRUE(t1.distance_to(t3) == -88) << "ivec distance_to5";
209
210 const std::vector< int* > h2(20, (int*)0);
211 std::deque< double > h3(30, 0.0);
212
207
208 const std::vector< int* > h2(20, (int*)0);
209 std::deque< double > h3(30, 0.0);
210
213 CPPUNIT_ASSERT_MESSAGE("range_of1", range_of(h2).begin() == h2.begin());
214 CPPUNIT_ASSERT_MESSAGE("range_of2", range_of(h3).end() == h3.end());
211 ASSERT_TRUE(range_of(h2).begin() == h2.begin()) << "range_of1";
212 ASSERT_TRUE(range_of(h3).end() == h3.end()) << "range_of2";
215 }
216
213 }
214
217 // insert your test code here.
218 void global()
219 {
220 int_test();
221 iterator_test();
222 }
223
224
225 // These macros are needed by auto register mechanism.
226 CPPUNIT_TEST_SUITE(range_test);
227 CPPUNIT_TEST(global);
228 CPPUNIT_TEST_SUITE_END();
229}; // class range_test
230
215}; // class range_test
216
231// -----------------------------------------------------------------------------
232CPPUNIT_TEST_SUITE_REGISTRATION(range_test);
217TEST_F(range_test, global)
218{
219 int_test();
220 iterator_test();
221}
222