Calculator Step 3

- Main Page
- Classes
- Files

- File List
- File Members

test_node.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/unit_test.hpp>
8
9 #include "node.hpp"
10 #include "variables.hpp"
11
12 BOOST_AUTO_TEST_SUITE(node_test);
13
14 BOOST_AUTO_TEST_CASE(test_basics)
15 {
16 node n(1.0);
17 BOOST_CHECK_EQUAL(1.0, n.evaluate());
18 BOOST_CHECK_EQUAL("1", n.to_string());
19 node s("ident");
20 BOOST_CHECK_EQUAL("ident", s.to_string());
21 }
22
23 BOOST_AUTO_TEST_CASE(test_negate)
24 {
25 node n(1.0);
26 node neg('-', n);
27 BOOST_CHECK_EQUAL(-1.0, neg.evaluate());
28 }
29
30 BOOST_AUTO_TEST_CASE(test_add)
31 {
32 node a(1.0), b(2.0), c(3.0);
33 BOOST_CHECK_EQUAL(3.0, node(a, '+', b).evaluate());
34 BOOST_CHECK_EQUAL(6.0, node(node(a, '+', b), '+', c).evaluate());
35 }
36
37 BOOST_AUTO_TEST_CASE(test_subtract)
38 {
39 node a(1.0), b(2.0), c(3.0);
40 BOOST_CHECK_EQUAL(1.0, node(c, '-', b).evaluate());
41 BOOST_CHECK_EQUAL(-4.0, node(node(a, '-', b), '-', c).evaluate());
42 }
43
44 BOOST_AUTO_TEST_CASE(test_multiply)
45 {
46 node a(1.0), b(2.0), c(3.0);
47 BOOST_CHECK_EQUAL(3.0, node(a, '*', c).evaluate());
48 BOOST_CHECK_EQUAL(6.0, node(node(a, '*', b), '*', c).evaluate());
49 }
50
51 BOOST_AUTO_TEST_CASE(test_divide)
52 {
53 node a(1.0), b(2.0), c(6.0);
54 BOOST_CHECK_EQUAL(0.5, node(a, '/', b).evaluate());
55 BOOST_CHECK_EQUAL(3.0, node(node(c, '/', a), '/', b).evaluate());
56 }
57
58 BOOST_AUTO_TEST_CASE(test_mix)
59 {
60 node a(node(1.5), '*', node(2.0));
61 BOOST_CHECK_EQUAL(3.0, a.evaluate());
62
63 node b(node(3.0), '/', node(1.5));
64 BOOST_CHECK_EQUAL(2.0, b.evaluate());
65
66 node c(a, '+', b);
67 BOOST_CHECK_EQUAL(5.0, c.evaluate());
68
69 node d(node(10), '-', node(3));
70 BOOST_CHECK_EQUAL(7.0, d.evaluate());
71
72 node e('-', node(1.0));
73 BOOST_CHECK_EQUAL(-1.0, e.evaluate());
74
75 node f(node(2), '*', e);
76 BOOST_CHECK_EQUAL(-2.0, f.evaluate());
77
78 node g(d, '+', f);
79 BOOST_CHECK_EQUAL(5.0, g.evaluate());
80
81 node h(c, '-', g);
82 BOOST_CHECK_EQUAL(0.0, h.evaluate());
83 }
84
85 BOOST_AUTO_TEST_CASE(test_var)
86 {
87 node n(node(node("half"), node(node(1), '/', node(2))));
88 BOOST_CHECK_EQUAL(0.5, n.evaluate());
89 BOOST_CHECK_EQUAL(0.5, get_variable("half"));
90 }
91
92 BOOST_AUTO_TEST_CASE(test_copy)
93 {
94 node n(node(node(1), '+', node(2)));
95 {
96 node m(n);
97 n = node('-', m);
98 BOOST_CHECK_EQUAL(3.0, m.evaluate());
99 }
100 BOOST_CHECK_EQUAL(-3.0, n.evaluate());
101 }
102
103 BOOST_AUTO_TEST_SUITE_END();
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(test_basics)
Definition: test_node.cpp:14
node
Definition: node.hpp:16
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
node::to_string
std::string to_string() const
Definition: node.cpp:56
node.hpp
node::evaluate
double evaluate() const
Definition: node.cpp:50
get_variable
double get_variable(std::string const &name)
Definition: variables.cpp:18
variables.hpp
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(node_test)