source: trunk/test/segment.cc @ 3344

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

new function SegmentSet::insert_merge with hint. refs #818

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.8 KB
Line 
1// $Id: segment.cc 3344 2014-11-06 12:09:32Z 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
203void test_set_bound(test::Suite& suite)
204{
205  SegmentSet<double> set;
206  set.insert(Segment<double>(0,2));
207  set.begin();
208  set.end();
209  if (!suite.add(set.lower_bound(-1)==set.begin()))
210    suite.err() << "error: expected set.lower_bound to return begin()\n";
211  if (!suite.add(set.lower_bound(1)==set.begin()))
212    suite.err() << "error: expected set.lower_bound to return begin()\n";
213  if (!suite.add(set.lower_bound(2)==set.end()))
214    suite.err() << "error: expected set.lower_bound to return end()\n";
215
216  if (!suite.add(set.upper_bound(0)==set.end()))
217    suite.err() << "error: expected set.upper_bound to return end()\n";
218  if (!suite.add(set.upper_bound(2)==set.end()))
219    suite.err() << "error: expected set.upper_bound to return end()\n";
220
221  set.insert_merge(Segment<double>(3,4));
222  SegmentSet<double>::const_iterator i=set.lower_bound(3.5);
223  if (i->begin() != 3 || i->end() != 4) {
224    suite.add(false);
225    suite.out() << "found: " << i->begin() << " " << i->end() << "\n";
226    suite.out() << "expected: 3 4\n";
227  }
228
229  i=set.lower_bound(1);
230  if (i->begin() != 0 || i->end() != 2) {
231    suite.add(false);
232    suite.out() << "found: " << i->begin() << " " << i->end() << "\n";
233    suite.out() << "expected: 0 2\n";
234  }
235
236  set.clear();
237}
238
239void test_segment_map(test::Suite& suite)
240{
241  typedef SegmentMap<double, std::string> Map;
242  Map map;
243  Map::element_type x = 0.0;
244  avoid_pedantic_warning(x);
245  Map::key_type key = Segment<double>(1.0,2.0);
246  Map::mapped_type  mapped = "foofana";
247
248  map.insert(Map::value_type(key, mapped));
249  suite.add(map.find(1.5) != map.end());
250  suite.add(map.find(1.5)->second == "foofana");
251
252  Map::iterator i = map.begin();
253  i = map.end();
254  const Map const_map(map);
255  Map::const_iterator ci = const_map.begin();
256  ci = const_map.end();
257
258  suite.add(map.size()==1);
259  map.clear();
260  suite.add(map.empty()==true);
261  suite.add(map.size()==0);
262  map = const_map;
263  suite.add(map.size()==1);
264  suite.add(const_map.count(1.0)==1);
265  suite.add(const_map.count(0.0)==0);
266  suite.add(const_map.empty()==false);
267  suite.add(const_map.find(1.0)==const_map.begin());
268  suite.add(const_map.find(30.0)==const_map.end());
269  ci = const_map.lower_bound(0.0);
270  ci = const_map.upper_bound(0.0);
271  i = map.lower_bound(0.0);
272  i = map.upper_bound(0.0);
273}
274
275void test_segment(test::Suite& suite)
276{
277  Segment<double> segment12(0,2);
278  Segment<double> segment13(1,3);
279  Segment<double> segment14 = intersection(segment12, segment13);
280  suite.add(segment14.begin()==1);
281  suite.add(segment14.end()==2);
282
283  if (!suite.add(compare(Segment<double>(0,2), Segment<double>(3,4))))
284    suite.err() << "error: compare: expected 1\n";
285  if (!suite.add(compare_3way(Segment<double>(0,2), Segment<double>(3,4))==-1))
286    suite.err() << "error: compare_3way: expected -1\n";
287
288  if (!suite.add(compare(Segment<double>(0,2), Segment<double>(1,2))==false))
289    suite.err() << "error: compare: expected 0\n";
290  if (!suite.add(compare_3way(Segment<double>(0,2), Segment<double>(1,2))==0))
291    suite.err() << "error: compare_3way: expected 0\n";
292
293  if (!suite.add(compare(Segment<double>(3,4), Segment<double>(0,2))==false))
294    suite.err() << "error: compare: expected 0\n";
295  if (!suite.add(compare_3way(Segment<double>(3,4), Segment<double>(0,2))==1))
296    suite.err() << "error: compare_3way: expected 1\n";
297
298}
Note: See TracBrowser for help on using the repository browser.