forked from eduardoayervecruz/ca-pa1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
102 lines (84 loc) · 2.37 KB
/
main.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
//---------------------------------------------------------------
//
// 4190.308 Computer Architecture (Fall 2022)
//
// Project #1:
//
// September 6, 2022
//
// Seongyeop Jeong (seongyeop.jeong@snu.ac.kr)
// Jaehoon Shim (mattjs@snu.ac.kr)
// IlKueon Kang (kangilkueon@snu.ac.kr)
// Wookje Han (gksdnrwp@snu.ac.kr)
// Jinsol Park (jinsolpark@snu.ac.kr)
// Systems Software & Architecture Laboratory
// Dept. of Computer Science and Engineering
// Seoul National University
//
//---------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include "pa1.h"
void print_ans(const u8* buf, const int buflen)
{
for (int i = 0; i < buflen; i++) {
if (i % 10 == 0 && i != 0)
printf("\n");
if (buf[i] == 0) {
printf("0x00, ");
continue;
}
printf("0x%02x, ", buf[i]);
}
printf("\n");
}
void print_buffer(const u8* buf, const int height, const int width)
{
for (int i = 0; i < height * width; i++) {
if (i % width == 0 && i != 0)
printf("\n");
if (buf[i] == 0) {
printf("0x00, ");
continue;
}
printf("0x%02x, ", buf[i]);
}
printf("\n");
}
int __test_routine(const int num) {
u8 dst[LEN_DST + LEN_GUARD] = {0,};
*(unsigned long *)(dst + LEN_DST) = GUARD_WORD;
printf("-------- Test #%d Encoding\n", num);
int len = encode(tc[num].input, tc[num].input_width, tc[num].input_height, dst);
printf("[Input] height (bytes): %d, width (bytes): %d\n", tc[num].input_height, tc[num].input_width);
print_buffer(tc[num].input, tc[num].input_height, tc[num].input_width);
printf("[Encode] length (bytes): %d\n", len);
print_ans(dst, len);
printf("[Answer] length (bytes): %d\n", tc[num].ans_len);
print_ans(tc[num].ans, tc[num].ans_len);
if (*(unsigned long *)(dst + LEN_DST) != GUARD_WORD)
return 1;
else if (len == -1 && tc[num].ans_len == 0)
return 0; // no more test for dstlen overflow
else if (len != tc[num].ans_len)
return 2;
else if (memcmp(dst, tc[num].ans, tc[num].ans_len) != 0)
return 3;
return 0;
}
int test_routine(const int num)
{
int ret = __test_routine(num);
if (!ret)
printf("-------- ENCODING CORRECT!\n\n");
else
printf("-------- ENCODING WRONG! %d\n\n", ret);
return !!ret;
}
int main() {
int ret = 0;
for (int i = 0; i < (sizeof tc / sizeof(testcase)); i++) {
ret += test_routine(i);
}
return ret;
}