-
Notifications
You must be signed in to change notification settings - Fork 18
/
test_assembler.c
196 lines (162 loc) · 5.54 KB
/
test_assembler.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <CUnit/Basic.h>
#include "src/utils.h"
#include "src/tables.h"
#include "src/translate_utils.h"
#include "src/translate.h"
const char* TMP_FILE = "test_output.txt";
const int BUF_SIZE = 1024;
/****************************************
* Helper functions
****************************************/
int do_nothing() {
return 0;
}
int init_log_file() {
set_log_file(TMP_FILE);
return 0;
}
int check_lines_equal(char **arr, int num) {
char buf[BUF_SIZE];
FILE *f = fopen(TMP_FILE, "r");
if (!f) {
CU_FAIL("Could not open temporary file");
return 0;
}
for (int i = 0; i < num; i++) {
if (!fgets(buf, BUF_SIZE, f)) {
CU_FAIL("Reached end of file");
return 0;
}
CU_ASSERT(!strncmp(buf, arr[i], strlen(arr[i])));
}
fclose(f);
return 0;
}
/****************************************
* Test cases for translate_utils.c
****************************************/
void test_translate_reg() {
CU_ASSERT_EQUAL(translate_reg("$0"), 0);
CU_ASSERT_EQUAL(translate_reg("$at"), 1);
CU_ASSERT_EQUAL(translate_reg("$v0"), 2);
CU_ASSERT_EQUAL(translate_reg("$a0"), 4);
CU_ASSERT_EQUAL(translate_reg("$a1"), 5);
CU_ASSERT_EQUAL(translate_reg("$a2"), 6);
CU_ASSERT_EQUAL(translate_reg("$a3"), 7);
CU_ASSERT_EQUAL(translate_reg("$t0"), 8);
CU_ASSERT_EQUAL(translate_reg("$t1"), 9);
CU_ASSERT_EQUAL(translate_reg("$t2"), 10);
CU_ASSERT_EQUAL(translate_reg("$t3"), 11);
CU_ASSERT_EQUAL(translate_reg("$s0"), 16);
CU_ASSERT_EQUAL(translate_reg("$s1"), 17);
CU_ASSERT_EQUAL(translate_reg("$3"), -1);
CU_ASSERT_EQUAL(translate_reg("asdf"), -1);
CU_ASSERT_EQUAL(translate_reg("hey there"), -1);
}
void test_translate_num() {
long int output;
CU_ASSERT_EQUAL(translate_num(&output, "35", -1000, 1000), 0);
CU_ASSERT_EQUAL(output, 35);
CU_ASSERT_EQUAL(translate_num(&output, "145634236", 0, 9000000000), 0);
CU_ASSERT_EQUAL(output, 145634236);
CU_ASSERT_EQUAL(translate_num(&output, "0xC0FFEE", -9000000000, 9000000000), 0);
CU_ASSERT_EQUAL(output, 12648430);
CU_ASSERT_EQUAL(translate_num(&output, "72", -16, 72), 0);
CU_ASSERT_EQUAL(output, 72);
CU_ASSERT_EQUAL(translate_num(&output, "72", -16, 71), -1);
CU_ASSERT_EQUAL(translate_num(&output, "72", 72, 150), 0);
CU_ASSERT_EQUAL(output, 72);
CU_ASSERT_EQUAL(translate_num(&output, "72", 73, 150), -1);
CU_ASSERT_EQUAL(translate_num(&output, "35x", -100, 100), -1);
}
/****************************************
* Test cases for tables.c
****************************************/
void test_table_1() {
int retval;
SymbolTable* tbl = create_table(SYMTBL_UNIQUE_NAME);
CU_ASSERT_PTR_NOT_NULL(tbl);
retval = add_to_table(tbl, "abc", 8);
CU_ASSERT_EQUAL(retval, 0);
retval = add_to_table(tbl, "efg", 12);
CU_ASSERT_EQUAL(retval, 0);
retval = add_to_table(tbl, "q45", 16);
CU_ASSERT_EQUAL(retval, 0);
retval = add_to_table(tbl, "q45", 24);
CU_ASSERT_EQUAL(retval, -1);
retval = add_to_table(tbl, "bob", 14);
CU_ASSERT_EQUAL(retval, -1);
retval = get_addr_for_symbol(tbl, "abc");
CU_ASSERT_EQUAL(retval, 8);
retval = get_addr_for_symbol(tbl, "q45");
CU_ASSERT_EQUAL(retval, 16);
retval = get_addr_for_symbol(tbl, "ef");
CU_ASSERT_EQUAL(retval, -1);
free_table(tbl);
char* arr[] = { "Error: name 'q45' already exists in table.",
"Error: address is not a multiple of 4." };
check_lines_equal(arr, 2);
SymbolTable* tbl2 = create_table(SYMTBL_NON_UNIQUE);
CU_ASSERT_PTR_NOT_NULL(tbl2);
retval = add_to_table(tbl2, "q45", 16);
CU_ASSERT_EQUAL(retval, 0);
retval = add_to_table(tbl2, "q45", 24);
CU_ASSERT_EQUAL(retval, 0);
free_table(tbl2);
}
void test_table_2() {
int retval, max = 100;
SymbolTable* tbl = create_table(SYMTBL_UNIQUE_NAME);
CU_ASSERT_PTR_NOT_NULL(tbl);
char buf[10];
for (int i = 0; i < max; i++) {
sprintf(buf, "%d", i);
retval = add_to_table(tbl, buf, 4 * i);
CU_ASSERT_EQUAL(retval, 0);
}
for (int i = 0; i < max; i++) {
sprintf(buf, "%d", i);
retval = get_addr_for_symbol(tbl, buf);
CU_ASSERT_EQUAL(retval, 4 * i);
}
free_table(tbl);
}
/****************************************
* Add your test cases here
****************************************/
int main(int argc, char** argv) {
CU_pSuite pSuite1 = NULL, pSuite2 = NULL;
if (CUE_SUCCESS != CU_initialize_registry()) {
return CU_get_error();
}
/* Suite 1 */
pSuite1 = CU_add_suite("Testing translate_utils.c", NULL, NULL);
if (!pSuite1) {
goto exit;
}
if (!CU_add_test(pSuite1, "test_translate_reg", test_translate_reg)) {
goto exit;
}
if (!CU_add_test(pSuite1, "test_translate_num", test_translate_num)) {
goto exit;
}
/* Suite 2 */
pSuite2 = CU_add_suite("Testing tables.c", init_log_file, NULL);
if (!pSuite2) {
goto exit;
}
if (!CU_add_test(pSuite2, "test_table_1", test_table_1)) {
goto exit;
}
if (!CU_add_test(pSuite2, "test_table_2", test_table_2)) {
goto exit;
}
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
exit:
CU_cleanup_registry();
return CU_get_error();;
}