123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- /*-----------------------------------------------------------------------------+
- 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 <libs/icl/test/disable_test_warnings.hpp>
- #include <string>
- #include <vector>
- #include <boost/mpl/list.hpp>
- #include "../unit_test_unwarned.hpp"
- // interval instance types
- #include "../test_type_lists.hpp"
- #include "../test_value_maker.hpp"
- #include <boost/type_traits/is_same.hpp>
- #include <boost/icl/rational.hpp>
- #include <boost/icl/detail/interval_morphism.hpp>
- #include <boost/icl/interval_map.hpp>
- #include <boost/icl/split_interval_set.hpp>
- #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<rational<int> > 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<RatioSetT,RatioSetT>(set_a)));
- BOOST_CHECK((has_length_complementarity(set_a)));
- BOOST_CHECK((has_length_as_distance<RatioSetT,RatioSetT>(set_a)));
- }
- BOOST_AUTO_TEST_CASE(test_between)
- {
- //LAW: between(a,b) == between(b,a);
- typedef int T;
- typedef interval<T>::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<int,int> map_a;
- map_a += make_pair(interval<int>::right_open(0,3),1);
- //cout << map_a << endl;
- //for(interval_map<int,int>::element_iterator elem = elements_begin(map_a);
- // elem != elements_end(map_a); elem++)
- // cout << "(" << elem->first << "," << elem->second << ")";
- //cout << "\n-------------------------------------\n";
- std::pair<const int, int> search_pair(2,1);
- //interval_map<int,int>::element_const_iterator found
- interval_map<int,int>::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<int&>(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<double> a_1 = continuous_interval<double>(-5.0, -2.3, interval_bounds::closed());
- continuous_interval<double> b_1 = continuous_interval<double>(-2.6, 4.0, interval_bounds::closed());
- split_interval_set<double> 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<double> c_1 = continuous_interval<double>(1.0, 3.0, interval_bounds::closed());
- continuous_interval<double> 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<double> L0T = continuous_interval<double>(0.0, 0.0, interval_bounds::closed());
- continuous_interval<double> C0T = continuous_interval<double>(0.0, 0.0, interval_bounds::left_open());
- continuous_interval<double> L0D = continuous_interval<double>(0.0, 0.0, interval_bounds::right_open());
- continuous_interval<double> C0D = continuous_interval<double>(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<double> L0_1T = continuous_interval<double>(0.0, 1.0, interval_bounds::closed());
- continuous_interval<double> L1_2T = continuous_interval<double>(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<double> L0_1D = continuous_interval<double>(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<double> C1_2T = continuous_interval<double>(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<int>(icl::right(L0_1T.bounds()).bits()), 1);
- BOOST_CHECK_EQUAL(static_cast<int>(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<double>::value));
- BOOST_CHECK((!has_std_infinity<int>::value));
- BOOST_CHECK(( has_max_infinity<int>::value ));
- BOOST_CHECK((!has_max_infinity<double>::value ));
- //--------------------------------------------------------------------------
- BOOST_CHECK_EQUAL( numeric_infinity<double>::value(), (std::numeric_limits<double>::infinity)() );
- BOOST_CHECK_EQUAL( numeric_infinity<int>::value(), (std::numeric_limits<int>::max)() );
- BOOST_CHECK_EQUAL( numeric_infinity<std::string>::value(), std::string() );
- //--------------------------------------------------------------------------
- BOOST_CHECK_EQUAL( icl::infinity<double>::value(), (std::numeric_limits<double>::infinity)() );
- BOOST_CHECK_EQUAL( icl::infinity<int>::value(), (std::numeric_limits<int>::max)() );
- BOOST_CHECK_EQUAL( icl::infinity<std::string>::value(), icl::identity_element<std::string>::value() );
- //--------------------------------------------------------------------------
- BOOST_CHECK_EQUAL( icl::infinity<boost::chrono::duration<double> >::value()
- , boost::chrono::duration<double>((std::numeric_limits<double>::infinity)()) );
- BOOST_CHECK_EQUAL( icl::infinity<boost::chrono::duration<int> >::value()
- , boost::chrono::duration<int>((std::numeric_limits<int>::max)()) );
- }
- BOOST_AUTO_TEST_CASE(test_difference_types)
- {
- BOOST_CHECK(( boost::is_same< int, difference_type_of<int>::type >::value ));
- BOOST_CHECK(( boost::is_same< double, difference_type_of<double>::type >::value ));
- BOOST_CHECK(( boost::is_same< std::ptrdiff_t, difference_type_of<int*>::type >::value ));
- BOOST_CHECK(( has_difference_type<std::string>::value ));
- BOOST_CHECK(( boost::is_same< std::string::difference_type, difference_type_of<std::string>::type >::value ));
- BOOST_CHECK(( boost::is_same< std::ptrdiff_t, difference_type_of<std::string>::type >::value ));
- BOOST_CHECK(( boost::is_same< boost::chrono::duration<int>
- , difference_type_of<boost::chrono::duration<int> >::type >::value ));
- BOOST_CHECK(( boost::is_same< boost::chrono::duration<double>
- , difference_type_of<boost::chrono::duration<double> >::type >::value ));
- BOOST_CHECK(( boost::is_same< Now::time_point::duration
- , difference_type_of<Now::time_point>::type >::value ));
- typedef boost::chrono::time_point<Now, boost::chrono::duration<double> > contin_timeT;
- BOOST_CHECK(( boost::is_same< contin_timeT::duration
- , difference_type_of<contin_timeT>::type >::value ));
- typedef boost::chrono::time_point<Now, boost::chrono::duration<int> > discr_timeT;
- BOOST_CHECK(( boost::is_same< boost::chrono::duration<int>
- , difference_type_of<discr_timeT>::type >::value ));
- }
- BOOST_AUTO_TEST_CASE(test_size_types)
- {
- BOOST_CHECK(( boost::is_same< std::size_t, size_type_of<int>::type >::value ));
- BOOST_CHECK(( boost::is_same< std::size_t, size_type_of<double>::type >::value ));
- BOOST_CHECK(( boost::is_same< std::size_t, size_type_of<int*>::type >::value ));
- BOOST_CHECK(( boost::is_same< std::size_t, size_type_of<std::string>::type >::value ));
- BOOST_CHECK(( boost::is_same< boost::chrono::duration<int>
- , size_type_of<boost::chrono::duration<int> >::type >::value ));
- BOOST_CHECK(( boost::is_same< boost::chrono::duration<double>
- , size_type_of<boost::chrono::duration<double> >::type >::value ));
- typedef boost::chrono::time_point<Now, boost::chrono::duration<int> > discr_timeT;
- BOOST_CHECK(( boost::is_same< boost::chrono::duration<int>
- , size_type_of<discr_timeT>::type >::value ));
- typedef boost::chrono::time_point<Now, boost::chrono::duration<double> > contin_timeT;
- BOOST_CHECK(( boost::is_same< contin_timeT::duration
- , size_type_of<contin_timeT>::type >::value ));
- }
- BOOST_AUTO_TEST_CASE(test_chrono_identity_elements)
- {
- //boost::chrono::duration<int> idel_i = icl::identity_element<boost::chrono::duration<int> >::value();
- //cout << "dur<int>0 = " << idel_i << endl;
- //boost::chrono::duration<double> idel_d = icl::identity_element<boost::chrono::duration<int> >::value();
- //cout << "dur<dbl>0 = " << idel_d << endl;
- BOOST_CHECK(( boost::is_same< boost::chrono::duration<int>
- , size_type_of<boost::chrono::duration<int> >::type >::value ));
- }
|