From cf2aa4c9d70fc8ed658c213b2c46bb48ee10e6f7 Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Sun, 20 Oct 2013 00:10:54 +0200 Subject: Implemented constant identifier functionality * SimpleParser optionally acceps a pointer to an ConstantMap containing string keys mapping to values * Constants are handled in their own ConstantNode class derived from the standard Node class * Operator precedence is now determined separated from the TokenType using a new PrecedenceLevel enum ** Conversion between tokens and their PrecedenceLevel is possible using the new utility function getPrecedence * Added additional test cases for constant identifier resolutions --- test.cc | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'test.cc') diff --git a/test.cc b/test.cc index 0b6f36b..8e62ab0 100644 --- a/test.cc +++ b/test.cc @@ -15,6 +15,8 @@ TEST_F(ParserTest, BasicCalc) { TEST_F(ParserTest, OperatorPriority) { EXPECT_EQ(10, SimpleParser::calculate("2+2*4")); EXPECT_EQ(4, SimpleParser::calculate("2+4/2")); + EXPECT_EQ(60, SimpleParser::calculate("100/5*3")); + EXPECT_EQ(60, SimpleParser::calculate("100*3/5")); EXPECT_EQ(42, SimpleParser::calculate("5+10*4-3")); EXPECT_EQ(17, SimpleParser::calculate("10+20/2-3")); EXPECT_EQ(261, SimpleParser::calculate("5+2^8")); @@ -31,6 +33,18 @@ TEST_F(ParserTest, BracketCalc) { EXPECT_EQ(6.25, SimpleParser::calculate("2.5*(2+3-(3/2+1))")); } +TEST_F(ParserTest, ConstantCalc) { + SimpleParser::ConstantMap constants; + constants["pi"] = 3.1415926535; + constants["answer"] = 42; + constants["g"] = 9.81; + + EXPECT_EQ(3.1415926535, SimpleParser::calculate("pi", &constants)); + EXPECT_EQ(6.283185307, SimpleParser::calculate("2*pi*1", &constants)); + EXPECT_EQ(0, SimpleParser::calculate("2*3*7-answer", &constants)); + EXPECT_EQ(1347.58, SimpleParser::calculate("1/2*(g*(-1))*answer^2+10000", &constants)); +} + int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); -- cgit v1.2.3