test_misc.cpp 12 KB

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