aboutsummaryrefslogtreecommitdiff
path: root/pu1/putest.c
blob: 013a48b7618f659790e10db2c1ea3d934632f3fe (plain) (blame)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include <stdio.h>
#include <string.h>

#define __WRAPPER
#include "pu.out.c"

/* ─── color & statistics ──────────────────────────────────────────────────── */
#define COLOR_RED   "\x1b[1;31m"
#define COLOR_BLUE  "\033[1;34m"
#define COLOR_SUM  "\033[1;33m"
#define COLOR_RESET "\x1b[0m"

static int tests_run    = 0;
static int tests_passed = 0;

/* PASS if cond true, else FAIL; color and bump counters */
#define PRINT_RESULT(cond)                                                                         \
    do {                                                                                           \
        tests_run++;                                                                               \
        if (cond) {                                                                                \
            tests_passed++;                                                                        \
            printf(COLOR_BLUE "PASS" COLOR_RESET "\n");                                            \
        } else {                                                                                   \
            printf(COLOR_RED "FAIL" COLOR_RESET "\n");                                             \
        }                                                                                          \
    } while (0)
/* ───────────────────────────────────────────────────────────────────────── */

void reset() {
    memset(m, 0, sizeof(m));
    a = 0;
    c = 0;
}

void test_opcode_0() {
    printf("Testing opcode 0 (hlt)... ");
    reset();
    m[ 0 ] = 0;
    m[ 7 ] = 0;
    pu();
    PRINT_RESULT(1);
}

void test_opcode_1() {
    printf("Testing opcode 1 (const in r0)... ");
    reset();
    m[ 0 ] = 1;
    m[ 1 ] = 42;
    m[ 2 ] = 0;
    m[ 7 ] = 0;
    pu();
    PRINT_RESULT(m[ 0 ] == 42);
}

void test_opcode_2() {
    printf("Testing opcode 4 (r0 in r2 if acc zero)... ");
    reset();
    m[ 0 ]  = 123;
    a       = 0;
    m[ 10 ] = 4;
    m[ 11 ] = 0;
    m[ 7 ]  = 10;
    pu();
    PRINT_RESULT(m[ 2 ] == 123);
}

void test_opcode_2_zero_acc() {
    printf("Testing opcode 4 with zero acc (should not store)... ");
    reset();
    m[ 0 ]  = 123;
    m[ 2 ]  = 999;
    a       = 1;
    m[ 10 ] = 4;
    m[ 11 ] = 0;
    m[ 7 ]  = 10;
    pu();
    PRINT_RESULT(m[ 2 ] == 999);
}

void test_opcode_10() {
    printf("Testing opcode 12 (r2 in r0)... ");
    reset();
    m[ 2 ]  = 456;
    m[ 10 ] = 12;
    m[ 11 ] = 0;
    m[ 7 ]  = 10;
    pu();
    PRINT_RESULT(m[ 0 ] == 456);
}

void test_opcode_18() {
    printf("Testing opcode 18 (r0 in m[r1])... ");
    reset();
    m[ 0 ]  = 789;
    m[ 1 ]  = 100;
    m[ 10 ] = 18;
    m[ 11 ] = 0;
    m[ 7 ]  = 10;
    pu();
    PRINT_RESULT(m[ 100 ] == 789);
}

void test_opcode_19() {
    printf("Testing opcode 19 (acc in r0)... ");
    reset();
    a       = 555;
    m[ 10 ] = 19;
    m[ 11 ] = 0;
    m[ 7 ]  = 10;
    pu();
    PRINT_RESULT(m[ 0 ] == 555);
}

void test_opcode_20() {
    printf("Testing opcode 20 (acc += r0)... ");
    reset();
    a       = 10;
    m[ 0 ]  = 5;
    m[ 10 ] = 20;
    m[ 11 ] = 0;
    m[ 7 ]  = 10;
    pu();
    PRINT_RESULT(a == 15);
}

