blob: f518fdc17bca362cdbf57fcce1c804f9a3981861 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
#include "unitparser.h"
#include "parser.h"
#include "lexer.h"
#include <bu/sio.h>
#include <bu/streamstack.h>
#include <bu/membuf.h>
UnitParser::UnitParser()
{
setName("Parser");
add( static_cast<Bu::UnitSuite::Test>(&UnitParser::order1),
"order1", Bu::UnitSuite::expectPass );
add( static_cast<Bu::UnitSuite::Test>(&UnitParser::assignment),
"assignment", 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.65");
unitTest(parse("1.59*40/(24*21)", 5) == "0.12619"); // bc says it's this: "0.12619");
unitTest(parse("10+2*2*2+2") == "20");
unitTest(parse("5-5-1") == "-1");
unitTest(parse("5-(1+2+2)-1") == "-1");
}
void UnitParser::assignment()
{
Bu::MemBuf mbIn("$test = 2*2*2");
Bu::MemBuf mbOut;
Lexer lex( mbIn );
lex.setScale( 0 );
Parser parser( lex, mbOut );
parser.parse();
// Bu::println("%1 == %2").arg( mbOut.getString().trimWhitespace() )
// .arg( parser.getVariable("test") );
unitTest( mbOut.getString().trimWhitespace() == parser.getVariable("test").toString() );
}
|