-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.sh
executable file
·88 lines (69 loc) · 1.69 KB
/
tests.sh
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
#!/usr/bin/env bash
TEST_FILE="test-program.koak"
test() {
echo;
name="$1";
program="$2";
expected="$3";
echo -n "$program" > "$TEST_FILE";
output=$(./koak "$TEST_FILE" 2> /dev/null);
rm "$TEST_FILE";
if [ "$output" = "$expected" ]; then
echo "$name: OK !";
else
echo "$name: KO !";
echo "Program: $program"
echo "Got: $output";
echo "Expected: $expected";
exit 1;
fi
}
test 'If condition' \
'i = 2; if i == 2 then 3 + 2 else -1; i = 1; if i == 2 then 3 + 2 else -1;' \
$'2\n5\n1\n-1';
test 'For loop' \
'ret = 0; for i = 2, i < 4, i = i + 1 in ret = ret + i;' \
$'0\n5';
test 'While loop' \
'i = 0; while i < 10 do i = i + 1;' \
$'0\n10';
test 'Function definition' \
'def add2(x:integer): integer x + 2; add2(40);' \
$'42';
test 'Binary operator definition' \
'def binary && (a:integer b:integer): integer if a then b else a; 42 && 53; 0 && 4;' \
$'53\n0';
test 'Unary operator definition' \
'def unary % (a:integer): integer if a then 0 else 1; %53; %0;' \
$'0\n1';
test 'Precedence 1' \
'3 + 2 * 3;' \
$'9';
test 'Precedence 2' \
'3 * 2 + 3;' \
$'9';
test 'Precedence 3' \
'3 * -2 + 3;' \
$'-3';
test 'Precedence 4' \
'1 + 2 * 3;' \
$'7';
test 'Precedence 5' \
'1 * 2 + 3;' \
$'5';
test 'Precedence 6' \
'(1 + 2) * 3;' \
$'9';
test 'Precedence 7' \
'1 * (2 + 3);' \
$'5';
test 'Associativity' \
'a = b = 1;' \
$'1';
test 'Lambdas (Simple)' \
'f = (x -> x * 2); f(8);' \
$'16';
test 'Lambdas (Advanced inference)' \
'f = (x -> x * 2.0); f(8);' \
$'16.0';
echo;