void test_opcode_21() {
    printf("Testing opcode 21 (acc -= r0)... ");
    reset();
    a       = 20;
    m[ 0 ]  = 7;
    m[ 10 ] = 21;
    m[ 11 ] = 0;
    m[ 7 ]  = 10;
    pu();
    PRINT_RESULT(a == 13);
}

void test_opcode_22() {
    printf("Testing opcode 22 (acc = !acc)... ");
    reset();
    a       = 5;
    m[ 10 ] = 22;
    m[ 11 ] = 0;
    m[ 7 ]  = 10;
    pu();
    PRINT_RESULT(a == 0);
}

void test_opcode_23() {
    printf("Testing opcode 23 (acc = 0)... ");
    reset();
    a       = 999;
    m[ 10 ] = 23;
    m[ 11 ] = 0;
    m[ 7 ]  = 10;
    pu();
    PRINT_RESULT(a == 0);
}

/* ─── assembler support ───────────────────────────────────────────────────── */

i;
int check_output(int* O, int* expected, int len, const char* test_name) {
    for (i = 0; i < len; i++) {
        if (O[ i ] != expected[ i ]) {
            printf(
                "%s: output[%d]=%d (expected %d): " COLOR_RED "FAIL" COLOR_RESET "\n",
                test_name,
                i,
                O[ i ],
                expected[ i ]);
            return 0;
        }
    }
    return 1;
}

void reset_output(int* O, int size) { memset(O, 0, size * sizeof(int)); }

void test_hlt() {
    printf("Testing hlt (opcode 0)... ");
    int O[ 10 ], ok;
    reset_output(O, 10);
    int   exp[] = { 0 };
    char* ret   = as("hlt", O);
    ok          = (ret == 0 && check_output(O, exp, 1, "hlt"));
    PRINT_RESULT(ok);
}

void test_con() {
    printf("Testing con 123 (opcode 1, 123)... ");
    int O[ 10 ], ok;
    reset_output(O, 10);
    int   exp[] = { 1, 123 };
    char* ret   = as("con 123", O);
    ok          = (ret == 0 && check_output(O, exp, 2, "con"));
    PRINT_RESULT(ok);
}

void test_ar1() {
    printf("Testing ar1 (opcode 2)... ");
    int O[ 10 ], ok;
    reset_output(O, 10);
    int   exp[] = { 2 };
    char* ret   = as("ar1", O);
    ok          = (ret == 0 && check_output(O, exp, 1, "ar1"));
    PRINT_RESULT(ok);
}

void test_ar8() {
    printf("Testing ar8 (opcode 9)... ");
    int O[ 10 ], ok;
    reset_output(O, 10);
    int   exp[] = { 9 };
    char* ret   = as("ar8", O);
    ok          = (ret == 0 && check_output(O, exp, 1, "ar8"));
    PRINT_RESULT(ok);
}

void test_r1a() {
    printf("Testing r1a (opcode 10)... ");
    int O[ 10 ], ok;
    reset_output(O, 10);
    int   exp[] = { 10 };
    char* ret   = as("r1a", O);
    ok          = (ret == 0 && check_output(O, exp, 1, "r1a"));
    PRINT_RESULT(ok);
}

void test_r8a() {
    printf("Testing r8a (opcode 17)... ");
    int O[ 10 ], ok;
    reset_output(O, 10);
    int   exp[] = { 17 };
    char* ret   = as("r8a", O);
    ok          = (ret == 0 && check_output(O, exp, 1, "r8a"));
    PRINT_RESULT(ok);
}

void test_mem() {
    printf("Testing mem (opcode 18)... ");
    int O[ 10 ], ok;
    reset_output(O, 10);
    int   exp[] = { 18 };
    char* ret   = as("mem", O);
    ok          = (ret == 0 && check_output(O, exp, 1, "mem"));
    PRINT_RESULT(ok);
}

