-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.sh
executable file
·174 lines (143 loc) · 3.58 KB
/
test.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
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
#!/bin/bash
# Use:
#
# Run all tests (either works):
#
# ./test.sh
# ./test.sh all
#
# Run all test cases through the "asm" test:
#
# ./test.sh asm
#
# Test with the "asm" test type the tests/const.tc and testse/char.tc files.
# The test compares the asm output of the compiler with the reference (in
# the "ref" directory):
#
# ./test.sh asm const.tc char.tc
#
# Test with the "output" test type the tests/indexing.tc file.
# The test compares the output of the execution of the compiled program
# with the reference output embedded in the source ".tc" file:
#
# ./test.sh asm indexing
#
# The script is inspired by https://github.com/kondziu/FMLtest
shopt -s nullglob
# How wide the test name is allowed to be (just aesthetics)
TEXT_WIDTH=74
# Locate this script on disk to use as root.
ROOT_DIR=$(dirname $(readlink -f "$0"))
# Where we build the tcg binary
BUILD_DIR="/tmp/build-tcg"
# Where the tests are
INPUT_DIR="$ROOT_DIR/tests/in"
# Where the reference assembly outputs are
REF_DIR="$ROOT_DIR/tests/ref"
# Where all output is saved to
OUTPUT_DIR="$ROOT_DIR/out"
# Exit code for this program (succesfull for now)
EXIT_CODE=0
tcg="$BUILD_DIR/tcg"
die() {
echo "$0: error: $@" >&2
exit 1
}
print_justified() {
message="$@"
printf "%-${TEXT_WIDTH}s" "$message"
}
print_green() {
echo -e "\e[32m$@\e[0m"
}
print_red() {
echo -e "\e[31m$@\e[0m"
}
print_yellow() {
echo -e "\e[33m$@\e[0m"
}
require_tcg() {
[[ -d "$BUILD_DIR" ]] || meson setup "$BUILD_DIR" -D b_sanitize=address,undefined
meson compile -C "$BUILD_DIR"
}
asm() {
expected="$REF_DIR/$test.asm"
output="$out.asm"
"$tcg" -S -o "$output" "$input"
}
output() {
binary="$out"
output="$out.out"
expected="$out.expected"
# Extract the relevant comments from the source file to get expected output
sed -n 's/[^/]*\/\/ > \(.*\)/\1/p' "$input" > "$expected"
"$tcg" -o "$binary" "$input" && \
"$binary" > "$output"
}
run_test() {
type=$1 # e.g. "asm" or "output"
test=${2%%.*} # e.g. "arrays" or "arrays.tc" or "arrays.tc.test"
mkdir -p "$OUTPUT_DIR/$type" || die "failed to create output dir"
echo "Signature: 8a477f597d28d172789f06886806bc55" > "$OUTPUT_DIR/CACHEDIR.TAG"
in="$INPUT_DIR/$test"
out="$OUTPUT_DIR/$type/$test"
input="$in.tc"
error_output="$out.err"
delta="$out.diff"
[[ -r "$input" ]] || die "Can't read '$input'"
# Report in
print_justified "Executing $type test: \"$test\"... "
# Run the test capturing stdout (to "$output") and stderr
"$type" 2>"$error_output"
exit_code=$?
# Compare output and expected output
diff -u "$expected" "$output" > "$delta"
diff_status=$?
# Compare results, print delta into a file, and report
if (( $exit_code == 0 && $diff_status == 0 )); then
if [[ -s "$error_output" ]]; then
print_yellow "stderr"
print_yellow "[stderr: \"$error_output\"]"
else
print_green "passed"
fi
else
print_red "failed"
(( $exit_code != 0 )) && print_red "[exit code: $exit_code]"
[[ -s "$delta" ]] && print_red "[diff: \"$delta\"]"
[[ -s "$error_output" ]] && print_yellow "[stderr: \"$error_output\"]"
EXIT_CODE=1
fi
}
run_tests() {
type="$1"
for test in "${tests[@]}"; do
run_test "$type" "$test"
done
}
# Read tests in directory with glob into array
tests=("$INPUT_DIR/"*".tc")
tests=("${tests[@]%.tc}") # remove extension
tests=("${tests[@]##*/}") # remove leading path
if [[ $# -eq 0 ]]; then
test_type="all"
else
test_type=$1
shift
fi
case "$test_type" in
all)
require_tcg
for suite in asm output; do
run_tests "$suite"
done
;;
*)
require_tcg
if [[ $# -gt 0 ]]; then
tests=("$@")
fi
run_tests "$test_type"
;;
esac
exit "$EXIT_CODE"