source: trunk/test/segment.cc @ 2611

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

fixes #680 and #679

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