-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_expr.c
125 lines (101 loc) · 3.75 KB
/
test_expr.c
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "dberror.h"
#include "expr.h"
#include "record_mgr.h"
#include "tables.h"
#include "test_helper.h"
// helper macros
#define OP_TRUE(left, right, op, message) \
do { \
Value *result; \
MAKE_VALUE(result, DT_INT, -1); \
op(left, right, result); \
bool b = result->v.boolV; \
free(result); \
ASSERT_TRUE(b,message); \
} while (0)
#define OP_FALSE(left, right, op, message) \
do { \
Value *result; \
MAKE_VALUE(result, DT_INT, -1); \
op(left, right, result); \
bool b = result->v.boolV; \
free(result); \
ASSERT_TRUE(!b,message); \
} while (0)
// test methods
static void testValueSerialize (void);
static void testOperators (void);
static void testExpressions (void);
char *testName;
// main method
int
main (void)
{
testName = "";
testValueSerialize();
testOperators();
testExpressions();
return 0;
}
// ************************************************************
void
testValueSerialize (void)
{
testName = "test value serialization and deserialization";
ASSERT_EQUALS_STRING(serializeValue(stringToValue("i10")), "10", "create Value 10");
ASSERT_EQUALS_STRING(serializeValue(stringToValue("f5.3")), "5.300000", "create Value 5.3");
ASSERT_EQUALS_STRING(serializeValue(stringToValue("sHello World")), "Hello World", "create Value Hello World");
ASSERT_EQUALS_STRING(serializeValue(stringToValue("bt")), "true", "create Value true");
ASSERT_EQUALS_STRING(serializeValue(stringToValue("btrue")), "true", "create Value true");
TEST_DONE();
}
// ************************************************************
void
testOperators (void)
{
Value *result;
testName = "test value comparison and boolean operators";
MAKE_VALUE(result, DT_INT, 0);
// equality
OP_TRUE(stringToValue("i10"),stringToValue("i10"), valueEquals, "10 = 10");
OP_FALSE(stringToValue("i9"),stringToValue("i10"), valueEquals, "9 != 10");
OP_TRUE(stringToValue("sHello World"),stringToValue("sHello World"), valueEquals, "Hello World = Hello World");
OP_FALSE(stringToValue("sHello Worl"),stringToValue("sHello World"), valueEquals, "Hello Worl != Hello World");
OP_FALSE(stringToValue("sHello Worl"),stringToValue("sHello Wor"), valueEquals, "Hello Worl != Hello Wor");
// smaller
OP_TRUE(stringToValue("i3"),stringToValue("i10"), valueSmaller, "3 < 10");
OP_TRUE(stringToValue("f5.0"),stringToValue("f6.5"), valueSmaller, "5.0 < 6.5");
// boolean
OP_TRUE(stringToValue("bt"),stringToValue("bt"), boolAnd, "t AND t = t");
OP_FALSE(stringToValue("bt"),stringToValue("bf"), boolAnd, "t AND f = f");
OP_TRUE(stringToValue("bt"),stringToValue("bf"), boolOr, "t OR f = t");
OP_FALSE(stringToValue("bf"),stringToValue("bf"), boolOr, "f OR f = f");
TEST_CHECK(boolNot(stringToValue("bf"), result));
ASSERT_TRUE(result->v.boolV, "!f = t");
TEST_DONE();
}
// ************************************************************
void
testExpressions (void)
{
Expr *op, *l, *r;
Value *res;
testName = "test complex expressions";
MAKE_CONS(l, stringToValue("i10"));
evalExpr(NULL, NULL, l, &res);
OP_TRUE(stringToValue("i10"), res, valueEquals, "Const 10");
MAKE_CONS(r, stringToValue("i20"));
evalExpr(NULL, NULL, r, &res);
OP_TRUE(stringToValue("i20"), res, valueEquals, "Const 20");
MAKE_BINOP_EXPR(op, l, r, OP_COMP_SMALLER);
evalExpr(NULL, NULL, op, &res);
OP_TRUE(stringToValue("bt"), res, valueEquals, "Const 10 < Const 20");
MAKE_CONS(l, stringToValue("bt"));
evalExpr(NULL, NULL, l, &res);
OP_TRUE(stringToValue("bt"), res, valueEquals, "Const true");
r = op;
MAKE_BINOP_EXPR(op, r, l, OP_BOOL_AND);
evalExpr(NULL, NULL, op, &res);
OP_TRUE(stringToValue("bt"), res, valueEquals, "(Const 10 < Const 20) AND true");
TEST_DONE();
}