Calculator  Step 4
Functions
test_node.cpp File Reference
#include <iostream>
#include <istream>
#include <ostream>
#include <sstream>
#include <string>
#include <boost/test/auto_unit_test.hpp>
#include "node.hpp"
#include "variables.hpp"

Go to the source code of this file.

Functions

 BOOST_AUTO_TEST_SUITE (node_test)
 
 BOOST_AUTO_TEST_CASE (test_basics)
 
 BOOST_AUTO_TEST_CASE (test_list)
 
 BOOST_AUTO_TEST_CASE (test_negate)
 
 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_mix)
 
 BOOST_AUTO_TEST_CASE (test_var)
 
 BOOST_AUTO_TEST_CASE (test_function_call)
 
 BOOST_AUTO_TEST_CASE (test_copy)
 
 BOOST_AUTO_TEST_SUITE_END ()
 

Function Documentation

BOOST_AUTO_TEST_CASE ( test_basics  )

Definition at line 14 of file test_node.cpp.

References node::evaluate(), and node::to_string().

15 {
16  node n(1.0);
17  BOOST_CHECK_EQUAL(1.0, n.evaluate());
18  BOOST_CHECK_EQUAL("1", n.to_string());
19 }
Definition: node.hpp:26
BOOST_AUTO_TEST_CASE ( test_list  )

Definition at line 21 of file test_node.cpp.

22 {
23  node_list nl;
24  nl.push_back(node(1.0));
25  nl.push_back(node(2.0));
26  nl.push_back(node(nl[0], '*', nl[1]));
27  BOOST_CHECK_EQUAL(3, nl.size());
28  node_list copy(nl);
29  nl.clear();
30  BOOST_CHECK_EQUAL(3, copy.size());
31  BOOST_CHECK_EQUAL("2", copy.at(1).to_string());
32 }
Definition: node.hpp:26
std::vector< node > node_list
A sequence of nodes.
Definition: node.hpp:13
BOOST_AUTO_TEST_CASE ( test_negate  )

Definition at line 34 of file test_node.cpp.

References node::evaluate().

35 {
36  node n(1.0);
37  node neg('-', n);
38  BOOST_CHECK_EQUAL(-1.0, neg.evaluate());
39 }
Definition: node.hpp:26
BOOST_AUTO_TEST_CASE ( test_add  )

Definition at line 41 of file test_node.cpp.

42 {
43  node a(1.0), b(2.0), c(3.0);
44  BOOST_CHECK_EQUAL(3.0, node(a, '+', b).evaluate());
45  BOOST_CHECK_EQUAL(6.0, node(node(a, '+', b), '+', c).evaluate());
46 }
Definition: node.hpp:26
BOOST_AUTO_TEST_CASE ( test_subtract  )

Definition at line 48 of file test_node.cpp.

49 {
50  node a(1.0), b(2.0), c(3.0);
51  BOOST_CHECK_EQUAL(1.0, node(c, '-', b).evaluate());
52  BOOST_CHECK_EQUAL(-4.0, node(node(a, '-', b), '-', c).evaluate());
53 }
Definition: node.hpp:26
BOOST_AUTO_TEST_CASE ( test_multiply  )

Definition at line 55 of file test_node.cpp.

56 {
57  node a(1.0), b(2.0), c(3.0);
58  BOOST_CHECK_EQUAL(3.0, node(a, '*', c).evaluate());
59  BOOST_CHECK_EQUAL(6.0, node(node(a, '*', b), '*', c).evaluate());
60 }
Definition: node.hpp:26
BOOST_AUTO_TEST_CASE ( test_divide  )

Definition at line 62 of file test_node.cpp.

63 {
64  node a(1.0), b(2.0), c(6.0);
65  BOOST_CHECK_EQUAL(0.5, node(a, '/', b).evaluate());
66  BOOST_CHECK_EQUAL(3.0, node(node(c, '/', a), '/', b).evaluate());
67 }
Definition: node.hpp:26
BOOST_AUTO_TEST_CASE ( test_mix  )

Definition at line 69 of file test_node.cpp.

References node::evaluate().

70 {
71  node a(node(1.5), '*', node(2.0));
72  BOOST_CHECK_EQUAL(3.0, a.evaluate());
73 
74  node b(node(3.0), '/', node(1.5));
75  BOOST_CHECK_EQUAL(2.0, b.evaluate());
76 
77  node c(a, '+', b);
78  BOOST_CHECK_EQUAL(5.0, c.evaluate());
79 
80  node d(node(10), '-', node(3));
81  BOOST_CHECK_EQUAL(7.0, d.evaluate());
82 
83  node e('-', node(1.0));
84  BOOST_CHECK_EQUAL(-1.0, e.evaluate());
85 
86  node f(node(2), '*', e);
87  BOOST_CHECK_EQUAL(-2.0, f.evaluate());
88 
89  node g(d, '+', f);
90  BOOST_CHECK_EQUAL(5.0, g.evaluate());
91 
92  node h(c, '-', g);
93  BOOST_CHECK_EQUAL(0.0, h.evaluate());
94 }
Definition: node.hpp:26
BOOST_AUTO_TEST_CASE ( test_var  )

Definition at line 96 of file test_node.cpp.

References get_variable(), and set_variable().

97 {
98  set_variable("half", node(node(1), '/', node(2)));
99  BOOST_CHECK_EQUAL(0.5, get_variable("half").evaluate());
100 }
Definition: node.hpp:26
void set_variable(std::string const &name, node value)
Definition: variables.cpp:56
node get_variable(std::string const &name)
Definition: variables.cpp:45
BOOST_AUTO_TEST_CASE ( test_function_call  )

Definition at line 102 of file test_node.cpp.

References node::evaluate(), and set_function().

103 {
104  identifier_list parms;
105  parms.push_back("a");
106  parms.push_back("b");
107  set_function("test", node(parms, node(node("a"), '+', node("b", node_list()))));
108  node_list args;
109  args.push_back(node(1.0));
110  args.push_back(node(2.0));
111  node call("test", args);
112  BOOST_CHECK_EQUAL(3.0, call.evaluate());
113 }
Definition: node.hpp:26
std::vector< std::string > identifier_list
A sequence of identifiers (e.g., parameter names).
Definition: node.hpp:19
void set_function(std::string const &name, node value)
Definition: variables.cpp:70
std::vector< node > node_list
A sequence of nodes.
Definition: node.hpp:13
BOOST_AUTO_TEST_CASE ( test_copy  )

Definition at line 115 of file test_node.cpp.

References node::evaluate().

116 {
117  node n(node(node(1), '+', node(2)));
118  {
119  node m(n);
120  n = node('-', m);
121  BOOST_CHECK_EQUAL(3.0, m.evaluate());
122  }
123  {
124  node a;
125  a = n;
126  BOOST_CHECK_EQUAL(-3.0, a.evaluate());
127  }
128  BOOST_CHECK_EQUAL(-3.0, n.evaluate());
129 }
Definition: node.hpp:26
double evaluate() const
Definition: node.cpp:57
BOOST_AUTO_TEST_SUITE ( node_test  )
BOOST_AUTO_TEST_SUITE_END ( )