aboutsummaryrefslogtreecommitdiff
path: root/test/test_internal.c
blob: cb0572a779b7c19b50d845fd24a2dc6a607ead7d (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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// This software disclaims copyright. Do what you want with it. Be gay, do
// crime. Originally written by Alexis Lockwood in 2021. Ⓐ

#include <munit.h>
#include "tsupport.h"
#include "ls_internal.h"
#include "ls_expr.h"
#include "ls_lex.h"

#include <stdbool.h>
#include <stddef.h>
#include <inttypes.h>
#include <stdio.h>

#pragma GCC diagnostic ignored "-Wunused-parameter"

#define DECLTEST(name) static MunitResult name(const MunitParameter params[], \
	void * data)

DECLTEST(_ls_lex)
{
	ls_t ls;
	ls_token_t tok;
        LS_TEST_CATCH(ls)
	{
		munit_errorf("Caught error: %d\n", ls.error);
		return MUNIT_FAIL;
	}

	// Do two-char opers parse right?
	static const char * operstrings[] = {
		"< <= = <> >= >",
		"<<==<>>=>",
		NULL
	};
	for (size_t i = 0; operstrings[i]; i++)
	{
		ls_test_setup(&ls, operstrings[i]);
		ls_lex(&ls, &tok);
		munit_assert_int(tok.ty, ==, LS_TOK_OPERATOR);
		munit_assert_int(tok.body.oper_val, ==, OPER_LT);
		ls_lex(&ls, &tok);
		munit_assert_int(tok.ty, ==, LS_TOK_OPERATOR);
		munit_assert_int(tok.body.oper_val, ==, OPER_LEQ);
		ls_lex(&ls, &tok);
		munit_assert_int(tok.ty, ==, LS_TOK_OPERATOR);
		munit_assert_int(tok.body.oper_val, ==, OPER_EQ);
		ls_lex(&ls, &tok);
		munit_assert_int(tok.ty, ==, LS_TOK_OPERATOR);
		munit_assert_int(tok.body.oper_val, ==, OPER_NEQ);
		ls_lex(&ls, &tok);
		munit_assert_int(tok.ty, ==, LS_TOK_OPERATOR);
		munit_assert_int(tok.body.oper_val, ==, OPER_GEQ);
		ls_lex(&ls, &tok);
		munit_assert_int(tok.ty, ==, LS_TOK_OPERATOR);
		munit_assert_int(tok.body.oper_val, ==, OPER_GT);
	}

	// Numbers are done separately in _ls_parse_num

	ls_test_setup(&ls, "foo");
	ls_lex(&ls, &tok);
	munit_assert_int(tok.ty, ==, LS_TOK_WORD);
	munit_assert_string_equal(tok.body.word_val, "foo");

	ls_test_setup(&ls, "foo bar");
	ls_lex(&ls, &tok);
	munit_assert_int(tok.ty, ==, LS_TOK_WORD);
	munit_assert_string_equal(tok.body.word_val, "foo");
	ls_lex(&ls, &tok);
	munit_assert_int(tok.ty, ==, LS_TOK_WORD);
	munit_assert_string_equal(tok.body.word_val, "bar");

	ls_test_setup(&ls, "abittoolong foo");
	ls_lex(&ls, &tok);
	munit_assert_int(tok.ty, ==, LS_TOK_WORD);
	munit_assert_string_equal(tok.body.word_val, "abittoolo");
	ls_lex(&ls, &tok);
	munit_assert_int(tok.ty, ==, LS_TOK_WORD);
	munit_assert_string_equal(tok.body.word_val, "foo");

	ls_test_setup(&ls, "  hasspace");
	ls_lex(&ls, &tok);
	munit_assert_int(tok.ty, ==, LS_TOK_WORD);
	munit_assert_string_equal(tok.body.word_val, "hasspace");
	ls_lex(&ls, &tok);
	munit_assert_int(tok.ty, ==, LS_TOK_NONE);

	ls_test_setup(&ls, "_foo_");
	ls_lex(&ls, &tok);
	munit_assert_int(tok.ty, ==, LS_TOK_WORD);
	munit_assert_string_equal(tok.body.word_val, "_foo_");

	ls_test_setup(&ls, "\x7F");
	LS_EXPECT_ERR(ls, LS_SYNTAX_ERROR, ls_lex(&ls, &tok));

	return MUNIT_OK;
}

