source: branches/0.18-stable/test/segment.cc @ 4028

Last change on this file since 4028 was 4028, checked in by Peter, 9 months ago

fixes #970

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 12.8 KB
Line 
1// $Id: segment.cc 4028 2021-01-18 12:46:27Z peter $
2
3/*
4  Copyright (C) 2010, 2011, 2012, 2014, 2015, 2016, 2017 Peter Johansson
5
6  This file is part of the yat library, http://dev.thep.lu.se/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 3 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with yat. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include <config.h>
23
24#include "Suite.h"
25
26#include "yat/utility/Segment.h"
27#include "yat/utility/SegmentMap.h"
28#include "yat/utility/SegmentSet.h"
29
30using namespace theplu::yat;
31using namespace utility;
32void test_comp(test::Suite& suite);
33void test_compare(test::Suite& suite);
34void test_count(test::Suite&);
35void test_erase(test::Suite&);
36void test_includes(test::Suite&);
37void test_insert(test::Suite&);
38void test_insert_merge(test::Suite&);
39void test_make_segment(test::Suite&);
40void test_overlap(test::Suite&);
41void test_segment(test::Suite& suite);
42void test_operator(test::Suite& suite);
43void test_segment_map(test::Suite& suite);
44void test_set_bound(test::Suite&);
45void test_union(test::Suite&);
46
47template<typename T>
48void avoid_pedantic_warning(T) {}
49
50int main(int argc, char* argv[])
51{
52  test::Suite suite(argc, argv);
53
54  test_comp(suite);
55  test_compare(suite);
56  test_count(suite);
57  test_erase(suite);
58  test_includes(suite);
59  test_insert(suite);
60  test_insert_merge(suite);
61  test_make_segment(suite);
62  test_overlap(suite);
63  test_segment(suite);
64  test_set_bound(suite);
65  test_union(suite);
66
67  test_segment_map(suite);
68
69  return suite.return_value();
70}
71
72
73void test_comp(test::Suite& suite)
74{
75  {
76    SegmentSet<double> set;
77    SegmentSet<double>::key_compare kcompare = set.key_comp();
78    test::avoid_compiler_warning(kcompare);
79    SegmentSet<double>::value_compare vcompare = set.value_comp();
80    test::avoid_compiler_warning(vcompare);
81  }
82
83  {
84    SegmentMap<double, std::string> map;
85    SegmentMap<double, std::string>::key_compare kcompare = map.key_comp();
86    test::avoid_compiler_warning(kcompare);
87    SegmentMap<double, std::string>::value_compare vcompare = map.value_comp();
88    test::avoid_compiler_warning(vcompare);
89  }
90}
91
92
93void test_compare_(test::Suite& suite, double lhs_begin, double lhs_end, 
94                   double rhs_begin, double rhs_end, int expected)
95{
96  int result = compare_3way(Segment<double>(lhs_begin,lhs_end), 
97                            Segment<double>(rhs_begin,rhs_end));
98  if (result!=expected) {
99    suite.add(false);
100    suite.err() << "error:"
101                << "[" << lhs_begin << ", " << lhs_end << ") vs "
102                << "[" << rhs_begin << ", " << rhs_end << ")\n"
103                << "result: " << result << "\n"
104                << "expected: " << expected << "\n"
105                << "\n";
106  }
107
108  compare_3way(lhs_begin, Segment<double>(rhs_begin,rhs_end));
109  compare_3way(Segment<double>(rhs_begin,rhs_end), lhs_begin);
110}
111
112void test_compare(test::Suite& suite, double lhs_begin, double lhs_end, 
113                  double rhs_begin, double rhs_end, int expected)
114{
115  test_compare_(suite, lhs_begin, lhs_end, rhs_begin, rhs_end, expected);
116  // test symmetry
117  test_compare_(suite, rhs_begin, rhs_end, lhs_begin, lhs_end, -expected);
118}
119
120void test_compare(test::Suite& suite)
121{
122  test_compare(suite, 0, 10, 20, 30, -1);
123  test_compare(suite, 0, 10, 10, 30, -1);
124  test_compare(suite, 0, 10, 5, 30, 0);
125  test_compare(suite, 0, 10, -5, 5, 0);
126  test_compare(suite, 0, 10, -5, 0, 1);
127  test_compare(suite, 0, 10, -5, -1, 1);
128  // zero sized
129  test_compare(suite, 0, 0, 20, 30, -1);
130  test_compare(suite, 0, 0, 0, 1, -1);
131  test_compare(suite, 0, 0, -5, 5, 0);
132  test_compare(suite, 0, 0, -5, 0, 1);
133  test_compare(suite, 0, 0, -5, -1, 1);
134
135  // zero sized on both sides
136  test_compare(suite, 0, 0, 1, 1, -1);
137  test_compare(suite, 0, 0, 0, 0, 0);
138  test_compare(suite, 0, 0, -1, -1, 1);
139}
140
141
142void test_erase(test::Suite& suite)
143{
144  suite.out() << "test_erase\n";
145  suite.out() << "  SegmentSet\n";
146  SegmentSet<double> set;
147  set.insert(Segment<double>(0,2));
148  set.insert(Segment<double>(10,20));
149  set.insert(Segment<double>(30,50));
150  SegmentSet<double>::iterator iter = set.find(15);
151  set.erase(iter);
152  if (!suite.add(set.size()==2))
153    suite.err() << "size: " << set.size() << " expected 2\n";
154  set.erase(set.begin(), set.end());
155  if (!suite.add(set.size()==0))
156    suite.err() << "size: " << set.size() << " expected 0\n";
157  suite.out() << "  SegmentMap\n";
158  typedef SegmentMap<double, std::string> Map;
159  Map map;
160  Map::key_type s(0,2);
161  map.insert(Map::value_type(s, "aha"));
162  map.erase(map.begin());
163  map.erase(map.begin(), map.end());
164}
165
166
167void test_insert(test::Suite& suite)
168{
169  Segment<double> segment;
170  Segment<double> segment2(0,2);
171
172  SegmentSet<double> set;
173  if (set.size()!=0) {
174    suite.add(false);
175    suite.err() << "expected size 0\n";
176  }
177  set.insert(segment2);
178  if (set.size()!=1) {
179    suite.add(false);
180    suite.err() << "expected size 1\n";
181  }
182  set.insert(segment2);
183  if (set.size()!=1) {
184    suite.add(false);
185    suite.err() << "expected size 1\n";
186  }
187  set.insert(Segment<double>(1,3));
188  if (set.size()!=1) {
189    suite.add(false);
190    suite.err() << "expected size 1\n";
191  }
192  else if (set.begin()->begin()!=0.0 || set.begin()->end()!=2.0) {
193    suite.add(false);
194    suite.err() << "error: expected segment to be [0, 2)\n"
195                << "found: [" << set.begin()->begin() 
196                << ", " << set.begin()->end() << ")\n";
197  }
198  if (!suite.add(set.find(0)!=set.end()))
199    suite.err() << "error: set.find(0): expected not end()\n";
200}
201
202void test_count(test::Suite& suite)
203{
204  SegmentSet<double> set;
205  set.insert(Segment<double>(0,2));
206  // test count
207  if (!suite.add(set.count(0)==1))
208    suite.err() << "error: set.count(0): expected 1\n";
209  if (!suite.add(set.count(1)==1))
210    suite.err() << "error: set.count(1): expected 1\n";
211  if (!suite.add(set.count(2)==0))
212    suite.err() << "error: set.count(2): expected 0\n";
213}
214
215void test_insert_merge(test::Suite& suite)
216{
217  SegmentSet<double> set;
218  set.insert(Segment<double>(0,2));
219  set.insert_merge(Segment<double>(3,5));
220  if (!suite.add(set.size()==2))
221    suite.err() << "error: set.size(): expected 2\n";
222
223  set.insert_merge(Segment<double>(12,13));
224  if (!suite.add(set.size()==3))
225    suite.err() << "error: set.size(): expected 3\n";
226
227  set.insert_merge(Segment<double>(1,4));
228  if (!suite.add(set.size()==2))
229    suite.err() << "error: set.size(): " << set.size() << " expected 2\n";
230
231  set.insert_merge(Segment<double>(0,100));
232  if (!suite.add(set.size()==1))
233    suite.err() << "error: set.size(): " << set.size() << " expected 1\n";
234  set.insert_merge(set.end(), Segment<double>(200, 1000));
235
236  SegmentSet<double> set2;
237  set2.insert_merge(set.begin(), set.end());
238  std::vector<Segment<double> > vec(set.size());
239  std::copy(set.begin(), set.end(), vec.begin());
240  set2.insert_merge(vec.begin(), vec.end());
241}
242
243
244void make_segment_foo(Segment<int> seg)
245{
246}
247
248
249void test_make_segment(test::Suite& suite)
250{
251  make_segment_foo(make_segment(12, 18));
252}
253
254
255void test_set_bound(test::Suite& suite)
256{
257  SegmentSet<double> set;
258  set.insert(Segment<double>(0,2));
259  set.begin();
260  set.end();
261  if (!suite.add(set.lower_bound(-1)==set.begin()))
262    suite.err() << "error: expected set.lower_bound to return begin()\n";
263  if (!suite.add(set.lower_bound(1)==set.begin()))
264    suite.err() << "error: expected set.lower_bound to return begin()\n";
265  if (!suite.add(set.lower_bound(2)==set.end()))
266    suite.err() << "error: expected set.lower_bound to return end()\n";
267
268  if (!suite.add(set.upper_bound(0)==set.end()))
269    suite.err() << "error: expected set.upper_bound to return end()\n";
270  if (!suite.add(set.upper_bound(2)==set.end()))
271    suite.err() << "error: expected set.upper_bound to return end()\n";
272
273  set.insert_merge(Segment<double>(3,4));
274  SegmentSet<double>::const_iterator i=set.lower_bound(3.5);
275  if (i->begin() != 3 || i->end() != 4) {
276    suite.add(false);
277    suite.out() << "found: " << i->begin() << " " << i->end() << "\n";
278    suite.out() << "expected: 3 4\n";
279  }
280
281  i=set.lower_bound(1);
282  if (i->begin() != 0 || i->end() != 2) {
283    suite.add(false);
284    suite.out() << "found: " << i->begin() << " " << i->end() << "\n";
285    suite.out() << "expected: 0 2\n";
286  }
287
288  set.clear();
289}
290
291void test_segment_map(test::Suite& suite)
292{
293  typedef SegmentMap<double, std::string> Map;
294  Map map;
295  Map::element_type x = 0.0;
296  avoid_pedantic_warning(x);
297  Map::key_type key = Segment<double>(1.0,2.0);
298  Map::mapped_type  mapped = "foofana";
299
300  map.insert(Map::value_type(key, mapped));
301  suite.add(map.find(1.5) != map.end());
302  suite.add(map.find(1.5)->second == "foofana");
303
304  Map::iterator i = map.begin();
305  i = map.end();
306  const Map const_map(map);
307  Map::const_iterator ci = const_map.begin();
308  ci = const_map.end();
309
310  suite.add(map.size()==1);
311  map.clear();
312  suite.add(map.empty()==true);
313  suite.add(map.size()==0);
314  map = const_map;
315  suite.add(map.size()==1);
316  suite.add(const_map.count(1.0)==1);
317  suite.add(const_map.count(0.0)==0);
318  suite.add(const_map.empty()==false);
319  suite.add(const_map.find(1.0)==const_map.begin());
320  suite.add(const_map.find(30.0)==const_map.end());
321  ci = const_map.lower_bound(0.0);
322  ci = const_map.upper_bound(0.0);
323  i = map.lower_bound(0.0);
324  i = map.upper_bound(0.0);
325}
326
327void test_segment(test::Suite& suite)
328{
329  Segment<double> segment12(0,2);
330  Segment<double> segment13(1,3);
331  if (segment12==segment13) {
332    suite.add(false);
333    suite.err() << "segment12==segment13 : expected false\n";
334  }
335  if (!(segment12!=segment13)) {
336    suite.add(false);
337    suite.err() << "segment12!=segment13 : expected true\n";
338  }
339  Segment<double> segment14 = intersection(segment12, segment13);
340  suite.add(segment14.begin()==1);
341  suite.add(segment14.end()==2);
342
343  if (!suite.add(compare(Segment<double>(0,2), Segment<double>(3,4))))
344    suite.err() << "error: compare: expected 1\n";
345  if (!suite.add(compare_3way(Segment<double>(0,2), Segment<double>(3,4))==-1))
346    suite.err() << "error: compare_3way: expected -1\n";
347
348  if (!suite.add(compare(Segment<double>(0,2), Segment<double>(1,2))==false))
349    suite.err() << "error: compare: expected 0\n";
350  if (!suite.add(compare_3way(Segment<double>(0,2), Segment<double>(1,2))==0))
351    suite.err() << "error: compare_3way: expected 0\n";
352
353  if (!suite.add(compare(Segment<double>(3,4), Segment<double>(0,2))==false))
354    suite.err() << "error: compare: expected 0\n";
355  if (!suite.add(compare_3way(Segment<double>(3,4), Segment<double>(0,2))==1))
356    suite.err() << "error: compare_3way: expected 1\n";
357
358}
359
360
361void test_includes(test::Suite& suite, const Segment<int>& a,
362                   const Segment<int>& b, bool answer)
363{
364  if (includes(a, b) != answer) {
365    suite.add(false);
366    suite.err() << "error: includes: "
367                << "[" << a.begin() << ", "
368                << a.end() << ") "
369                << "[" << b.begin() << ", "
370                << b.end() << ") "
371                << "expected: " << answer << "\n";
372  }
373}
374
375
376void test_includes(test::Suite& suite)
377{
378  Segment<int> a(0, 10);
379  Segment<int> b(0, 5);
380  Segment<int> c(5, 10);
381
382  test_includes(suite, a, a, true);
383  test_includes(suite, a, b, false);
384  test_includes(suite, a, c, false);
385
386  test_includes(suite, b, a, true);
387  test_includes(suite, b, b, true);
388  test_includes(suite, b, c, false);
389
390  test_includes(suite, c, a, true);
391  test_includes(suite, c, b, false);
392  test_includes(suite, c, c, true);
393}
394
395
396void test_overlap(test::Suite& suite, const Segment<int>& a,
397                  const Segment<int>& b, bool answer)
398{
399  if (overlap(a, b) != answer) {
400    suite.add(false);
401    suite.err() << "error: overlap: "
402                << "[" << a.begin() << ", "
403                << a.end() << ") "
404                << "[" << b.begin() << ", "
405                << b.end() << ") "
406                << "expected: " << answer << "\n";
407  }
408}
409
410
411void test_overlap(test::Suite& suite)
412{
413  Segment<int> a(0, 10);
414  Segment<int> b(0, 5);
415  Segment<int> c(5, 10);
416
417  test_overlap(suite, a, a, true);
418  test_overlap(suite, a, b, true);
419  test_overlap(suite, a, c, true);
420
421  test_overlap(suite, b, a, true);
422  test_overlap(suite, b, b, true);
423  test_overlap(suite, b, c, false);
424
425  test_overlap(suite, c, a, true);
426  test_overlap(suite, c, b, false);
427  test_overlap(suite, c, c, true);
428}
429
430
431void test_union(test::Suite& suite, const Segment<int>& a,
432                const Segment<int>& b, const Segment<int>& answer)
433{
434  if (set_union(a, b) != answer) {
435    suite.add(false);
436    suite.err() << "error: union: "
437                << "[" << a.begin() << ", "
438                << a.end() << ") "
439                << "[" << b.begin() << ", "
440                << b.end() << ") "
441                << "expected: [" << answer.begin() << "," << answer.end()
442                << " )\n";
443  }
444}
445
446
447void test_union(test::Suite& suite)
448{
449  Segment<int> a(0, 10);
450  Segment<int> b(0, 5);
451  Segment<int> c(5, 12);
452
453  test_union(suite, a, a, a);
454  test_union(suite, a, b, a);
455  test_union(suite, a, c, Segment<int>(0,12));
456
457  test_union(suite, b, a, a);
458  test_union(suite, b, b, b);
459  test_union(suite, b, c, Segment<int>(0,12));
460
461  test_union(suite, c, a, Segment<int>(0,12));
462  test_union(suite, c, b, Segment<int>(0,12));
463  test_union(suite, c, c, c);
464}
Note: See TracBrowser for help on using the repository browser.