summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/polygon/test/polygon_interval_test.cpp
blob: 070d34f76d0343cbd2c55bd71676a21575bc248d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Boost.Polygon library polygon_interval_test.cpp file

//          Copyright Andrii Sydorchuk 2012.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

// See http://www.boost.org for updates, documentation, and revision history.

#include <boost/core/lightweight_test.hpp>
#include <boost/polygon/interval_concept.hpp>
#include <boost/polygon/interval_data.hpp>
#include <boost/polygon/interval_traits.hpp>

using namespace boost::polygon;

void interval_data_test()
{
  typedef interval_data<int> interval_type;
  interval_type interval1(1, 2);
  interval_type interval2;
  interval2 = interval1;

  BOOST_TEST_EQ(interval1.low(), 1);
  BOOST_TEST_EQ(interval1.high(), 2);
  BOOST_TEST_EQ(interval1.get(LOW), 1);
  BOOST_TEST_EQ(interval1.get(HIGH), 2);
  BOOST_TEST(interval1 == interval2);
  BOOST_TEST(!(interval1 != interval2));
  BOOST_TEST(!(interval1 < interval2));
  BOOST_TEST(!(interval1 > interval2));
  BOOST_TEST(interval1 <= interval2);
  BOOST_TEST(interval1 >= interval2);

  interval1.low(2);
  interval1.high(1);
  BOOST_TEST_EQ(interval1.low(), 2);
  BOOST_TEST_EQ(interval1.high(), 1);
  BOOST_TEST(!(interval1 == interval2));
  BOOST_TEST(interval1 != interval2);

  interval2.set(LOW, 2);
  interval2.set(HIGH, 1);
  BOOST_TEST(interval1 == interval2);
}

void interval_traits_test()
{
  typedef interval_data<int> interval_type;

  interval_type interval = interval_mutable_traits<interval_type>::construct(1, 2);
  BOOST_TEST_EQ(interval_traits<interval_type>::get(interval, LOW), 1);
  BOOST_TEST_EQ(interval_traits<interval_type>::get(interval, HIGH), 2);

  interval_mutable_traits<interval_type>::set(interval, LOW, 3);
  interval_mutable_traits<interval_type>::set(interval, HIGH, 4);
  BOOST_TEST_EQ(interval_traits<interval_type>::get(interval, LOW), 3);
  BOOST_TEST_EQ(interval_traits<interval_type>::get(interval, HIGH), 4);
}

template <typename T>
struct Interval {
  T left;
  T right;
};

namespace boost {
namespace polygon {
  template <typename T>
  struct geometry_concept< Interval<T> > {
    typedef interval_concept type;
  };

  template <typename T>
  struct interval_traits< Interval<T> > {
    typedef T coordinate_type;

    static coordinate_type get(const Interval<T>& interval, direction_1d dir) {
      return (dir == LOW) ? interval.left : interval.right;
    }
  };

  template <typename T>
  struct interval_mutable_traits< Interval<T> > {
    typedef T coordinate_type;

    static void set(Interval<T>& interval, direction_1d dir, T value) {
      (dir == LOW) ? interval.left = value : interval.right = value;
    }

    static Interval<T> construct(coordinate_type left, coordinate_type right) {
      Interval<T> interval;
      interval.left = left;
      interval.right = right;
      return interval;
    }
  };
}  // polygon
}  // boost

void interval_concept_test1()
{
  typedef Interval<int> interval_type;

  interval_type interval1 = construct<interval_type>(2, 1);
  BOOST_TEST_EQ(interval1.left, 1);
  BOOST_TEST_EQ(interval1.right, 2);

  set(interval1, LOW, 3);
  set(interval1, HIGH, 4);
  BOOST_TEST_EQ(get(interval1, LOW), 3);
  BOOST_TEST_EQ(get(interval1, HIGH), 4);

  interval_type interval2 = copy_construct<interval_type>(interval1);
  BOOST_TEST(equivalence(interval1, interval2));

  low(interval2, 1);
  high(interval2, 2);
  BOOST_TEST_EQ(low(interval2), 1);
  BOOST_TEST_EQ(high(interval2), 2);

  assign(interval1, interval2);
  BOOST_TEST(equivalence(interval1, interval2));
}

void interval_concept_test2()
{
  typedef Interval<int> interval_type;

  interval_type interval1 = construct<interval_type>(1, 3);
  BOOST_TEST_EQ(center(interval1), 2);
  BOOST_TEST_EQ(delta(interval1), 2);

  flip(interval1, -1);
  BOOST_TEST_EQ(low(interval1), -5);
  BOOST_TEST_EQ(high(interval1), -3);

  scale_up(interval1, 2);
  BOOST_TEST_EQ(low(interval1), -10);
  BOOST_TEST_EQ(high(interval1), -6);

  scale_down(interval1, 2);
  BOOST_TEST_EQ(low(interval1), -5);
  BOOST_TEST_EQ(high(interval1), -3);

  move(interval1, 5);
  BOOST_TEST_EQ(low(interval1), 0);
  BOOST_TEST_EQ(high(interval1), 2);

  convolve(interval1, 1);
  BOOST_TEST_EQ(low(interval1), 1);
  BOOST_TEST_EQ(high(interval1), 3);

  deconvolve(interval1, 2);
  BOOST_TEST_EQ(low(interval1), -1);
  BOOST_TEST_EQ(high(interval1), 1);

  interval_type interval2 = construct<interval_type>(-1, 2);
  convolve(interval1, interval2);
  BOOST_TEST_EQ(low(interval1), -2);
  BOOST_TEST_EQ(high(interval1), 3);

  deconvolve(interval1, interval2);
  BOOST_TEST_EQ(low(interval1), -1);
  BOOST_TEST_EQ(high(interval1), 1);

  reflected_convolve(interval1, interval2);
  BOOST_TEST_EQ(low(interval1), -3);
  BOOST_TEST_EQ(high(interval1), 2);

  reflected_deconvolve(interval1, interval2);
  BOOST_TEST_EQ(low(interval1), -1);
  BOOST_TEST_EQ(high(interval1), 1);
}

