-
Notifications
You must be signed in to change notification settings - Fork 12
/
test.c
174 lines (163 loc) · 5.2 KB
/
test.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
#include <stdio.h>
#include <string.h>
#include "citrine.h"
#ifdef INCLUDETESTS
int ctr_internal_testcounter = 1;
/**
* Displays test no, or aborts testing because of failed assumption.
*
* @param integer t
*/
void ctr_test( int t ) {
if (t) {
printf("[%d]", ctr_internal_testcounter++);
} else {
printf("Error test #%d failed.", ctr_internal_testcounter);
exit(1);
}
}
/**
* Perform token test
*/
void ctr_coretest_tokens() {
char* buffer;
int token;
ctr_program_length = 40;
buffer = calloc(40,1);
sprintf( buffer, "%s", CTR_DICT_PAREN_OPEN );
ctr_clex_load( buffer );
token = ctr_clex_tok();
ctr_test(token == CTR_TOKEN_PAROPEN);
ctr_test(strlen(ctr_clex_tok_value())==0);
ctr_test(strcmp(ctr_clex_tok_describe(token),CTR_DICT_PAREN_OPEN)==0);
sprintf( buffer, "%s", CTR_DICT_PAREN_CLOSE );
ctr_clex_load( buffer );
token = ctr_clex_tok();
ctr_test(token == CTR_TOKEN_PARCLOSE);
ctr_test(strlen(ctr_clex_tok_value())==0);
ctr_test(strcmp(ctr_clex_tok_describe(token),CTR_DICT_PAREN_CLOSE)==0);
sprintf( buffer, "%s", CTR_DICT_BLOCK_START );
ctr_clex_load( buffer );
token = ctr_clex_tok();
ctr_test(token == CTR_TOKEN_BLOCKOPEN);
ctr_test(strlen(ctr_clex_tok_value())==0);
ctr_test(strcmp(ctr_clex_tok_describe(token),CTR_DICT_BLOCK_START)==0);
sprintf( buffer, "%s", CTR_DICT_BLOCK_END );
ctr_clex_load( buffer );
token = ctr_clex_tok();
ctr_test(token == CTR_TOKEN_BLOCKCLOSE);
ctr_test(strlen(ctr_clex_tok_value())==0);
ctr_test(strcmp(ctr_clex_tok_describe(token),CTR_DICT_BLOCK_END)==0);
sprintf( buffer, "%s", CTR_DICT_END_OF_LINE );
ctr_clex_load( buffer );
token = ctr_clex_tok();
ctr_test(token == CTR_TOKEN_DOT);
ctr_test(strlen(ctr_clex_tok_value())==0);
ctr_test(strcmp(ctr_clex_tok_describe(token),CTR_DICT_END_OF_LINE)==0);
sprintf( buffer, "%s", CTR_DICT_MESSAGE_CHAIN );
ctr_clex_load( buffer );
token = ctr_clex_tok();
ctr_test(token == CTR_TOKEN_CHAIN);
ctr_test(strlen(ctr_clex_tok_value())==0);
ctr_test(strcmp(ctr_clex_tok_describe(token),CTR_DICT_MESSAGE_CHAIN)==0);
sprintf( buffer, "%s", CTR_DICT_PARAMETER_PREFIX );
ctr_clex_load( buffer );
token = ctr_clex_tok();
ctr_test(token == CTR_TOKEN_COLON);
ctr_test(strlen(ctr_clex_tok_value())==0);
ctr_test(strcmp(ctr_clex_tok_describe(token),CTR_DICT_PARAMETER_PREFIX)==0);
sprintf( buffer, "%s ", CTR_DICT_RETURN );
ctr_clex_load( buffer );
token = ctr_clex_tok();
ctr_test(token == CTR_TOKEN_RET);
ctr_test(strlen(ctr_clex_tok_value())==0);
ctr_test(strcmp(ctr_clex_tok_describe(token),CTR_DICT_RETURN)==0);
sprintf( buffer, "%s", CTR_DICT_ASSIGN );
ctr_clex_load( buffer );
token = ctr_clex_tok();
ctr_test(token == CTR_TOKEN_ASSIGNMENT);
ctr_test(strlen(ctr_clex_tok_value())==0);
ctr_test(strcmp(ctr_clex_tok_describe(token),CTR_DICT_ASSIGN)==0);
ctr_program_length = 0;
ctr_clex_load( buffer );
token = ctr_clex_tok();
ctr_test(token == CTR_TOKEN_FIN);
ctr_test(strlen(ctr_clex_tok_value())==0);
ctr_test(strcmp(ctr_clex_tok_describe(token),"end of program")==0);
free(buffer);
}
/**
* Perform parser test
*/
void ctr_coretest_parser() {
char* buffer;
ctr_program_length = 40;
buffer = calloc(40,1);
sprintf(buffer, "☞ x ≔ 1.");
ctr_clex_load(buffer);
ctr_tnode* tree_node = ctr_cparse_expr(0);
ctr_test(tree_node->type == CTR_AST_NODE_EXPRASSIGNMENT);
ctr_test(tree_node->nodes->node->type == CTR_AST_NODE_REFERENCE);
ctr_test(tree_node->nodes->node->vlen == 1);
ctr_test(strcmp(tree_node->nodes->node->value,"x")==0);
ctr_test(tree_node->nodes->next->node->type == CTR_AST_NODE_LTRNUM);
free(buffer);
}
/**
* Test memory functions.
*/
void ctr_coretest_memory() {
char* chunk;
size_t size;
size_t expected_size;
chunk = ctr_heap_allocate(10);
size = (size_t) *((size_t*) ((char*)chunk - sizeof(size_t)));
expected_size = (ctr_gc_mode & 8) ? 32 : (10 + sizeof(size_t));
ctr_test(size == expected_size);
chunk = ctr_heap_allocate(100);
size = (size_t) *((size_t*) ((char*)chunk - sizeof(size_t)));
expected_size = (ctr_gc_mode & 8) ? 128 : (100 + sizeof(size_t));
ctr_test(size == expected_size);
chunk = ctr_heap_allocate(32);
size = (size_t) *((size_t*) ((char*)chunk - sizeof(size_t)));
expected_size = (ctr_gc_mode & 8) ? 64 : (32 + sizeof(size_t));
ctr_test(size == expected_size);
chunk = ctr_heap_allocate((32 - sizeof(size_t)));
size = (size_t) *((size_t*) ((char*)chunk - sizeof(size_t)));
expected_size = (ctr_gc_mode & 8) ? 32 : 33;
ctr_test(size == expected_size);
/* heap allocator will not fail for size 0, will just return an empty block */
/* btw, also not fail for -1 = just a huge block (unsigned!) */
chunk = ctr_heap_allocate(0);
size = (size_t) *((size_t*) ((char*)chunk - sizeof(size_t)));
expected_size = (ctr_gc_mode & 8) ? 32 : sizeof(size_t);
ctr_test(size == expected_size);
/* heap free will not crash on NULL */
ctr_heap_free(NULL);
}
/**
* Run Core tests.
*/
void ctr_coretest() {
printf("Running Internal Tests\n");
ctr_coretest_tokens();
ctr_coretest_parser();
ctr_coretest_memory();
exit(0);
}
#else
/**
* No Core tests.
*/
void ctr_coretest() {
printf("No Core Tests Included\n");
exit(0);
}
#endif
/**
* Start internal unit tests.
*/
ctr_object* ctr_program_test( ctr_object* myself, ctr_argument* argumentList ) {
ctr_coretest();
return CtrStdNil;
}