From abe7e449143052b07fae688da690c2a7d387a291 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 14 Nov 2013 15:35:47 -0700 Subject: Fixed (?) parsing order of operations bug, added unit tests. --- src/unitparser.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/unitparser.cpp (limited to 'src/unitparser.cpp') diff --git a/src/unitparser.cpp b/src/unitparser.cpp new file mode 100644 index 0000000..891f7b3 --- /dev/null +++ b/src/unitparser.cpp @@ -0,0 +1,39 @@ +#include "unitparser.h" +#include "parser.h" +#include "lexer.h" + +#include +#include +#include + +UnitParser::UnitParser() +{ + setName("Parser"); + add( static_cast(&UnitParser::order1), + "order1", Bu::UnitSuite::expectPass ); +} + +UnitParser::~UnitParser() +{ +} + +Bu::String parse( const Bu::String sEq, int iScale=0, int iRadix=10 ) +{ + Bu::MemBuf mbIn( sEq ); + Bu::MemBuf mbOut; + Lexer lex( mbIn ); + lex.setScale( 5 ); + Parser parser( lex, mbOut ); + parser.parse(); + return mbOut.getString().trimWhitespace(); +} + +void UnitParser::order1() +{ + unitTest(parse("2+3*5") == "17"); + unitTest(parse("2+3-5") == "0"); + unitTest(parse("(2+3)*5") == "25"); + unitTest(parse("1.59*40/24*21", 5) == "55.125"); + unitTest(parse("1.59*40/(24*21)", 5) == "0.125"); // bc says it's this: "0.12619"); +} + -- cgit v1.2.3