-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_program_tests.cpp
More file actions
215 lines (179 loc) · 6.49 KB
/
parse_program_tests.cpp
File metadata and controls
215 lines (179 loc) · 6.49 KB
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
#include <catch2/catch_test_macros.hpp>
#include <parse_program.hpp>
TEST_CASE("parse ADD")
{
SECTION("valid") {
auto instructions = parse_program("ADD R0, R1, R2");
std::visit([](const auto& instruction) {
if constexpr (std::is_same_v<std::decay_t<decltype(instruction)>, AddInstruction>) {
REQUIRE(instruction.register_1 == Register::R0);
REQUIRE(instruction.register_2 == Register::R1);
REQUIRE(instruction.output_register == Register::R2);
} else {
throw std::runtime_error("invalid instruction type");
}
}, instructions.at(0).value().variant);
}
SECTION("invalid register") {
REQUIRE_THROWS( parse_program("ADD R0, R1, RX") );
REQUIRE_THROWS( parse_program("ADD 0, R1, R2") );
}
SECTION("too few arguments") {
REQUIRE_THROWS( parse_program("ADD R0, R1") );
}
}
TEST_CASE("parse SUB")
{
SECTION("valid") {
auto instructions = parse_program("SUB R1, R2, R3");
std::visit([](const auto& instruction) {
if constexpr (std::is_same_v<std::decay_t<decltype(instruction)>, SubInstruction>) {
REQUIRE(instruction.register_1 == Register::R1);
REQUIRE(instruction.register_2 == Register::R2);
REQUIRE(instruction.output_register == Register::R3);
} else {
throw std::runtime_error("invalid instruction type");
}
}, instructions.at(0).value().variant);
}
SECTION("invalid register") {
REQUIRE_THROWS( parse_program("SUB R0, R1, RX") );
REQUIRE_THROWS( parse_program("SUB 0, R1, R2") );
}
SECTION("too few arguments") {
REQUIRE_THROWS( parse_program("SUB R0, R1") );
}
}
TEST_CASE("parse CMP")
{
SECTION("valid") {
auto instructions = parse_program("CMP R0, R1");
std::visit([](const auto& instruction) {
if constexpr (std::is_same_v<std::decay_t<decltype(instruction)>, CompareInstruction>) {
REQUIRE(instruction.register_1 == Register::R0);
REQUIRE(instruction.register_2 == Register::R1);
} else {
throw std::runtime_error("invalid instruction type");
}
}, instructions.at(0).value().variant);
}
SECTION("invalid register") {
REQUIRE_THROWS( parse_program("CMP R0, RX") );
REQUIRE_THROWS( parse_program("CMP 0, R1") );
}
SECTION("too few arguments") {
REQUIRE_THROWS( parse_program("CMP R0") );
}
}
TEST_CASE("parse LOAD")
{
SECTION("valid") {
auto instructions = parse_program("LOAD 42, R2");
std::visit([](const auto& instruction) {
if constexpr (std::is_same_v<std::decay_t<decltype(instruction)>, LoadInstruction>) {
REQUIRE(instruction.memory_address == 42);
REQUIRE(instruction.output_register == Register::R2);
} else {
throw std::runtime_error("invalid instruction type");
}
}, instructions.at(0).value().variant);
}
SECTION("invalid register") {
REQUIRE_THROWS( parse_program("LOAD 42, RX") );
}
SECTION("invalid memory address") {
REQUIRE_THROWS( parse_program("LOAD 257, R1") );
}
SECTION("too few arguments") {
REQUIRE_THROWS( parse_program("LOAD 42") );
}
SECTION("invalid argument order") {
REQUIRE_THROWS( parse_program("LOAD R0, 42") );
}
}
TEST_CASE("parse STORE")
{
SECTION("STORE") {
auto instructions = parse_program("STORE R1, 99");
std::visit([](const auto& instruction) {
if constexpr (std::is_same_v<std::decay_t<decltype(instruction)>, StoreInstruction>) {
REQUIRE(instruction._register == Register::R1);
REQUIRE(instruction.memory_address == 99);
} else {
throw std::runtime_error("invalid instruction type");
}
}, instructions.at(0).value().variant);
}
SECTION("invalid register") {
REQUIRE_THROWS( parse_program("STORE RX, 99") );
}
SECTION("invalid memory address") {
REQUIRE_THROWS( parse_program("STORE R1, 257") );
}
SECTION("too few arguments") {
REQUIRE_THROWS( parse_program("STORE R1") );
}
SECTION("invalid argument order") {
REQUIRE_THROWS( parse_program("STORE 42, R1") );
}
}
TEST_CASE("parse JMP")
{
SECTION("valid") {
auto instructions = parse_program("JMP 10");
std::visit([](const auto& instruction) {
if constexpr (std::is_same_v<std::decay_t<decltype(instruction)>, JumpInstruction>) {
REQUIRE(instruction.program_counter_index == 10);
} else {
throw std::runtime_error("invalid instruction type");
}
}, instructions.at(0).value().variant);
}
SECTION("invalid memory address") {
REQUIRE_THROWS( parse_program("JMP 257") );
}
SECTION("too few arguments") {
REQUIRE_THROWS( parse_program("JMP") );
}
}
TEST_CASE("parse JMP_EQ")
{
SECTION("valid") {
auto instructions = parse_program("JMP_EQ 20");
std::visit([](const auto& instruction) {
if constexpr (std::is_same_v<std::decay_t<decltype(instruction)>, JumpEqualInstruction>) {
REQUIRE(instruction.program_counter_index == 20);
} else {
throw std::runtime_error("invalid instruction type");
}
}, instructions.at(0).value().variant);
}
SECTION("invalid memory address") {
REQUIRE_THROWS( parse_program("JMP_EQ 257") );
}
SECTION("invalid number of arguments") {
REQUIRE_THROWS( parse_program("JMP_EQ") );
}
}
TEST_CASE("parse HALT")
{
SECTION("valid") {
auto instructions = parse_program("HALT");
std::visit([](const auto& instruction) {
REQUIRE(std::is_same_v<std::decay_t<decltype(instruction)>, HaltInstruction>);
}, instructions.at(0).value().variant);
}
}
TEST_CASE("parse PRINT")
{
SECTION("valid") {
auto instructions = parse_program("PRINT 10");
std::visit([](const auto& instruction) {
if constexpr (std::is_same_v<std::decay_t<decltype(instruction)>, PrintInstruction>) {
REQUIRE(instruction.memory_address == 10);
} else {
throw std::runtime_error("invalid instruction type");
}
}, instructions.at(0).value().variant);
}
}