DECLTEST(_ls_convert_kw)
{
	// Same hash
	munit_assert_int(ls_convert_kw("ABS"), ==, LS_KW_ABS);
	munit_assert_int(ls_convert_kw("CLOSE"), ==, LS_KW_CLOSE);

	munit_assert_int(ls_convert_kw("ERASE"), ==, LS_KW_ERASE);
	munit_assert_int(ls_convert_kw("ErAsE"), ==, LS_KW_ERASE);

	munit_assert_int(ls_convert_kw("FOO"), ==, LS_NOT_A_KW);

	return MUNIT_OK;
}

DECLTEST(_ls_parse_num)
{
	ls_t ls;
        LS_TEST_CATCH(ls)
	{
		munit_errorf("Caught error %d\n", ls.error);
		return MUNIT_FAIL;
	}

	ls_value_t val;
	ls_token_t tok;

	ls_test_setup(&ls, "42, &H42, &o42, &42, &b101010");

	ls_eval_expr(&ls, &val, NULL);
	munit_assert_int(val.body.integer.value, ==, 42);
	ls_lex(&ls, &tok); // eat the comma

	ls_eval_expr(&ls, &val, NULL);
	munit_assert_int(val.body.integer.value, ==, 0x42);
	ls_lex(&ls, &tok); // eat the comma

	ls_eval_expr(&ls, &val, NULL);
	munit_assert_int(val.body.integer.value, ==, 042);
	ls_lex(&ls, &tok); // eat the comma

	ls_eval_expr(&ls, &val, NULL);
	munit_assert_int(val.body.integer.value, ==, 042);
	ls_lex(&ls, &tok); // eat the comma

	ls_eval_expr(&ls, &val, NULL);
	munit_assert_int(val.body.integer.value, ==, 42);
	ls_lex(&ls, &tok); // eat the comma

	ls_test_setup(&ls, "0e42");
	LS_EXPECT_ERR(ls, LS_SYNTAX_ERROR, ls_eval_expr(&ls, &val, NULL));
	return MUNIT_OK;
}

static void _check_expr(char const * s, int expected)
{
	char const * testing = NULL;
	ls_t ls;

        LS_TEST_CATCH(ls)
	{
		if (testing)
			munit_errorf("Caught error %d in \"%s\"\n",
				ls.error, testing);
		else
			munit_errorf("Caught error %d\n", ls.error);
	}

	testing = s;
	ls_value_t val;
	ls_test_setup(&ls, s);
	size_t sz_pool = ls_mem_avail(&ls);
	ls_eval_expr(&ls, &val, NULL);
	munit_assert_int(val.ty, ==, LS_TY_INT);
	munit_assert_int(val.body.integer.value, ==, expected);
	munit_assert_size(sz_pool, ==, ls_mem_avail(&ls));
}

DECLTEST(_ls_eval_expr)
{
	_check_expr("42", 42);
	_check_expr("(&H28 + (1 + 1))", 42);
	_check_expr("2 + 3 * 4", 14);
	_check_expr("(2 + 3) * 4", 20);
	_check_expr("abs -42", 42);
	_check_expr("\x80 - 42", 42); // compressed notation for abs -42
	_check_expr("10 + -2", 8);
	_check_expr("-2 + 10", 8);
	_check_expr("-2 + -10", -12);
	_check_expr("43 xor 75", 96);
	_check_expr("not 42", -43);
	_check_expr("43 < 42", 0);
	_check_expr("43 <> 42", 1);
	_check_expr("43 = 42", 0);

	// Same strings with all unnecessary whitespace removed. We test them
	// both ways: with whitespace, to ensure whitespace is always handled
	// correctly; without whitespace, to ensure none of the lexer branches
	// accidentally consume extra chars at the end.
	_check_expr("(&H28+(1+1))", 42);
	_check_expr("2+3*4", 14);
	_check_expr("(2+3)*4", 20);
	_check_expr("abs-42", 42);
	_check_expr("\x80-42", 42); // compressed notation for abs -42
	_check_expr("10+-2", 8);
	_check_expr("-2+10", 8);
	_check_expr("-2+-10", -12);
	_check_expr("43xor 75", 96);
	_check_expr("43\27375", 96); // most compact form
	_check_expr("not 42", -43);
	_check_expr("43<42", 0);
	_check_expr("43<>42", 1);
	_check_expr("43=42", 0);

	return MUNIT_OK;
}

