test_parse.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 "parse.hpp"
00010 
00011 BOOST_AUTO_TEST_CASE(test_number)
00012 {
00013   std::istringstream in("1\n3.14159\n1.23e45\n45.67e+8\n");
00014   std::ostringstream out;
00015   parse_loop(in, out);
00016   BOOST_CHECK_EQUAL("> 1\n> 3.14159\n> 1.23e+45\n> 4.567e+09\n> ", out.str());
00017 }
00018 
00019 BOOST_AUTO_TEST_CASE(test_negative)
00020 {
00021   std::istringstream in("-1\n-3.14159\n-1.23e-45\n-34.56e-7\n");
00022   std::ostringstream out;
00023   parse_loop(in, out);
00024   BOOST_CHECK_EQUAL("> -1\n> -3.14159\n> -1.23e-45\n> -3.456e-06\n> ", out.str());
00025 }
00026 
00027 BOOST_AUTO_TEST_CASE(test_add)
00028 {
00029   std::istringstream in("1 + 2\n1 + 2 + 3");
00030   std::ostringstream out;
00031   parse_loop(in, out);
00032   BOOST_CHECK_EQUAL("> 3\n> 6\n> ", out.str());
00033 }
00034 
00035 BOOST_AUTO_TEST_CASE(test_subtract)
00036 {
00037   std::istringstream in("1 - 2\n5 - 1 - 2");
00038   std::ostringstream out;
00039   parse_loop(in, out);
00040   BOOST_CHECK_EQUAL("> -1\n> 2\n> ", out.str());
00041 }
00042 
00043 BOOST_AUTO_TEST_CASE(test_multiply)
00044 {
00045   std::istringstream in("1 * 2\n5 * 2 * 1.5");
00046   std::ostringstream out;
00047   parse_loop(in, out);
00048   BOOST_CHECK_EQUAL("> 2\n> 15\n> ", out.str());
00049 }
00050 
00051 BOOST_AUTO_TEST_CASE(test_divide)
00052 {
00053   std::istringstream in("1 / 2\n10 / 2 / 2");
00054   std::ostringstream out;
00055   parse_loop(in, out);
00056   BOOST_CHECK_EQUAL("> 0.5\n> 2.5\n> ", out.str());
00057 }
00058 
00059 BOOST_AUTO_TEST_CASE(test_mix)
00060 {
00061   std::istringstream in(" 1.5 * 2 + 3 / 1.5 - (10 - 3) + 2 * -1");
00062   std::ostringstream out;
00063   parse_loop(in, out);
00064   BOOST_CHECK_EQUAL("> -4\n> ", out.str());
00065 }
00066 
00067 BOOST_AUTO_TEST_CASE(test_var)
00068 {
00069   std::istringstream in(" def half = 1 / 2\ndef one=1.0\n one + half");
00070   std::ostringstream out;
00071   parse_loop(in, out);
00072   BOOST_CHECK_EQUAL("> > > 1.5\n> ", out.str());
00073 }
00074 
00075 BOOST_AUTO_TEST_CASE(test_predefined_vars)
00076 {
00077   std::istringstream in(" pi - e");
00078   std::ostringstream out;
00079   parse_loop(in, out);
00080   BOOST_CHECK_EQUAL("> 0.423311\n> ", out.str());
00081 }
00082 
00083 BOOST_AUTO_TEST_CASE(test_function)
00084 {
00085   std::istringstream in("def times(a, b) = a * b\ntimes(2, 3)\n");
00086   std::ostringstream out;
00087   parse_loop(in, out);
00088   BOOST_CHECK_EQUAL("> > 6\n> ", out.str());
00089 }
00090 
00091 BOOST_AUTO_TEST_CASE(test_error1)
00092 {
00093   std::istringstream in("1+++2");
00094   std::ostringstream out;
00095   parse_loop(in, out);
00096   BOOST_CHECK_EQUAL("> syntax error: expected a primary, got +\n> ", out.str());
00097 }
00098 
00099 BOOST_AUTO_TEST_CASE(test_error2)
00100 {
00101   std::istringstream in("1..2");
00102   std::ostringstream out;
00103   parse_loop(in, out);
00104   BOOST_CHECK_EQUAL("> syntax error: expected digit after decimal point, got '.'\n> ", out.str());
00105 }
00106 
00107 BOOST_AUTO_TEST_CASE(test_error3)
00108 {
00109   std::istringstream in("1 @ 2");
00110   std::ostringstream out;
00111   parse_loop(in, out);
00112   BOOST_CHECK_EQUAL("> syntax error: expected digit, got '@'\n> ", out.str());
00113 }
00114 
00115 BOOST_AUTO_TEST_CASE(test_error4)
00116 {
00117   std::istringstream in("(1 + 2");
00118   std::ostringstream out;
00119   parse_loop(in, out);
00120   BOOST_CHECK_EQUAL("> syntax error: expected ')', got end of line\n> ", out.str());
00121 }
00122 
00123 BOOST_AUTO_TEST_CASE(test_error5)
00124 {
00125   std::istringstream in("pi(2)");
00126   std::ostringstream out;
00127   parse_loop(in, out);
00128   BOOST_CHECK_EQUAL("> wrong number of arguments in call to pi(), expected 0, got 1\n> ", out.str());
00129 }
00130 
00131 BOOST_AUTO_TEST_CASE(test_error6)
00132 {
00133   std::istringstream in("def times(a, b) = a * b\ntimes(2)");
00134   std::ostringstream out;
00135   parse_loop(in, out);
00136   BOOST_CHECK_EQUAL("> > wrong number of arguments in call to times(), expected 2, got 1\n> ", out.str());
00137 }
00138 
00139 BOOST_AUTO_TEST_CASE(test_error7)
00140 {
00141   std::istringstream in("def times(a, b) = a * b\ndivide(2, 3)");
00142   std::ostringstream out;
00143   parse_loop(in, out);
00144   BOOST_CHECK_EQUAL("> > unknown function: divide\n> ", out.str());
00145 }
00146 
00147 BOOST_AUTO_TEST_SUITE_END();

Generated on Sun Nov 30 10:05:26 2008 for Calculator by  doxygen 1.5.3