source: trunk/test/segment.cc @ 3455

Last change on this file since 3455 was 3455, checked in by Peter, 7 years ago

update copyright years

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