summaryrefslogtreecommitdiff
path: root/src/unitparser.cpp
diff options
context:
space:
mode:
authorMike Buland <mike@xagasoft.com>2013-11-14 15:35:47 -0700
committerMike Buland <mike@xagasoft.com>2013-11-14 15:35:47 -0700
commitabe7e449143052b07fae688da690c2a7d387a291 (patch)
tree00742d14bce96777ce65589f9abeeca4cdadbc25 /src/unitparser.cpp
parentbc0484899acb3495f95d7400ff2eb804ae580096 (diff)
downloadclic-abe7e449143052b07fae688da690c2a7d387a291.tar.gz
clic-abe7e449143052b07fae688da690c2a7d387a291.tar.bz2
clic-abe7e449143052b07fae688da690c2a7d387a291.tar.xz
clic-abe7e449143052b07fae688da690c2a7d387a291.zip
Fixed (?) parsing order of operations bug, added unit tests.
Diffstat (limited to 'src/unitparser.cpp')
-rw-r--r--src/unitparser.cpp39
1 files changed, 39 insertions, 0 deletions
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 @@
1#include "unitparser.h"
2#include "parser.h"
3#include "lexer.h"
4
5#include <bu/sio.h>
6#include <bu/streamstack.h>
7#include <bu/membuf.h>
8
9UnitParser::UnitParser()
10{
11 setName("Parser");
12 add( static_cast<Bu::UnitSuite::Test>(&UnitParser::order1),
13 "order1", Bu::UnitSuite::expectPass );
14}
15
16UnitParser::~UnitParser()
17{
18}
19
20Bu::String parse( const Bu::String sEq, int iScale=0, int iRadix=10 )
21{
22 Bu::MemBuf mbIn( sEq );
23 Bu::MemBuf mbOut;
24 Lexer lex( mbIn );
25 lex.setScale( 5 );
26 Parser parser( lex, mbOut );
27 parser.parse();
28 return mbOut.getString().trimWhitespace();
29}
30
31void UnitParser::order1()
32{
33 unitTest(parse("2+3*5") == "17");
34 unitTest(parse("2+3-5") == "0");
35 unitTest(parse("(2+3)*5") == "25");
36 unitTest(parse("1.59*40/24*21", 5) == "55.125");
37 unitTest(parse("1.59*40/(24*21)", 5) == "0.125"); // bc says it's this: "0.12619");
38}
39