Calculator  Step 6
test_number.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <istream>
3 #include <ostream>
4 #include <sstream>
5 #include <string>
6 
7 #include <boost/test/auto_unit_test.hpp>
8 
9 #include "calc_error.hpp"
10 #include "number.hpp"
11 
12 BOOST_AUTO_TEST_SUITE(number_test);
13 
15 {
16  number n(1L);
17  BOOST_CHECK_EQUAL("1", n.to_string());
18  number m(2.0);
19  BOOST_CHECK_EQUAL("2", m.to_string());
20  m = n;
21  BOOST_CHECK_EQUAL("1", m.to_string());
22  number x((number(n)));
23  BOOST_CHECK_EQUAL("1", x.to_string());
24 }
25 
26 BOOST_AUTO_TEST_CASE(test_library)
27 {
28  number i(1), f(2.0), v;
29  std::stringstream stream;
30  i.save(stream);
31  f.save(stream);
32  v.save(stream);
33 
34  number n1(stream);
35  number n2(stream);
36  number n3(stream);
37 
38  BOOST_CHECK_EQUAL(number(1), n1);
39  BOOST_CHECK_EQUAL(number(2), n2);
40  BOOST_CHECK_EQUAL("", n3.to_string());
41 }
42 
43 BOOST_AUTO_TEST_CASE(test_compare)
44 {
45  BOOST_CHECK(number(1) < number(2));
46  BOOST_CHECK(number(1) <= number(2));
47  BOOST_CHECK(number(1) != number(2));
48  BOOST_CHECK(number(2) > number(1));
49  BOOST_CHECK(number(2) >= number(1));
50  BOOST_CHECK(number(2) != number(1));
51  BOOST_CHECK(number(1) == number(1));
52  BOOST_CHECK(not (number(1) != number(1)));
53  BOOST_CHECK(not (number(1) > number(2)));
54  BOOST_CHECK(not (number(1) >= number(2)));
55  BOOST_CHECK(not (number(1) == number(2)));
56  BOOST_CHECK(not (number(2) < number(1)));
57  BOOST_CHECK(not (number(2) <= number(1)));
58  BOOST_CHECK(not (number(2) == number(1)));
59 
60  BOOST_CHECK(number(1) < number(1.5));
61  BOOST_CHECK(number(1) <= number(1.5));
62  BOOST_CHECK(number(1) != number(1.5));
63  BOOST_CHECK(number(1.5) > number(1));
64  BOOST_CHECK(number(1.5) >= number(1));
65  BOOST_CHECK(number(1.5) != number(1));
66  BOOST_CHECK(number(1.0) == number(1));
67 }
68 
70 {
71  number a(1.0), b(2), c;
72  c = a + b;
73  BOOST_CHECK_EQUAL(number(3), c);
74 }
75 
76 BOOST_AUTO_TEST_CASE(test_subtract)
77 {
78  number a(2.0), b(1), c;
79  c = a - b;
80  BOOST_CHECK_EQUAL(number(1), c);
81 }
82 
83 BOOST_AUTO_TEST_CASE(test_multiply)
84 {
85  number a(2.0), b(3), c;
86  c = a * b;
87  BOOST_CHECK_EQUAL(number(6), c);
88 }
89 
91 {
92  number a(5), b(2), c;
93  c = a / b;
94  BOOST_CHECK_EQUAL(number(rational<long>(5, 2)), c);
95  a = number(5.0);
96  c = a / b;
97  BOOST_CHECK_EQUAL(number(2.5), c);
98  BOOST_CHECK_THROW(a / number(0), calc_error);
99 }
100 
101 BOOST_AUTO_TEST_CASE(test_rational)
102 {
103  number half(rational<long>(1, 2));
104  number a(half);
105  a = a * number(2);
106  BOOST_CHECK_EQUAL(number(1), a);
107  a = a / number(3);
108  a = a * number(3);
109  BOOST_CHECK_EQUAL(number(1), a);
110  a = a - half;
111  BOOST_CHECK_EQUAL(half, a);
112  a = half + a;
113  BOOST_CHECK_EQUAL(number(1), a);
114 }
115 
std::string to_string() const
Definition: number.cpp:45
BOOST_AUTO_TEST_CASE(test_basics)
Definition: test_node.cpp:14
BOOST_AUTO_TEST_SUITE_END()
Represent a rational number (fraction) as a numerator and denominator.
Definition: rational.hpp:26
void save(std::ostream &stream) const
Definition: number.cpp:39
BOOST_AUTO_TEST_SUITE(node_test)