add a test for -limit functionality
[rofl0r-jobflow.git] / test.sh
blobbef899a090d7edb24660376a5dd7990f0416679d
1 #!/bin/sh
2 test -z "$JF" && JF=./jobflow
3 TMP=/tmp/jobflow.test.$$
4 gcc tests/stdin_printer.c -o tests/stdin_printer.out || { error compiling tests/stdin_printer.c ; exit 1 ; }
5 gcc tests/cpuwaster.c -o tests/cpuwaster.out || { error compiling tests/cpuwaster.c ; exit 1 ; }
6 tmp() {
7 echo $TMP.$testno
9 md5() {
10 md5sum "$1"|cut -d " " -f 1
12 fs() {
13 wc -c "$1"|cut -d " " -f 1
15 equal() {
16 test $(md5 "$1") = $(md5 "$2")
18 equal_size() {
19 test $(fs "$1") = $(fs "$2")
21 cleanup() {
22 rm -f $(tmp).1 $(tmp).2 $(tmp).3 $(tmp).4
24 test_equal() {
25 if equal "$1" "$2" ; then
26 cleanup
27 else
28 echo "test $testno failed."
29 echo "inspect $(tmp).* for analysis"
32 test_equal_size() {
33 if equal_size "$1" "$2" ; then
34 cleanup
35 else
36 echo "test $testno failed."
37 echo "inspect $(tmp).* for analysis"
40 dotest() {
41 [ -z "$testno" ] && testno=0
42 testno=$((testno + 1))
43 echo "running test $testno ($1)"
46 dotest "argpermutation std"
47 echo foo1337bar > $(tmp).1
48 echo 1337 | $JF -exec echo 'foo{}bar' > $(tmp).2
49 test_equal $(tmp).1 $(tmp).2
51 dotest "argpermutation std 2x"
52 echo foo1337bar1337 > $(tmp).1
53 echo 1337 | $JF -exec echo 'foo{}bar{}' > $(tmp).2
54 test_equal $(tmp).1 $(tmp).2
56 dotest "argpermutation dot"
57 echo foobar.png > $(tmp).1
58 echo foobar.bmp | $JF -exec echo '{.}.png' > $(tmp).2
59 test_equal $(tmp).1 $(tmp).2
61 dotest "argpermutation dot 2x"
62 echo 'mv foobar.pcx foobar.png' > $(tmp).1
63 echo foobar.bmp | $JF -exec echo 'mv {.}.pcx {.}.png' > $(tmp).2
64 test_equal $(tmp).1 $(tmp).2
66 dotest "seq 10 catmode skip 5"
67 seq 10 > $(tmp).1
68 $JF -skip=5 < $(tmp).1 > $(tmp).2
69 tail -n 5 < $(tmp).1 > $(tmp).3
70 test_equal $(tmp).2 $(tmp).3
72 dotest "seq 10 catmode skip 5 count 3"
73 seq 10 > $(tmp).1
74 $JF -skip=5 -count=3 < $(tmp).1 > $(tmp).2
75 tail -n 5 < $(tmp).1 | head -n 3 > $(tmp).3
76 test_equal $(tmp).2 $(tmp).3
78 dotest "seq 10000 bulk skip 1337"
79 seq 10000 | sort -u > $(tmp).1
80 $JF -bulk=4K -skip=1337 -exec tests/stdin_printer.out < $(tmp).1 | sort -u > $(tmp).2
81 tail -n $((10000 - 1337)) < $(tmp).1 > $(tmp).3
82 test_equal $(tmp).2 $(tmp).3
84 dotest "seq 100000 bulk skip 31337 3x"
85 seq 100000 | sort -u > $(tmp).1
86 $JF -bulk=4K -threads=3 -skip=31337 -exec tests/stdin_printer.out < $(tmp).1 | sort -u > $(tmp).2
87 tail -n $((100000 - 31337)) < $(tmp).1 > $(tmp).3
88 test_equal $(tmp).2 $(tmp).3
90 dotest "seq 100 catmode"
91 seq 100 > $(tmp).1
92 $JF < $(tmp).1 > $(tmp).2
93 test_equal $(tmp).1 $(tmp).2
95 dotest "seq 100 echo"
96 seq 100 > $(tmp).1
97 $JF -threads=1 -exec echo {} < $(tmp).1 > $(tmp).2
98 test_equal $(tmp).1 $(tmp).2
100 dotest "seq 10000 pipe cat"
101 seq 10000 | sort -u > $(tmp).1
102 $JF -threads=1 -exec cat < $(tmp).1 > $(tmp).2
103 test_equal $(tmp).1 $(tmp).2
105 dotest "seq 10000 pipe cat 3x"
106 # since cat reads input in chunks and not in lines, we can
107 # observe interesting effects: if one of the chunks processed
108 # by one of the cat instances happens not to end after a newline
109 # character, the contents of that line will be written up to
110 # the last character, and then another process will dump its
111 # stdout, so we'll have a line containing ouput from both
112 # processes. so it may happen that e.g. one process dumps first
113 # 2 bytes of string "100", i.e. "10" without newline, then another
114 # process will write "1\n", so the end result may have "101\n"
115 # twice, which would get filtered out by sort -u.
116 seq 10000 > $(tmp).1
117 $JF -threads=3 -exec cat < $(tmp).1 > $(tmp).2
118 test_equal_size $(tmp).1 $(tmp).2
120 dotest "seq 10000 pipe cat buffered 3x"
121 # same restrictions as above apply, but since we use -buffered
122 seq 10000 | sort -u > $(tmp).1
123 $JF -threads=3 -buffered -exec cat < $(tmp).1 | sort -u > $(tmp).2
124 test_equal $(tmp).1 $(tmp).2
126 dotest "seq 10000 pipe linecat 3x"
127 seq 10000 | sort -u > $(tmp).1
128 $JF -threads=3 -exec tests/stdin_printer.out < $(tmp).1 | sort -u > $(tmp).2
129 test_equal $(tmp).1 $(tmp).2
131 dotest "seq 10000 echo 3x"
132 seq 10000 | sort -u > $(tmp).1
133 $JF -threads=3 -exec echo {} < $(tmp).1 | sort -u > $(tmp).2
134 test_equal $(tmp).1 $(tmp).2
136 RNDLINES=7331
138 dotest "random skip echo"
139 od < /dev/urandom | head -n $RNDLINES > $(tmp).1
140 $JF -threads=1 -skip=$((RNDLINES - 10)) -exec echo {} < $(tmp).1 > $(tmp).2
141 tail -n 10 < $(tmp).1 > $(tmp).3
142 test_equal $(tmp).2 $(tmp).3
144 dotest "random echo"
145 od < /dev/urandom | head -n $RNDLINES > $(tmp).1
146 $JF -threads=1 -exec echo {} < $(tmp).1 > $(tmp).2
147 test_equal $(tmp).1 $(tmp).2
149 dotest "random echo 2x"
150 od < /dev/urandom | head -n $RNDLINES > $(tmp).1
151 $JF -threads=2 -exec echo {} < $(tmp).1 | sort -u > $(tmp).2
152 test_equal $(tmp).1 $(tmp).2
154 dotest "random echo 3x"
155 od < /dev/urandom | head -n $RNDLINES > $(tmp).1
156 $JF -threads=3 -exec echo {} < $(tmp).1 | sort -u > $(tmp).2
157 test_equal $(tmp).1 $(tmp).2
159 dotest "random echo 4x"
160 od < /dev/urandom | head -n $RNDLINES > $(tmp).1
161 $JF -threads=4 -exec echo {} < $(tmp).1 | sort -u > $(tmp).2
162 test_equal $(tmp).1 $(tmp).2
164 dotest "random echo 17x"
165 od < /dev/urandom | head -n $RNDLINES > $(tmp).1
166 $JF -threads=17 -exec echo {} < $(tmp).1 | sort -u > $(tmp).2
167 test_equal $(tmp).1 $(tmp).2
169 dotest "random echo buffered 17x"
170 od < /dev/urandom | head -n $RNDLINES > $(tmp).1
171 $JF -threads=17 -buffered -exec echo {} < $(tmp).1 | sort -u > $(tmp).2
172 test_equal $(tmp).1 $(tmp).2
174 dotest "random pipe"
175 od < /dev/urandom | head -n $RNDLINES > $(tmp).1
176 $JF -threads=1 -exec cat < $(tmp).1 > $(tmp).2
177 test_equal $(tmp).1 $(tmp).2
179 dotest "random pipe 3x"
180 od < /dev/urandom | head -n $RNDLINES > $(tmp).1
181 $JF -threads=3 -exec cat < $(tmp).1 | sort -u > $(tmp).2
182 test_equal $(tmp).1 $(tmp).2
184 dotest "random pipe 17x"
185 od < /dev/urandom | head -n $RNDLINES > $(tmp).1
186 $JF -threads=17 -exec cat < $(tmp).1 | sort -u > $(tmp).2
187 test_equal $(tmp).1 $(tmp).2
189 dotest "random pipe buffered 17x"
190 od < /dev/urandom | head -n $RNDLINES > $(tmp).1
191 $JF -threads=17 -buffered -exec tests/stdin_printer.out < $(tmp).1 | sort -u > $(tmp).2
192 test_equal $(tmp).1 $(tmp).2
194 dotest "limit cpu 1sec"
195 seq 1 | $JF -limits cpu=1 -exec tests/cpuwaster.out 2 && echo "test $testno failed."