-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPP2Test.cpp
More file actions
163 lines (130 loc) · 4.68 KB
/
Copy pathPP2Test.cpp
File metadata and controls
163 lines (130 loc) · 4.68 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
/**************************************************************************************************/
// Test File for PP3
// Requires the Catch2 header file
// How to compile: g++ -std=c++17 -Wall -I$(CATCH_SINGLE_INCLUDE) (All cpp files)
// Example if Catch2 and source files are in this directory and at directory level:
// Example: g++ -std=c++17 -Wall *.cpp
// To see what tests were successful and failed, run your executable with the -s flag
// Example: a.out -s
// A successful test should output: All tests passed (12 assertions in 1 test case)
// A successful test WITH EC should output: All tests passed (15 assertions in 2 test cases)
/**************************************************************************************************/
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include <cmath>
#include "GritVM.hpp"
TEST_CASE("Project 3 Test Cases for GritVM") {
GritVM vm;
std::vector<long> initialMemory;
std::vector<long> outputMemory;
std::vector<long> checkMemory;
SECTION("GritVM should be in WAITING state when no program is loaded") {
REQUIRE(vm.run() == WAITING);
}
SECTION("GritVM should throw if the file cannot be loaded and be state READY if it is") {
CHECK_THROWS(vm.load("", initialMemory));
REQUIRE(vm.load("test.gvm", initialMemory) == READY);
}
SECTION("GritVM should run a program if READY and be HALTED when program completes") {
std::vector<long> initialMemory = { 10 };
vm.load("test.gvm", initialMemory);
REQUIRE(vm.run() == HALTED);
}
SECTION("GritVM produces proper output for test.gvm") {
long n1 = (rand() + 1) % 50, n2 = (rand() + 1) % 50;
initialMemory = { n1 };
checkMemory = {624, n1, 0};
vm.load("test.gvm", initialMemory);
vm.run();
REQUIRE(vm.getDataMem() == checkMemory);
vm.reset();
initialMemory = { n2 };
checkMemory = {624, n2, 0};
vm.load("test.gvm", initialMemory);
vm.run();
REQUIRE(vm.getDataMem() == checkMemory);
}
SECTION("GritVM produces proper output for sumn.gvm") {
long n1 = (rand() + 1) % 50, n2 = (rand() + 1) % 50;
auto func = [](long n) -> long {
long sum = 0;
for (int i = 1; i <= n; i++) sum += i;
return sum;
};
initialMemory = { n1 };
checkMemory = {n1, func(n1), n1 + 1};
vm.load("sumn.gvm", initialMemory);
vm.run();
REQUIRE(vm.getDataMem() == checkMemory);
vm.reset();
initialMemory = { n2 };
checkMemory = {n2, func(n2), n2 + 1};
vm.load("sumn.gvm", initialMemory);
vm.run();
REQUIRE(vm.getDataMem() == checkMemory);
}
SECTION("GritVM produces proper output for surfarea.gvm") {
long l = (rand() + 1) % 50, w = (rand() + 1) % 50, h = (rand() + 1) % 50;
auto func = [](long l, long w, long h) -> long {
return 2*((l*w) + (h*w) + (l*h));
};
initialMemory = { l, w, h };
checkMemory = { func(l,w,h) };
vm.load("surfarea.gvm", initialMemory);
vm.run();
REQUIRE(vm.getDataMem() == checkMemory);
vm.reset();
l = (rand() + 1) % 50, w = (rand() + 1) % 50, h = (rand() + 1) % 50;
initialMemory = { l, w, h };
checkMemory = { func(l,w,h) };
vm.load("surfarea.gvm", initialMemory);
vm.run();
REQUIRE(vm.getDataMem() == checkMemory);
}
SECTION("GritVM produces proper output for altseq.gvm") {
long n = (rand() + 1) % 10, n1 = (rand() + 1) % 10;
auto func = [](long n) -> long {
return std::pow(-2, n) * 3 + 4;
};
initialMemory = { n };
checkMemory = { n, func(n), n+1 };
vm.load("altseq.gvm", initialMemory);
vm.run();
REQUIRE(vm.getDataMem() == checkMemory);
vm.reset();
initialMemory = { n1 };
checkMemory = { n1, func(n1), n1+1 };
vm.load("altseq.gvm", initialMemory);
vm.run();
REQUIRE(vm.getDataMem() == checkMemory);
}
}
TEST_CASE("Project 3 Test Cases for GritVM (EXTRA CREDIT!)") {
GritVM vm;
std::vector<long> initialMemory;
std::vector<long> outputMemory;
std::vector<long> checkMemory;
SECTION("GritVM produces proper output for toh.gvm (The minimum steps for TOH)") {
long n = (rand() + 1) % 10, n1 = (rand() + 1) % 10, n2 = (rand() + 1) % 10;
auto func = [](long n) -> long {
return std::pow(2, n) - 1;
};
initialMemory = { n };
checkMemory = { n, func(n) };
vm.load("toh.gvm", initialMemory);
vm.run();
REQUIRE(vm.getDataMem() == checkMemory);
vm.reset();
initialMemory = { n1 };
checkMemory = { n1, func(n1) };
vm.load("toh.gvm", initialMemory);
vm.run();
REQUIRE(vm.getDataMem() == checkMemory);
vm.reset();
initialMemory = { n2 };
checkMemory = { n2, func(n2) };
vm.load("toh.gvm", initialMemory);
vm.run();
REQUIRE(vm.getDataMem() == checkMemory);
}
}