-
Notifications
You must be signed in to change notification settings - Fork 39
/
run_tests.sh
executable file
·110 lines (99 loc) · 2.31 KB
/
run_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
set -euo pipefail
shopt -s inherit_errexit
USE_VALGRIND=0
HUFFCODE="./huffcode"
usage() {
echo "Usage: run_tests.sh [USE_VALGRIND]"
}
while [[ $# -gt 0 ]]; do
case "$1" in
--use-valgrind)
USE_VALGRIND=1
shift
;;
--huffcode)
shift
if [[ $# -eq 0 ]]; then
echo "Missing argument for --huffcode"
exit 1
fi
HUFFCODE=$1
shift
;;
*)
echo "Unexpected parameter $1"
exit 1
;;
esac
done
SCRATCH=$(mktemp --tmpdir -d huffman.XXXXXXXXXX)
# echo "Using temporary directory $SCRATCH"
cleanup() {
# echo "Cleaning up $SCRATCH"
rm -r "$SCRATCH"
}
trap cleanup EXIT
if [[ $USE_VALGRIND == 1 ]]; then
VALGRIND="valgrind \
--tool=memcheck \
--memcheck:leak-check=yes \
--leak-check=full \
--errors-for-leak-kinds=all \
--show-leak-kinds=all \
--track-origins=yes \
--error-exitcode=1"
HUFFCODE="$VALGRIND $HUFFCODE"
fi
compress_test() {
local IN=$1
local OUT="$SCRATCH/$(basename $IN)"
echo "compress_test $IN"
set +e
$HUFFCODE -i "$IN" -o "$OUT.compressed"
if [ "$?" != 0 ]; then
echo "FAILURE: huffcode encode failed on $IN"
exit 1
fi
$HUFFCODE -d -i "$OUT.compressed" -o "$OUT.decompressed"
if [ "$?" != 0 ]; then
echo "FAILURE: huffcode decode failed on $OUT.compressed"
exit 1
fi
diff "$OUT.decompressed" "$IN"
if [ "$?" != 0 ]; then
echo "FAILURE: huffcode decode didn't match original on $IN"
exit 1
fi
set -e
}
# Only decompress. Does not verify against originally compressed file,
# but is used to make sure invalid code tables do not crash the decompressor.
decompress_test() {
local IN=$1
local OUT="$SCRATCH/$(basename $IN)"
echo "decompress_test $IN"
set +e
$HUFFCODE -d -i "$IN" -o "$OUT.decompressed"
local STATUS=$?
if [ "$STATUS" != 1 ]; then
echo "Expected test to fail with 1 but got $STATUS."
exit 1
fi
set -e
}
echo "Basic Checks..."
compress_test "test_data/3_line"
compress_test "test_data/empty"
compress_test "test_data/1_byte"
compress_test "test_data/2_byte_2_symbol"
compress_test "test_data/2_byte_1_symbol"
compress_test "test_data/3_byte_1_symbol"
compress_test "test_data/3_byte_3_symbol"
compress_test "test_data/128_byte_1_symbol"
compress_test "test_data/128_byte_2_symbol"
echo "Issue 2 Regression Checks..."
for file in "test_data/reported_issues/issue2/"*; do
decompress_test "$file"
done
echo "All Tests Passed"