Calculator  Step 2
Functions
test_parse.cpp File Reference
#include <iostream>
#include <istream>
#include <ostream>
#include <sstream>
#include <string>
#include <boost/test/auto_unit_test.hpp>
#include "parse.hpp"

Go to the source code of this file.

Functions

 BOOST_AUTO_TEST_CASE (test_number)
 
 BOOST_AUTO_TEST_CASE (test_negative)
 
 BOOST_AUTO_TEST_CASE (test_add)
 
 BOOST_AUTO_TEST_CASE (test_subtract)
 
 BOOST_AUTO_TEST_CASE (test_multiply)
 
 BOOST_AUTO_TEST_CASE (test_divide)
 
 BOOST_AUTO_TEST_CASE (test_mix)
 
 BOOST_AUTO_TEST_CASE (test_var)
 
 BOOST_AUTO_TEST_CASE (test_predefined_vars)
 
 BOOST_AUTO_TEST_CASE (test_error1)
 
 BOOST_AUTO_TEST_CASE (test_error2)
 
 BOOST_AUTO_TEST_CASE (test_error3)
 
 BOOST_AUTO_TEST_CASE (test_error4)
 

Function Documentation

BOOST_AUTO_TEST_CASE ( test_number  )

Definition at line 11 of file test_parse.cpp.

References parse_loop().

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 }
void parse_loop(std::istream &input, std::ostream &output)
Definition: parse.cpp:267
BOOST_AUTO_TEST_CASE ( test_negative  )

Definition at line 19 of file test_parse.cpp.

References parse_loop().

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 }
void parse_loop(std::istream &input, std::ostream &output)
Definition: parse.cpp:267
BOOST_AUTO_TEST_CASE ( test_add  )

Definition at line 27 of file test_parse.cpp.

References parse_loop().

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 }
void parse_loop(std::istream &input, std::ostream &output)
Definition: parse.cpp:267
BOOST_AUTO_TEST_CASE ( test_subtract  )

Definition at line 35 of file test_parse.cpp.

References parse_loop().

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 }
void parse_loop(std::istream &input, std::ostream &output)
Definition: parse.cpp:267
BOOST_AUTO_TEST_CASE ( test_multiply  )

Definition at line 43 of file test_parse.cpp.

References parse_loop().

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 }
void parse_loop(std::istream &input, std::ostream &output)
Definition: parse.cpp:267
BOOST_AUTO_TEST_CASE ( test_divide  )

Definition at line 51 of file test_parse.cpp.

References parse_loop().

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 }
void parse_loop(std::istream &input, std::ostream &output)
Definition: parse.cpp:267
BOOST_AUTO_TEST_CASE ( test_mix  )

Definition at line 59 of file test_parse.cpp.

References parse_loop().

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 }
void parse_loop(std::istream &input, std::ostream &output)
Definition: parse.cpp:267
BOOST_AUTO_TEST_CASE ( test_var  )

Definition at line 67 of file test_parse.cpp.

References parse_loop().

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 }
void parse_loop(std::istream &input, std::ostream &output)
Definition: parse.cpp:267
BOOST_AUTO_TEST_CASE ( test_predefined_vars  )

Definition at line 75 of file test_parse.cpp.

References parse_loop().

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 }
void parse_loop(std::istream &input, std::ostream &output)
Definition: parse.cpp:267
BOOST_AUTO_TEST_CASE ( test_error1  )

Definition at line 83 of file test_parse.cpp.

References parse_loop().

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 }
void parse_loop(std::istream &input, std::ostream &output)
Definition: parse.cpp:267
BOOST_AUTO_TEST_CASE ( test_error2  )

Definition at line 91 of file test_parse.cpp.

References parse_loop().

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 }
void parse_loop(std::istream &input, std::ostream &output)
Definition: parse.cpp:267
BOOST_AUTO_TEST_CASE ( test_error3  )

Definition at line 99 of file test_parse.cpp.

References parse_loop().

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 }
void parse_loop(std::istream &input, std::ostream &output)
Definition: parse.cpp:267
BOOST_AUTO_TEST_CASE ( test_error4  )

Definition at line 107 of file test_parse.cpp.

References parse_loop().

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 }
void parse_loop(std::istream &input, std::ostream &output)
Definition: parse.cpp:267