source: trunk/test/segment.cc @ 3345

Last change on this file since 3345 was 3345, checked in by Peter, 8 years ago

new function SegmentSet::insert_merge(first, last). closes #818

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