This repository has been archived by the owner on Sep 14, 2018. It is now read-only.
forked from matthiaskramm/mrscake
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_ast.c
129 lines (106 loc) · 2.83 KB
/
test_ast.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
/* test_ast.c
Test routines for AST implementation.
Part of the data prediction package.
Copyright (c) 2010-2011 Matthias Kramm <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include <assert.h>
#include "easy_ast.h"
#include "io.h"
#include "serialize.h"
environment_t test_environment()
{
row_t*row = row_new(4);
row->inputs[0] = variable_new_continuous(1.0);
row->inputs[1] = variable_new_continuous(2.0);
row->inputs[2] = variable_new_continuous(4.0);
row->inputs[3] = variable_new_categorical(5);
environment_t e;
e.row = row;
return e;
}
node_t*test_serialization(node_t*node)
{
writer_t *w = growingmemwriter_new();
node_write(node, w, 0);
node_destroy(node);
reader_t*r = growingmemwriter_getreader(w);
w->finish(w);
node = node_read(r);
r->dealloc(r);
return node;
}
void test_if()
{
START_CODE(node)
IF
GT
ADD
RAW_PARAM(0)
RAW_PARAM(1)
END;
RAW_PARAM(2)
END;
THEN
CATEGORY_CONSTANT(1)
ELSE
CATEGORY_CONSTANT(2)
END;
END_CODE;
node_print(node);
node = test_serialization(node);
environment_t env = test_environment();
constant_t v = node_eval(node, &env);
constant_print(&v);puts("\n");
v = node_eval(node, &env);
assert(v.c == 2);
assert(v.type == CONSTANT_CATEGORY);
env.row->inputs[2].value = 2.5;
v = node_eval(node, &env);
assert(v.c == 1);
assert(v.type == CONSTANT_CATEGORY);
row_destroy(env.row);
node_destroy(node);
}
void test_array()
{
START_CODE(node)
IF
IN
RAW_PARAM(3)
ARRAY_CONSTANT(array_create(3, 1,2,3));
END;
THEN
CATEGORY_CONSTANT(1)
ELSE
CATEGORY_CONSTANT(2)
END;
END_CODE;
node_print(node);
node = test_serialization(node);
environment_t env = test_environment();
constant_t v = node_eval(node, &env);
assert(v.c == 2);
assert(v.type == CONSTANT_CATEGORY);
env.row->inputs[3].category = 3;
v = node_eval(node, &env);
assert(v.c == 1);
assert(v.type == CONSTANT_CATEGORY);
row_destroy(env.row);
node_destroy(node);
}
int main()
{
test_if();
test_array();
return 0;
}