DECLTEST(_ls_lookup_var)
{
	ls_t ls;
        LS_TEST_CATCH(ls)
	{
		munit_errorf("Caught error: %d\n", ls.error);
		return MUNIT_FAIL;
	}

	ls_test_setup(&ls, "2*foo + bar/2");

	// Create the global stack frame, currently there's no function to
	// do that
	ls.callstack = ls_alloc(&ls);
	ls.callstack->ty = LS_TY_SCTX_CALL;
	ls.callstack->prev = NULL;
	ls.callstack->next = NULL;
	ls.callstack->body.sctx_call.pc = LS_ADDR_NULL;
	ls.callstack->body.sctx_call.readptr = LS_ADDR_NULL;

	ls_value_t * var_foo = ls_new_var(&ls, "foo");
	ls_write_int_var(&ls, var_foo, 20);

	ls_value_t * var_bar = ls_new_var(&ls, "bar");
	ls_write_int_var(&ls, var_bar, 4);

	ls_value_t val;
	size_t sz_pool = ls_mem_avail(&ls);
	ls_eval_expr(&ls, &val, NULL);
	munit_assert_int(val.ty, ==, LS_TY_INT);
	munit_assert_int(val.body.integer.value, ==, 42);
	munit_assert_size(sz_pool, ==, ls_mem_avail(&ls));

	return MUNIT_OK;
}

DECLTEST(_ls_let)
{
	ls_t ls;
        LS_TEST_CATCH(ls)
	{
		munit_errorf("Caught error: %d\n", ls.error);
		return MUNIT_FAIL;
	}

	ls_test_setup(&ls,
		"x = 13\n"
		"let y = x * 100\n"
		"z = y + 37\n"
	);

	// Create the global stack frame, currently there's no function to
	// do that
	ls.callstack = ls_alloc(&ls);
	ls.callstack->ty = LS_TY_SCTX_CALL;
	ls.callstack->prev = NULL;
	ls.callstack->next = NULL;
	ls.callstack->body.sctx_call.pc = LS_ADDR_NULL;
	ls.callstack->body.sctx_call.readptr = LS_ADDR_NULL;

	ls_exec_line(&ls);
	ls_exec_line(&ls);
	ls_exec_line(&ls);

	ls_value_t * var_z = ls_find_var(&ls, "z");
	munit_assert_int(ls_read_int_var(&ls, var_z), ==, 1337);

	return MUNIT_OK;
}

DECLTEST(_ls_run)
{
	static const size_t szpool = 1000;
	ls_t ls;
	ls_value_t pool[szpool];

	ls_setup_run(&ls,
		"x = 13;let y=x*100\n"
		"z = y + 37\n"
	);

	ls_error_t e = ls_run(&ls, pool, szpool);
	munit_assert_int(e, ==, LS_OK);

	ls_value_t * var_z = ls_find_var(&ls, "z");
	munit_assert_int(ls_read_int_var(&ls, var_z), ==, 1337);
/*
	fprintf(stdout, "\n");
	for (size_t i = 0; i < 16; i++)
	{
		ls_print_value(stdout, &pool[i], &pool[0]);
		fprintf(stdout, "\n");
	}
*/
	return MUNIT_OK;
}

DECLTEST(_ls_simple_script)
{
	static const size_t szpool = 1000;
	ls_t ls;
	ls_value_t pool[szpool];

	ls_setup_run(&ls,
		"x = 13\n"
		"y = 24\n"
		"y = y + x\n"
		"x = y + x * 100\n"
		"print \"Hello, world! \",\n"
		"print \"x = \", x, \", twelve = \", 12\n"
	);

	ls_error_t e = ls_run(&ls, pool, szpool);
	munit_assert_int(e, ==, LS_OK);

	fprintf(stdout, "\n");
	for (size_t i = 0; i < 16; i++)
	{
		ls_print_value(stdout, &pool[i], &pool[0]);
		fprintf(stdout, "\n");
	}

	return MUNIT_OK;
}

#define TEST(name, func) {name, func, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL}
static MunitTest tests[] = {
	TEST("/ls_lex", _ls_lex),
	TEST("/ls_convert_kw", _ls_convert_kw),
	TEST("/ls_parse_num", _ls_parse_num),
	TEST("/ls_eval_expr", _ls_eval_expr),
	TEST("/ls_lookup_var", _ls_lookup_var),
	TEST("/ls_let", _ls_let),
	TEST("/ls_run", _ls_run),
	TEST("/ls_simple_script", _ls_simple_script),
	{ 0 }
};

static const MunitSuite suite = {
	"/",
	tests,
	NULL, // suites
	1, // iterations
	MUNIT_SUITE_OPTION_NONE
};

int main(int argc, char ** argv)
{
	return munit_suite_main(&suite, NULL, argc, argv);
}