xref: /trunk/main/o3tl/qa/test-range.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "preextstl.h"
29 #include "cppunit/TestAssert.h"
30 #include "cppunit/TestFixture.h"
31 #include "cppunit/extensions/HelperMacros.h"
32 #include "postextstl.h"
33 
34 #include <o3tl/range.hxx>
35 #include <vector>
36 #include <deque>
37 
38 
39 
40 using o3tl::range;
41 using o3tl::make_range;
42 using o3tl::range_of;
43 using std::size_t;
44 
45 
46 class range_test : public CppUnit::TestFixture
47 {
48 public:
49 
50     void int_test()
51     {
52         range<int>
53             t1(12,88);
54         range<int>
55             t2(33,33);
56 
57         // ctor
58         CPPUNIT_ASSERT_MESSAGE("int ctor1", t1.begin() == 12);
59         CPPUNIT_ASSERT_MESSAGE("int ctor2", t1.end() == 88);
60         CPPUNIT_ASSERT_MESSAGE("int ctor3", t2.begin() == 33);
61         CPPUNIT_ASSERT_MESSAGE("int ctor4", t2.end() == 33);
62 
63         // make_range
64         CPPUNIT_ASSERT_MESSAGE("int make_range1", make_range(0,8).begin() == 0);
65         CPPUNIT_ASSERT_MESSAGE("int make_range2", make_range(0,8).end() == 8);
66 
67         // size
68         CPPUNIT_ASSERT_MESSAGE("int size1", t1.size() == size_t(t1.end() - t1.begin()) );
69         CPPUNIT_ASSERT_MESSAGE("int size2", t2.size() == size_t(0) );
70 
71         // contains
72         range<int>      t3(0,10);
73         range<int>      t4(7, 15);
74         range<int>      t5(12, 12);
75         range<int>      t6(13, 77);
76         range<int>      t7(87, 87);
77         range<int>      t8(87, 88);
78         range<int>      t9(88, 88);
79         range<int>      t10(33, 120);
80         range<int>      t11(90, 100);
81         range<int>      t12(200,200);
82 
83         CPPUNIT_ASSERT_MESSAGE("int contains1", t1.contains(t1));
84         CPPUNIT_ASSERT_MESSAGE("int contains2", t1.contains(t2));
85         CPPUNIT_ASSERT_MESSAGE("int contains3", ! t1.contains(t3));
86         CPPUNIT_ASSERT_MESSAGE("int contains4", ! t1.contains(t4));
87         CPPUNIT_ASSERT_MESSAGE("int contains5", t1.contains(t5));
88         CPPUNIT_ASSERT_MESSAGE("int contains6", t1.contains(t6));
89         CPPUNIT_ASSERT_MESSAGE("int contains7", t1.contains(t7));
90         CPPUNIT_ASSERT_MESSAGE("int contains8", t1.contains(t8));
91         CPPUNIT_ASSERT_MESSAGE("int contains9", ! t1.contains(t9));
92         CPPUNIT_ASSERT_MESSAGE("int contains10", ! t1.contains(t10));
93         CPPUNIT_ASSERT_MESSAGE("int contains11", ! t1.contains(t11));
94         CPPUNIT_ASSERT_MESSAGE("int contains12", ! t1.contains(t12));
95 
96         CPPUNIT_ASSERT_MESSAGE("int contains n1", t1.contains(50));
97         CPPUNIT_ASSERT_MESSAGE("int contains n2", t1.contains(12));
98         CPPUNIT_ASSERT_MESSAGE("int contains n3", t1.contains(87));
99         CPPUNIT_ASSERT_MESSAGE("int contains n4", ! t1.contains(3));
100         CPPUNIT_ASSERT_MESSAGE("int contains n5", ! t1.contains(11));
101         CPPUNIT_ASSERT_MESSAGE("int contains n6", ! t1.contains(88));
102         CPPUNIT_ASSERT_MESSAGE("int contains n7", ! t1.contains(100));
103 
104         // overlaps
105         range<int>      t13(88,99);
106 
107         CPPUNIT_ASSERT_MESSAGE("int overlaps1", t1.overlaps(t1));
108         CPPUNIT_ASSERT_MESSAGE("int overlaps2", t1.overlaps(t2));
109         CPPUNIT_ASSERT_MESSAGE("int overlaps3", ! t1.overlaps(t3));
110         CPPUNIT_ASSERT_MESSAGE("int overlaps4", t1.overlaps(t4));
111         CPPUNIT_ASSERT_MESSAGE("int overlaps5", t1.overlaps(t5));
112         CPPUNIT_ASSERT_MESSAGE("int overlaps6", t1.overlaps(t6));
113         CPPUNIT_ASSERT_MESSAGE("int overlaps7", t1.overlaps(t7));
114         CPPUNIT_ASSERT_MESSAGE("int overlaps8", t1.overlaps(t8));
115         CPPUNIT_ASSERT_MESSAGE("int overlaps9", ! t1.overlaps(t9));
116         CPPUNIT_ASSERT_MESSAGE("int overlaps10", t1.overlaps(t10));
117         CPPUNIT_ASSERT_MESSAGE("int overlaps11", ! t1.overlaps(t11));
118         CPPUNIT_ASSERT_MESSAGE("int overlaps12", ! t1.overlaps(t12));
119         CPPUNIT_ASSERT_MESSAGE("int overlaps13", ! t1.overlaps(t13));
120 
121         // distance_to
122         CPPUNIT_ASSERT_MESSAGE("int distance_to1", t1.distance_to(t13) == 0);
123         CPPUNIT_ASSERT_MESSAGE("int distance_to2", t1.distance_to(t9) == 0);
124         CPPUNIT_ASSERT_MESSAGE("int distance_to3", t1.distance_to(t11) == 2);
125         CPPUNIT_ASSERT_MESSAGE("int distance_to4", t1.distance_to(t8) == -1);
126         CPPUNIT_ASSERT_MESSAGE("int distance_to5", t1.distance_to(t3) == -88);
127     }
128 
129     void iterator_test()
130     {
131         typedef std::vector<char>::const_iterator   test_it;
132         const std::vector<char>            hv(200,'x');
133 
134 
135         test_it hit1 = hv.begin() + 12;
136         test_it hit2 = hv.begin() + 88;
137 
138         range<test_it>
139             t1(hit1, hit2);
140         range<test_it>
141             t2(hv.begin()+33, hv.begin()+33);
142 
143         // ctor
144         CPPUNIT_ASSERT_MESSAGE("ivec ctor1", t1.begin() == hit1);
145         CPPUNIT_ASSERT_MESSAGE("ivec ctor2", t1.end() == hit2);
146         CPPUNIT_ASSERT_MESSAGE("ivec ctor3", t2.begin() == hv.begin()+33);
147         CPPUNIT_ASSERT_MESSAGE("ivec ctor4", t2.end() == hv.begin()+33);
148 
149         // make_range
150         CPPUNIT_ASSERT_MESSAGE("ivec make_range1", make_range(hv.begin(), hv.begin()+8).begin() == hv.begin());
151         CPPUNIT_ASSERT_MESSAGE("ivec make_range2", make_range(hv.begin(), hv.begin()+8).end() == hv.begin()+8);
152 
153         // size
154         CPPUNIT_ASSERT_MESSAGE("ivec size1", t1.size() == size_t(t1.end() - t1.begin()) );
155         CPPUNIT_ASSERT_MESSAGE("ivec size2", t2.size() == size_t(0) );
156 
157         // contains
158         range<test_it>      t3(hv.begin(), hv.begin() + 10);
159         range<test_it>      t4(hv.begin() + 7, hv.begin() + 15);
160         range<test_it>      t5(hit1, hit1);
161         range<test_it>      t6(hv.begin() + 13, hv.begin() + 77);
162         range<test_it>      t7(hv.begin() + 87, hv.begin() + 87);
163         range<test_it>      t8(hv.begin() + 87, hit2);
164         range<test_it>      t9(hit2, hit2);
165         range<test_it>      t10(hv.begin() + 33, hv.begin() + 120);
166         range<test_it>      t11(hv.begin() + 90, hv.begin() + 100);
167         range<test_it>      t12(hv.begin() + 200,hv.begin() + 200);
168 
169         CPPUNIT_ASSERT_MESSAGE("ivec contains1", t1.contains(t1));
170         CPPUNIT_ASSERT_MESSAGE("ivec contains2", t1.contains(t2));
171         CPPUNIT_ASSERT_MESSAGE("ivec contains3", ! t1.contains(t3));
172         CPPUNIT_ASSERT_MESSAGE("ivec contains4", ! t1.contains(t4));
173         CPPUNIT_ASSERT_MESSAGE("ivec contains5", t1.contains(t5));
174         CPPUNIT_ASSERT_MESSAGE("ivec contains6", t1.contains(t6));
175         CPPUNIT_ASSERT_MESSAGE("ivec contains7", t1.contains(t7));
176         CPPUNIT_ASSERT_MESSAGE("ivec contains8", t1.contains(t8));
177         CPPUNIT_ASSERT_MESSAGE("ivec contains9", ! t1.contains(t9));
178         CPPUNIT_ASSERT_MESSAGE("ivec contains10", ! t1.contains(t10));
179         CPPUNIT_ASSERT_MESSAGE("ivec contains11", ! t1.contains(t11));
180         CPPUNIT_ASSERT_MESSAGE("ivec contains12", ! t1.contains(t12));
181 
182         CPPUNIT_ASSERT_MESSAGE("ivec contains n1", t1.contains(hv.begin() + 50));
183         CPPUNIT_ASSERT_MESSAGE("ivec contains n2", t1.contains(hit1));
184         CPPUNIT_ASSERT_MESSAGE("ivec contains n3", t1.contains(hv.begin() + 87));
185         CPPUNIT_ASSERT_MESSAGE("ivec contains n4", ! t1.contains(hv.begin() + 3));
186         CPPUNIT_ASSERT_MESSAGE("ivec contains n5", ! t1.contains(hv.begin() + 11));
187         CPPUNIT_ASSERT_MESSAGE("ivec contains n6", ! t1.contains(hit2));
188         CPPUNIT_ASSERT_MESSAGE("ivec contains n7", ! t1.contains(hv.begin() + 100));
189 
190         // overlaps
191         range<test_it>      t13(hit2, hv.begin() + 99);
192 
193         CPPUNIT_ASSERT_MESSAGE("ivec overlaps1", t1.overlaps(t1));
194         CPPUNIT_ASSERT_MESSAGE("ivec overlaps2", t1.overlaps(t2));
195         CPPUNIT_ASSERT_MESSAGE("ivec overlaps3", ! t1.overlaps(t3));
196         CPPUNIT_ASSERT_MESSAGE("ivec overlaps4", t1.overlaps(t4));
197         CPPUNIT_ASSERT_MESSAGE("ivec overlaps5", t1.overlaps(t5));
198         CPPUNIT_ASSERT_MESSAGE("ivec overlaps6", t1.overlaps(t6));
199         CPPUNIT_ASSERT_MESSAGE("ivec overlaps7", t1.overlaps(t7));
200         CPPUNIT_ASSERT_MESSAGE("ivec overlaps8", t1.overlaps(t8));
201         CPPUNIT_ASSERT_MESSAGE("ivec overlaps9", ! t1.overlaps(t9));
202         CPPUNIT_ASSERT_MESSAGE("ivec overlaps10", t1.overlaps(t10));
203         CPPUNIT_ASSERT_MESSAGE("ivec overlaps11", ! t1.overlaps(t11));
204         CPPUNIT_ASSERT_MESSAGE("ivec overlaps12", ! t1.overlaps(t12));
205         CPPUNIT_ASSERT_MESSAGE("ivec overlaps13", ! t1.overlaps(t13));
206 
207         // distance_to
208         CPPUNIT_ASSERT_MESSAGE("ivec distance_to1", t1.distance_to(t13) == 0);
209         CPPUNIT_ASSERT_MESSAGE("ivec distance_to2", t1.distance_to(t8) == -1);
210         CPPUNIT_ASSERT_MESSAGE("ivec distance_to3", t1.distance_to(t9) == 0);
211         CPPUNIT_ASSERT_MESSAGE("ivec distance_to4", t1.distance_to(t11) == 2);
212         CPPUNIT_ASSERT_MESSAGE("ivec distance_to5", t1.distance_to(t3) == -88);
213 
214         const std::vector< int* >   h2(20, (int*)0);
215         std::deque< double >        h3(30, 0.0);
216 
217         CPPUNIT_ASSERT_MESSAGE("range_of1", range_of(h2).begin() == h2.begin());
218         CPPUNIT_ASSERT_MESSAGE("range_of2", range_of(h3).end() == h3.end());
219     }
220 
221     // insert your test code here.
222     void global()
223     {
224         int_test();
225         iterator_test();
226     }
227 
228 
229     // These macros are needed by auto register mechanism.
230     CPPUNIT_TEST_SUITE(range_test);
231     CPPUNIT_TEST(global);
232     CPPUNIT_TEST_SUITE_END();
233 }; // class range_test
234 
235 // -----------------------------------------------------------------------------
236 CPPUNIT_TEST_SUITE_REGISTRATION(range_test);
237