-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_utils.c
194 lines (148 loc) · 4.96 KB
/
test_utils.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
#include <stdio.h>
#include <stdlib.h>
#include <CUnit/Basic.h>
#include "utils.h"
#include "types.h"
void test_sign_extend_number();
void test_parse_instruction_rtype();
void test_parse_instruction_itype();
void test_parse_instruction_stype();
void test_parse_instruction_sbtype();
void test_parse_instruction_ujtype();
void test_parse_instruction_utype();
void test_get_branch_offset();
void test_get_jump_offset();
void test_get_store_offset();
int main(int arc, char **argv) {
CU_pSuite pSuite1 = NULL;
if (CUE_SUCCESS != CU_initialize_registry()) {
return CU_get_error();
}
pSuite1 = CU_add_suite("Testing sign_extend_number", NULL, NULL);
if (!pSuite1) {
goto exit;
}
if (!CU_add_test(pSuite1, "test_sign_extend_number", test_sign_extend_number)) {
goto exit;
}
if (!CU_add_test(pSuite1, "test_parse_instruction_rtype", test_parse_instruction_rtype)) {
goto exit;
}
if (!CU_add_test(pSuite1, "test_parse_instruction_itype", test_parse_instruction_itype)) {
goto exit;
}
if (!CU_add_test(pSuite1, "test_parse_instruction_stype", test_parse_instruction_stype)) {
goto exit;
}
if (!CU_add_test(pSuite1, "test_parse_instruction_sbtype", test_parse_instruction_sbtype)) {
goto exit;
}
if (!CU_add_test(pSuite1, "test_parse_instruction_ujtype", test_parse_instruction_ujtype)) {
goto exit;
}
if (!CU_add_test(pSuite1, "test_parse_instruction_utype", test_parse_instruction_utype)) {
goto exit;
}
if (!CU_add_test(pSuite1, "test_get_branch_offset", test_get_branch_offset)) {
goto exit;
}
if (!CU_add_test(pSuite1, "test_get_jump_offset", test_get_jump_offset)) {
goto exit;
}
if (!CU_add_test(pSuite1, "test_get_store_offset", test_get_store_offset)) {
goto exit;
}
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
exit:
CU_cleanup_registry();
return CU_get_error();
}
void test_get_store_offset() {
Instruction sw = parse_instruction(0x01312023);
int offset = get_store_offset(sw);
CU_ASSERT_EQUAL(offset, 0);
Instruction sb = parse_instruction(0x00510023);
offset = get_store_offset(sb);
CU_ASSERT_EQUAL(offset, 0);
Instruction sh = parse_instruction(0x005110a3);
offset = get_store_offset(sh);
CU_ASSERT_EQUAL(offset, 1);
}
void test_get_jump_offset() {
Instruction jal_1 = parse_instruction(0xff5ff06f);
int offset = get_jump_offset(jal_1);
CU_ASSERT_EQUAL(offset, -12);
Instruction jal_2 = parse_instruction(0x0080006f);
offset = get_jump_offset(jal_2);
CU_ASSERT_EQUAL(offset, 8);
}
void test_get_branch_offset() {
Instruction beq_1 = parse_instruction(0x00040863);
int offset = get_branch_offset(beq_1);
CU_ASSERT_EQUAL(offset, 16);
Instruction beq_2 = parse_instruction(0x00650c63);
offset = get_branch_offset(beq_2);
CU_ASSERT_EQUAL(offset, 24);
}
void test_sign_extend_number() {
CU_ASSERT_EQUAL(sign_extend_number(0xFF, 8), 0xFFFFFFFF);
CU_ASSERT_EQUAL(sign_extend_number(0xFF, 9), 0xFF);
CU_ASSERT_EQUAL(sign_extend_number(0x1234, 16), 0x1234);
CU_ASSERT_EQUAL(sign_extend_number(0x1234, 13), 0xFFFFF234);
CU_ASSERT_EQUAL(sign_extend_number(0x0, 1), 0);
CU_ASSERT_EQUAL(sign_extend_number(0x1, 1), 0xFFFFFFFF);
}
void test_parse_instruction_rtype() {
Instruction inst;
inst = parse_instruction(0x009402b3);
CU_ASSERT_EQUAL(inst.rtype.opcode, 0x33);
CU_ASSERT_EQUAL(inst.rtype.funct3, 0x0);
CU_ASSERT_EQUAL(inst.rtype.rd, 0x5);
CU_ASSERT_EQUAL(inst.rtype.rs1, 0x8);
CU_ASSERT_EQUAL(inst.rtype.rs2, 0x9);
CU_ASSERT_EQUAL(inst.rtype.funct7, 0x0);
}
void test_parse_instruction_itype() {
Instruction inst;
inst = parse_instruction(0x00a50313);
CU_ASSERT_EQUAL(inst.itype.opcode, 0x13);
CU_ASSERT_EQUAL(inst.itype.funct3, 0x0);
CU_ASSERT_EQUAL(inst.itype.rd, 0x6);
CU_ASSERT_EQUAL(inst.itype.rs1, 10);
CU_ASSERT_EQUAL(inst.itype.imm, 10);
}
void test_parse_instruction_stype() {
Instruction inst;
inst = parse_instruction(0x014aa023);
CU_ASSERT_EQUAL(inst.stype.opcode, 0x23);
CU_ASSERT_EQUAL(inst.stype.funct3, 0x2);
CU_ASSERT_EQUAL(inst.stype.rs1, 21);
CU_ASSERT_EQUAL(inst.stype.rs2, 20);
CU_ASSERT_EQUAL(inst.stype.imm7, 0);
CU_ASSERT_EQUAL(inst.stype.imm5, 0);
}
void test_parse_instruction_sbtype() {
Instruction inst;
inst = parse_instruction(0x00058063);
CU_ASSERT_EQUAL(inst.sbtype.opcode, 0x63);
CU_ASSERT_EQUAL(inst.sbtype.funct3, 0x0);
CU_ASSERT_EQUAL(inst.sbtype.rs1, 11);
CU_ASSERT_EQUAL(inst.sbtype.rs2, 0);
CU_ASSERT_EQUAL(inst.sbtype.imm7, 0);
CU_ASSERT_EQUAL(inst.sbtype.imm5, 0);
}
void test_parse_instruction_utype() {
Instruction inst;
inst = parse_instruction(0xFFFFF437);
CU_ASSERT_EQUAL(inst.utype.opcode, 0x37);
CU_ASSERT_EQUAL(inst.utype.rd, 8);
CU_ASSERT_EQUAL(inst.utype.imm, 0xFFFFF);
}
void test_parse_instruction_ujtype() {
Instruction inst;
inst = parse_instruction(0x000000EF);
CU_ASSERT_EQUAL(inst.ujtype.opcode, 0x6F);
CU_ASSERT_EQUAL(inst.ujtype.rd, 1);
CU_ASSERT_EQUAL(inst.ujtype.imm, 0);
}