]>
Commit | Line | Data |
---|---|---|
92f5a8d4 | 1 | /*-----------------------------------------------------------------------------+ |
7c673cae FG |
2 | Copyright (c) 2008-2009: 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 | #define BOOST_TEST_MODULE icl::misc unit test | |
9 | ||
10 | #define BOOST_ICL_TEST_CHRONO | |
11 | ||
12 | #include <libs/icl/test/disable_test_warnings.hpp> | |
13 | #include <string> | |
14 | #include <vector> | |
15 | #include <boost/mpl/list.hpp> | |
16 | #include "../unit_test_unwarned.hpp" | |
17 | ||
18 | ||
19 | // interval instance types | |
20 | #include "../test_type_lists.hpp" | |
21 | #include "../test_value_maker.hpp" | |
22 | ||
23 | #include <boost/type_traits/is_same.hpp> | |
24 | #include <boost/icl/rational.hpp> | |
25 | ||
26 | #include <boost/icl/detail/interval_morphism.hpp> | |
27 | #include <boost/icl/interval_map.hpp> | |
28 | #include <boost/icl/split_interval_set.hpp> | |
29 | #include "../test_laws.hpp" | |
30 | ||
31 | using namespace std; | |
32 | using namespace boost; | |
33 | using namespace unit_test; | |
34 | using namespace boost::icl; | |
35 | ||
36 | ||
37 | BOOST_AUTO_TEST_CASE(test_law_complementarity) | |
38 | { | |
39 | //LAW Inner complementarity: x + between(x) == hull(x) | |
40 | //LAW: length(x) + length(between(x)) = length(hull(x)) | |
7c673cae FG |
41 | typedef interval_set<rational<int> > RatioSetT; |
42 | typedef RatioSetT::interval_type IntervalT; | |
43 | typedef RatioSetT::element_type RatT; | |
44 | typedef RatioSetT::difference_type DiffT; | |
45 | ||
46 | RatioSetT set_a; | |
47 | (((set_a += IntervalT(RatT(0), RatT(1) ) ) | |
48 | -= IntervalT(RatT(1,9), RatT(2,9)) ) | |
49 | -= IntervalT(RatT(3,9), RatT(4,9)) ) | |
50 | -= IntervalT(RatT(5,9), RatT(6,9)); | |
51 | ||
52 | RatioSetT between_a = RatioSetT(hull(set_a)) - set_a; | |
92f5a8d4 | 53 | RatioSetT between_a2; |
7c673cae FG |
54 | between(between_a2, set_a); |
55 | BOOST_CHECK_EQUAL( between_a, between_a2 ); | |
56 | ||
57 | DiffT len_set_a = length(set_a); | |
58 | DiffT len_between_a = length(between_a); | |
59 | ||
60 | //cout << set_a << " length= " << len_set_a << endl; | |
61 | //cout << between_a << " length= " << len_between_a << endl; | |
62 | ||
63 | RatioSetT span_a = set_a + between_a; | |
64 | RatioSetT hull_a = RatioSetT(hull(set_a)); | |
65 | //cout << span_a << endl; | |
66 | ||
67 | BOOST_CHECK_EQUAL( span_a, hull_a ); | |
68 | BOOST_CHECK_EQUAL( len_set_a + len_between_a, length(hull_a) ); | |
69 | ||
70 | BOOST_CHECK((has_inner_complementarity<RatioSetT,RatioSetT>(set_a))); | |
71 | BOOST_CHECK((has_length_complementarity(set_a))); | |
72 | BOOST_CHECK((has_length_as_distance<RatioSetT,RatioSetT>(set_a))); | |
73 | } | |
74 | ||
75 | ||
76 | BOOST_AUTO_TEST_CASE(test_between) | |
77 | { | |
78 | //LAW: between(a,b) == between(b,a); | |
79 | typedef int T; | |
80 | typedef interval<T>::type IntervalT; | |
7c673cae FG |
81 | |
82 | IntervalT itv_a = I_D(1,3); | |
83 | IntervalT itv_b = I_D(5,7); | |
84 | ||
85 | IntervalT beween_a_b = between(itv_a, itv_b); | |
86 | IntervalT beween_b_a = between(itv_b, itv_a); | |
87 | ||
88 | //cout << beween_a_b << endl; | |
89 | //cout << beween_b_a << endl; | |
90 | BOOST_CHECK_EQUAL( beween_a_b, beween_b_a ); | |
91 | } | |
92 | ||
93 | ||
94 | BOOST_AUTO_TEST_CASE(element_iteration) | |
95 | { | |
96 | interval_map<int,int> map_a; | |
97 | map_a += make_pair(interval<int>::right_open(0,3),1); | |
98 | //cout << map_a << endl; | |
99 | ||
100 | //for(interval_map<int,int>::element_iterator elem = elements_begin(map_a); | |
101 | // elem != elements_end(map_a); elem++) | |
102 | // cout << "(" << elem->first << "," << elem->second << ")"; | |
103 | //cout << "\n-------------------------------------\n"; | |
104 | ||
105 | std::pair<const int, int> search_pair(2,1); | |
106 | ||
92f5a8d4 TL |
107 | //interval_map<int,int>::element_const_iterator found |
108 | interval_map<int,int>::element_iterator found | |
7c673cae FG |
109 | = std::find(elements_begin(map_a), elements_end(map_a), search_pair); |
110 | // cout << "(" << found->first << "," << found->second << ")\n"; | |
111 | BOOST_CHECK_EQUAL( found->first, 2 ); | |
112 | BOOST_CHECK_EQUAL( found->second, 1 ); | |
113 | ||
114 | // Assignment of an associated value via element_iterator | |
115 | const_cast<int&>(found->second) = 2; | |
116 | // cout << map_a << endl; | |
117 | BOOST_CHECK_EQUAL( map_a.begin()->second, 2 ); | |
118 | } | |
119 | ||
120 | ||
121 | BOOST_AUTO_TEST_CASE(test_interval_bounds_1) | |
122 | { | |
123 | BOOST_CHECK_EQUAL(left_bracket(interval_bounds::closed()), "["); | |
124 | BOOST_CHECK_EQUAL(left_bracket(interval_bounds::right_open()), "["); | |
125 | BOOST_CHECK_EQUAL(left_bracket(interval_bounds::left_open()), "("); | |
126 | BOOST_CHECK_EQUAL(left_bracket(interval_bounds::open()), "("); | |
127 | BOOST_CHECK_EQUAL(right_bracket(interval_bounds::closed()), "]"); | |
128 | BOOST_CHECK_EQUAL(right_bracket(interval_bounds::right_open()), ")"); | |
129 | BOOST_CHECK_EQUAL(right_bracket(interval_bounds::left_open()), "]"); | |
130 | BOOST_CHECK_EQUAL(right_bracket(interval_bounds::open()), ")"); | |
131 | ||
132 | continuous_interval<double> a_1 = continuous_interval<double>(-5.0, -2.3, interval_bounds::closed()); | |
133 | continuous_interval<double> b_1 = continuous_interval<double>(-2.6, 4.0, interval_bounds::closed()); | |
134 | ||
135 | split_interval_set<double> a, b, a_o_b, b_o_a; | |
136 | a_o_b += a_1; | |
137 | a_o_b += b_1; | |
138 | ||
139 | b_o_a += b_1; | |
140 | b_o_a += a_1; | |
141 | ||
142 | BOOST_CHECK_EQUAL(a_o_b, b_o_a); | |
143 | ||
144 | continuous_interval<double> c_1 = continuous_interval<double>(1.0, 3.0, interval_bounds::closed()); | |
145 | continuous_interval<double> b_2 = right_subtract(b_1, c_1); | |
146 | ||
147 | BOOST_CHECK_EQUAL(b_2.bounds(), interval_bounds::right_open()); | |
148 | BOOST_CHECK_EQUAL(icl::bounds(b_2), interval_bounds::right_open()); | |
149 | ||
150 | continuous_interval<double> L0T = continuous_interval<double>(0.0, 0.0, interval_bounds::closed()); | |
151 | continuous_interval<double> C0T = continuous_interval<double>(0.0, 0.0, interval_bounds::left_open()); | |
152 | continuous_interval<double> L0D = continuous_interval<double>(0.0, 0.0, interval_bounds::right_open()); | |
153 | continuous_interval<double> C0D = continuous_interval<double>(0.0, 0.0, interval_bounds::open()); | |
154 | ||
155 | BOOST_CHECK_EQUAL(icl::is_empty(L0T), false); | |
156 | BOOST_CHECK_EQUAL(icl::is_empty(C0T), true); | |
157 | BOOST_CHECK_EQUAL(icl::is_empty(L0D), true); | |
158 | BOOST_CHECK_EQUAL(icl::is_empty(C0D), true); | |
159 | ||
160 | ||
161 | continuous_interval<double> L0_1T = continuous_interval<double>(0.0, 1.0, interval_bounds::closed()); | |
162 | continuous_interval<double> L1_2T = continuous_interval<double>(1.0, 2.0, interval_bounds::closed()); | |
163 | BOOST_CHECK_EQUAL(icl::exclusive_less(L0_1T, L1_2T), false); | |
164 | BOOST_CHECK_EQUAL(icl::inner_bounds(L0_1T, L1_2T) == interval_bounds::open(), true); | |
165 | ||
166 | continuous_interval<double> L0_1D = continuous_interval<double>(0.0, 1.0, interval_bounds::right_open()); | |
167 | BOOST_CHECK_EQUAL(icl::exclusive_less(L0_1D, L1_2T), true); | |
168 | BOOST_CHECK_EQUAL(icl::inner_bounds(L0_1D, L1_2T) == interval_bounds::right_open(), true); | |
169 | ||
170 | continuous_interval<double> C1_2T = continuous_interval<double>(1.0, 2.0, interval_bounds::left_open()); | |
171 | BOOST_CHECK_EQUAL(icl::exclusive_less(L0_1T, C1_2T), true); | |
172 | BOOST_CHECK_EQUAL(icl::inner_bounds(L0_1T, C1_2T) == interval_bounds::left_open(), true); | |
173 | ||
174 | BOOST_CHECK_EQUAL(icl::exclusive_less(L0_1D, C1_2T), true); | |
175 | BOOST_CHECK_EQUAL(icl::inner_bounds(L0_1D, C1_2T) == interval_bounds::closed(), true); | |
176 | ||
177 | BOOST_CHECK_EQUAL(static_cast<int>(icl::right(L0_1T.bounds()).bits()), 1); | |
178 | BOOST_CHECK_EQUAL(static_cast<int>(icl::right(L0_1D.bounds()).bits()), 0); | |
179 | ||
180 | BOOST_CHECK_EQUAL(icl::right_bounds(L0_1D, L0_1T), interval_bounds::left_open()); | |
181 | } | |
182 | ||
183 | ||
184 | BOOST_AUTO_TEST_CASE(test_infinities) | |
185 | { | |
186 | BOOST_CHECK(( has_std_infinity<double>::value)); | |
187 | BOOST_CHECK((!has_std_infinity<int>::value)); | |
188 | ||
189 | BOOST_CHECK(( has_max_infinity<int>::value )); | |
190 | BOOST_CHECK((!has_max_infinity<double>::value )); | |
191 | ||
192 | //-------------------------------------------------------------------------- | |
193 | BOOST_CHECK_EQUAL( numeric_infinity<double>::value(), (std::numeric_limits<double>::infinity)() ); | |
194 | BOOST_CHECK_EQUAL( numeric_infinity<int>::value(), (std::numeric_limits<int>::max)() ); | |
195 | BOOST_CHECK_EQUAL( numeric_infinity<std::string>::value(), std::string() ); | |
196 | ||
197 | //-------------------------------------------------------------------------- | |
198 | BOOST_CHECK_EQUAL( icl::infinity<double>::value(), (std::numeric_limits<double>::infinity)() ); | |
199 | BOOST_CHECK_EQUAL( icl::infinity<int>::value(), (std::numeric_limits<int>::max)() ); | |
200 | BOOST_CHECK_EQUAL( icl::infinity<std::string>::value(), icl::identity_element<std::string>::value() ); | |
201 | ||
202 | //-------------------------------------------------------------------------- | |
203 | BOOST_CHECK_EQUAL( icl::infinity<boost::chrono::duration<double> >::value() | |
204 | , boost::chrono::duration<double>((std::numeric_limits<double>::infinity)()) ); | |
205 | BOOST_CHECK_EQUAL( icl::infinity<boost::chrono::duration<int> >::value() | |
206 | , boost::chrono::duration<int>((std::numeric_limits<int>::max)()) ); | |
207 | } | |
208 | ||
209 | ||
210 | BOOST_AUTO_TEST_CASE(test_difference_types) | |
211 | { | |
212 | BOOST_CHECK(( boost::is_same< int, difference_type_of<int>::type >::value )); | |
213 | BOOST_CHECK(( boost::is_same< double, difference_type_of<double>::type >::value )); | |
214 | BOOST_CHECK(( boost::is_same< std::ptrdiff_t, difference_type_of<int*>::type >::value )); | |
215 | ||
216 | BOOST_CHECK(( has_difference_type<std::string>::value )); | |
217 | BOOST_CHECK(( boost::is_same< std::string::difference_type, difference_type_of<std::string>::type >::value )); | |
218 | BOOST_CHECK(( boost::is_same< std::ptrdiff_t, difference_type_of<std::string>::type >::value )); | |
219 | ||
220 | BOOST_CHECK(( boost::is_same< boost::chrono::duration<int> | |
221 | , difference_type_of<boost::chrono::duration<int> >::type >::value )); | |
222 | BOOST_CHECK(( boost::is_same< boost::chrono::duration<double> | |
223 | , difference_type_of<boost::chrono::duration<double> >::type >::value )); | |
224 | ||
225 | BOOST_CHECK(( boost::is_same< Now::time_point::duration | |
226 | , difference_type_of<Now::time_point>::type >::value )); | |
227 | ||
228 | typedef boost::chrono::time_point<Now, boost::chrono::duration<double> > contin_timeT; | |
229 | BOOST_CHECK(( boost::is_same< contin_timeT::duration | |
230 | , difference_type_of<contin_timeT>::type >::value )); | |
231 | ||
232 | typedef boost::chrono::time_point<Now, boost::chrono::duration<int> > discr_timeT; | |
233 | BOOST_CHECK(( boost::is_same< boost::chrono::duration<int> | |
234 | , difference_type_of<discr_timeT>::type >::value )); | |
235 | } | |
236 | ||
237 | BOOST_AUTO_TEST_CASE(test_size_types) | |
238 | { | |
239 | BOOST_CHECK(( boost::is_same< std::size_t, size_type_of<int>::type >::value )); | |
240 | BOOST_CHECK(( boost::is_same< std::size_t, size_type_of<double>::type >::value )); | |
241 | BOOST_CHECK(( boost::is_same< std::size_t, size_type_of<int*>::type >::value )); | |
242 | BOOST_CHECK(( boost::is_same< std::size_t, size_type_of<std::string>::type >::value )); | |
243 | BOOST_CHECK(( boost::is_same< boost::chrono::duration<int> | |
244 | , size_type_of<boost::chrono::duration<int> >::type >::value )); | |
245 | BOOST_CHECK(( boost::is_same< boost::chrono::duration<double> | |
246 | , size_type_of<boost::chrono::duration<double> >::type >::value )); | |
247 | ||
248 | typedef boost::chrono::time_point<Now, boost::chrono::duration<int> > discr_timeT; | |
249 | BOOST_CHECK(( boost::is_same< boost::chrono::duration<int> | |
250 | , size_type_of<discr_timeT>::type >::value )); | |
251 | ||
252 | typedef boost::chrono::time_point<Now, boost::chrono::duration<double> > contin_timeT; | |
253 | BOOST_CHECK(( boost::is_same< contin_timeT::duration | |
254 | , size_type_of<contin_timeT>::type >::value )); | |
255 | } | |
256 | ||
257 | BOOST_AUTO_TEST_CASE(test_chrono_identity_elements) | |
258 | { | |
259 | //boost::chrono::duration<int> idel_i = icl::identity_element<boost::chrono::duration<int> >::value(); | |
260 | //cout << "dur<int>0 = " << idel_i << endl; | |
261 | //boost::chrono::duration<double> idel_d = icl::identity_element<boost::chrono::duration<int> >::value(); | |
262 | //cout << "dur<dbl>0 = " << idel_d << endl; | |
263 | ||
264 | BOOST_CHECK(( boost::is_same< boost::chrono::duration<int> | |
265 | , size_type_of<boost::chrono::duration<int> >::type >::value )); | |
266 | } |