-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
202 lines (170 loc) · 5.36 KB
/
test.js
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
/* 1. Integration Tests */
/* 1.1 Test Deterministic Functionality */
function test_self_evaluating() {
parse_and_eval("4;");
assert_equal(4, final_result);
}
function test_unary_minus() {
parse_and_eval("6 * -1;");
assert_equal(-6, final_result);
parse_and_eval("-12 - 8;");
assert_equal(-20, final_result);
}
function test_assignment() {
parse_and_eval("const a = 23; a;");
assert_equal(23, final_result);
}
function test_conditional_expressions() {
parse_and_eval("100 % 2 === 0 ? true: false;");
assert_equal(true, final_result);
parse_and_eval("100 % 2 !== 0 ? true: false;");
assert_equal(false, final_result);
}
function test_function() {
parse_and_eval("const f = () => (x, y) => x + y; f()(12, 13);");
assert_equal(25, final_result);
}
function test_subfunction() {
parse_and_eval("function f(x) { function g(y) { return y * 2; } return x * g(10); } f(6);");
assert_equal(120, final_result);
}
function test_function_without_return() {
parse_and_eval("function f(x) { 4; x + 4; 10;} f(5);");
assert_equal(undefined, final_result);
}
function test_binary_boolean_operations() {
parse_and_eval("true && (false || true) && (true && false);");
assert_equal(false, final_result);
}
function test_shortcircuiting() {
parse_and_eval("function foo() { return foo(); }\
true || foo();"
);
assert_equal(true, final_result);
parse_and_eval("function foo() { return foo(); }\
false && foo();"
);
assert_equal(false, final_result);
}
function test_distinct() {
parse_and_eval("distinct(list(1, 2, list(2)));");
assert_equal(true, final_result);
parse_and_eval("distinct(list(4, 5, (() => 5)()));");
assert_equal(false, final_result);
}
/* 1.2 Test Non-Deterministic Functionality */
/**
* Tests the behaviour of the empty amb statement.
*/
function test_nondet_empty() {
parse_and_eval("amb();");
assert_equal(null, final_result);
}
/**
* Tests the behaviour of an amb statement that never runs out of values.
*/
function test_nondet_infinite() {
parse_and_eval("\
const integer_from = n => amb(n, integer_from(n + 1)); \
integer_from(1); \
");
for (let i = 1; i <= 10; i=i+1) {
assert_equal(i, final_result);
final_result = try_again();
}
}
/**
* Tests whether 'require' conditions are satisfied in producing a result.
*/
function test_nondet_require() {
parse_and_eval("function int_between(low, high) { \
return low > high ? amb() : amb(low, int_between(low + 1, high)); \
} \
function is_divisible_by_3(x) { return (x % 3) === 0;} \
function is_even(x) { return (x % 2) === 0;} \
function is_one(x) { return x === 1;}\
let integer = int_between(1, 12); \
require(is_one(integer) || (is_even(integer) && is_divisible_by_3(integer))); \
integer;"
);
assert_equal(1, final_result);
assert_equal(6, try_again());
assert_equal(12, try_again());
assert_equal(null, try_again());
}
/**
* Tests whether multiple amb statements give all combinations, in the right order.
*/
function test_nondet_combinations() {
parse_and_eval("list(amb(1, 2, 3), amb('a', 'b'));");
assert_equal(list(1, "a"), final_result);
assert_equal(list(1, "b"), try_again());
assert_equal(list(2, "a"), try_again());
assert_equal(list(2, "b"), try_again());
assert_equal(list(3, "a"), try_again());
assert_equal(list(3, "b"), try_again());
assert_equal(null, try_again());
}
/**
* Tests whether the evaluation of a choice is undone before proceeding to the next choice.
*/
function test_nondet_undo() {
parse_and_eval("let num = 5; \
function reassign_num() { num = 10; return num; } \
amb(reassign_num(), num); \
");
assert_equal(10, final_result);
final_result = try_again();
assert_equal(5, final_result);
}
/**
* Tests whether an amb application can be used in a return statement,
* and tests that statements after a return statement are ignored.
*/
function test_nondet_amb_return() {
parse_and_eval("function f(x) {\
return amb(x*2, list(1,2,3), 'test_string');\
const f = amb(10, 20);\
f;\
}\
f(4);");
assert_equal(8, final_result);
assert_equal(list(1,2,3), try_again());
assert_equal("test_string", try_again());
assert_equal(null, try_again());
}
/**
* Tests whether a function value can be obtained non-deterministically.
*/
function test_nondet_functionval() {
parse_and_eval("\
const is_even = num => (num % 2) === 0;\
const add_five = num => num + 5;\
const nondet_func = amb(is_even, add_five, num => !is_even(num));\
nondet_func(5);\
");
assert_equal(false, final_result);
assert_equal(10, try_again());
assert_equal(true, try_again());
assert_equal(null, try_again());
}
run(
list(
test_self_evaluating,
test_unary_minus,
test_assignment,
test_conditional_expressions,
test_function,
test_subfunction,
test_function_without_return,
test_shortcircuiting,
test_distinct,
test_nondet_empty,
test_nondet_infinite,
test_nondet_require,
test_nondet_combinations,
test_nondet_undo,
test_nondet_amb_return,
test_nondet_functionval
), null, null, null
);