Calculator Step 4

- Main Page
- Classes
- Files

- File List
- File Members

test_parse.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/auto_unit_test.hpp>
8
9 #include "parse.hpp"
10
11 BOOST_AUTO_TEST_SUITE(parse_test);
12
13 BOOST_AUTO_TEST_CASE(test_number)
14 {
15 std::istringstream in("1\n3.14159\n1.23e45\n45.67e+8\n");
16 std::ostringstream out;
17 parse_loop(in, out);
18 BOOST_CHECK_EQUAL("> 1\n> 3.14159\n> 1.23e+45\n> 4.567e+09\n> ", out.str());
19 }
20
21 BOOST_AUTO_TEST_CASE(test_negative)
22 {
23 std::istringstream in("-1\n-3.14159\n-1.23e-45\n-34.56e-7\n");
24 std::ostringstream out;
25 parse_loop(in, out);
26 BOOST_CHECK_EQUAL("> -1\n> -3.14159\n> -1.23e-45\n> -3.456e-06\n> ", out.str());
27 }
28
29 BOOST_AUTO_TEST_CASE(test_add)
30 {
31 std::istringstream in("1 + 2\n1 + 2 + 3");
32 std::ostringstream out;
33 parse_loop(in, out);
34 BOOST_CHECK_EQUAL("> 3\n> 6\n> ", out.str());
35 }
36
37 BOOST_AUTO_TEST_CASE(test_subtract)
38 {
39 std::istringstream in("1 - 2\n5 - 1 - 2");
40 std::ostringstream out;
41 parse_loop(in, out);
42 BOOST_CHECK_EQUAL("> -1\n> 2\n> ", out.str());
43 }
44
45 BOOST_AUTO_TEST_CASE(test_multiply)
46 {
47 std::istringstream in("1 * 2\n5 * 2 * 1.5");
48 std::ostringstream out;
49 parse_loop(in, out);
50 BOOST_CHECK_EQUAL("> 2\n> 15\n> ", out.str());
51 }
52
53 BOOST_AUTO_TEST_CASE(test_divide)
54 {
55 std::istringstream in("1 / 2\n10 / 2 / 2");
56 std::ostringstream out;
57 parse_loop(in, out);
58 BOOST_CHECK_EQUAL("> 0.5\n> 2.5\n> ", out.str());
59 }
60
61 BOOST_AUTO_TEST_CASE(test_mix)
62 {
63 std::istringstream in(" 1.5 * 2 + 3 / 1.5 - (10 - 3) + 2 * -1");
64 std::ostringstream out;
65 parse_loop(in, out);
66 BOOST_CHECK_EQUAL("> -4\n> ", out.str());
67 }
68
69 BOOST_AUTO_TEST_CASE(test_var)
70 {
71 std::istringstream in(" def half = 1 / 2\ndef one=1.0\n one + half");
72 std::ostringstream out;
73 parse_loop(in, out);
74 BOOST_CHECK_EQUAL("> > > 1.5\n> ", out.str());
75 }
76
77 BOOST_AUTO_TEST_CASE(test_predefined_vars)
78 {
79 std::istringstream in(" pi - e");
80 std::ostringstream out;
81 parse_loop(in, out);
82 BOOST_CHECK_EQUAL("> 0.423311\n> ", out.str());
83 }
84
85 BOOST_AUTO_TEST_CASE(test_function)
86 {
87 std::istringstream in("def times(a, b) = a * b\ntimes(2, 3)\n");
88 std::ostringstream out;
89 parse_loop(in, out);
90 BOOST_CHECK_EQUAL("> > 6\n> ", out.str());
91 }
92
93 BOOST_AUTO_TEST_CASE(test_error1)
94 {
95 std::istringstream in("1+++2");
96 std::ostringstream out;
97 parse_loop(in, out);
98 BOOST_CHECK_EQUAL("> syntax error: expected a primary, got +\n> ", out.str());
99 }
100
101 BOOST_AUTO_TEST_CASE(test_error2)
102 {
103 std::istringstream in("1..2");
104 std::ostringstream out;
105 parse_loop(in, out);
106 BOOST_CHECK_EQUAL("> syntax error: expected digit after decimal point, got '.'\n> ", out.str());
107 }
108
109 BOOST_AUTO_TEST_CASE(test_error3)
110 {
111 std::istringstream in("1 @ 2");
112 std::ostringstream out;
113 parse_loop(in, out);
114 BOOST_CHECK_EQUAL("> syntax error: expected digit, got '@'\n> ", out.str());
115 }
116
117 BOOST_AUTO_TEST_CASE(test_error4)
118 {
119 std::istringstream in("(1 + 2");
120 std::ostringstream out;
121 parse_loop(in, out);
122 BOOST_CHECK_EQUAL("> syntax error: expected ')', got end of line\n> ", out.str());
123 }
124
125 BOOST_AUTO_TEST_CASE(test_error5)
126 {
127 std::istringstream in("pi(2)");
128 std::ostringstream out;
129 parse_loop(in, out);
130 BOOST_CHECK_EQUAL("> wrong number of arguments in call to pi(), expected 0, got 1\n> ", out.str());
131 }
132
133 BOOST_AUTO_TEST_CASE(test_error6)
134 {
135 std::istringstream in("def times(a, b) = a * b\ntimes(2)");
136 std::ostringstream out;
137 parse_loop(in, out);
138 BOOST_CHECK_EQUAL("> > wrong number of arguments in call to times(), expected 2, got 1\n> ", out.str());
139 }
140
141 BOOST_AUTO_TEST_CASE(test_error7)
142 {
143 std::istringstream in("def times(a, b) = a * b\ndivide(2, 3)");
144 std::ostringstream out;
145 parse_loop(in, out);
146 BOOST_CHECK_EQUAL("> > unknown function: divide\n> ", out.str());
147 }
148
149 BOOST_AUTO_TEST_SUITE_END();
parse_loop
void parse_loop(std::istream &input, std::ostream &output)
Definition: parse.cpp:338
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(test_basics)
Definition: test_node.cpp:14
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
parse.hpp
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(node_test)