-
Notifications
You must be signed in to change notification settings - Fork 39
/
test-scripts.sh
executable file
·302 lines (266 loc) · 8.46 KB
/
test-scripts.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/bin/sh
n_tests=0
failed=0
test_finished() {
rm -rf tc.*
echo
if [ "$failed" = "0" ]
then
echo "PASS ($n_tests tests) -- `hostname` / `uname -srm`"
exit 0
else
echo "FAIL ($failed/$n_tests failed) -- `hostname` / `uname -srm`"
exit 1
fi
}
print() {
if [ `/bin/echo -n x` = "-n" ]
then
/bin/echo $1'\c'
else
/bin/echo -n $1
fi
}
progress() {
print '.'
}
begin_test() {
progress
n_tests=`expr $n_tests "+" 1`
}
test_failed() {
failed=`expr $failed "+" 1`
}
assert_equal() {
begin_test
_f="no"
excmd=$1; shift
mycmd=$1; shift
eval "$excmd" >tc.out.expected 2>tc.err.expected
eval "$mycmd" >tc.out.real 2>tc.err.real
assert_not_coredump || return
cmp tc.out.real tc.out.expected >/dev/null 2>&1
if [ "$?" != "0" ]
then
echo "stdout differ: \"$excmd\" and \"$mycmd\""
diff -u tc.out.expected tc.out.real
echo "----"
_f="yes"
fi
cmp tc.err.real tc.err.expected >/dev/null 2>&1
if [ "$?" != "0" ]
then
echo "stderr differ: \"$excmd\" and \"$mycmd\""
diff -u tc.err.expected tc.err.real
echo "----"
_f="yes"
fi
[ "$_f" = "yes" ] && test_failed
}
assert_equal_stdout() {
begin_test
_f="no"
excmd=$1; shift
mycmd=$1; shift
eval "$excmd" >tc.out.expected 2>/dev/null
eval "$mycmd" >tc.out.real 2>/dev/null
assert_not_coredump || return
cmp tc.out.real tc.out.expected >/dev/null 2>&1
if [ "$?" != "0" ]
then
echo "stdout differ: \"$excmd\" and \"$mycmd\""
diff -u tc.out.expected tc.out.real
echo "----"
test_failed
fi
}
assert_stdout() {
begin_test
expected=$1; shift
mycmd=$1; shift
echo "$expected" > tc.out.expected
eval "$mycmd" >tc.out.real 2>/dev/null
assert_not_coredump || return
cmp tc.out.expected tc.out.real >/dev/null 2>&1
if [ "$?" != "0" ]
then
echo "stdout differ: string \"$expected\" and cmd \"$mycmd\""
diff -u tc.out.expected tc.out.real
echo "----"
test_failed
fi
}
rm -f core
assert_not_coredump() {
begin_test
if [ -f core ]
then
echo "core dumped: $mycmd"
echo "----"
test_failed
return 1
fi
rm -f core
return 0
}
assert_not_exist() {
begin_test
file=$1; shift
if [ -f $file ]
then
echo "exists: $file"
echo "----"
test_failed
return 1
fi
}
assert_directory() {
begin_test
dir=$1; shift
if [ ! -d $dir ]
then
echo "not directory: $dir"
echo "----"
test_failed
return 1
fi
}
Head() {
if [ `uname -s` = "SunOS" ]
then
:
else
head ${@+"$@"}
fi
}
uidof() {
id $1 | sed -e 's/=/ /' -e 's/(/ /' | awk '{print $2}'
}
assert_stdout "argc=1
argv[0]=./args" './args'
assert_stdout "argc=2
argv[0]=./args
argv[1]=x" './args x'
assert_stdout "argc=3
argv[0]=./args
argv[1]=x
argv[2]=y" './args x y'
assert_stdout "Hello, World!" './hello'
assert_stdout "" './echo'
assert_stdout "a" './echo a'
assert_stdout "a b c" './echo a b c'
assert_stdout "a b c" './echo a "b c"'
assert_stdout "a b c" './echo a "b c"'
assert_equal 'cat cat.c' './cat cat.c'
assert_equal 'cat httpd2.c' './cat httpd2.c'
assert_equal 'cat cat.c' './cat2 cat.c'
assert_equal 'cat httpd2.c' './cat2 httpd2.c'
assert_equal 'cat cat.c' './cat3 cat.c'
assert_equal 'cat httpd2.c' './cat3 httpd2.c'
assert_equal 'cat cat.c' './cat3 < cat.c'
assert_equal 'cat httpd2.c' './cat3 < httpd2.c'
assert_equal 'cat data.esc' './cat-escape data.esc0'
assert_equal 'cat cat.c' './cat4 cat.c'
assert_equal 'cat httpd2.c' './cat4 httpd2.c'
assert_equal 'cat cat.c' './cat4 < cat.c'
assert_equal 'cat httpd2.c' './cat4 < httpd2.c'
assert_equal 'cat data.esc' './cat4 -e data.esc0'
assert_equal 'cat data.esc' './cat4 -e < data.esc0'
assert_equal 'Head -n 0 head.c' './head 0 < head.c'
assert_equal 'head -n 5 head.c' './head 5 < head.c'
assert_equal 'head -n 999 head.c' './head 999 < head.c'
assert_equal 'Head -n 0 head.c' './head2 0 < head.c'
assert_equal 'head -n 5 head.c' './head2 5 < head.c'
assert_equal 'head -n 999 head.c' './head2 999 < head.c'
assert_equal 'Head -n 0 head.c' './head3 -n 0 < head.c'
assert_equal 'head -n 5 head.c' './head3 -n 5 < head.c'
assert_equal 'head -n 999 head.c' './head3 -n 999 < head.c'
if [ -f head4 ]; then
assert_equal 'Head -n 0 head.c' './head4 -n 0 < head.c'
assert_equal 'head -n 5 head.c' './head4 -n 5 < head.c'
assert_equal 'head -n 999 head.c' './head4 -n 999 < head.c'
assert_equal 'Head -n 0 head.c' './head4 --lines=0 < head.c'
assert_equal 'head -n 5 head.c' './head4 --lines=5 < head.c'
assert_equal 'head -n 999 head.c' './head4 --lines=999 < head.c'
fi
assert_equal 'tail -0 tail.c' './tail2 0 < tail.c'
assert_equal 'tail -5 tail.c' './tail2 5 < tail.c'
assert_equal 'tail -999 tail.c' './tail2 999 < tail.c'
assert_equal 'grep close head.c' './grep close head.c'
assert_equal 'grep NOTMATCH head.c' './grep NOTMATCH head.c'
assert_equal 'grep close /dev/null' './grep close /dev/null'
assert_equal 'grep close head.c' './grep close head.c'
assert_equal 'grep "open|close" head.c' './grep "open|close" head.c'
assert_equal 'grep close head.c' './grep2 close head.c'
assert_equal 'grep NOTMATCH head.c' './grep2 NOTMATCH head.c'
assert_equal 'grep close /dev/null' './grep2 close /dev/null'
assert_equal 'grep close head.c' './grep2 close head.c'
assert_equal 'grep "open|close" head.c' './grep2 "open|close" head.c'
assert_equal 'grep -v close head.c' './grep2 -v close head.c'
assert_equal 'grep -v NOTMATCH head.c' './grep2 -v NOTMATCH head.c'
assert_equal 'grep CLOSE head.c' './grep2 CLOSE head.c'
assert_equal 'grep -i CLOSE head.c' './grep2 -i CLOSE head.c'
assert_equal './grep2 -i close head.c' './grep2 -i cLoSe head.c'
assert_equal 'grep close head.c' './grep3 close head.c'
assert_equal 'grep NOTMATCH head.c' './grep3 NOTMATCH head.c'
assert_equal 'grep close /dev/null' './grep3 close /dev/null'
assert_equal 'grep close head.c' './grep3 close head.c'
assert_equal 'grep "open|close" head.c' './grep3 "open|close" head.c'
# FIXME: ln
# FIXME: symlink
touch tc.tmp
./rm tc.tmp
assert_not_exist tc.tmp
touch tc.tmp1 tc.tmp2 tc.tmp3
./rm tc.tmp*
assert_not_exist tc.tmp1
assert_not_exist tc.tmp2
assert_not_exist tc.tmp3
rm -f tc.tmp*
mkdir -p tc.tmpdir
./rmdir tc.tmpdir
assert_not_exist tc.tmpdir
mkdir -p tc.tmpdir1 tc.tmpdir2 tc.tmpdir3
./rmdir tc.tmpdir1 tc.tmpdir2 tc.tmpdir3
assert_not_exist tc.tmpdir1
assert_not_exist tc.tmpdir2
assert_not_exist tc.tmpdir3
rm -rf tc.tmpdir*
rm -rf tc.tmpdir
./mkdir tc.tmpdir
assert_not_coredump
assert_directory tc.tmpdir
rm -rf tc.tmpdir*
./mkdir tc.tmpdir1 tc.tmpdir2 tc.tmpdir3
assert_not_coredump
assert_directory tc.tmpdir1
assert_directory tc.tmpdir2
assert_directory tc.tmpdir3
rm -rf tc.tmpdir*
assert_equal 'ls -a' './ls . | sort'
assert_equal 'ls -a /' './ls / | sort'
assert_equal 'env | grep -v "^_"' './env | grep -v "^_"'
assert_equal '/bin/pwd' './pwd'
assert_equal '/bin/pwd' './pwd2'
if [ -f pwd3 ]; then
assert_equal '/bin/pwd' './pwd3'
fi
assert_stdout "id=`uidof root`" './user root'
assert_stdout "id=`uidof nobody`" './user nobody'
assert_equal "id | awk '{print \$1}'" "./id | awk '{print \$1}'"
assert_equal "id | awk '{print \$2}'" "./id | awk '{print \$2}'"
assert_equal_stdout 'find .' './traverse .'
print '' > tc.tmp
assert_stdout "0" './wc-l-stdio tc.tmp'
print 'a' > tc.tmp
assert_stdout "1" './wc-l-stdio tc.tmp'
echo 'a' > tc.tmp
assert_stdout "1" './wc-l-stdio tc.tmp'
echo 'a' > tc.tmp
print 'b' >> tc.tmp
assert_stdout "2" './wc-l-stdio tc.tmp'
echo 'a' > tc.tmp
echo 'b' >> tc.tmp
assert_stdout "2" './wc-l-stdio tc.tmp'
rm -f tc.tmp
test_finished