-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathprog.alt-test.sh
More file actions
executable file
·137 lines (116 loc) · 2.31 KB
/
prog.alt-test.sh
File metadata and controls
executable file
·137 lines (116 loc) · 2.31 KB
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
#!/usr/bin/env bash
# Assume a POSIX compliant shell.
make alt || exit 1
PROG="./prog.alt"
args=$(getopt 'f:w' "$@")
status="$?"
if [[ $status -ne 0 ]]; then
echo "usage: prog.alt-test.sh [-w][-f flags]"
exit 2
fi
set -- "$args"
while [[ $# -gt 0 ]]; do
case "$1" in
-w)
PROG="./prog-w64"
;;
-f)
flags=$2; shift
;;
--)
shift; break
;;
*)
;;
esac
shift
done
function distance
{
typeset A="$1"; shift
typeset B="$1"; shift
typeset expect="$1"; shift
printf "%s" "$A" | sed -e 's/./&\
/g' >"$A.tmp"
printf "%s" "$B" | sed -e 's/./&\
/g' >"$B.tmp"
echo "<<< A='$A' B='$B' $*"
typeset dist
dist=$("$PROG" -d "$@" "$A.tmp" "$B.tmp" | tr -d '\r')
printf '>>> got=%d expect=%d ' "$dist" "$expect"
if [[ "$dist" -ne "$expect" ]]; then
echo FAIL
return 1
fi
echo -OK-
return 0
}
function dif
{
typeset A="$1"; shift
typeset B="$1"; shift
typeset expect="$1"; shift
echo "==== $A $B"
"$PROG" "$A" "$B" | tee patch.tmp
typeset ex=$?
if [[ "$expect" -eq 0 ]]; then
printf "got=%d expect=%d " "$ex" "$expect"
if [[ $expect -ne $ex ]]; then
echo FAIL
return 1
fi
echo -OK-
return 0
fi
printf "forward patch "
cp "$A" copy.tmp
if ! patch -f -s copy.tmp patch.tmp ; then
echo FAIL
return 1
fi
echo -OK-
printf "forward compare "
if ! cmp "$B" copy.tmp ; then
echo FAIL
return 1
fi
echo -OK-
printf "reverse patch "
if ! patch -R -f -s copy.tmp patch.tmp ; then
echo FAIL
return 1
fi
echo -OK-
printf "reverse compare "
if ! cmp "$A" copy.tmp ; then
echo FAIL
return 1
fi
echo -OK-
return 0
}
distance "1" "1" 0 "$flags"
distance "1" "A" 2 "$flags"
distance "123" "ABCDE" 8 "$flags"
distance "ABCDE" "123" 8 "$flags"
distance "ABD" "ABCD" 1 "$flags"
distance "ABCD" "ABD" 1 "$flags"
distance "ABCD" "ACDBECFD" 4 "$flags"
distance "ABCDEF" "ABXYEFCD" 6 "$flags"
distance "ABCDEFGHIJK" "ABCEFGIJKDEFGHIJK" 6 "$flags"
distance "ABCABBA" "CBABAC" 5 "$flags"
distance "ACEBDABBABED" "ACBDEACBED" 6 "$flags"
touch EMPTY.tmp
dif 1.tmp 1.tmp 0
dif EMPTY.tmp 123.tmp 1
dif 123.tmp EMPTY.tmp 1
dif ABCD.tmp ACDBECFD.tmp 1
dif ACDBECFD.tmp ABCD.tmp 1
dif 123.tmp ABCDE.tmp 1
dif ABCDE.tmp 123.tmp 1
# Note that prog and diff(1) have same edit distance,
# but differ in the output.
dif CBABAC.tmp ABCABBA.tmp 1
dif ABCABBA.tmp CBABAC.tmp 1
rm copy.tmp* 2>/dev/null
echo DONE