void test_aca() {
    printf("Testing aca (opcode 19)... ");
    int O[ 10 ], ok;
    reset_output(O, 10);
    int   exp[] = { 19 };
    char* ret   = as("aca", O);
    ok          = (ret == 0 && check_output(O, exp, 1, "aca"));
    PRINT_RESULT(ok);
}

void test_add() {
    printf("Testing add (opcode 20)... ");
    int O[ 10 ], ok;
    reset_output(O, 10);
    int   exp[] = { 20 };
    char* ret   = as("add", O);
    ok          = (ret == 0 && check_output(O, exp, 1, "add"));
    PRINT_RESULT(ok);
}

void test_sub() {
    printf("Testing sub (opcode 21)... ");
    int O[ 10 ], ok;
    reset_output(O, 10);
    int   exp[] = { 21 };
    char* ret   = as("sub", O);
    ok          = (ret == 0 && check_output(O, exp, 1, "sub"));
    PRINT_RESULT(ok);
}

void test_not() {
    printf("Testing not (opcode 22)... ");
    int O[ 10 ], ok;
    reset_output(O, 10);
    int   exp[] = { 22 };
    char* ret   = as("not", O);
    ok          = (ret == 0 && check_output(O, exp, 1, "not"));
    PRINT_RESULT(ok);
}

void test_clr() {
    printf("Testing clr (opcode 23)... ");
    int O[ 10 ], ok;
    reset_output(O, 10);
    int   exp[] = { 23 };
    char* ret   = as("clr", O);
    ok          = (ret == 0 && check_output(O, exp, 1, "clr"));
    PRINT_RESULT(ok);
}

void test_multi_instruction() {
    printf("Testing multi-instruction (hlt con 123 ar1 clr)... ");
    int O[ 10 ], ok;
    reset_output(O, 10);
    int   exp[] = { 0, 1, 123, 2, 23 };
    char* ret   = as("hlt con 123 ar1 clr", O);
    ok          = (ret == 0 && check_output(O, exp, 5, "multi-instruction"));
    PRINT_RESULT(ok);
}

void test_whitespace() {
    printf("Testing whitespace handling (hlt  \\t\\n con 456 \\n clr)... ");
    int O[ 10 ], ok;
    reset_output(O, 10);
    int   exp[] = { 0, 1, 456, 23 };
    char* ret   = as("hlt  \t\n con 456 \n clr", O);
    ok          = (ret == 0 && check_output(O, exp, 4, "whitespace"));
    PRINT_RESULT(ok);
}

void test_comment() {
    printf("Testing comment handling (should ignore line)... ");
    int O[ 10 ], ok;
    reset_output(O, 10);
    int   exp[] = { 1, 90 };
    char* ret   = as("# hello\ncon 90", O);
    ok          = (ret == 0 && check_output(O, exp, 2, "comment"));
    PRINT_RESULT(ok);
}

void test_error() {
    printf("Testing error case (invalid instruction)... ");
    int O[ 10 ], ok;
    reset_output(O, 10);
    char* ret = as("xyz", O);
    ok        = (ret != 0);
    PRINT_RESULT(ok);
}

int main() {
    printf(COLOR_SUM "Running CPU unit tests...\n\n" COLOR_RESET);

    test_opcode_0();
    test_opcode_1();
    test_opcode_2();
    test_opcode_2_zero_acc();
    test_opcode_10();
    test_opcode_18();
    test_opcode_19();
    test_opcode_20();
    test_opcode_21();
    test_opcode_22();
    test_opcode_23();

    printf(COLOR_SUM "\nRunning assembler unit tests...\n\n" COLOR_RESET);

    test_hlt();
    test_con();
    test_ar1();
    test_ar8();
    test_r1a();
    test_r8a();
    test_mem();
    test_aca();
    test_add();
    test_sub();
    test_not();
    test_clr();
    test_multi_instruction();
    test_whitespace();
    test_comment();
    test_error();

    printf(
        "\nTotal tests run: %d, passed: %d, failed: %d\n",
        tests_run,
        tests_passed,
        tests_run - tests_passed);

    return 0;
}