00001 #include <iostream> 00002 #include <istream> 00003 #include <ostream> 00004 #include <sstream> 00005 #include <string> 00006 00007 #include <boost/test/auto_unit_test.hpp> 00008 00009 #include "calc_error.hpp" 00010 #include "number.hpp" 00011 00012 BOOST_AUTO_TEST_CASE(test_basics) 00013 { 00014 number n(1L); 00015 BOOST_CHECK_EQUAL("1", n.to_string()); 00016 number m(2.0); 00017 BOOST_CHECK_EQUAL("2", m.to_string()); 00018 m = n; 00019 BOOST_CHECK_EQUAL("1", m.to_string()); 00020 number x((number(n))); 00021 BOOST_CHECK_EQUAL("1", x.to_string()); 00022 } 00023 00024 BOOST_AUTO_TEST_CASE(test_library) 00025 { 00026 number i(1), f(2.0), v; 00027 std::stringstream stream; 00028 i.save(stream); 00029 f.save(stream); 00030 v.save(stream); 00031 00032 number n1(stream); 00033 number n2(stream); 00034 number n3(stream); 00035 00036 BOOST_CHECK_EQUAL(number(1), n1); 00037 BOOST_CHECK_EQUAL(number(2), n2); 00038 BOOST_CHECK_EQUAL("", n3.to_string()); 00039 } 00040 00041 BOOST_AUTO_TEST_CASE(test_compare) 00042 { 00043 BOOST_CHECK(number(1) < number(2)); 00044 BOOST_CHECK(number(1) <= number(2)); 00045 BOOST_CHECK(number(1) != number(2)); 00046 BOOST_CHECK(number(2) > number(1)); 00047 BOOST_CHECK(number(2) >= number(1)); 00048 BOOST_CHECK(number(2) != number(1)); 00049 BOOST_CHECK(number(1) == number(1)); 00050 BOOST_CHECK(not (number(1) != number(1))); 00051 BOOST_CHECK(not (number(1) > number(2))); 00052 BOOST_CHECK(not (number(1) >= number(2))); 00053 BOOST_CHECK(not (number(1) == number(2))); 00054 BOOST_CHECK(not (number(2) < number(1))); 00055 BOOST_CHECK(not (number(2) <= number(1))); 00056 BOOST_CHECK(not (number(2) == number(1))); 00057 00058 BOOST_CHECK(number(1) < number(1.5)); 00059 BOOST_CHECK(number(1) <= number(1.5)); 00060 BOOST_CHECK(number(1) != number(1.5)); 00061 BOOST_CHECK(number(1.5) > number(1)); 00062 BOOST_CHECK(number(1.5) >= number(1)); 00063 BOOST_CHECK(number(1.5) != number(1)); 00064 BOOST_CHECK(number(1.0) == number(1)); 00065 } 00066 00067 BOOST_AUTO_TEST_CASE(test_add) 00068 { 00069 number a(1.0), b(2), c; 00070 c = a + b; 00071 BOOST_CHECK_EQUAL(number(3), c); 00072 } 00073 00074 BOOST_AUTO_TEST_CASE(test_subtract) 00075 { 00076 number a(2.0), b(1), c; 00077 c = a - b; 00078 BOOST_CHECK_EQUAL(number(1), c); 00079 } 00080 00081 BOOST_AUTO_TEST_CASE(test_multiply) 00082 { 00083 number a(2.0), b(3), c; 00084 c = a * b; 00085 BOOST_CHECK_EQUAL(number(6), c); 00086 } 00087 00088 BOOST_AUTO_TEST_CASE(test_divide) 00089 { 00090 number a(5), b(2), c; 00091 c = a / b; 00092 BOOST_CHECK_EQUAL(number(rational<long>(5, 2)), c); 00093 a = number(5.0); 00094 c = a / b; 00095 BOOST_CHECK_EQUAL(number(2.5), c); 00096 BOOST_CHECK_THROW(a / number(0), calc_error); 00097 } 00098 00099 BOOST_AUTO_TEST_CASE(test_rational) 00100 { 00101 number half(rational<long>(1, 2)); 00102 number a(half); 00103 a = a * number(2); 00104 BOOST_CHECK_EQUAL(number(1), a); 00105 a = a / number(3); 00106 a = a * number(3); 00107 BOOST_CHECK_EQUAL(number(1), a); 00108 a = a - half; 00109 BOOST_CHECK_EQUAL(half, a); 00110 a = half + a; 00111 BOOST_CHECK_EQUAL(number(1), a); 00112 }