Calculator  Step 6
Functions
test_number.cpp File Reference
#include <iostream>
#include <istream>
#include <ostream>
#include <sstream>
#include <string>
#include <boost/test/auto_unit_test.hpp>
#include "calc_error.hpp"
#include "number.hpp"

Go to the source code of this file.

Functions

 BOOST_AUTO_TEST_SUITE (number_test)
 
 BOOST_AUTO_TEST_CASE (test_basics)
 
 BOOST_AUTO_TEST_CASE (test_library)
 
 BOOST_AUTO_TEST_CASE (test_compare)
 
 BOOST_AUTO_TEST_CASE (test_add)
 
 BOOST_AUTO_TEST_CASE (test_subtract)
 
 BOOST_AUTO_TEST_CASE (test_multiply)
 
 BOOST_AUTO_TEST_CASE (test_divide)
 
 BOOST_AUTO_TEST_CASE (test_rational)
 
 BOOST_AUTO_TEST_SUITE_END ()
 

Function Documentation

BOOST_AUTO_TEST_CASE ( test_basics  )

Definition at line 14 of file test_number.cpp.

References number::to_string().

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 }
BOOST_AUTO_TEST_CASE ( test_library  )

Definition at line 26 of file test_number.cpp.

References number::save(), and number::to_string().

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 }
void save(std::ostream &stream) const
Definition: number.cpp:39
BOOST_AUTO_TEST_CASE ( test_compare  )

Definition at line 43 of file test_number.cpp.

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 }
BOOST_AUTO_TEST_CASE ( test_add  )

Definition at line 69 of file test_number.cpp.

70 {
71  number a(1.0), b(2), c;
72  c = a + b;
73  BOOST_CHECK_EQUAL(number(3), c);
74 }
BOOST_AUTO_TEST_CASE ( test_subtract  )

Definition at line 76 of file test_number.cpp.

77 {
78  number a(2.0), b(1), c;
79  c = a - b;
80  BOOST_CHECK_EQUAL(number(1), c);
81 }
BOOST_AUTO_TEST_CASE ( test_multiply  )

Definition at line 83 of file test_number.cpp.

84 {
85  number a(2.0), b(3), c;
86  c = a * b;
87  BOOST_CHECK_EQUAL(number(6), c);
88 }
BOOST_AUTO_TEST_CASE ( test_divide  )

Definition at line 90 of file test_number.cpp.

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 }
Represent a rational number (fraction) as a numerator and denominator.
Definition: rational.hpp:26
BOOST_AUTO_TEST_CASE ( test_rational  )

Definition at line 101 of file test_number.cpp.

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 }
Represent a rational number (fraction) as a numerator and denominator.
Definition: rational.hpp:26
BOOST_AUTO_TEST_SUITE ( number_test  )
BOOST_AUTO_TEST_SUITE_END ( )