/*-----------------------------------------------------------------------------+ Copyright (c) 2008-2009: Joachim Faulhaber +------------------------------------------------------------------------------+ Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENCE.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +-----------------------------------------------------------------------------*/ #define BOOST_TEST_MODULE icl::misc unit test #define BOOST_ICL_TEST_CHRONO #include #include #include #include #include "../unit_test_unwarned.hpp" // interval instance types #include "../test_type_lists.hpp" #include "../test_value_maker.hpp" #include #include #include #include #include #include "../test_laws.hpp" using namespace std; using namespace boost; using namespace unit_test; using namespace boost::icl; BOOST_AUTO_TEST_CASE(test_law_complementarity) { //LAW Inner complementarity: x + between(x) == hull(x) //LAW: length(x) + length(between(x)) = length(hull(x)) typedef interval_set > RatioSetT; typedef RatioSetT::interval_type IntervalT; typedef RatioSetT::element_type RatT; typedef RatioSetT::difference_type DiffT; RatioSetT set_a; (((set_a += IntervalT(RatT(0), RatT(1) ) ) -= IntervalT(RatT(1,9), RatT(2,9)) ) -= IntervalT(RatT(3,9), RatT(4,9)) ) -= IntervalT(RatT(5,9), RatT(6,9)); RatioSetT between_a = RatioSetT(hull(set_a)) - set_a; RatioSetT between_a2; between(between_a2, set_a); BOOST_CHECK_EQUAL( between_a, between_a2 ); DiffT len_set_a = length(set_a); DiffT len_between_a = length(between_a); //cout << set_a << " length= " << len_set_a << endl; //cout << between_a << " length= " << len_between_a << endl; RatioSetT span_a = set_a + between_a; RatioSetT hull_a = RatioSetT(hull(set_a)); //cout << span_a << endl; BOOST_CHECK_EQUAL( span_a, hull_a ); BOOST_CHECK_EQUAL( len_set_a + len_between_a, length(hull_a) ); BOOST_CHECK((has_inner_complementarity(set_a))); BOOST_CHECK((has_length_complementarity(set_a))); BOOST_CHECK((has_length_as_distance(set_a))); } BOOST_AUTO_TEST_CASE(test_between) { //LAW: between(a,b) == between(b,a); typedef int T; typedef interval::type IntervalT; IntervalT itv_a = I_D(1,3); IntervalT itv_b = I_D(5,7); IntervalT beween_a_b = between(itv_a, itv_b); IntervalT beween_b_a = between(itv_b, itv_a); //cout << beween_a_b << endl; //cout << beween_b_a << endl; BOOST_CHECK_EQUAL( beween_a_b, beween_b_a ); } BOOST_AUTO_TEST_CASE(element_iteration) { interval_map map_a; map_a += make_pair(interval::right_open(0,3),1); //cout << map_a << endl; //for(interval_map::element_iterator elem = elements_begin(map_a); // elem != elements_end(map_a); elem++) // cout << "(" << elem->first << "," << elem->second << ")"; //cout << "\n-------------------------------------\n"; std::pair search_pair(2,1); //interval_map::element_const_iterator found interval_map::element_iterator found = std::find(elements_begin(map_a), elements_end(map_a), search_pair); // cout << "(" << found->first << "," << found->second << ")\n"; BOOST_CHECK_EQUAL( found->first, 2 ); BOOST_CHECK_EQUAL( found->second, 1 ); // Assignment of an associated value via element_iterator const_cast(found->second) = 2; // cout << map_a << endl; BOOST_CHECK_EQUAL( map_a.begin()->second, 2 ); } BOOST_AUTO_TEST_CASE(test_interval_bounds_1) { BOOST_CHECK_EQUAL(left_bracket(interval_bounds::closed()), "["); BOOST_CHECK_EQUAL(left_bracket(interval_bounds::right_open()), "["); BOOST_CHECK_EQUAL(left_bracket(interval_bounds::left_open()), "("); BOOST_CHECK_EQUAL(left_bracket(interval_bounds::open()), "("); BOOST_CHECK_EQUAL(right_bracket(interval_bounds::closed()), "]"); BOOST_CHECK_EQUAL(right_bracket(interval_bounds::right_open()), ")"); BOOST_CHECK_EQUAL(right_bracket(interval_bounds::left_open()), "]"); BOOST_CHECK_EQUAL(right_bracket(interval_bounds::open()), ")"); continuous_interval a_1 = continuous_interval(-5.0, -2.3, interval_bounds::closed()); continuous_interval b_1 = continuous_interval(-2.6, 4.0, interval_bounds::closed()); split_interval_set a, b, a_o_b, b_o_a; a_o_b += a_1; a_o_b += b_1; b_o_a += b_1; b_o_a += a_1; BOOST_CHECK_EQUAL(a_o_b, b_o_a); continuous_interval c_1 = continuous_interval(1.0, 3.0, interval_bounds::closed()); continuous_interval b_2 = right_subtract(b_1, c_1); BOOST_CHECK_EQUAL(b_2.bounds(), interval_bounds::right_open()); BOOST_CHECK_EQUAL(icl::bounds(b_2), interval_bounds::right_open()); continuous_interval L0T = continuous_interval(0.0, 0.0, interval_bounds::closed()); continuous_interval C0T = continuous_interval(0.0, 0.0, interval_bounds::left_open()); continuous_interval L0D = continuous_interval(0.0, 0.0, interval_bounds::right_open()); continuous_interval C0D = continuous_interval(0.0, 0.0, interval_bounds::open()); BOOST_CHECK_EQUAL(icl::is_empty(L0T), false); BOOST_CHECK_EQUAL(icl::is_empty(C0T), true); BOOST_CHECK_EQUAL(icl::is_empty(L0D), true); BOOST_CHECK_EQUAL(icl::is_empty(C0D), true); continuous_interval L0_1T = continuous_interval(0.0, 1.0, interval_bounds::closed()); continuous_interval L1_2T = continuous_interval(1.0, 2.0, interval_bounds::closed()); BOOST_CHECK_EQUAL(icl::exclusive_less(L0_1T, L1_2T), false); BOOST_CHECK_EQUAL(icl::inner_bounds(L0_1T, L1_2T) == interval_bounds::open(), true); continuous_interval L0_1D = continuous_interval(0.0, 1.0, interval_bounds::right_open()); BOOST_CHECK_EQUAL(icl::exclusive_less(L0_1D, L1_2T), true); BOOST_CHECK_EQUAL(icl::inner_bounds(L0_1D, L1_2T) == interval_bounds::right_open(), true); continuous_interval C1_2T = continuous_interval(1.0, 2.0, interval_bounds::left_open()); BOOST_CHECK_EQUAL(icl::exclusive_less(L0_1T, C1_2T), true); BOOST_CHECK_EQUAL(icl::inner_bounds(L0_1T, C1_2T) == interval_bounds::left_open(), true); BOOST_CHECK_EQUAL(icl::exclusive_less(L0_1D, C1_2T), true); BOOST_CHECK_EQUAL(icl::inner_bounds(L0_1D, C1_2T) == interval_bounds::closed(), true); BOOST_CHECK_EQUAL(static_cast(icl::right(L0_1T.bounds()).bits()), 1); BOOST_CHECK_EQUAL(static_cast(icl::right(L0_1D.bounds()).bits()), 0); BOOST_CHECK_EQUAL(icl::right_bounds(L0_1D, L0_1T), interval_bounds::left_open()); } BOOST_AUTO_TEST_CASE(test_infinities) { BOOST_CHECK(( has_std_infinity::value)); BOOST_CHECK((!has_std_infinity::value)); BOOST_CHECK(( has_max_infinity::value )); BOOST_CHECK((!has_max_infinity::value )); //-------------------------------------------------------------------------- BOOST_CHECK_EQUAL( numeric_infinity::value(), (std::numeric_limits::infinity)() ); BOOST_CHECK_EQUAL( numeric_infinity::value(), (std::numeric_limits::max)() ); BOOST_CHECK_EQUAL( numeric_infinity::value(), std::string() ); //-------------------------------------------------------------------------- BOOST_CHECK_EQUAL( icl::infinity::value(), (std::numeric_limits::infinity)() ); BOOST_CHECK_EQUAL( icl::infinity::value(), (std::numeric_limits::max)() ); BOOST_CHECK_EQUAL( icl::infinity::value(), icl::identity_element::value() ); //-------------------------------------------------------------------------- BOOST_CHECK_EQUAL( icl::infinity >::value() , boost::chrono::duration((std::numeric_limits::infinity)()) ); BOOST_CHECK_EQUAL( icl::infinity >::value() , boost::chrono::duration((std::numeric_limits::max)()) ); } BOOST_AUTO_TEST_CASE(test_difference_types) { BOOST_CHECK(( boost::is_same< int, difference_type_of::type >::value )); BOOST_CHECK(( boost::is_same< double, difference_type_of::type >::value )); BOOST_CHECK(( boost::is_same< std::ptrdiff_t, difference_type_of::type >::value )); BOOST_CHECK(( has_difference_type::value )); BOOST_CHECK(( boost::is_same< std::string::difference_type, difference_type_of::type >::value )); BOOST_CHECK(( boost::is_same< std::ptrdiff_t, difference_type_of::type >::value )); BOOST_CHECK(( boost::is_same< boost::chrono::duration , difference_type_of >::type >::value )); BOOST_CHECK(( boost::is_same< boost::chrono::duration , difference_type_of >::type >::value )); BOOST_CHECK(( boost::is_same< Now::time_point::duration , difference_type_of::type >::value )); typedef boost::chrono::time_point > contin_timeT; BOOST_CHECK(( boost::is_same< contin_timeT::duration , difference_type_of::type >::value )); typedef boost::chrono::time_point > discr_timeT; BOOST_CHECK(( boost::is_same< boost::chrono::duration , difference_type_of::type >::value )); } BOOST_AUTO_TEST_CASE(test_size_types) { BOOST_CHECK(( boost::is_same< std::size_t, size_type_of::type >::value )); BOOST_CHECK(( boost::is_same< std::size_t, size_type_of::type >::value )); BOOST_CHECK(( boost::is_same< std::size_t, size_type_of::type >::value )); BOOST_CHECK(( boost::is_same< std::size_t, size_type_of::type >::value )); BOOST_CHECK(( boost::is_same< boost::chrono::duration , size_type_of >::type >::value )); BOOST_CHECK(( boost::is_same< boost::chrono::duration , size_type_of >::type >::value )); typedef boost::chrono::time_point > discr_timeT; BOOST_CHECK(( boost::is_same< boost::chrono::duration , size_type_of::type >::value )); typedef boost::chrono::time_point > contin_timeT; BOOST_CHECK(( boost::is_same< contin_timeT::duration , size_type_of::type >::value )); } BOOST_AUTO_TEST_CASE(test_chrono_identity_elements) { //boost::chrono::duration idel_i = icl::identity_element >::value(); //cout << "dur0 = " << idel_i << endl; //boost::chrono::duration idel_d = icl::identity_element >::value(); //cout << "dur0 = " << idel_d << endl; BOOST_CHECK(( boost::is_same< boost::chrono::duration , size_type_of >::type >::value )); }