xref: /aoo42x/main/o3tl/qa/test-range.cxx (revision 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
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 "gtest/gtest.h"
26 #include "postextstl.h"
27 
28 #include <o3tl/range.hxx>
29 #include <vector>
30 #include <deque>
31 
32 
33 
34 using o3tl::range;
35 using o3tl::make_range;
36 using o3tl::range_of;
37 using std::size_t;
38 
39 
40 class range_test : public ::testing::Test
41 {
42 public:
43 
int_test()44     void int_test()
45     {
46         range<int>
47             t1(12,88);
48         range<int>
49             t2(33,33);
50 
51         // ctor
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";
56 
57         // make_range
58         ASSERT_TRUE(make_range(0,8).begin() == 0) << "int make_range1";
59         ASSERT_TRUE(make_range(0,8).end() == 8) << "int make_range2";
60 
61         // size
62         ASSERT_TRUE(t1.size() == size_t(t1.end() - t1.begin())) << "int size1";
63         ASSERT_TRUE(t2.size() == size_t(0)) << "int size2";
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 
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";
89 
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";
97 
98         // overlaps
99         range<int>      t13(88,99);
100 
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";
114 
115         // distance_to
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";
121     }
122 
iterator_test()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
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";
142 
143         // make_range
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";
146 
147         // size
148         ASSERT_TRUE(t1.size() == size_t(t1.end() - t1.begin())) << "ivec size1";
149         ASSERT_TRUE(t2.size() == size_t(0)) << "ivec size2";
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 
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";
175 
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";
183 
184         // overlaps
185         range<test_it>      t13(hit2, hv.begin() + 99);
186 
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";
200 
201         // distance_to
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";
207 
208         const std::vector< int* >   h2(20, (int*)0);
209         std::deque< double >        h3(30, 0.0);
210 
211         ASSERT_TRUE(range_of(h2).begin() == h2.begin()) << "range_of1";
212         ASSERT_TRUE(range_of(h3).end() == h3.end()) << "range_of2";
213     }
214 
215 }; // class range_test
216 
TEST_F(range_test,global)217 TEST_F(range_test, global)
218 {
219     int_test();
220     iterator_test();
221 }
222 
223