source: trunk/test/segment.cc @ 3598

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

implement make_segment. closes #834

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