diff options
author | Adrian Kummerländer | 2012-10-12 21:25:12 +0200 |
---|---|---|
committer | Adrian Kummerländer | 2012-10-12 21:25:12 +0200 |
commit | 16e2509100611447887f6050a5eb1c21ac9170c4 (patch) | |
tree | 308ed29fbeadfd6703c9cadf4881b898a7b75ba9 | |
parent | 7c83dbbd089e29a420a388e78b14c7cb675f17a1 (diff) | |
download | SimpleParser-16e2509100611447887f6050a5eb1c21ac9170c4.tar SimpleParser-16e2509100611447887f6050a5eb1c21ac9170c4.tar.gz SimpleParser-16e2509100611447887f6050a5eb1c21ac9170c4.tar.bz2 SimpleParser-16e2509100611447887f6050a5eb1c21ac9170c4.tar.lz SimpleParser-16e2509100611447887f6050a5eb1c21ac9170c4.tar.xz SimpleParser-16e2509100611447887f6050a5eb1c21ac9170c4.tar.zst SimpleParser-16e2509100611447887f6050a5eb1c21ac9170c4.zip |
Added gtest based test cases to be executed after every compile
-rw-r--r-- | Makefile | 11 | ||||
-rw-r--r-- | test.cpp | 44 |
2 files changed, 52 insertions, 3 deletions
@@ -1,8 +1,13 @@ -LIB_FILES = tree.cpp parser.cpp +LIB_FILES = tree.cpp parser.cpp PROG_FILES = main.cpp +TEST_FILES = test.cpp -all: parser +all: parser test parser: $(PROG_FILES) $(LIB_FILES) - g++ -O2 -g -o parser $(PROG_FILES) $(LIB_FILES) + g++ -O2 -g -o bin/parser $(PROG_FILES) $(LIB_FILES) + +test: $(LIB_FILES) $(TEST_FILES) + g++ -o bin/test -lgtest $(LIB_FILES) $(TEST_FILES) + ./bin/test diff --git a/test.cpp b/test.cpp new file mode 100644 index 0000000..2d0ef10 --- /dev/null +++ b/test.cpp @@ -0,0 +1,44 @@ +#include "parser.h" +#include "gtest/gtest.h" + +class ParserTest : public ::testing::Test { + +}; + +TEST_F(ParserTest, BasicCalc) { + Parser p; + + EXPECT_EQ(4, p.calculate("2+2", false).result); + EXPECT_EQ(0, p.calculate("2-2", false).result); + EXPECT_EQ(42, p.calculate("21*2", false).result); + EXPECT_EQ(21, p.calculate("42/2", false).result); +} + +TEST_F(ParserTest, OperatorPriority) { + Parser p; + + EXPECT_EQ(10, p.calculate("2+2*4", false).result); + EXPECT_EQ(4, p.calculate("2+4/2", false).result); + EXPECT_EQ(42, p.calculate("5+10*4-3", false).result); + EXPECT_EQ(17, p.calculate("10+20/2-3", false).result); + EXPECT_EQ(261, p.calculate("5+2^8", false).result); + EXPECT_EQ(32768, p.calculate("2*2^16/4", false).result); + EXPECT_EQ(32772, p.calculate("2*2^16/4+4", false).result); +} + +TEST_F(ParserTest, BracketCalc) { + Parser p; + + EXPECT_EQ(16, p.calculate("(2+2)*4", false).result); + EXPECT_EQ(10, p.calculate("2+(2*4)", false).result); + EXPECT_EQ(10, p.calculate("(10-5)*(5-3)", false).result); + EXPECT_EQ(4, p.calculate("((((2))*((2))))", false).result); + EXPECT_EQ(37, p.calculate("(((5))*(((((3+2*2)))))+2)", false).result); + EXPECT_EQ(6.25, p.calculate("2.5*(2+3-(3/2+1))", false).result); +} + +int main(int argc, char **argv) { + testing::InitGoogleTest(&argc, argv); + + return RUN_ALL_TESTS(); +} |