source: trunk/test/segment.cc @ 3550

Last change on this file since 3550 was 3550, checked in by Peter, 6 years ago

Update copyright years. Happy New Year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.4 KB
Line 
1// $Id: segment.cc 3550 2017-01-03 05:41:02Z peter $
2
3/*
4  Copyright (C) 2010, 2011, 2012, 2014, 2015, 2016 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_compare(test::Suite& suite);
33void test_count(test::Suite&);
34void test_erase(test::Suite&);
35void test_insert(test::Suite&);
36void test_insert_merge(test::Suite&);
37void test_segment(test::Suite& suite);
38void test_operator(test::Suite& suite);
39void test_segment_map(test::Suite& suite);
40void test_set_bound(test::Suite&);
41
42template<typename T>
43void avoid_pedantic_warning(T) {}
44
45int main(int argc, char* argv[])
46{ 
47  test::Suite suite(argc, argv);
48
49  test_compare(suite);
50  test_count(suite);
51  test_erase(suite);
52  test_insert(suite);
53  test_insert_merge(suite);
54  test_segment(suite);
55  test_set_bound(suite);
56
57  test_segment_map(suite);
58
59  return suite.return_value();
60}
61
62
63void test_compare_(test::Suite& suite, double lhs_begin, double lhs_end, 
64                   double rhs_begin, double rhs_end, int expected)
65{
66  int result = compare_3way(Segment<double>(lhs_begin,lhs_end), 
67                            Segment<double>(rhs_begin,rhs_end));
68  if (result!=expected) {
69    suite.add(false);
70    suite.err() << "error:"
71                << "[" << lhs_begin << ", " << lhs_end << ") vs "
72                << "[" << rhs_begin << ", " << rhs_end << ")\n"
73                << "result: " << result << "\n"
74                << "expected: " << expected << "\n"
75                << "\n";
76  }
77
78  compare_3way(lhs_begin, Segment<double>(rhs_begin,rhs_end));
79  compare_3way(Segment<double>(rhs_begin,rhs_end), lhs_begin);
80}
81
82void test_compare(test::Suite& suite, double lhs_begin, double lhs_end, 
83                  double rhs_begin, double rhs_end, int expected)
84{
85  test_compare_(suite, lhs_begin, lhs_end, rhs_begin, rhs_end, expected);
86  // test symmetry
87  test_compare_(suite, rhs_begin, rhs_end, lhs_begin, lhs_end, -expected);
88}
89
90void test_compare(test::Suite& suite)
91{
92  test_compare(suite, 0, 10, 20, 30, -1);
93  test_compare(suite, 0, 10, 10, 30, -1);
94  test_compare(suite, 0, 10, 5, 30, 0);
95  test_compare(suite, 0, 10, -5, 5, 0);
96  test_compare(suite, 0, 10, -5, 0, 1);
97  test_compare(suite, 0, 10, -5, -1, 1);
98  // zero sized
99  test_compare(suite, 0, 0, 20, 30, -1);
100  test_compare(suite, 0, 0, 0, 1, -1);
101  test_compare(suite, 0, 0, -5, 5, 0);
102  test_compare(suite, 0, 0, -5, 0, 1);
103  test_compare(suite, 0, 0, -5, -1, 1);
104
105  // zero sized on both sides
106  test_compare(suite, 0, 0, 1, 1, -1);
107  test_compare(suite, 0, 0, 0, 0, 0);
108  test_compare(suite, 0, 0, -1, -1, 1);
109}
110
111
112void test_erase(test::Suite& suite)
113{
114  suite.out() << "test_erase\n";
115  suite.out() << "  SegmentSet\n";
116  SegmentSet<double> set;
117  set.insert(Segment<double>(0,2));
118  set.insert(Segment<double>(10,20));
119  set.insert(Segment<double>(30,50));
120  SegmentSet<double>::iterator iter = set.find(15);
121  set.erase(iter);
122  if (!suite.add(set.size()==2))
123    suite.err() << "size: " << set.size() << " expected 2\n";
124  set.erase(set.begin(), set.end());
125  if (!suite.add(set.size()==0))
126    suite.err() << "size: " << set.size() << " expected 0\n";
127  suite.out() << "  SegmentMap\n";
128  typedef SegmentMap<double, std::string> Map;
129  Map map;
130  Map::key_type s(0,2);
131  map.insert(Map::value_type(s, "aha"));
132  map.erase(map.begin());
133  map.erase(map.begin(), map.end());
134}
135
136
137void test_insert(test::Suite& suite)
138{
139  Segment<double> segment;
140  Segment<double> segment2(0,2);
141
142  SegmentSet<double> set;
143  if (set.size()!=0) {
144    suite.add(false);
145    suite.err() << "expected size 0\n";
146  }
147  set.insert(segment2);
148  if (set.size()!=1) {
149    suite.add(false);
150    suite.err() << "expected size 1\n";
151  }
152  set.insert(segment2);
153  if (set.size()!=1) {
154    suite.add(false);
155    suite.err() << "expected size 1\n";
156  }
157  set.insert(Segment<double>(1,3));
158  if (set.size()!=1) {
159    suite.add(false);
160    suite.err() << "expected size 1\n";
161  }
162  else if (set.begin()->begin()!=0.0 || set.begin()->end()!=2.0) {
163    suite.add(false);
164    suite.err() << "error: expected segment to be [0, 2)\n"
165                << "found: [" << set.begin()->begin() 
166                << ", " << set.begin()->end() << ")\n";
167  }
168  if (!suite.add(set.find(0)!=set.end()))
169    suite.err() << "error: set.find(0): expected not end()\n";
170}
171
172void test_count(test::Suite& suite)
173{
174  SegmentSet<double> set;
175  set.insert(Segment<double>(0,2));
176  // test count
177  if (!suite.add(set.count(0)==1))
178    suite.err() << "error: set.count(0): expected 1\n";
179  if (!suite.add(set.count(1)==1))
180    suite.err() << "error: set.count(1): expected 1\n";
181  if (!suite.add(set.count(2)==0))
182    suite.err() << "error: set.count(2): expected 0\n";
183}
184
185void test_insert_merge(test::Suite& suite)
186{
187  SegmentSet<double> set;
188  set.insert(Segment<double>(0,2));
189  set.insert_merge(Segment<double>(3,5));
190  if (!suite.add(set.size()==2))
191    suite.err() << "error: set.size(): expected 2\n";
192
193  set.insert_merge(Segment<double>(12,13));
194  if (!suite.add(set.size()==3))
195    suite.err() << "error: set.size(): expected 3\n";
196
197  set.insert_merge(Segment<double>(1,4));
198  if (!suite.add(set.size()==2))
199    suite.err() << "error: set.size(): " << set.size() << " expected 2\n";
200
201  set.insert_merge(Segment<double>(0,100));
202  if (!suite.add(set.size()==1))
203    suite.err() << "error: set.size(): " << set.size() << " expected 1\n";
204  set.insert_merge(set.end(), Segment<double>(200, 1000));
205
206  SegmentSet<double> set2;
207  set2.insert_merge(set.begin(), set.end());
208  std::vector<Segment<double> > vec(set.size());
209  std::copy(set.begin(), set.end(), vec.begin());
210  set2.insert_merge(vec.begin(), vec.end());
211}
212
213void test_set_bound(test::Suite& suite)
214{
215  SegmentSet<double> set;
216  set.insert(Segment<double>(0,2));
217  set.begin();
218  set.end();
219  if (!suite.add(set.lower_bound(-1)==set.begin()))
220    suite.err() << "error: expected set.lower_bound to return begin()\n";
221  if (!suite.add(set.lower_bound(1)==set.begin()))
222    suite.err() << "error: expected set.lower_bound to return begin()\n";
223  if (!suite.add(set.lower_bound(2)==set.end()))
224    suite.err() << "error: expected set.lower_bound to return end()\n";
225
226  if (!suite.add(set.upper_bound(0)==set.end()))
227    suite.err() << "error: expected set.upper_bound to return end()\n";
228  if (!suite.add(set.upper_bound(2)==set.end()))
229    suite.err() << "error: expected set.upper_bound to return end()\n";
230
231  set.insert_merge(Segment<double>(3,4));
232  SegmentSet<double>::const_iterator i=set.lower_bound(3.5);
233  if (i->begin() != 3 || i->end() != 4) {
234    suite.add(false);
235    suite.out() << "found: " << i->begin() << " " << i->end() << "\n";
236    suite.out() << "expected: 3 4\n";
237  }
238
239  i=set.lower_bound(1);
240  if (i->begin() != 0 || i->end() != 2) {
241    suite.add(false);
242    suite.out() << "found: " << i->begin() << " " << i->end() << "\n";
243    suite.out() << "expected: 0 2\n";
244  }
245
246  set.clear();
247}
248
249void test_segment_map(test::Suite& suite)
250{
251  typedef SegmentMap<double, std::string> Map;
252  Map map;
253  Map::element_type x = 0.0;
254  avoid_pedantic_warning(x);
255  Map::key_type key = Segment<double>(1.0,2.0);
256  Map::mapped_type  mapped = "foofana";
257
258  map.insert(Map::value_type(key, mapped));
259  suite.add(map.find(1.5) != map.end());
260  suite.add(map.find(1.5)->second == "foofana");
261
262  Map::iterator i = map.begin();
263  i = map.end();
264  const Map const_map(map);
265  Map::const_iterator ci = const_map.begin();
266  ci = const_map.end();
267
268  suite.add(map.size()==1);
269  map.clear();
270  suite.add(map.empty()==true);
271  suite.add(map.size()==0);
272  map = const_map;
273  suite.add(map.size()==1);
274  suite.add(const_map.count(1.0)==1);
275  suite.add(const_map.count(0.0)==0);
276  suite.add(const_map.empty()==false);
277  suite.add(const_map.find(1.0)==const_map.begin());
278  suite.add(const_map.find(30.0)==const_map.end());
279  ci = const_map.lower_bound(0.0);
280  ci = const_map.upper_bound(0.0);
281  i = map.lower_bound(0.0);
282  i = map.upper_bound(0.0);
283}
284
285void test_segment(test::Suite& suite)
286{
287  Segment<double> segment12(0,2);
288  Segment<double> segment13(1,3);
289  if (segment12==segment13) {
290    suite.add(false);
291    suite.err() << "segment12==segment13 : expected false\n";
292  }
293  if (!(segment12!=segment13)) {
294    suite.add(false);
295    suite.err() << "segment12!=segment13 : expected true\n";
296  }
297  Segment<double> segment14 = intersection(segment12, segment13);
298  suite.add(segment14.begin()==1);
299  suite.add(segment14.end()==2);
300
301  if (!suite.add(compare(Segment<double>(0,2), Segment<double>(3,4))))
302    suite.err() << "error: compare: expected 1\n";
303  if (!suite.add(compare_3way(Segment<double>(0,2), Segment<double>(3,4))==-1))
304    suite.err() << "error: compare_3way: expected -1\n";
305
306  if (!suite.add(compare(Segment<double>(0,2), Segment<double>(1,2))==false))
307    suite.err() << "error: compare: expected 0\n";
308  if (!suite.add(compare_3way(Segment<double>(0,2), Segment<double>(1,2))==0))
309    suite.err() << "error: compare_3way: expected 0\n";
310
311  if (!suite.add(compare(Segment<double>(3,4), Segment<double>(0,2))==false))
312    suite.err() << "error: compare: expected 0\n";
313  if (!suite.add(compare_3way(Segment<double>(3,4), Segment<double>(0,2))==1))
314    suite.err() << "error: compare_3way: expected 1\n";
315
316}
Note: See TracBrowser for help on using the repository browser.