-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreaded_interpreter_tests.cpp
More file actions
181 lines (143 loc) · 4.2 KB
/
threaded_interpreter_tests.cpp
File metadata and controls
181 lines (143 loc) · 4.2 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
#include "catch.hpp"
#include "threaded_interpreter.hpp"
#include <thread>
void waitForStartup(ThreadedInterpreter& interp, OutputQueue& oq) {
while (!interp.isStartupLoaded()) {}
OutputMessage startupMsg;
oq.try_pop(startupMsg);
}
Expression run_threaded(const std::string& program, bool runSemantic = false) {
InputQueue iq;
OutputQueue oq;
ThreadedInterpreter interp(&iq, &oq);
waitForStartup(interp, oq);
// run the program by queueing it
iq.push(program);
// wait for an output
OutputMessage msg;
oq.wait_pop(msg);
// Once the output has been recieved, check if it was an error or expression
if (msg.type == ExpressionType) {
return msg.exp;
} else if (msg.type == ErrorType) {
// If an error was thrown, then run semantic should be true (if intended to error)
REQUIRE(runSemantic);
}
// just here for the sake of returning something
return Expression();
}
TEST_CASE("Constructs and stops on destruction", "[ThreadedInterpreter]") {
InputQueue iq;
OutputQueue oq;
ThreadedInterpreter interp(&iq, &oq);
interp.isActive();
}
TEST_CASE("Direct stream evaluation", "[ThreadedInterpreter]") {
std::string program = "(+ 4 3)";
std::istringstream stream(program);
// evaluate the stream directly
OutputQueue oq;
ThreadedInterpreter interp(&oq, stream);
// wait for the output
OutputMessage msg;
oq.wait_pop(msg);
REQUIRE(msg.exp == Expression(7));
}
TEST_CASE("Test simple expressions", "[ThreadedInterpreter]") {
{
std::string program = "(/ 1 2)";
INFO(program);
Expression result = run_threaded(program);
REQUIRE(result == Expression(0.5));
}
}
TEST_CASE("Test error catching", "[ThreadedInterpreter]") {
{
std::string program = "(/ 1 +)";
INFO(program);
Expression result = run_threaded(program, true);
}
{
std::string program = ")";
INFO(program);
Expression result = run_threaded(program, true);
}
}
TEST_CASE("Test reset capability", "[ThreadedInterpreter]") {
{
// when a reset happens, all environment data should be reset
InputQueue iq;
OutputQueue oq;
ThreadedInterpreter interp(&iq, &oq);
waitForStartup(interp, oq);
// define some variable, and make sure it's retrievable
iq.push("(define a 22)");
iq.push("(a)");
// Grab the second output
OutputMessage msg;
oq.wait_pop(msg);
oq.wait_pop(msg);
REQUIRE(msg.exp == Expression(22));
// reset the kernel
interp.reset();
// check to make sure the environment was reset
iq.push("(a)");
oq.wait_pop(msg);
REQUIRE(msg.type == ErrorType);
}
}
TEST_CASE("Make sure the startup file loads", "[ThreadedInterpreter]") {
{ // points
InputQueue iq;
OutputQueue oq;
ThreadedInterpreter interp(&iq, &oq);
waitForStartup(interp, oq);
iq.push("(define p (make-point 17 5))");
iq.push("(get-property \"object-name\" p)");
iq.push("(get-property \"size\" p)");
OutputMessage msg;
oq.wait_pop(msg);
REQUIRE(msg.exp == Expression(std::vector<Expression>{Expression(17), Expression(5)}));
oq.wait_pop(msg);
REQUIRE(msg.exp == Expression(Atom("\"point\"")));
oq.wait_pop(msg);
REQUIRE(msg.exp == Expression(0));
}
{ // lines
InputQueue iq;
OutputQueue oq;
ThreadedInterpreter interp(&iq, &oq);
waitForStartup(interp, oq);
iq.push("(define l (make-line (list 17 5) (list 22 22)))");
iq.push("(get-property \"object-name\" l)");
iq.push("(get-property \"thickness\" l)");
OutputMessage msg;
oq.wait_pop(msg);
oq.wait_pop(msg);
REQUIRE(msg.exp == Expression(Atom("\"line\"")));
oq.wait_pop(msg);
REQUIRE(msg.exp == Expression(1));
}
{ // text
InputQueue iq;
OutputQueue oq;
ThreadedInterpreter interp(&iq, &oq);
waitForStartup(interp, oq);
iq.push("(define t (make-text \"Hi\"))");
iq.push("(get-property \"object-name\" t)");
iq.push("(get-property \"position\" t)");
iq.push("(get-property \"text-scale\" t)");
iq.push("(get-property \"text-rotation\" t)");
OutputMessage msg;
oq.wait_pop(msg);
REQUIRE(msg.exp == Expression(Atom("\"Hi\"")));
oq.wait_pop(msg);
REQUIRE(msg.exp == Expression(Atom("\"text\"")));
oq.wait_pop(msg);
REQUIRE(msg.exp == Expression(std::vector<Expression>{Expression(0), Expression(0)}));
oq.wait_pop(msg);
REQUIRE(msg.exp == Expression(1));
oq.wait_pop(msg);
REQUIRE(msg.exp == Expression(0));
}
}