Calculator  Step 2
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 
12 {
13  std::istringstream in("1\n3.14159\n1.23e45\n45.67e+8\n");
14  std::ostringstream out;
15  parse_loop(in, out);
16  BOOST_CHECK_EQUAL("> 1\n> 3.14159\n> 1.23e+45\n> 4.567e+09\n> ", out.str());
17 }
18 
19 BOOST_AUTO_TEST_CASE(test_negative)
20 {
21  std::istringstream in("-1\n-3.14159\n-1.23e-45\n-34.56e-7\n");
22  std::ostringstream out;
23  parse_loop(in, out);
24  BOOST_CHECK_EQUAL("> -1\n> -3.14159\n> -1.23e-45\n> -3.456e-06\n> ", out.str());
25 }
26 
28 {
29  std::istringstream in("1 + 2\n1 + 2 + 3");
30  std::ostringstream out;
31  parse_loop(in, out);
32  BOOST_CHECK_EQUAL("> 3\n> 6\n> ", out.str());
33 }
34 
35 BOOST_AUTO_TEST_CASE(test_subtract)
36 {
37  std::istringstream in("1 - 2\n5 - 1 - 2");
38  std::ostringstream out;
39  parse_loop(in, out);
40  BOOST_CHECK_EQUAL("> -1\n> 2\n> ", out.str());
41 }
42 
43 BOOST_AUTO_TEST_CASE(test_multiply)
44 {
45  std::istringstream in("1 * 2\n5 * 2 * 1.5");
46  std::ostringstream out;
47  parse_loop(in, out);
48  BOOST_CHECK_EQUAL("> 2\n> 15\n> ", out.str());
49 }
50 
52 {
53  std::istringstream in("1 / 2\n10 / 2 / 2");
54  std::ostringstream out;
55  parse_loop(in, out);
56  BOOST_CHECK_EQUAL("> 0.5\n> 2.5\n> ", out.str());
57 }
58 
60 {
61  std::istringstream in(" 1.5 * 2 + 3 / 1.5 - (10 - 3) + 2 * -1");
62  std::ostringstream out;
63  parse_loop(in, out);
64  BOOST_CHECK_EQUAL("> -4\n> ", out.str());
65 }
66 
68 {
69  std::istringstream in(" var half = 1 / 2\nvar one=1.0\n one + half");
70  std::ostringstream out;
71  parse_loop(in, out);
72  BOOST_CHECK_EQUAL("> 0.5\n> 1\n> 1.5\n> ", out.str());
73 }
74 
75 BOOST_AUTO_TEST_CASE(test_predefined_vars)
76 {
77  std::istringstream in(" pi - e");
78  std::ostringstream out;
79  parse_loop(in, out);
80  BOOST_CHECK_EQUAL("> 0.423311\n> ", out.str());
81 }
82 
84 {
85  std::istringstream in("1+++2");
86  std::ostringstream out;
87  parse_loop(in, out);
88  BOOST_CHECK_EQUAL("> syntax error: expected a primary, but got +\n> ", out.str());
89 }
90 
92 {
93  std::istringstream in("1..2");
94  std::ostringstream out;
95  parse_loop(in, out);
96  BOOST_CHECK_EQUAL("> syntax error: expected digit after decimal point, got '.'\n> ", out.str());
97 }
98 
100 {
101  std::istringstream in("1 @ 2");
102  std::ostringstream out;
103  parse_loop(in, out);
104  BOOST_CHECK_EQUAL("> syntax error: expected digit, got '@'\n> ", out.str());
105 }
106 
108 {
109  std::istringstream in("(1 + 2");
110  std::ostringstream out;
111  parse_loop(in, out);
112  BOOST_CHECK_EQUAL("> syntax error: EOF when expecting ')'\n> ", out.str());
113 }
114 
void parse_loop(std::istream &input, std::ostream &output)
Definition: parse.cpp:267
BOOST_AUTO_TEST_CASE(test_number)
Definition: test_parse.cpp:11