source: trunk/test/segment.cc @ 2370

Last change on this file since 2370 was 2370, checked in by Peter, 11 years ago

closes #646

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