Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <sal/types.h>
21 : #include "cppunit/TestAssert.h"
22 : #include "cppunit/TestFixture.h"
23 : #include "cppunit/extensions/HelperMacros.h"
24 :
25 : #include <o3tl/range.hxx>
26 : #include <vector>
27 : #include <deque>
28 :
29 : using o3tl::range;
30 : using o3tl::make_range;
31 : using o3tl::range_of;
32 : using std::size_t;
33 :
34 6 : class range_test : public CppUnit::TestFixture
35 : {
36 : public:
37 :
38 2 : void int_test()
39 : {
40 : range<int>
41 2 : t1(12,88);
42 : range<int>
43 4 : t2(33,33);
44 :
45 : // ctor
46 2 : CPPUNIT_ASSERT_MESSAGE("int ctor1", t1.begin() == 12);
47 2 : CPPUNIT_ASSERT_MESSAGE("int ctor2", t1.end() == 88);
48 2 : CPPUNIT_ASSERT_MESSAGE("int ctor3", t2.begin() == 33);
49 2 : CPPUNIT_ASSERT_MESSAGE("int ctor4", t2.end() == 33);
50 :
51 : // make_range
52 2 : CPPUNIT_ASSERT_MESSAGE("int make_range1", make_range(0,8).begin() == 0);
53 2 : CPPUNIT_ASSERT_MESSAGE("int make_range2", make_range(0,8).end() == 8);
54 :
55 : // size
56 2 : CPPUNIT_ASSERT_MESSAGE("int size1", t1.size() == size_t(t1.end() - t1.begin()) );
57 2 : CPPUNIT_ASSERT_MESSAGE("int size2", t2.size() == size_t(0) );
58 :
59 : // contains
60 4 : range<int> t3(0,10);
61 4 : range<int> t4(7, 15);
62 4 : range<int> t5(12, 12);
63 4 : range<int> t6(13, 77);
64 4 : range<int> t7(87, 87);
65 4 : range<int> t8(87, 88);
66 4 : range<int> t9(88, 88);
67 4 : range<int> t10(33, 120);
68 4 : range<int> t11(90, 100);
69 4 : range<int> t12(200,200);
70 :
71 2 : CPPUNIT_ASSERT_MESSAGE("int contains1", t1.contains(t1));
72 2 : CPPUNIT_ASSERT_MESSAGE("int contains2", t1.contains(t2));
73 2 : CPPUNIT_ASSERT_MESSAGE("int contains3", ! t1.contains(t3));
74 2 : CPPUNIT_ASSERT_MESSAGE("int contains4", ! t1.contains(t4));
75 2 : CPPUNIT_ASSERT_MESSAGE("int contains5", t1.contains(t5));
76 2 : CPPUNIT_ASSERT_MESSAGE("int contains6", t1.contains(t6));
77 2 : CPPUNIT_ASSERT_MESSAGE("int contains7", t1.contains(t7));
78 2 : CPPUNIT_ASSERT_MESSAGE("int contains8", t1.contains(t8));
79 2 : CPPUNIT_ASSERT_MESSAGE("int contains9", ! t1.contains(t9));
80 2 : CPPUNIT_ASSERT_MESSAGE("int contains10", ! t1.contains(t10));
81 2 : CPPUNIT_ASSERT_MESSAGE("int contains11", ! t1.contains(t11));
82 2 : CPPUNIT_ASSERT_MESSAGE("int contains12", ! t1.contains(t12));
83 :
84 2 : CPPUNIT_ASSERT_MESSAGE("int contains n1", t1.contains(50));
85 2 : CPPUNIT_ASSERT_MESSAGE("int contains n2", t1.contains(12));
86 2 : CPPUNIT_ASSERT_MESSAGE("int contains n3", t1.contains(87));
87 2 : CPPUNIT_ASSERT_MESSAGE("int contains n4", ! t1.contains(3));
88 2 : CPPUNIT_ASSERT_MESSAGE("int contains n5", ! t1.contains(11));
89 2 : CPPUNIT_ASSERT_MESSAGE("int contains n6", ! t1.contains(88));
90 2 : CPPUNIT_ASSERT_MESSAGE("int contains n7", ! t1.contains(100));
91 :
92 : // overlaps
93 4 : range<int> t13(88,99);
94 :
95 2 : CPPUNIT_ASSERT_MESSAGE("int overlaps1", t1.overlaps(t1));
96 2 : CPPUNIT_ASSERT_MESSAGE("int overlaps2", t1.overlaps(t2));
97 2 : CPPUNIT_ASSERT_MESSAGE("int overlaps3", ! t1.overlaps(t3));
98 2 : CPPUNIT_ASSERT_MESSAGE("int overlaps4", t1.overlaps(t4));
99 2 : CPPUNIT_ASSERT_MESSAGE("int overlaps5", t1.overlaps(t5));
100 2 : CPPUNIT_ASSERT_MESSAGE("int overlaps6", t1.overlaps(t6));
101 2 : CPPUNIT_ASSERT_MESSAGE("int overlaps7", t1.overlaps(t7));
102 2 : CPPUNIT_ASSERT_MESSAGE("int overlaps8", t1.overlaps(t8));
103 2 : CPPUNIT_ASSERT_MESSAGE("int overlaps9", ! t1.overlaps(t9));
104 2 : CPPUNIT_ASSERT_MESSAGE("int overlaps10", t1.overlaps(t10));
105 2 : CPPUNIT_ASSERT_MESSAGE("int overlaps11", ! t1.overlaps(t11));
106 2 : CPPUNIT_ASSERT_MESSAGE("int overlaps12", ! t1.overlaps(t12));
107 2 : CPPUNIT_ASSERT_MESSAGE("int overlaps13", ! t1.overlaps(t13));
108 :
109 : // distance_to
110 2 : CPPUNIT_ASSERT_MESSAGE("int distance_to1", t1.distance_to(t13) == 0);
111 2 : CPPUNIT_ASSERT_MESSAGE("int distance_to2", t1.distance_to(t9) == 0);
112 2 : CPPUNIT_ASSERT_MESSAGE("int distance_to3", t1.distance_to(t11) == 2);
113 2 : CPPUNIT_ASSERT_MESSAGE("int distance_to4", t1.distance_to(t8) == -1);
114 4 : CPPUNIT_ASSERT_MESSAGE("int distance_to5", t1.distance_to(t3) == -88);
115 2 : }
116 :
117 2 : void iterator_test()
118 : {
119 : typedef std::vector<char>::const_iterator test_it;
120 2 : const std::vector<char> hv(200,'x');
121 :
122 :
123 2 : test_it hit1 = hv.begin() + 12;
124 2 : test_it hit2 = hv.begin() + 88;
125 :
126 : range<test_it>
127 4 : t1(hit1, hit2);
128 : range<test_it>
129 4 : t2(hv.begin()+33, hv.begin()+33);
130 :
131 : // ctor
132 2 : CPPUNIT_ASSERT_MESSAGE("ivec ctor1", t1.begin() == hit1);
133 2 : CPPUNIT_ASSERT_MESSAGE("ivec ctor2", t1.end() == hit2);
134 2 : CPPUNIT_ASSERT_MESSAGE("ivec ctor3", t2.begin() == hv.begin()+33);
135 2 : CPPUNIT_ASSERT_MESSAGE("ivec ctor4", t2.end() == hv.begin()+33);
136 :
137 : // make_range
138 2 : CPPUNIT_ASSERT_MESSAGE("ivec make_range1", make_range(hv.begin(), hv.begin()+8).begin() == hv.begin());
139 2 : CPPUNIT_ASSERT_MESSAGE("ivec make_range2", make_range(hv.begin(), hv.begin()+8).end() == hv.begin()+8);
140 :
141 : // size
142 2 : CPPUNIT_ASSERT_MESSAGE("ivec size1", t1.size() == size_t(t1.end() - t1.begin()) );
143 2 : CPPUNIT_ASSERT_MESSAGE("ivec size2", t2.size() == size_t(0) );
144 :
145 : // contains
146 4 : range<test_it> t3(hv.begin(), hv.begin() + 10);
147 4 : range<test_it> t4(hv.begin() + 7, hv.begin() + 15);
148 4 : range<test_it> t5(hit1, hit1);
149 4 : range<test_it> t6(hv.begin() + 13, hv.begin() + 77);
150 4 : range<test_it> t7(hv.begin() + 87, hv.begin() + 87);
151 4 : range<test_it> t8(hv.begin() + 87, hit2);
152 4 : range<test_it> t9(hit2, hit2);
153 4 : range<test_it> t10(hv.begin() + 33, hv.begin() + 120);
154 4 : range<test_it> t11(hv.begin() + 90, hv.begin() + 100);
155 4 : range<test_it> t12(hv.begin() + 200,hv.begin() + 200);
156 :
157 2 : CPPUNIT_ASSERT_MESSAGE("ivec contains1", t1.contains(t1));
158 2 : CPPUNIT_ASSERT_MESSAGE("ivec contains2", t1.contains(t2));
159 2 : CPPUNIT_ASSERT_MESSAGE("ivec contains3", ! t1.contains(t3));
160 2 : CPPUNIT_ASSERT_MESSAGE("ivec contains4", ! t1.contains(t4));
161 2 : CPPUNIT_ASSERT_MESSAGE("ivec contains5", t1.contains(t5));
162 2 : CPPUNIT_ASSERT_MESSAGE("ivec contains6", t1.contains(t6));
163 2 : CPPUNIT_ASSERT_MESSAGE("ivec contains7", t1.contains(t7));
164 2 : CPPUNIT_ASSERT_MESSAGE("ivec contains8", t1.contains(t8));
165 2 : CPPUNIT_ASSERT_MESSAGE("ivec contains9", ! t1.contains(t9));
166 2 : CPPUNIT_ASSERT_MESSAGE("ivec contains10", ! t1.contains(t10));
167 2 : CPPUNIT_ASSERT_MESSAGE("ivec contains11", ! t1.contains(t11));
168 2 : CPPUNIT_ASSERT_MESSAGE("ivec contains12", ! t1.contains(t12));
169 :
170 2 : CPPUNIT_ASSERT_MESSAGE("ivec contains n1", t1.contains(hv.begin() + 50));
171 2 : CPPUNIT_ASSERT_MESSAGE("ivec contains n2", t1.contains(hit1));
172 2 : CPPUNIT_ASSERT_MESSAGE("ivec contains n3", t1.contains(hv.begin() + 87));
173 2 : CPPUNIT_ASSERT_MESSAGE("ivec contains n4", ! t1.contains(hv.begin() + 3));
174 2 : CPPUNIT_ASSERT_MESSAGE("ivec contains n5", ! t1.contains(hv.begin() + 11));
175 2 : CPPUNIT_ASSERT_MESSAGE("ivec contains n6", ! t1.contains(hit2));
176 2 : CPPUNIT_ASSERT_MESSAGE("ivec contains n7", ! t1.contains(hv.begin() + 100));
177 :
178 : // overlaps
179 4 : range<test_it> t13(hit2, hv.begin() + 99);
180 :
181 2 : CPPUNIT_ASSERT_MESSAGE("ivec overlaps1", t1.overlaps(t1));
182 2 : CPPUNIT_ASSERT_MESSAGE("ivec overlaps2", t1.overlaps(t2));
183 2 : CPPUNIT_ASSERT_MESSAGE("ivec overlaps3", ! t1.overlaps(t3));
184 2 : CPPUNIT_ASSERT_MESSAGE("ivec overlaps4", t1.overlaps(t4));
185 2 : CPPUNIT_ASSERT_MESSAGE("ivec overlaps5", t1.overlaps(t5));
186 2 : CPPUNIT_ASSERT_MESSAGE("ivec overlaps6", t1.overlaps(t6));
187 2 : CPPUNIT_ASSERT_MESSAGE("ivec overlaps7", t1.overlaps(t7));
188 2 : CPPUNIT_ASSERT_MESSAGE("ivec overlaps8", t1.overlaps(t8));
189 2 : CPPUNIT_ASSERT_MESSAGE("ivec overlaps9", ! t1.overlaps(t9));
190 2 : CPPUNIT_ASSERT_MESSAGE("ivec overlaps10", t1.overlaps(t10));
191 2 : CPPUNIT_ASSERT_MESSAGE("ivec overlaps11", ! t1.overlaps(t11));
192 2 : CPPUNIT_ASSERT_MESSAGE("ivec overlaps12", ! t1.overlaps(t12));
193 2 : CPPUNIT_ASSERT_MESSAGE("ivec overlaps13", ! t1.overlaps(t13));
194 :
195 : // distance_to
196 2 : CPPUNIT_ASSERT_MESSAGE("ivec distance_to1", t1.distance_to(t13) == 0);
197 2 : CPPUNIT_ASSERT_MESSAGE("ivec distance_to2", t1.distance_to(t8) == -1);
198 2 : CPPUNIT_ASSERT_MESSAGE("ivec distance_to3", t1.distance_to(t9) == 0);
199 2 : CPPUNIT_ASSERT_MESSAGE("ivec distance_to4", t1.distance_to(t11) == 2);
200 2 : CPPUNIT_ASSERT_MESSAGE("ivec distance_to5", t1.distance_to(t3) == -88);
201 :
202 4 : const std::vector< int* > h2(20, (int*)0);
203 4 : std::deque< double > h3(30, 0.0);
204 :
205 2 : CPPUNIT_ASSERT_MESSAGE("range_of1", range_of(h2).begin() == h2.begin());
206 4 : CPPUNIT_ASSERT_MESSAGE("range_of2", range_of(h3).end() == h3.end());
207 2 : }
208 :
209 : // insert your test code here.
210 2 : void global()
211 : {
212 2 : int_test();
213 2 : iterator_test();
214 2 : }
215 :
216 :
217 : // These macros are needed by auto register mechanism.
218 4 : CPPUNIT_TEST_SUITE(range_test);
219 2 : CPPUNIT_TEST(global);
220 4 : CPPUNIT_TEST_SUITE_END();
221 : }; // class range_test
222 :
223 :
224 6 : CPPUNIT_TEST_SUITE_REGISTRATION(range_test);
225 :
226 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|