runtime.c 3.21 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * Cherry programming language
 * Copyright (C) 2013 Christoph Mueller
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
*/

19
#include "./unittest.h"
20
21
#include <cherry/runtime.h>

22
static void test_initialization(cy_const_pointer_t data)
Chris Müller's avatar
Chris Müller committed
23
{
24
	assert(IS_NULL(org_cherry_emptylist));
Chris Müller's avatar
Chris Müller committed
25
26
27
28
29
30
31
32
	assert(IS_BOOLEAN(org_cherry_true));
	assert(IS_BOOLEAN(org_cherry_false));
	assert(IS_SYMBOL(org_cherry_symbol_quote));
	assert(IS_SYMBOL(org_cherry_symbol_define));
	assert(IS_SYMBOL(org_cherry_symbol_lambda));
	assert(IS_SYMBOL(org_cherry_symbol_if));
}

33
static void test_symbollist(cy_const_pointer_t data)
34
{
35
36
	struct org_cherry_symbollist* sym = org_cherry_symbollist();

Chris Müller's avatar
Chris Müller committed
37
38
39
40
41
42
43
44
45
46
	struct org_cherry_value* A = org_cherry_symbollist_get(sym, "A");
	struct org_cherry_value* B = org_cherry_symbollist_get(sym, "B");
	struct org_cherry_value* C = org_cherry_symbollist_get(sym, "A");

	assert(A != B);
	assert(A == C);
	assert(B != C);
}


47
static void test_symbols(cy_const_pointer_t data)
Chris Müller's avatar
Chris Müller committed
48
49
50
51
{
	struct org_cherry_value* A = org_cherry_symbol("A");
	struct org_cherry_value* B = org_cherry_symbol("B");
	struct org_cherry_value* C = org_cherry_symbol("A");
52
53
54
55

	assert(A != B);
	assert(A == C);
	assert(B != C);
56
57
}

Chris Müller's avatar
Chris Müller committed
58

59
static void test_environment(cy_const_pointer_t data)
Chris Müller's avatar
Chris Müller committed
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
{
	struct org_cherry_symbollist* env = org_cherry_symbollist();

	assert(org_cherry_env_add(env, org_cherry_symbol("A"), org_cherry_fixnum(10)));
	assert(org_cherry_env_add(env, org_cherry_symbol("B"), org_cherry_true));

	assert(org_cherry_env_lookup(env, org_cherry_symbol("A"))->meta.type == CY_FIXNUM);
	assert(org_cherry_env_lookup(env, org_cherry_symbol("A"))->fixnum_value == 10);
	assert(org_cherry_env_lookup(env, org_cherry_symbol("B"))->meta.type == CY_BOOLEAN);
	assert(org_cherry_env_lookup(env, org_cherry_symbol("B")) == org_cherry_true);

	env = org_cherry_env_push(env);

	assert(org_cherry_env_add(env, org_cherry_symbol("B"), org_cherry_fixnum(15)));

	assert(org_cherry_env_lookup(env, org_cherry_symbol("B"))->meta.type == CY_FIXNUM);
	assert(org_cherry_env_lookup(env, org_cherry_symbol("B"))->fixnum_value == 15);

	assert(org_cherry_env_lookup(env, org_cherry_symbol("A"))->meta.type == CY_FIXNUM);
	assert(org_cherry_env_lookup(env, org_cherry_symbol("A"))->fixnum_value == 10);
	
	env = org_cherry_env_pop(env);

	assert(org_cherry_env_lookup(env, org_cherry_symbol("B"))->meta.type != CY_FIXNUM);
}


87
88
void test_suite_runtime()
{
89
90
91
92
	unittest_run("org.cherry.runtime/initialize", test_initialization, 0, 100);
	unittest_run("org.cherry.runtime/symbols", test_symbols, 0, 100);
	unittest_run("org.cherry.runtime/symbollist", test_symbollist, 0, 100);
	unittest_run("org.cherry.runtime/environment", test_environment, 0, 100);
93
94
}