summary refs log tree commit diff
path: root/tools/unit.sh
blob: ace28fb5cac2a2fff5a17e8cfafebf4ef1eea9d4 (plain) (blame)
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
#!/bin/sh

DIR=`cd $(dirname "$0"); pwd`
QBE=$DIR/../obj/qbe

TMP=/tmp/qbe.zzzz

DRV=$TMP.c
ASM=$TMP.s
BIN=$TMP.bin
OUT=$TMP.out

cleanup() {
	rm -f $DRV $ASM $BIN $OUT
}

extract() {
	WHAT="$1"
	FILE="$2"

	awk "
		/^# >>> $WHAT/ {
			p = 1
			next
		}
		/^# <<</ {
			p = 0
		}
		p
	" $FILE \
	| sed -e 's/# //' \
	| sed -e 's/#$//'
}

once() {
	T="$1"

	if ! test -f $T
	then
		echo "invalid test file $T" >&2
		exit 1
	fi

	printf "%-45s" "$(basename $T)..."

	if ! $QBE -o $ASM $T
	then
		echo "[qbe fail]"
		return 1
	fi

	extract driver $T > $DRV
	extract output $T > $OUT

	if test -s $DRV
	then
		LNK="$DRV $ASM"
	else
		LNK="$ASM"
	fi

	if ! cc $PIE -g -o $BIN $LNK
	then
		echo "[cc fail]"
		return 1
	fi

	if test -s $OUT
	then
		$BIN a b c | diff - $OUT
		RET=$?
		REASON="output"
	else
		$BIN a b c
		RET=$?
		REASON="returned $RET"
	fi

	if test $RET -ne 0
	then
		echo "[$REASON fail]"
		return 1
	fi

	echo "[ok]"
}


#trap cleanup TERM QUIT

if test -z "$1"
then
	echo "usage: tools/unit.sh {all, SSAFILE}" 2>&1
	exit 1
fi

for wtf in -nopie -no-pie
do
	if echo "int main() { return 0; }" |
	   cc $wtf -x c -o /dev/null - >/dev/null 2>&1
	then
		PIE=$wtf
	fi
done

case $1 in
	"all")
		F=0
		for T in $DIR/../test/[!_]*.ssa
		do
			once $T
			F=`expr $F + $?`
		done
		if test $F -ge 1
		then
			echo
			echo "$F test(s) failed!"
		else
			echo
			echo "All is fine!"
		fi
		exit $F
		;;
	*)
		once $1
		exit $?
		;;
esac