*** empty log message ***
[rofl0r-order-pp.git] / test / Test-shell
blobd6233a95f801b29af9a0d21e307634046317a1d1
1 #!/bin/bash
3 # (C) Copyright Vesa Karvonen 2004.
5 # Distributed under the Boost Software License, Version 1.0.
6 # (See accompanying file LICENSE.)
8 set -e
10 TIMEFORMAT='Time: %R seconds'
12 dbFile=test.db
13 tmpFile=.test.tmp
15 debugFlag=1
17 function check-and-add-preprocessor {
18 if $1 >& /dev/null ; then
19 availablePreprocessors="[$2] $availablePreprocessors"
23 if ! test -f .preprocessor ; then
24 availablePreprocessors=''
26 check-and-add-preprocessor "cpp --help" cpp
27 check-and-add-preprocessor "wave -v" wave
29 read -e -p "Choose preprocessor: $availablePreprocessors? "
30 if test "$(echo "$availablePreprocessors" | grep "\\[$REPLY\\]")" != "$availablePreprocessors" ; then
31 echo "
32 Sorry, \"$REPLY\" isn't one of the preprocessors:
34 $availablePreprocessors
36 If you wish to use another preprocessor, you need to customize this
37 script for the preprocessor, because there is no standard on the
38 command line options accepted by a preprocessor."
39 exit 1
41 echo "$REPLY" > .preprocessor
42 echo "Wrote $REPLY to the .preprocessor file."
45 preprocessor=$(cat .preprocessor)
47 function eval-in-cpp {
48 cpp -P \
49 -std=c99 \
50 -Wall \
51 -pedantic-errors \
52 $(if test $debugFlag = 1 ; then echo '-DORDER_PP_DEBUG' ; fi) \
53 -I "../inc/" \
54 -I "../../chaos-pp/" \
55 -include "../inc/order/interpreter.h" \
56 -include "test-defs.h"
59 function eval-in-wave {
60 wave --variadics \
61 $(if test $debugFlag = 1 ; then echo '--define ORDER_PP_DEBUG' ; fi) \
62 --include "../inc/" \
63 --include "../../chaos-pp/" \
64 --forceinclude "order/interpreter.h" \
65 --forceinclude "test-defs.h"
68 function eval-in {
69 eval-in-$preprocessor
72 function spacify {
73 sed -e 's|,| , |g' \
74 -e 's|(| ( |g' \
75 -e 's|)| ) |g' \
76 -e 's| \+| |g'
79 function sed-from {
80 local tmp="$1"
81 shift 1
82 echo "$tmp" | sed "$@"
85 function test-cnt {
86 cat $dbFile | wc -l | sed 's# ##g'
89 function get-line {
90 head -n $(($1+1)) | tail -n1
93 function get-line-from {
94 echo "$2" | get-line $1
97 function get-test {
98 cat $dbFile | get-line $1
101 function add-test {
102 echo "$1#$2#$3" >> $dbFile
103 clean-test-db
106 function clean-test-db {
107 local tmp="$(cat $dbFile)"
108 echo "$tmp" | sort | uniq > $dbFile
111 function remove-test {
112 local lines=$(test-cnt)
113 local before=$(head -n $1 $dbFile)
114 local after=$(tail -n $(($lines-1-$1)) $dbFile)
115 echo -n > $dbFile
116 if test "$before" != "" ; then echo "$before" >> $dbFile ; fi
117 if test "$after" != "" ; then echo "$after" >> $dbFile ; fi
120 function replace-test {
121 local lines=$(test-cnt)
122 local before=$(head -n $1 $dbFile)
123 local after=$(tail -n $(($lines-1-$1)) $dbFile)
124 echo -n > $dbFile
125 if test "$before" != "" ; then echo "$before" >> $dbFile ; fi
126 add-test "$2" "$3" "$4"
127 if test "$after" != "" ; then echo "$after" >> $dbFile ; fi
130 function preprocess-positive-tests {
131 sed -e 's|^.*#.*#O.*$||g' \
132 -e 's|#.*||g' \
133 -e 's|^\(.\+\)$|ORDER_PP(\1)|g' \
134 $dbFile | \
135 spacify | \
136 eval-in | \
137 tail -n $(test-cnt)
140 function differing-lines {
141 echo "$1" | \
142 nl -s '#' -v 0 | \
143 grep -v -e "^\\($(echo -n "$2" | sed 's#\([^a-zA-Z0-9()?+|{, ]\)#\\\1#g' | nl -s '#' -v 0 | tr "\n" '$' | sed 's#\$#\\\|#g')\\)\$" | \
144 sed -e 's|#.*||g' -e 's|[ \t]||g' | tr "\n" ' '
147 function run-tests {
148 echo "Total of $(test-cnt) tests."
149 echo "Running positive tests..."
151 local allTerm=$(sed -e 's|#.*||g' $dbFile)
152 local allExpected=$(sed -e 's|^[^#]*#||g' -e 's|#.*$||g' $dbFile)
153 local allErrors=$(sed -e 's|.*#||g' $dbFile)
155 local posActual=$(preprocess-positive-tests)
156 local posExpected=$(sed -e 's|^.*#.*#O.*$||g' -e 's|^[^#]*#||g' -e 's|#.*$||g' $dbFile)
158 echo "Comparing results..."
160 for i in $(differing-lines "$posActual" "$posExpected") ; do
161 term=$(get-line-from $i "$allTerm")
162 local result=$(get-line-from $i "$posActual")
163 local expected=$(get-line-from $i "$posExpected")
165 echo "Term: $term"
166 echo "Expected: $expected"
167 echo "Result: $result"
168 read -e -n 1 -p "[(U)se result | (R)emove test | Skip]? "
169 case "$REPLY" in
170 ("u"|"U")
171 echo "Using result"
172 replace-test $i "$term" "$result" "" ;;
173 ("r"|"R")
174 echo "Removing test"
175 remove-test $i ;;
177 echo "Skipping" ;;
178 esac
179 done
181 echo "Running and comparing negative tests..."
183 local oldDebugFlag=$debugFlag
184 debugFlag=1
186 for i in $(differing-lines "$(sed -e 's|.*#.*#O.*|\$|g' -e 's|[^$]\+|#|g' $dbFile)" "$(sed -e 's|.\+|#|g' $dbFile)") ; do
187 term=$(get-line-from $i "$allTerm")
188 local expectedResult=$(get-line-from $i "$allExpected")
189 local expectedErrors=$(get-line-from $i "$allErrors")
191 do-eval
193 if test "$result#$errors" != "$expectedResult#$expectedErrors" ; then
194 echo "Term: $term"
195 echo "Expected result: $expectedResult"
196 echo "Actual result: $result"
197 echo "Expected error: $expectedErrors"
198 echo "Actual error: $errors"
199 read -e -n 1 -p "[(U)se result | (R)emove test | Skip]? "
200 case "$REPLY" in
201 ("u"|"U")
202 echo "Using result"
203 replace-test $i "$term" "$result" "$errors" ;;
204 ("r"|"R")
205 echo "Removing test"
206 remove-test $i ;;
208 echo "Skipping" ;;
209 esac
211 done
213 debugFlag=$oldDebugFlag
216 function do-eval {
217 result=$(echo "ORDER_PP($term)" | spacify | eval-in 2>"$tmpFile" | tail -n1)
218 resultChars=$(echo "$result" | wc -m | sed 's# ##g')
219 resultLines=$(echo "$result" | wc -l | sed 's# ##g')
220 messages="$(cat "$tmpFile")"
221 if test -n "$messages" ; then
222 errors=$(echo "$messages" | grep -o 'ORDER_PP_[a-zA-Z0-9_]\+' | sort | uniq | tr $'\n' ' ' | sed 's# $##g')
223 else
224 errors=""
228 function interactive-eval {
229 read -e -p "Term: "
230 if test "$REPLY" != "" ; then
231 term="$REPLY"
232 else
233 echo "Term: $term"
236 time do-eval
238 if test -n "$messages" ; then
239 echo "Errors: $errors"
240 echo
241 echo "$messages"
242 echo
244 if test $(($COLUMNS-9)) -lt $resultChars ; then
245 echo "The output contains $resultChars characters on $resultLines lines!"
246 read -e -n 1 -p "[(S)kip | (L)ess | Display]? "
247 case "$REPLY" in
248 ("l"|"L")
249 echo "Result: $result" | less ;;
250 ("s"|"S")
251 echo "Skipping" ;;
253 echo "Result: $result" ;;
254 esac
255 else
256 echo "Result: $result"
260 function interactive-trace {
261 read -e -p "Term: "
262 if test "$REPLY" != "" ; then
263 term="$REPLY"
264 else
265 echo "Term: $term"
268 local steps=0
270 result=$(echo "(,,ORDER_PP_DEF($(echo "$term" | spacify)),8EXIT,)" | eval-in)
271 echo ": $result"
273 while read -e -n 1 -p "[(#$steps) | Enter to step]? " ; do
274 if test "$REPLY" != "" ; then
275 break
278 steps=$(($steps+1))
280 result=$(echo "ORDER_PP_CM_DN_0 $result" | eval-in)
281 echo ": $result"
283 if ! echo "$result" | grep -q '^(' ; then
284 echo "[ Program stopped. ]"
285 break
287 done
290 function interactive-add-test {
291 interactive-eval
292 read -e -n 1 -p "Add [(N)o | Yes]? "
293 case "$REPLY" in
294 ("n"|"N")
295 echo "Not added.";;
297 add-test "$term" "$result" "$errors"
298 echo "Added." ;;
299 esac
302 echo "Database: $dbFile"
303 if ! cp -r $dbFile $dbFile.bak ; then
304 echo "Backup failed. Terminating."
305 exit
306 else
307 echo "Backed up database to: $dbFile.bak"
310 # IFS=''
311 REPLY=''
313 while true ; do
314 if test -z "$REPLY" ; then
315 if test "$#" -gt 0 ; then
316 REPLY="$(echo "$1" | sed 's#^-*##g')"
317 shift
318 else
319 read -e -n 1 -p "[(A)dd | (C)lean | (D)ebug | (M)ode: $debugFlag | (Q)uit | (S)how | (T)est | Eval]? "
322 case "$REPLY" in
323 (a*|A)
324 interactive-add-test ;;
325 (t*|T)
326 time run-tests ;;
327 (s*|S)
328 less $dbFile ;;
329 (c*|C)
330 clean-test-db ;;
331 (q*|Q)
332 exit ;;
333 (d*|D)
334 interactive-trace ;;
335 (m*|M)
336 debugFlag=$((1-debugFlag)) ;;
338 interactive-eval
339 REPLY="" ;;
340 esac
341 REPLY="$(echo "$REPLY" | sed 's#^.##g')"
342 done