1 /*-----------------------------------------------------------------------------+
2 Copyright (c) 2008-2010: Joachim Faulhaber
3 +------------------------------------------------------------------------------+
4 Distributed under the Boost Software License, Version 1.0.
5 (See accompanying file LICENCE.txt or copy at
6 http://www.boost.org/LICENSE_1_0.txt)
7 +-----------------------------------------------------------------------------*/
8 #ifndef LIBS_ICL_TEST_TEST_ICL_interval_shared_hpp_JOFA_100306__
9 #define LIBS_ICL_TEST_TEST_ICL_interval_shared_hpp_JOFA_100306__
11 #include <boost/icl/interval_set.hpp>
13 template <class DomainT, ICL_COMPARE Compare,
14 ICL_INTERVAL(ICL_COMPARE) Interval>
15 void test_inner_complement(const ICL_INTERVAL_TYPE(Interval,DomainT,Compare)& itv1,
16 const ICL_INTERVAL_TYPE(Interval,DomainT,Compare)& itv2)
18 typedef interval_set<DomainT,Compare,Interval> ItvSetT;
19 typedef ICL_INTERVAL_TYPE(Interval,DomainT,Compare) IntervalT;
21 BOOST_CHECK_EQUAL(icl::length(inner_complement(itv1,itv2)), icl::distance(itv1,itv2));
22 BOOST_CHECK_EQUAL(icl::length(inner_complement(itv1,itv2)), icl::distance(itv2,itv1));
23 BOOST_CHECK_EQUAL(icl::length(inner_complement(itv2,itv1)), icl::distance(itv1,itv2));
24 BOOST_CHECK_EQUAL(icl::length(inner_complement(itv2,itv1)), icl::distance(itv2,itv1));
26 IntervalT in_comp = inner_complement(itv1,itv2);
27 ItvSetT itvset, inner_comp;
28 itvset.add(itv1).add(itv2);
29 ItvSetT hullset = ItvSetT(hull(itvset));
30 inner_comp = hullset - itvset;
31 IntervalT inner_comp_itv;
32 if(inner_comp.begin() != inner_comp.end())
33 inner_comp_itv = *inner_comp.begin();
35 BOOST_CHECK_EQUAL(inner_complement(itv1,itv2), inner_comp_itv);
36 BOOST_CHECK_EQUAL(inner_complement(itv2,itv1), inner_comp_itv);
37 BOOST_CHECK_EQUAL(icl::length(inner_comp), icl::distance(itv1,itv2));
38 BOOST_CHECK_EQUAL(icl::length(inner_comp), icl::distance(itv2,itv1));
40 BOOST_CHECK(icl::disjoint(itv1, in_comp));
41 BOOST_CHECK(icl::disjoint(itv2, in_comp));
43 IntervalT itv1_comp = hull(itv1, in_comp);
44 IntervalT itv2_comp = hull(itv2, in_comp);
46 if(!icl::is_empty(in_comp))
48 BOOST_CHECK(icl::intersects(itv1_comp, in_comp));
49 BOOST_CHECK(icl::intersects(itv2_comp, in_comp));
51 BOOST_CHECK_EQUAL(itv1_comp & itv2_comp, in_comp);
52 BOOST_CHECK_EQUAL( icl::is_empty(itv1_comp & itv2_comp), icl::disjoint(itv1_comp, itv2_comp));
53 BOOST_CHECK_EQUAL(!icl::is_empty(itv1_comp & itv2_comp), icl::intersects(itv1_comp, itv2_comp));
57 template <class IntervalT>
58 void test_inner_complement_(const IntervalT& itv1, const IntervalT& itv2)
60 typedef typename interval_traits<IntervalT>::domain_type DomainT;
61 // For the test of plain interval types we assume that std::less is
62 // the compare functor
63 test_inner_complement<DomainT, std::less, IntervalT>(itv1, itv2);
66 #ifndef BOOST_ICL_USE_STATIC_BOUNDED_INTERVALS
68 void interval_ctor_specific()
70 BOOST_CHECK_EQUAL(icl::length(icl::interval<double>::type()), 0.0);
71 BOOST_CHECK_EQUAL(icl::cardinality(icl::interval<double>::closed(5.0, 5.0)), 1);
72 BOOST_CHECK_EQUAL(icl::cardinality(icl::interval<std::string>::closed("test", "test")), 1);
73 BOOST_CHECK_EQUAL(icl::cardinality(icl::interval<std::string>::closed("best","test")),
74 icl::cardinality(icl::interval<double>::closed(0.0,0.1)));
75 BOOST_CHECK_EQUAL(icl::cardinality(icl::interval<std::string>::right_open("best","test")),
76 icl::infinity<size_type_of<icl::interval<std::string>::type>::type >::value() );
77 BOOST_CHECK_EQUAL(icl::cardinality(icl::interval<double>::right_open(0.0, 1.0)),
78 icl::infinity<size_type_of<icl::interval<double>::type>::type >::value() );
81 #endif // ndef BOOST_ICL_USE_STATIC_BOUNDED_INTERVALS
84 void interval_equal_4_integral_types()
86 typedef typename icl::interval<T>::type IntervalT;
91 BOOST_CHECK_EQUAL(IntervalT(), IntervalT(v7,v3));
93 //I: (I)nside = closed bound
96 IntervalT I3_7I = icl::interval<T>::closed(v3,v7);
97 IntervalT I3__8D = icl::interval<T>::right_open(v3,v8);
98 IntervalT C2__7I = icl::interval<T>::left_open(v2,v7);
99 IntervalT C2___8D = icl::interval<T>::open(v2,v8);
101 BOOST_CHECK_EQUAL( I3_7I , I3_7I );
102 BOOST_CHECK_EQUAL( I3_7I , I3__8D );
103 BOOST_CHECK_EQUAL( I3_7I , C2__7I );
104 BOOST_CHECK_EQUAL( I3_7I , C2___8D );
106 BOOST_CHECK_EQUAL( I3__8D, I3__8D );
107 BOOST_CHECK_EQUAL( I3__8D, C2__7I );
108 BOOST_CHECK_EQUAL( I3__8D, C2___8D );
110 BOOST_CHECK_EQUAL( C2__7I , C2__7I );
111 BOOST_CHECK_EQUAL( C2__7I , C2___8D );
113 BOOST_CHECK_EQUAL( C2___8D, C2___8D );
117 void interval_less_4_integral_types()
119 typedef typename icl::interval<T>::type IntervalT;
125 BOOST_CHECK_EQUAL(IntervalT() < IntervalT(v7,v3), false);
126 BOOST_CHECK_EQUAL(icl::interval<T>::open(v2,v3) < icl::interval<T>::right_open(v7,v7), false);
127 BOOST_CHECK_EQUAL(icl::interval<T>::left_open(v3,v3) < icl::interval<T>::closed(v7,v3), false);
129 BOOST_CHECK_EQUAL(IntervalT() < IntervalT(v3,v4), true);
130 BOOST_CHECK_EQUAL(icl::interval<T>::open(v2,v3) < icl::interval<T>::right_open(v7,v8), true);
132 //I: (I)nside = closed bound
134 //D: right open bound
135 IntervalT I3_7I = icl::interval<T>::closed(v3,v7);
136 IntervalT I4_7I = icl::interval<T>::closed(v4,v7);
138 IntervalT I3__8D = icl::interval<T>::right_open(v3,v8);
139 IntervalT C2__7I = icl::interval<T>::left_open(v2,v7);
140 IntervalT C2___8D = icl::interval<T>::open(v2,v8);
142 BOOST_CHECK_EQUAL( I3_7I < I3_7I , false);
143 BOOST_CHECK_EQUAL( I3_7I < I3__8D , false);
144 BOOST_CHECK_EQUAL( I3_7I < C2__7I , false);
145 BOOST_CHECK_EQUAL( I3_7I < C2___8D , false);
147 BOOST_CHECK_EQUAL( I3_7I < I4_7I , true);
150 BOOST_CHECK_EQUAL( I3__8D< I3__8D , false);
151 BOOST_CHECK_EQUAL( I3__8D< C2__7I , false);
152 BOOST_CHECK_EQUAL( I3__8D< C2___8D , false);
154 BOOST_CHECK_EQUAL( C2__7I < C2__7I , false);
155 BOOST_CHECK_EQUAL( C2__7I < C2___8D , false);
157 BOOST_CHECK_EQUAL( C2___8D< C2___8D , false);
161 void interval_equal_4_bicremental_continuous_types()
163 typedef typename icl::interval<T>::type IntervalT;
166 BOOST_CHECK_EQUAL(IntervalT(), IntervalT(v7,v3));
168 //I: (I)nside = closed bound
169 //O: (O)utside = open bound
170 IntervalT I3_7I = icl::interval<T>::closed(v3,v7);
171 IntervalT I3_7D = icl::interval<T>::right_open(v3,v7);
172 IntervalT C3_7I = icl::interval<T>::left_open(v3,v7);
173 IntervalT C3_7D = icl::interval<T>::open(v3,v7);
175 BOOST_CHECK_EQUAL( I3_7I , I3_7I );
176 BOOST_CHECK_EQUAL( I3_7I == I3_7D, false );
177 BOOST_CHECK_EQUAL( I3_7I == C3_7D, false );
178 BOOST_CHECK_EQUAL( I3_7I == C3_7D, false );
179 BOOST_CHECK_EQUAL( I3_7I != I3_7D, true );
180 BOOST_CHECK_EQUAL( I3_7I != C3_7D, true );
181 BOOST_CHECK_EQUAL( I3_7I != C3_7D, true );
183 BOOST_CHECK_EQUAL( I3_7D , I3_7D );
184 BOOST_CHECK_EQUAL( I3_7D == C3_7I, false );
185 BOOST_CHECK_EQUAL( I3_7D == C3_7D, false );
186 BOOST_CHECK_EQUAL( I3_7D != C3_7I, true );
187 BOOST_CHECK_EQUAL( I3_7D != C3_7D, true );
189 BOOST_CHECK_EQUAL( C3_7I , C3_7I );
190 BOOST_CHECK_EQUAL( C3_7I == C3_7D, false );
191 BOOST_CHECK_EQUAL( C3_7I != C3_7D, true );
193 BOOST_CHECK_EQUAL( C3_7D, C3_7D );
197 void interval_touches_4_bicremental_types()
199 typedef typename icl::interval<T>::type IntervalT;
204 IntervalT I3_7D = icl::interval<T>::right_open(v3,v7);
205 IntervalT I7_9I = icl::interval<T>::closed(v7,v9);
206 BOOST_CHECK_EQUAL( icl::touches(I3_7D, I7_9I), true );
208 IntervalT I3_7I = icl::interval<T>::closed(v3,v7);
209 IntervalT C7_9I = icl::interval<T>::left_open(v7,v9);
210 BOOST_CHECK_EQUAL( icl::touches(I3_7I, C7_9I), true );
212 BOOST_CHECK_EQUAL( icl::touches(I3_7D, C7_9I), false );
213 BOOST_CHECK_EQUAL( icl::touches(I3_7I, I7_9I), false );
217 void interval_touches_4_integral_types()
219 typedef typename icl::interval<T>::type IntervalT;
225 IntervalT I3_6I = icl::interval<T>::closed(v3,v6);
226 IntervalT I7_9I = icl::interval<T>::closed(v7,v9);
227 BOOST_CHECK_EQUAL( icl::touches(I3_6I, I7_9I), true );
229 IntervalT I3_7D = icl::interval<T>::right_open(v3,v7);
230 IntervalT C6_9I = icl::interval<T>::left_open(v6,v9);
231 BOOST_CHECK_EQUAL( icl::touches(I3_7D, C6_9I), true );
235 void interval_infix_intersect_4_bicremental_types()
237 typedef typename icl::interval<T>::type IntervalT;
240 IntervalT I3_7D = I_D(3,7);
242 IntervalT I0_3D = I_D(0,3);
243 section = I3_7D & I0_3D;
244 BOOST_CHECK_EQUAL( icl::disjoint(I0_3D, I3_7D), true );
245 BOOST_CHECK_EQUAL( icl::is_empty(section), true );
246 BOOST_CHECK_EQUAL( section, IntervalT() );
248 IntervalT I0_5D = I_D(0,5);
249 section = I3_7D & I0_5D;
250 BOOST_CHECK_EQUAL( section, I_D(3,5) );
252 IntervalT I0_9D = I_D(0,9);
253 section = I3_7D & I0_9D;
254 BOOST_CHECK_EQUAL( section, I3_7D );
256 IntervalT I4_5I = I_I(4,5);
257 section = I3_7D & I4_5I;
258 BOOST_CHECK_EQUAL( section, I4_5I );
260 IntervalT C4_6D = C_D(4,6);
261 section = I3_7D & C4_6D;
262 BOOST_CHECK_EQUAL( section, C4_6D );
264 IntervalT C4_9I = C_I(4,9);
265 section = I3_7D & C4_9I;
266 BOOST_CHECK_EQUAL( section, C_D(4,7) );
268 IntervalT I7_9I = I_I(7,9);
269 section = I3_7D & I7_9I;
270 BOOST_CHECK_EQUAL( icl::exclusive_less(I3_7D, I7_9I), true );
271 BOOST_CHECK_EQUAL( icl::disjoint(I3_7D, I7_9I), true );
272 BOOST_CHECK_EQUAL( icl::is_empty(section), true );
276 void interval_subtract_4_bicremental_types()
278 typedef typename icl::interval<T>::type IntervalT;
280 IntervalT diff_1, diff_2;
281 IntervalT I0_3D = I_D(0,3);
282 IntervalT I2_6D = I_D(2,6);
283 IntervalT I4_7D = I_D(4,7);
284 IntervalT I6_7D = I_D(6,7);
285 IntervalT I2_4D = I_D(2,4);
287 diff_1 = right_subtract(I2_6D, I4_7D);
288 BOOST_CHECK_EQUAL( diff_1, I2_4D );
290 diff_1 = right_subtract(I0_3D, I4_7D);
291 BOOST_CHECK_EQUAL( diff_1, I0_3D );
293 // ---------------------------------
294 diff_1 = left_subtract(I4_7D, I2_6D);
295 BOOST_CHECK_EQUAL( diff_1, I6_7D );
297 diff_1 = left_subtract(I4_7D, I0_3D);
298 BOOST_CHECK_EQUAL( diff_1, I4_7D );
302 #endif // LIBS_ICL_TEST_TEST_ICL_interval_shared_hpp_JOFA_100306__