void interval_concept_test3()
{
  typedef Interval<int> interval_type;

  interval_type interval1 = construct<interval_type>(1, 3);
  BOOST_TEST_EQ(euclidean_distance(interval1, -2), 3);
  BOOST_TEST_EQ(euclidean_distance(interval1, 2), 0);
  BOOST_TEST_EQ(euclidean_distance(interval1, 4), 1);

  interval_type interval2 = construct<interval_type>(-1, 0);
  BOOST_TEST_EQ(euclidean_distance(interval1, interval2), 1);
  BOOST_TEST(!intersects(interval1, interval2));
  BOOST_TEST(!boundaries_intersect(interval1, interval2));
  BOOST_TEST(!intersect(interval2, interval1));
  BOOST_TEST_EQ(low(interval2), -1);
  BOOST_TEST_EQ(high(interval2), 0);

  interval_type interval3 = construct<interval_type>(-1, 6);
  BOOST_TEST_EQ(euclidean_distance(interval1, interval3), 0);
  BOOST_TEST(intersects(interval1, interval3));
  BOOST_TEST(!boundaries_intersect(interval1, interval3));
  BOOST_TEST(intersect(interval3, interval1));
  BOOST_TEST_EQ(low(interval3), 1);
  BOOST_TEST_EQ(high(interval3), 3);

  interval_type interval4 = construct<interval_type>(5, 6);
  BOOST_TEST_EQ(euclidean_distance(interval1, interval4), 2);
  BOOST_TEST(!intersects(interval1, interval4));
  BOOST_TEST(!boundaries_intersect(interval1, interval4));
  BOOST_TEST(!intersect(interval4, interval1));
  BOOST_TEST_EQ(low(interval4), 5);
  BOOST_TEST_EQ(high(interval4), 6);

  interval_type interval5 = construct<interval_type>(3, 5);
  BOOST_TEST_EQ(euclidean_distance(interval1, interval5), 0);
  BOOST_TEST(!intersects(interval1, interval5, false));
  BOOST_TEST(boundaries_intersect(interval1, interval5));
  BOOST_TEST(intersect(interval5, interval1));
  BOOST_TEST_EQ(low(interval5), 3);
  BOOST_TEST_EQ(high(interval5), 3);
}

void interval_concept_test4()
{
  typedef Interval<int> interval_type;

  interval_type interval1 = construct<interval_type>(1, 3);
  interval_type interval2 = construct<interval_type>(3, 5);
  BOOST_TEST(!abuts(interval1, interval2, LOW));
  BOOST_TEST(abuts(interval1, interval2, HIGH));
  BOOST_TEST(abuts(interval1, interval2));

  bloat(interval1, 1);
  BOOST_TEST_EQ(low(interval1), 0);
  BOOST_TEST_EQ(high(interval1), 4);
  BOOST_TEST(!abuts(interval1, interval2));

  bloat(interval1, LOW, 1);
  BOOST_TEST_EQ(low(interval1), -1);
  BOOST_TEST_EQ(high(interval1), 4);

  shrink(interval1, LOW, 1);
  BOOST_TEST_EQ(low(interval1), 0);
  BOOST_TEST_EQ(high(interval1), 4);

  shrink(interval1, 1);
  BOOST_TEST_EQ(low(interval1), 1);
  BOOST_TEST_EQ(high(interval1), 3);

  BOOST_TEST(encompass(interval1, 4));
  BOOST_TEST_EQ(low(interval1), 1);
  BOOST_TEST_EQ(high(interval1), 4);

  BOOST_TEST(encompass(interval1, interval2));
  BOOST_TEST_EQ(low(interval1), 1);
  BOOST_TEST_EQ(high(interval1), 5);

  interval1 = get_half(interval1, LOW);
  BOOST_TEST_EQ(low(interval1), 1);
  BOOST_TEST_EQ(high(interval1), 3);

  BOOST_TEST(join_with(interval1, interval2));
  BOOST_TEST_EQ(low(interval1), 1);
  BOOST_TEST_EQ(high(interval1), 5);
}

int main()
{
    interval_data_test();
    interval_traits_test();
    interval_concept_test1();
    interval_concept_test2();
    interval_concept_test3();
    interval_concept_test4();
    return boost::report_errors();
}