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