[MERGE] libi2ncommon: (reinhard) added contains_exact method to interval classes.
[libi2ncommon] / test / test_timefunc.cpp
CommitLineData
8d2b7645
TJ
1/** @file
2 * @brief unit test for time related functions.
3 *
4 * @copyright Copyright © 2001-2008 by Intra2net AG
5 * @license commercial
6 * @contact info@intra2net.com
7 *
8 */
9
10#include <cppunit/extensions/TestFactoryRegistry.h>
11#include <cppunit/ui/text/TestRunner.h>
12#include <cppunit/extensions/HelperMacros.h>
13
14#include <timefunc.hxx>
15#include <filefunc.hxx>
16
17using namespace std;
18using namespace I2n;
19using namespace CppUnit;
20
21namespace
22{
23
24
25} // eo namespace <anonymous>
26
27
28class TestTimeFunc : public TestFixture
29{
30 CPPUNIT_TEST_SUITE(TestTimeFunc);
31
32 CPPUNIT_TEST(AddIntervalsDisjoint);
33 CPPUNIT_TEST(AddIntervalsInclude);
34 CPPUNIT_TEST(AddIntervalsEmbrace);
35 CPPUNIT_TEST(AddIntervalsJoin1);
36 CPPUNIT_TEST(AddIntervalsJoin1b);
37 CPPUNIT_TEST(AddIntervalsJoin2);
38
39 CPPUNIT_TEST(SubIntervalsDisjoint);
40 CPPUNIT_TEST(SubIntervalsExact);
41 CPPUNIT_TEST(SubIntervalsSplit1);
42 CPPUNIT_TEST(SubIntervalsCutFront);
43 CPPUNIT_TEST(SubIntervalsCutBack);
44 CPPUNIT_TEST(SubIntervalsCutMore);
45
46 CPPUNIT_TEST(IntervalComparisons);
47
48 CPPUNIT_TEST_SUITE_END();
49
50protected:
51
52 typedef std::list< std::string > StringList;
53
54 std::set<std::string> used_check_files;
55
56 std::string get_check_file_path(std::string tag)
57 {
58 std::string result;
59 result= "__unittest__" + tag + ".dat";
60 used_check_files.insert(result);
61 return result;
62 } // eo get_check_file_path
63
64
65 void remove_check_files()
66 {
67 for (std::set<std::string>::iterator it= used_check_files.begin();
68 it != used_check_files.end();
69 ++it)
70 {
71 std::string filepath(*it);
72 if (path_exists(filepath))
73 {
74 unlink(filepath);
75 }
76 //TODO
77 }
78 used_check_files.clear();
79 } // eo remove_check_files
80
81
82
83public:
84
85 void setUp()
86 {
87 } // eo setUp
88
89
90 void tearDown()
91 {
92 remove_check_files();
93 } // eo tearDown
94
95
96 /*
97 * the tests:
98 */
99
100
101 void AddIntervalsDisjoint()
102 {
103 Intervals intervals;
104
105 intervals.add( Interval( 10, 100 ) );
106 intervals.add( Interval( 600, 620 ) );
107
108 CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
109 CPPUNIT_ASSERT_EQUAL( 2u, intervals.size() );
110
111 CPPUNIT_ASSERT_EQUAL( 10u, intervals.front().lower_bound() );
112 CPPUNIT_ASSERT_EQUAL( 100u, intervals.front().upper_bound() );
113
114 CPPUNIT_ASSERT_EQUAL( 600u, intervals.back().lower_bound() );
115 CPPUNIT_ASSERT_EQUAL( 620u, intervals.back().upper_bound() );
116 } // eo AddIntervalsDisjoint()
117
118
119
120 void AddIntervalsInclude()
121 {
122 Intervals intervals;
123
124 intervals.add( Interval( 10, 100 ) );
125 intervals.add( Interval( 10, 80 ) );
126
127 CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
128 CPPUNIT_ASSERT_EQUAL( 1u, intervals.size() );
129
130 CPPUNIT_ASSERT_EQUAL( 10u, intervals.front().lower_bound() );
131 CPPUNIT_ASSERT_EQUAL( 100u, intervals.front().upper_bound() );
80f30818 132 CPPUNIT_ASSERT_EQUAL( false, intervals.front().changed() );
8d2b7645
TJ
133 } // eo AddIntervalsInclude()
134
135
136
137 void AddIntervalsEmbrace()
138 {
139 Intervals intervals;
140
141 intervals.add( Interval( 10, 100 ) );
142 intervals.add( Interval( 5, 120 ) );
143
144 CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
145 CPPUNIT_ASSERT_EQUAL( 1u, intervals.size() );
146
147 CPPUNIT_ASSERT_EQUAL( 5u, intervals.front().lower_bound() );
148 CPPUNIT_ASSERT_EQUAL( 120u, intervals.front().upper_bound() );
80f30818 149 CPPUNIT_ASSERT_EQUAL( true, intervals.front().changed() );
8d2b7645
TJ
150 } // eo AddIntervalsEmbrace()
151
152
153
154 void AddIntervalsJoin1()
155 {
156 Intervals intervals;
157
158 intervals.add( Interval( 10, 100 ) );
159 intervals.add( Interval( 60, 120 ) );
160
161 CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
162 CPPUNIT_ASSERT_EQUAL( 1u, intervals.size() );
163
164 CPPUNIT_ASSERT_EQUAL( 10u, intervals.front().lower_bound() );
165 CPPUNIT_ASSERT_EQUAL( 120u, intervals.front().upper_bound() );
80f30818 166 CPPUNIT_ASSERT_EQUAL( true, intervals.front().changed() );
8d2b7645
TJ
167 } // eo AddIntervalsJoin1()
168
169
170
171 void AddIntervalsJoin1b()
172 {
173 Intervals intervals;
174
175 intervals.add( Interval( 10, 100 ) );
176 intervals.add( Interval( 100, 120 ) );
177
178 CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
179 CPPUNIT_ASSERT_EQUAL( 1u, intervals.size() );
180
181 CPPUNIT_ASSERT_EQUAL( 10u, intervals.front().lower_bound() );
182 CPPUNIT_ASSERT_EQUAL( 120u, intervals.front().upper_bound() );
80f30818 183 CPPUNIT_ASSERT_EQUAL( true, intervals.front().changed() );
8d2b7645
TJ
184 } // eo AddIntervalsJoin1b()
185
186
187
188 void AddIntervalsJoin2()
189 {
190 Intervals intervals;
191
192 intervals.add( Interval( 10, 100 ) );
193 intervals.add( Interval( 200, 250 ) );
194
195 CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
196 CPPUNIT_ASSERT_EQUAL( 2u, intervals.size() );
197
198 CPPUNIT_ASSERT_EQUAL( 10u, intervals.front().lower_bound() );
199 CPPUNIT_ASSERT_EQUAL( 100u, intervals.front().upper_bound() );
200 CPPUNIT_ASSERT_EQUAL( 200u, intervals.back().lower_bound() );
201 CPPUNIT_ASSERT_EQUAL( 250u, intervals.back().upper_bound() );
202
203 // now add the gap; the intervals should collapse to one covering all:
204 intervals.add( Interval(100, 200) );
205
206 CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
207 CPPUNIT_ASSERT_EQUAL( 1u, intervals.size() );
208
209 CPPUNIT_ASSERT_EQUAL( 10u, intervals.front().lower_bound() );
210 CPPUNIT_ASSERT_EQUAL( 250u, intervals.front().upper_bound() );
80f30818 211 CPPUNIT_ASSERT_EQUAL( true, intervals.front().changed() );
8d2b7645
TJ
212 } // eo AddIntervalsJoin2()
213
214
215
216 void SubIntervalsDisjoint()
217 {
218 Intervals intervals;
219
220 intervals.add( Interval(10, 100) );
221 intervals.sub( Interval(0, 10) );
222
223 CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
224 CPPUNIT_ASSERT_EQUAL( 1u, intervals.size() );
225
226 CPPUNIT_ASSERT_EQUAL( 10u, intervals.front().lower_bound() );
227 CPPUNIT_ASSERT_EQUAL( 100u, intervals.front().upper_bound() );
80f30818 228 CPPUNIT_ASSERT_EQUAL( false, intervals.front().changed() );
8d2b7645
TJ
229 } // eo SubIntervalsDisjoint()
230
231
232
233 void SubIntervalsExact()
234 {
235 Intervals intervals;
236
237 intervals.add( Interval(10, 100) );
238 intervals.sub( Interval(10, 100) );
239
240 CPPUNIT_ASSERT_EQUAL( true, intervals.empty() );
241 CPPUNIT_ASSERT_EQUAL( 0u, intervals.size() );
242 } // eo SubIntervalsExact()
243
244
245
246 void SubIntervalsSplit1()
247 {
248 Intervals intervals;
249
250 intervals.add( Interval(10, 100) );
251 intervals.sub( Interval(20, 40) );
252
253 CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
254 CPPUNIT_ASSERT_EQUAL( 2u, intervals.size() );
255
256 CPPUNIT_ASSERT_EQUAL( 10u, intervals.front().lower_bound() );
257 CPPUNIT_ASSERT_EQUAL( 20u, intervals.front().upper_bound() );
258
259 CPPUNIT_ASSERT_EQUAL( 40u, intervals.back().lower_bound() );
260 CPPUNIT_ASSERT_EQUAL( 100u, intervals.back().upper_bound() );
80f30818
TJ
261 CPPUNIT_ASSERT_EQUAL( false, intervals.front().changed() );
262 CPPUNIT_ASSERT_EQUAL( true, intervals.back().changed() );
8d2b7645
TJ
263 } // eo SubIntervalsSplit1()
264
265
266 void SubIntervalsCutFront()
267 {
268 Intervals intervals;
269
270 intervals.add( Interval(10, 100) );
271 intervals.sub( Interval(10, 20) );
272
273 CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
274 CPPUNIT_ASSERT_EQUAL( 1u, intervals.size() );
275
276 CPPUNIT_ASSERT_EQUAL( 20u, intervals.front().lower_bound() );
277 CPPUNIT_ASSERT_EQUAL( 100u, intervals.front().upper_bound() );
80f30818 278 CPPUNIT_ASSERT_EQUAL( true, intervals.front().changed() );
8d2b7645
TJ
279 } // eo SubIntervalsCutFront()
280
281
282 void SubIntervalsCutBack()
283 {
284 Intervals intervals;
285
286 intervals.add( Interval(10, 100) );
287 intervals.sub( Interval(87, 100) );
288
289 CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
290 CPPUNIT_ASSERT_EQUAL( 1u, intervals.size() );
291
292 CPPUNIT_ASSERT_EQUAL( 10u, intervals.front().lower_bound() );
293 CPPUNIT_ASSERT_EQUAL( 87u, intervals.front().upper_bound() );
80f30818 294 CPPUNIT_ASSERT_EQUAL( true, intervals.front().changed() );
8d2b7645
TJ
295 } // eo SubIntervalsCutBack()
296
297
298
299 void SubIntervalsCutMore()
300 {
301 Intervals intervals;
302
303 intervals.add( Interval( 10, 100) );
304 intervals.add( Interval(110, 200) );
305 intervals.add( Interval(210, 300) );
306
307 // this should remove the first 2 intervals and cut the third:
308 intervals.sub( Interval(8, 220) );
309
310 CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
311 CPPUNIT_ASSERT_EQUAL( 1u, intervals.size() );
312
313 CPPUNIT_ASSERT_EQUAL( 220u, intervals.front().lower_bound() );
314 CPPUNIT_ASSERT_EQUAL( 300u, intervals.front().upper_bound() );
80f30818 315 CPPUNIT_ASSERT_EQUAL( true, intervals.front().changed() );
8d2b7645
TJ
316 } // eo SubIntervalsCutMore()
317
318
319 void IntervalComparisons()
320 {
321 Intervals intervals1;
322 Intervals intervals2;
323
324 intervals1.add( Interval( 10, 120) );
325
326 intervals2.add( Interval( 10, 110 ) );
327 intervals2.add( Interval( 100, 120 ) );
328
329 CPPUNIT_ASSERT_EQUAL( 1u, intervals2.size() );
330
331 CPPUNIT_ASSERT( intervals1 == intervals2 );
332 CPPUNIT_ASSERT_EQUAL( true, intervals1.contains( intervals2 ));
333 CPPUNIT_ASSERT_EQUAL( true, intervals2.contains( intervals1 ));
334
335 intervals2.sub( Interval( 40, 50) );
336
337 CPPUNIT_ASSERT( intervals1 != intervals2 );
338 CPPUNIT_ASSERT_EQUAL( true, intervals1.contains( intervals2 ));
339 CPPUNIT_ASSERT_EQUAL( false, intervals2.contains( intervals1 ));
340 } // eo IntervalComparisons()
341
342
343}; // eo class TestTimeFunc
344
345CPPUNIT_TEST_SUITE_REGISTRATION(TestTimeFunc);