#include "ratio.hpp" #include #define BOOST_TEST_MODULE ratio_test #include BOOST_AUTO_TEST_CASE( ratio_class_test ) { using namespace boost::utility; typedef ratio<5,3> five_thirds; // Sanity check BOOST_MPL_ASSERT_RELATION( (five_thirds::num), ==, 5 ); BOOST_MPL_ASSERT_RELATION( (five_thirds::den), ==, 3 ); typedef ratio<25,15> also_five_thirds; BOOST_MPL_ASSERT_RELATION( (five_thirds::num), ==, (also_five_thirds::num ) ); BOOST_MPL_ASSERT_RELATION( (five_thirds::den), ==, (also_five_thirds::den ) ); typedef ratio<-8,4> minus_two; BOOST_MPL_ASSERT_RELATION( (minus_two::num), ==, -2 ); BOOST_MPL_ASSERT_RELATION( (minus_two::den), ==, 1 ); typedef ratio<64,-8> minus_eight; BOOST_MPL_ASSERT_RELATION( (minus_eight::num), ==, -8 ); BOOST_MPL_ASSERT_RELATION( (minus_eight::den), ==, 1 ); typedef ratio<-9,-8> nine_over_eight; BOOST_MPL_ASSERT_RELATION( (nine_over_eight::num), ==, 9 ); BOOST_MPL_ASSERT_RELATION( (nine_over_eight::den), ==, 8 ); typedef ratio<0,4> zero_over_one; BOOST_MPL_ASSERT_RELATION( (zero_over_one::num), ==, 0 ); BOOST_MPL_ASSERT_RELATION( (zero_over_one::den), ==, 1 ); //typedef ratio<99,0> infinity; // should fail to compile } BOOST_AUTO_TEST_CASE( ratio_arithmetic_test ) { using namespace boost::utility; typedef ratio_add< ratio<4,16>, ratio<9,8> > eleven_over_eight; BOOST_MPL_ASSERT_RELATION( (eleven_over_eight::type::num), ==, 11 ); BOOST_MPL_ASSERT_RELATION( (eleven_over_eight::type::den), ==, 8 ); typedef ratio_subtract< ratio<6>, ratio<3,2> > four_and_a_half; BOOST_MPL_ASSERT_RELATION( (four_and_a_half::type::num), ==, 9 ); BOOST_MPL_ASSERT_RELATION( (four_and_a_half::type::den), ==, 2 ); typedef ratio_multiply< ratio<23,11>, ratio<96> > big_number; BOOST_MPL_ASSERT_RELATION( (big_number::type::num), ==, 2208 ); BOOST_MPL_ASSERT_RELATION( (big_number::type::den), ==, 11 ); typedef ratio_divide< ratio<-7,11>, ratio<-4,-12> > just_above_minus_two; BOOST_MPL_ASSERT_RELATION( (just_above_minus_two::type::num), ==, -21 ); BOOST_MPL_ASSERT_RELATION( (just_above_minus_two::type::den), ==, 11 ); typedef ratio_divide, ratio<25,15> > should_be_one; BOOST_MPL_ASSERT_RELATION( (should_be_one::type::num), ==, 1 ); BOOST_MPL_ASSERT_RELATION( (should_be_one::type::den), ==, 1 ); } BOOST_AUTO_TEST_CASE( ratio_comparison_test ) { using namespace boost::utility; BOOST_MPL_ASSERT(( ratio_equal< ratio<5,3>, ratio<25,15> > )); BOOST_MPL_ASSERT(( ratio_not_equal< ratio<5,4>, ratio<25,15> > )); BOOST_MPL_ASSERT(( ratio_less< ratio<2>, ratio<25,10> > )); BOOST_MPL_ASSERT(( ratio_less_equal< ratio<2>, ratio<25,10> > )); BOOST_MPL_ASSERT(( ratio_less_equal< ratio<5,2>, ratio<25,10> > )); BOOST_MPL_ASSERT(( ratio_greater< ratio<3>, ratio<25,10> > )); BOOST_MPL_ASSERT(( ratio_greater_equal< ratio<5>, ratio<25,10> > )); BOOST_MPL_ASSERT(( ratio_greater_equal< ratio<5,2>, ratio<25,10> > )); }