More functional Makefile.
[Jack-Compiler.git] / test.c
blob2bc5fcdc46e2e80460e736b78a4e6126fc7de479
1 #include <stdbool.h>
2 #include <stdio.h>
3 #include <string.h>
5 #include "error.h"
6 #include "jack.h"
7 #include "parse.h"
8 #include "test.h"
9 #include "token.h"
11 #define PASSED 0
12 #define FAILED 1
14 void test_msg_start(char *msg)
16 int msg_length = strlen(msg);
17 printf("%s", msg);
19 /* 74 is 80 - length of "PASSED" */
20 while(msg_length < 74)
22 putchar('.');
23 msg_length++;
27 void test_msg_end(int pass)
29 if(pass == PASSED)
31 printf("PASSED\n");
32 } else {
33 printf("FAILED\n");
34 exit_error(0, "UNIT TESTING FAILED");
38 int test_c_comments()
40 int result = 0;
41 char *pStart = NULL;
42 char source[] = "/* this is a test */ \tclass \0";
44 test_msg_start("Testing C Comments");
46 pC = pStart = source;
47 pC = advance(pC, pT);
49 /* token should be correct */
50 if(strcmp(pT, "class") != 0) { result++; }
52 /* pointer to code should be in correct position */
53 if(pC - pStart != 27) { result++; }
55 if(result == PASSED)
57 test_msg_end(PASSED);
58 } else {
59 test_msg_end(FAILED);
62 return result;
65 int test_nested_c_comments()
67 int result = 0;
68 char *pStart = NULL;
69 char source[] = "/* this /*is a test */ \tclass \0";
71 test_msg_start("Testing Nested C Comments");
72 pC = pStart = source;
73 pC = advance(pC, pT);
75 /* token should be correct */
76 if(strcmp(pT, "class") != 0) { result++; }
78 /* pointer to code should be in correct position */
79 if(pC - pStart != 29) { result++; }
81 if(result == PASSED)
83 test_msg_end(PASSED);
84 } else {
85 test_msg_end(FAILED);
88 return result;
91 int test_cpp_comments()
93 int result = 0;
94 char *pStart = NULL;
95 char source[] = "// this is a test\nclass \0";
97 test_msg_start("Testing C++ Comments");
98 pC = pStart = source;
99 pC = advance(pC, pT);
101 /* token should be correct */
102 if(strcmp(pT, "class") != 0) { result++; }
104 /* pointer to code should be in correct position */
105 if(pC - pStart != 23) { result++; }
107 if(result == PASSED)
109 test_msg_end(PASSED);
110 } else {
111 test_msg_end(FAILED);
114 return result;
117 int test_nested_cpp_comments()
119 int result = 0;
120 char *pStart = NULL;
121 char source[] = "// this is a //test\nclass \0";
123 test_msg_start("Testing Nested C++ Comments");
124 pC = pStart = source;
125 pC = advance(pC, pT);
127 /* token should be correct */
128 if(strcmp(pT, "class") != 0) { result++; }
130 /* pointer to code should be in correct position */
131 if(pC - pStart != 25) { result++; }
133 if(result == PASSED)
135 test_msg_end(PASSED);
136 } else {
137 test_msg_end(FAILED);
140 return result;
143 int test_literal_strings()
145 int result = 0;
146 char *pStart = NULL;
147 char source[] = "\"S\" \0";
149 test_msg_start("Testing String Literals");
150 pC = pStart = source;
151 pC = advance(pC, pT);
153 /* token should be correct */
154 if(strcmp(pT, "S") != 0) { result++; }
156 /* pointer to code should be in correct position */
157 if(pC - pStart != 3) { result++; }
159 if(result == PASSED)
161 test_msg_end(PASSED);
162 } else {
163 test_msg_end(FAILED);
166 return result;
169 int test_symbol_recog()
171 int result = 0;
172 char *pStart = NULL;
173 char source[] = "(){\0";
175 test_msg_start("Testing Symbol Recognition");
176 pC = pStart = source;
177 pC = advance(pC, pT);
179 /* token should be correct */
180 if(strcmp(pT, "(") != 0) { result++; }
182 /* pointer to code should be in correct position */
183 if(pC - pStart != 1) { result++; }
185 if(result == PASSED)
187 test_msg_end(PASSED);
188 } else {
189 test_msg_end(FAILED);
192 return result;
195 int test_keyword_recog()
197 int result = 0;
198 return result;
201 int test_token_type()
203 int result = 0;
204 int i = 0;
206 test_msg_start("Testing Token Type Recognition");
208 while(keywords[i] != NULL)
210 strcpy(pT, keywords[i]);
211 tk = -1; /* be certain this value is changed */
212 tk = token_type(pT);
213 if(tk != KEYWORD) { result++; }
214 i++;
217 if(result == PASSED)
219 test_msg_end(PASSED);
220 } else {
221 test_msg_end(FAILED);
224 return result;
227 int test_end_of_code()
229 int result = 0;
230 char source[] = "\n\0";
232 test_msg_start("Testing For EOF Source");
234 pC = source;
235 result = has_more_tokens(pC);
237 if(result == PASSED)
239 test_msg_end(PASSED);
240 } else {
241 test_msg_end(FAILED);
244 return result;
247 int test_no_code()
249 int result = 0;
250 test_msg_start("Testing For NULL Source");
252 pC = NULL;
253 result = has_more_tokens(pC);
255 if(result == PASSED)
257 test_msg_end(PASSED);
258 } else {
259 test_msg_end(FAILED);
262 return result;
265 int test_parse_expression()
267 int result = 0;
268 char source[] = "-(-(2 + (-12 / 12)*83));\n";
270 test_msg_start("Testing Expression Parsing - Numerical");
272 pC = source;
273 pC = advance(pC, pT);
274 tk = token_type(pT);
276 parse_expression(0);
278 /* pointer to code should be in correct position */
279 if(pC - source != 24) { result++; }
281 /* token should be correct */
282 if(strcmp(pT, ";") != 0) { result++; }
284 if(result == PASSED)
286 test_msg_end(PASSED);
287 } else {
288 test_msg_end(FAILED);
291 return result;
294 int test_parse_expression_keyword()
296 int result = 0, i = 0;
297 char *source[] =
299 "true;\n",
300 "null;\n",
301 "this;\n",
302 "false;\n"
305 test_msg_start("Testing Expression Parsing - Keywords");
307 while(i <= 3)
309 pC = source[i];
310 pC = advance(pC, pT);
311 tk = token_type(pT);
313 parse_expression(0);
315 /* pointer to code should be in correct position */
316 if(i <= 2)
318 if(pC - source[i] != 5) { result++; }
319 } else {
320 if(pC - source[i] != 6) { result++; }
323 /* token should be correct */
324 if(strcmp(pT, ";") != 0) { result++; }
325 i++;
328 if(result == PASSED)
330 test_msg_end(PASSED);
331 } else {
332 test_msg_end(FAILED);
335 return result;
338 int test_parse_expression_nested_array()
340 int result = 0;
341 char source[] = "data1[data2[12]];\n";
343 test_msg_start("Testing Expression Parsing - Nested Arrays");
345 pC = source;
346 pC = advance(pC, pT);
347 tk = token_type(pT);
349 parse_expression(0);
351 /* pointer to code should be in correct position */
352 if(pC - source != 17) { result++; }
354 /* token should be correct */
355 if(strcmp(pT, ";") != 0) { result++; }
357 if(result == PASSED)
359 test_msg_end(PASSED);
360 } else {
361 test_msg_end(FAILED);
364 return result;
367 int test_parse_expression_nested_sub()
369 int result = 0;
370 char source[] = "data1(data2(12));\n";
372 test_msg_start("Testing Expression Parsing - Nested Subroutine");
374 pC = source;
375 pC = advance(pC, pT);
376 tk = token_type(pT);
378 parse_expression(0);
380 /* pointer to code should be in correct position */
381 if(pC - source != 17) { result++; }
383 /* token should be correct */
384 if(strcmp(pT, ";") != 0) { result++; }
386 if(result == PASSED)
388 test_msg_end(PASSED);
389 } else {
390 test_msg_end(FAILED);
393 return result;
396 int test_parse_expression_class_sub()
398 int result = 0;
399 char source[] = "class_name.sub_name();\n";
401 test_msg_start("Testing Expression Parsing - Class Subroutine");
403 pC = source;
404 pC = advance(pC, pT);
405 tk = token_type(pT);
407 parse_expression(0);
409 /* pointer to code should be in correct position */
410 if(pC - source != 22) { result++; }
412 /* token should be correct */
413 if(strcmp(pT, ";") != 0) { result++; }
415 if(result == PASSED)
417 test_msg_end(PASSED);
418 } else {
419 test_msg_end(FAILED);
422 return result;
425 int test_parse_expression_list()
427 int result = 0;
428 char source[] = "class_name.sub_name(87, var_name);\n";
430 test_msg_start("Testing Expression Parsing - Expression List");
432 pC = source;
433 pC = advance(pC, pT);
434 tk = token_type(pT);
436 parse_expression(0);
438 /* pointer to code should be in correct position */
439 if(pC - source != 34) { result++; }
441 /* token should be correct */
442 if(strcmp(pT, ";") != 0) { result++; }
444 if(result == PASSED)
446 test_msg_end(PASSED);
447 } else {
448 test_msg_end(FAILED);
451 return result;
454 int test_all()
456 int result = 0;
458 /* modify settings regardless of command line options */
459 settings.tokens = 0;
461 result += test_c_comments();
462 result += test_nested_c_comments();
463 result += test_cpp_comments();
464 result += test_nested_cpp_comments();
465 result += test_literal_strings();
466 result += test_symbol_recog();
467 result += test_keyword_recog();
468 result += test_token_type();
469 result += test_end_of_code();
470 result += test_no_code();
471 result += test_parse_expression();
472 result += test_parse_expression_keyword();
473 result += test_parse_expression_nested_array();
474 result += test_parse_expression_nested_sub();
475 result += test_parse_expression_class_sub();
476 result += test_parse_expression_list();
478 return result;