Added tokens to fit specification.
[Jack-Compiler.git] / test.c
blob89c0d957720b298916915f633a4b8123e35eabc4
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_all()
267 int result = 0;
269 result += test_c_comments();
270 result += test_nested_c_comments();
271 result += test_cpp_comments();
272 result += test_nested_cpp_comments();
273 result += test_literal_strings();
274 result += test_symbol_recog();
275 result += test_keyword_recog();
276 result += test_token_type();
277 result += test_end_of_code();
278 result += test_no_code();
280 return result;