xref: /trunk/main/o3tl/qa/test-range.cxx (revision 31682d32)
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
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
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"
28 #include "postextstl.h"
29 
30 #include <o3tl/range.hxx>
31 #include <vector>
32 #include <deque>
33 
34 
35 
36 using o3tl::range;
37 using o3tl::make_range;
38 using o3tl::range_of;
39 using std::size_t;
40 
41 
42 class range_test : public CppUnit::TestFixture
43 {
44 public:
45 
46     void int_test()
47     {
48         range<int>
49             t1(12,88);
50         range<int>
51             t2(33,33);
52 
53         // 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);
58 
59         // 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);
62 
63         // 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) );
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 
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));
91 
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));
99 
100         // overlaps
101         range<int>      t13(88,99);
102 
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));
116 
117         // 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);
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
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);
144 
145         // 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);
148 
149         // 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) );
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 
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));
177 
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));
185 
186         // overlaps
187         range<test_it>      t13(hit2, hv.begin() + 99);
188 
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));
202 
203         // 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);
209 
210         const std::vector< int* >   h2(20, (int*)0);
211         std::deque< double >        h3(30, 0.0);
212 
213         CPPUNIT_ASSERT_MESSAGE("range_of1", range_of(h2).begin() == h2.begin());
214         CPPUNIT_ASSERT_MESSAGE("range_of2", range_of(h3).end() == h3.end());
215     }
216 
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 
231 // -----------------------------------------------------------------------------
232 CPPUNIT_TEST_SUITE_REGISTRATION(range_test);
233