]> git.lizzy.rs Git - nothing.git/blob - test/interpreter_suite.h
(#781) Don't show the cursor in game play mode
[nothing.git] / test / interpreter_suite.h
1 #ifndef INTERPRETER_SUITE_H_
2 #define INTERPRETER_SUITE_H_
3
4 #include "test.h"
5 #include "ebisp/builtins.h"
6 #include "ebisp/expr.h"
7 #include "ebisp/interpreter.h"
8
9 TEST(equal_test)
10 {
11     Gc *gc = create_gc();
12
13     struct Expr nil1 = NIL(gc);
14     struct Expr nil2 = NIL(gc);
15     ASSERT_TRUE(equal(nil1, nil2), {
16             fprintf(stderr, "Expected: ");
17             print_expr_as_sexpr(stderr, nil1);
18             fprintf(stderr, "\n");
19
20             fprintf(stderr, "Actual: ");
21             print_expr_as_sexpr(stderr, nil2);
22             fprintf(stderr, "\n");
23     });
24
25     struct Expr list1 =
26         CONS(gc, SYMBOL(gc, "a"),
27              CONS(gc, SYMBOL(gc, "b"),
28                   CONS(gc, SYMBOL(gc, "c"),
29                        NIL(gc))));
30     struct Expr list2 =
31         CONS(gc, SYMBOL(gc, "a"),
32              CONS(gc, SYMBOL(gc, "b"),
33                   CONS(gc, SYMBOL(gc, "c"),
34                        NIL(gc))));
35     ASSERT_TRUE(equal(list1, list2), {
36             fprintf(stderr, "Expected: ");
37             print_expr_as_sexpr(stderr, list1);
38             fprintf(stderr, "\n");
39
40             fprintf(stderr, "Actual: ");
41             print_expr_as_sexpr(stderr, list2);
42             fprintf(stderr, "\n");
43     });
44
45     destroy_gc(gc);
46
47     return 0;
48 }
49
50 TEST(assoc_test)
51 {
52     Gc *gc = create_gc();
53
54     struct Expr nil = NIL(gc);
55
56     struct Expr a = SYMBOL(gc, "a");
57     struct Expr b = SYMBOL(gc, "b");
58     struct Expr c = SYMBOL(gc, "c");
59
60     struct Expr a_pair = CONS(gc, a, NUMBER(gc, 10.0f));
61     struct Expr b_pair = CONS(gc, b, NUMBER(gc, 20.0f));
62     struct Expr c_pair = CONS(gc, c, NUMBER(gc, 30.0f));
63
64     struct Expr alist =
65         CONS(gc, a_pair,
66              CONS(gc, b_pair,
67                   CONS(gc, c_pair, nil)));
68
69     struct Expr assoc_result = assoc(a, alist);
70
71     ASSERT_TRUE(equal(a_pair, assoc_result), {
72             fprintf(stderr, "Expected: ");
73             print_expr_as_sexpr(stderr, a_pair);
74             fprintf(stderr, "\n");
75
76             fprintf(stderr, "Actual: ");
77             print_expr_as_sexpr(stderr, assoc_result);
78             fprintf(stderr, "\n");
79     });
80
81     destroy_gc(gc);
82
83     return 0;
84 }
85
86 TEST(match_list_test)
87 {
88     Gc *gc = create_gc();
89
90     struct Expr input = list(
91         gc, "dsqe",
92         42,
93         "hello",
94         "world",
95         CONS(gc, NUMBER(gc, 1), NUMBER(gc, 2)));
96
97     long int d = 0;
98     const char *s = NULL;
99     const char *q = NULL;
100     struct Expr e = NIL(gc);
101
102     struct EvalResult result = match_list(gc, "dsqe", input, &d, &s, &q, &e);
103     ASSERT_FALSE(result.is_error, {
104             fprintf(stderr, "Unpack failed: ");
105             print_expr_as_sexpr(stderr, result.expr);
106             fprintf(stderr, "\n");
107     });
108
109     ASSERT_TRUE(d == 42, {
110         fprintf(stderr, "Expected: 42, Actual: %ld\n", d);
111     });
112     ASSERT_TRUE(strcmp(s, "hello") == 0, {
113         fprintf(stderr, "Expected: hello, Actual: %s\n", s);
114     });
115     ASSERT_TRUE(strcmp(q, "world") == 0, {
116         fprintf(stderr, "Expected: world, Actual: %s\n", q);
117     });
118     ASSERT_TRUE(equal(e, CONS(gc, NUMBER(gc, 1), NUMBER(gc, 2))), {
119             fprintf(stderr, "Expected: ");
120             print_expr_as_sexpr(stderr, CONS(gc, NUMBER(gc, 1), NUMBER(gc, 2)));
121             fprintf(stderr, ", Actual: ");
122             print_expr_as_sexpr(stderr, e);
123             fprintf(stderr, "\n");
124     });
125
126     destroy_gc(gc);
127
128     return 0;
129 }
130
131 TEST(match_list_empty_list_test)
132 {
133     Gc *gc = create_gc();
134
135     long int d = 0;
136     struct EvalResult result = match_list(gc, "d", NIL(gc), &d);
137
138     ASSERT_TRUE(result.is_error, {
139             fprintf(stderr, "Unpack did not fail\n");
140     });
141
142     destroy_gc(gc);
143
144     return 0;
145 }
146
147 TEST(match_list_head_tail_test)
148 {
149     Gc *gc = create_gc();
150
151     struct Expr input = list(gc, "dddd", 1, 2, 3, 4);
152
153     long int x = 0;
154     struct Expr xs = NIL(gc);
155
156     struct EvalResult result = match_list(gc, "d*", input, &x, &xs);
157     ASSERT_TRUE(!result.is_error, {
158             fprintf(stderr, "Could not unpack input: ");
159             print_expr_as_sexpr(stderr, result.expr);
160             fprintf(stderr, "\n");
161     });
162
163     ASSERT_TRUE(x == 1, {
164             fprintf(stderr, "Expected: 1, Actual: %ld\n", x);
165     });
166
167     ASSERT_TRUE(equal(xs, list(gc, "ddd", 2, 3, 4)), {
168             fprintf(stderr, "Expected: (2 3 4), Actual: ");
169             print_expr_as_sexpr(stderr, xs);
170             fprintf(stderr, "\n");
171     })
172
173     destroy_gc(gc);
174
175     return 0;
176 }
177
178 TEST(match_list_wildcard_test)
179 {
180     Gc *gc = create_gc();
181
182     struct Expr input = list(
183         gc, "dddd", 1, 2, 3, 4);
184
185     long int x = 0, y = 0;
186     struct EvalResult result = match_list(gc, "dddd", input, &x, NULL, &y, NULL);
187     ASSERT_TRUE(!result.is_error, {
188             fprintf(stderr, "Matching failed: ");
189             print_expr_as_sexpr(stderr, result.expr);
190             fprintf(stderr, "\n");
191     });
192
193     ASSERT_LONGINTEQ(1L, x);
194     ASSERT_LONGINTEQ(3L, y);
195
196     destroy_gc(gc);
197
198     return 0;
199 }
200
201 TEST(match_list_singleton_tail_test)
202 {
203     Gc *gc = create_gc();
204
205     struct Expr input = list(gc, "d", 1);
206     long int x;
207     struct Expr xs = NIL(gc);
208     struct EvalResult res = match_list(gc, "d*", input, &x, &xs);
209
210     ASSERT_TRUE(!res.is_error, {
211             fprintf(stderr, "Matching failed: ");
212             print_expr_as_sexpr(stderr, res.expr);
213             fprintf(stderr, "\n");
214     });
215
216     ASSERT_LONGINTEQ(1L, x);
217     ASSERT_TRUE(nil_p(xs), {
218             fprintf(stderr, "Tail doesn't appear to be NIL: ");
219             print_expr_as_sexpr(stderr, xs);
220             fprintf(stderr, "\n");
221     });
222
223     destroy_gc(gc);
224
225     return 0;
226 }
227
228 TEST_SUITE(interpreter_suite)
229 {
230     TEST_RUN(equal_test);
231     TEST_RUN(assoc_test);
232     TEST_RUN(match_list_test);
233     TEST_RUN(match_list_empty_list_test);
234     TEST_RUN(match_list_head_tail_test);
235     TEST_RUN(match_list_wildcard_test);
236     TEST_RUN(match_list_singleton_tail_test);
237
238     return 0;
239 }
240
241 #endif  // INTERPRETER_SUITE_H_