source: trunk/test/segment.cc @ 2919

Last change on this file since 2919 was 2919, checked in by Peter, 10 years ago

update copyright years

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