- Main Page
- Classes
- Files

# test_node.cpp

Go to the documentation of this file.

    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 "node.hpp"
    00010 #include "variables.hpp"
    00011
    00012 BOOST_AUTO_TEST_CASE(test_basics)
    00013 {
    00014   node n(1.0);
    00015   BOOST_CHECK_EQUAL(1.0, n.evaluate());
    00016   BOOST_CHECK_EQUAL("1", n.to_string());
    00017   node s("ident");
    00018   BOOST_CHECK_EQUAL("ident", s.to_string());
    00019 }
    00020
    00021 BOOST_AUTO_TEST_CASE(test_negate)
    00022 {
    00023   node n(1.0);
    00024   node neg('-', n);
    00025   BOOST_CHECK_EQUAL(-1.0, neg.evaluate());
    00026 }
    00027
    00028 BOOST_AUTO_TEST_CASE(test_add)
    00029 {
    00030   node a(1.0), b(2.0), c(3.0);
    00031   BOOST_CHECK_EQUAL(3.0, node(a, '+', b).evaluate());
    00032   BOOST_CHECK_EQUAL(6.0, node(node(a, '+', b), '+', c).evaluate());
    00033 }
    00034
    00035 BOOST_AUTO_TEST_CASE(test_subtract)
    00036 {
    00037   node a(1.0), b(2.0), c(3.0);
    00038   BOOST_CHECK_EQUAL(1.0, node(c, '-', b).evaluate());
    00039   BOOST_CHECK_EQUAL(-4.0, node(node(a, '-', b), '-', c).evaluate());
    00040 }
    00041
    00042 BOOST_AUTO_TEST_CASE(test_multiply)
    00043 {
    00044   node a(1.0), b(2.0), c(3.0);
    00045   BOOST_CHECK_EQUAL(3.0, node(a, '*', c).evaluate());
    00046   BOOST_CHECK_EQUAL(6.0, node(node(a, '*', b), '*', c).evaluate());
    00047 }
    00048
    00049 BOOST_AUTO_TEST_CASE(test_divide)
    00050 {
    00051   node a(1.0), b(2.0), c(6.0);
    00052   BOOST_CHECK_EQUAL(0.5, node(a, '/', b).evaluate());
    00053   BOOST_CHECK_EQUAL(3.0, node(node(c, '/', a), '/', b).evaluate());
    00054 }
    00055
    00056 BOOST_AUTO_TEST_CASE(test_mix)
    00057 {
    00058   node a(node(1.5), '*', node(2.0));
    00059   BOOST_CHECK_EQUAL(3.0, a.evaluate());
    00060
    00061   node b(node(3.0), '/', node(1.5));
    00062   BOOST_CHECK_EQUAL(2.0, b.evaluate());
    00063
    00064   node c(a, '+', b);
    00065   BOOST_CHECK_EQUAL(5.0, c.evaluate());
    00066
    00067   node d(node(10), '-', node(3));
    00068   BOOST_CHECK_EQUAL(7.0, d.evaluate());
    00069
    00070   node e('-', node(1.0));
    00071   BOOST_CHECK_EQUAL(-1.0, e.evaluate());
    00072
    00073   node f(node(2), '*', e);
    00074   BOOST_CHECK_EQUAL(-2.0, f.evaluate());
    00075
    00076   node g(d, '+', f);
    00077   BOOST_CHECK_EQUAL(5.0, g.evaluate());
    00078
    00079   node h(c, '-', g);
    00080   BOOST_CHECK_EQUAL(0.0, h.evaluate());
    00081 }
    00082
    00083 BOOST_AUTO_TEST_CASE(test_var)
    00084 {
    00085   node n(node(node("half"), node(node(1), '/', node(2))));
    00086   BOOST_CHECK_EQUAL(0.5, n.evaluate());
    00087   BOOST_CHECK_EQUAL(0.5, get_variable("half"));
    00088 }
    00089
    00090 BOOST_AUTO_TEST_CASE(test_copy)
    00091 {
    00092   node n(node(node(1), '+', node(2)));
    00093   {
    00094     node m(n);
    00095     n = node('-', m);
    00096     BOOST_CHECK_EQUAL(3.0, m.evaluate());
    00097   }
    00098   BOOST_CHECK_EQUAL(-3.0, n.evaluate());
    00099 }
    00100
    00101 BOOST_AUTO_TEST_SUITE_END();