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 } 00018 00019 BOOST_AUTO_TEST_CASE(test_list) 00020 { 00021 node_list nl; 00022 nl.push_back(node(1.0)); 00023 nl.push_back(node(2.0)); 00024 nl.push_back(node(nl[0], '*', nl[1])); 00025 BOOST_CHECK_EQUAL(3, nl.size()); 00026 node_list copy(nl); 00027 nl.clear(); 00028 BOOST_CHECK_EQUAL(3, copy.size()); 00029 BOOST_CHECK_EQUAL("2", copy.at(1).to_string()); 00030 } 00031 00032 BOOST_AUTO_TEST_CASE(test_negate) 00033 { 00034 node n(1.0); 00035 node neg('-', n); 00036 BOOST_CHECK_EQUAL(-1.0, neg.evaluate()); 00037 } 00038 00039 BOOST_AUTO_TEST_CASE(test_add) 00040 { 00041 node a(1.0), b(2.0), c(3.0); 00042 BOOST_CHECK_EQUAL(3.0, node(a, '+', b).evaluate()); 00043 BOOST_CHECK_EQUAL(6.0, node(node(a, '+', b), '+', c).evaluate()); 00044 } 00045 00046 BOOST_AUTO_TEST_CASE(test_subtract) 00047 { 00048 node a(1.0), b(2.0), c(3.0); 00049 BOOST_CHECK_EQUAL(1.0, node(c, '-', b).evaluate()); 00050 BOOST_CHECK_EQUAL(-4.0, node(node(a, '-', b), '-', c).evaluate()); 00051 } 00052 00053 BOOST_AUTO_TEST_CASE(test_multiply) 00054 { 00055 node a(1.0), b(2.0), c(3.0); 00056 BOOST_CHECK_EQUAL(3.0, node(a, '*', c).evaluate()); 00057 BOOST_CHECK_EQUAL(6.0, node(node(a, '*', b), '*', c).evaluate()); 00058 } 00059 00060 BOOST_AUTO_TEST_CASE(test_divide) 00061 { 00062 node a(1.0), b(2.0), c(6.0); 00063 BOOST_CHECK_EQUAL(0.5, node(a, '/', b).evaluate()); 00064 BOOST_CHECK_EQUAL(3.0, node(node(c, '/', a), '/', b).evaluate()); 00065 } 00066 00067 BOOST_AUTO_TEST_CASE(test_mix) 00068 { 00069 node a(node(1.5), '*', node(2.0)); 00070 BOOST_CHECK_EQUAL(3.0, a.evaluate()); 00071 00072 node b(node(3.0), '/', node(1.5)); 00073 BOOST_CHECK_EQUAL(2.0, b.evaluate()); 00074 00075 node c(a, '+', b); 00076 BOOST_CHECK_EQUAL(5.0, c.evaluate()); 00077 00078 node d(node(10), '-', node(3)); 00079 BOOST_CHECK_EQUAL(7.0, d.evaluate()); 00080 00081 node e('-', node(1.0)); 00082 BOOST_CHECK_EQUAL(-1.0, e.evaluate()); 00083 00084 node f(node(2), '*', e); 00085 BOOST_CHECK_EQUAL(-2.0, f.evaluate()); 00086 00087 node g(d, '+', f); 00088 BOOST_CHECK_EQUAL(5.0, g.evaluate()); 00089 00090 node h(c, '-', g); 00091 BOOST_CHECK_EQUAL(0.0, h.evaluate()); 00092 } 00093 00094 BOOST_AUTO_TEST_CASE(test_var) 00095 { 00096 set_variable("half", node(node(1.0), '/', node(2))); 00097 BOOST_CHECK_EQUAL(0.5, get_variable("half").evaluate()); 00098 } 00099 00100 BOOST_AUTO_TEST_CASE(test_function_call) 00101 { 00102 identifier_list parms; 00103 parms.push_back("a"); 00104 parms.push_back("b"); 00105 set_function("test", node(parms, node(node("a"), '+', node("b", node_list())))); 00106 node_list args; 00107 args.push_back(node(1.0)); 00108 args.push_back(node(2.0)); 00109 node call("test", args); 00110 BOOST_CHECK_EQUAL(3.0, call.evaluate()); 00111 } 00112 00113 BOOST_AUTO_TEST_CASE(test_copy) 00114 { 00115 node n(node(node(1), '+', node(2))); 00116 { 00117 node m(n); 00118 n = node('-', m); 00119 BOOST_CHECK_EQUAL(3.0, m.evaluate()); 00120 } 00121 { 00122 node a; 00123 a = n; 00124 BOOST_CHECK_EQUAL(-3.0, a.evaluate()); 00125 } 00126 BOOST_CHECK_EQUAL(-3.0, n.evaluate()); 00127 } 00128 00129 BOOST_AUTO_TEST_SUITE_END();