-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
198 lines (160 loc) · 4.19 KB
/
main.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* @file main.c
*
* @date Apr 6, 2014
* @author Craig Hesling
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "errors.h"
#include "types.h"
#include "workspace.h"
#include "expression.h"
#define BLACK 30
#define RED 31
#define GREEN 32
#define YELLOW 33
#define BLUE 34
#define PURPLE 35
#define TEAL 36
#define GREY 37
#define WHITE 38
#define xCOLOR(color,str) "\033["#color"m"str"\033[m"
#define COLOR(color,str) xCOLOR(color, #str)
#define EXPECT(exp) COLOR(GREEN, exp)
#define RESULT(res) COLOR(PURPLE, res)
/** Random Tests.
* Tests the atol() system function.
*/
void
rtests(void) {
long int i = 0;
char buf[] = "-199999";
/// Execute atol().
i = atol(buf);
/* Show results */
printf("Values: %s = %ld\n", buf, i);
}
/** Tests \ref expression_to_string and then \ref expression_evaluate functions.
* It tests expression_to_string and expression_evaluate on a manually created expression.
* The expression is ( (10 + 20) + 128 ) where e4 is the outer most root node and e3 is inner parent node of e0 and e1.
*
*
* @dot
* digraph G{
* e4 [label="e4\n+"];
* e3 [label="e3\n+"];
* e0 [label="e0\n10"];
* e1 [label="e1\n20"];
* e2 [label="e2\n128"];
* e4 -> e3;
* e4 -> e2;
* e3 -> e0;
* e3 -> e1;
* }
* @enddot
*/
void
test0(void) {
struct expression e0, e1, e2, e3, e4; //, e5, e6, e7, e8, e9;
exp_buf buf;
value_t result;
// e0, e1, e2 are all leaf values
e0.type = e1.type = e2.type = EXP_VALUE;
// e3, e4 are tree operations
e3.type = e4.type = EXP_TREE;
/// Call \ref value_new_lint for e0 e1 and e2.
/// @code
puts("## Try value_new_lint()");
e0.data.val = value_new_lint(10);
e1.data.val = value_new_lint(20);
e2.data.val = value_new_lint(128);
/// @endcode
// e3 and e4: +
e3.data.tree.op = e4.data.tree.op = '+';
// e3: e0 + e1
e3.data.tree.left = &e0;
e3.data.tree.right = &e1;
// e4 is root node
// e4: e3 + e2
e4.data.tree.left = &e3;
e4.data.tree.right = &e2;
/// Call \ref expression_to_string on e4.
/// @code
puts("## Try expression_to_string()");
// get string of root node e4
expression_to_string(buf, &e4);
puts("## Show resultant string");
puts("Expect: "EXPECT("((10 + 20) + 128)"));
printf("Expression String: "RESULT("%s")"\n", buf);
/// @endcode
/// Call \ref expression_evaluate on e4.
/// @code
puts("## Try expression_evaluate()");
result = expression_evaluate(&e4);
assert(result.type == VAL_LINT);
puts("## Show resultant values");
puts("Expect: "EXPECT("((10 + 20) + 128) = 158"));
printf("Expression: "RESULT("%s = %ld")"\n", buf, result.data.lint);
/// @endcode
}
/** Tests \ref string_to_expression on the string argument and then tests \ref expression_to_string on it's output.
* @param str The expression string to use.
*/
void
test1(char *str) {
char buf[512] = {0};
expression_t e1;
value_t val;
/* Check that there was a valid str argument given */
if (!str) {
fprintf(stderr, "test1: Error - str is NULL\n");
exit (2);
}
if (strlen(str) < 1) {
fprintf(stderr, "test1: Error - str is not even 1 char long\n");
exit (2);
}
printf("Given String: \"%s\"\n", str);
/// Call \ref string_to_expression on str
/// @code
e1 = string_to_expression (strlen(str), str);
/// @endcode
/// Call \ref expression_to_string on result, e1
/// @code
expression_to_string (buf, e1);
/// @endcode
printf("Expression(just string): "RESULT("%s")"\n", buf);
// Show results
val = expression_evaluate(e1);
printf("Expression(both string and value): "RESULT("%s = %ld")"\n", buf, val.data.lint);
expression_free(e1);
}
/// @callgraph
int main(int argc, char *argv[]) {
workspace_init();
//char buf[512] = {0};
//expression_t e1;
puts("# Random Tests:");
rtests();
printf("\n\n\n");
puts("# test0:");
test0();
printf("\n\n\n");
puts("# test1 (\"129\"):");
test1("129");
printf("\n\n\n");
puts("# test1 (\"1 + 1\"):");
test1("1 + 1");
printf("\n\n\n");
puts("# main:");
if (argc != 2) {
fprintf(stderr, "main: Error - Need expression as only argument for test1()\n");
return 1;
}
puts("# test1 ( argv[1] ):");
test1(argv[1]);
return 0;
}