Update to Zstandard 1.4.4
[freebsd.git] / sys / contrib / zstd / tests / playTests.sh
1 #!/bin/sh
2
3 set -e
4
5 die() {
6     println "$@" 1>&2
7     exit 1
8 }
9
10 roundTripTest() {
11     if [ -n "$3" ]; then
12         cLevel="$3"
13         proba="$2"
14     else
15         cLevel="$2"
16         proba=""
17     fi
18     if [ -n "$4" ]; then
19         dLevel="$4"
20     else
21         dLevel="$cLevel"
22     fi
23
24     rm -f tmp1 tmp2
25     println "roundTripTest: ./datagen $1 $proba | $ZSTD -v$cLevel | $ZSTD -d$dLevel"
26     ./datagen $1 $proba | $MD5SUM > tmp1
27     ./datagen $1 $proba | $ZSTD --ultra -v$cLevel | $ZSTD -d$dLevel  | $MD5SUM > tmp2
28     $DIFF -q tmp1 tmp2
29 }
30
31 fileRoundTripTest() {
32     if [ -n "$3" ]; then
33         local_c="$3"
34         local_p="$2"
35     else
36         local_c="$2"
37         local_p=""
38     fi
39     if [ -n "$4" ]; then
40         local_d="$4"
41     else
42         local_d="$local_c"
43     fi
44
45     rm -f tmp.zstd tmp.md5.1 tmp.md5.2
46     println "fileRoundTripTest: ./datagen $1 $local_p > tmp && $ZSTD -v$local_c -c tmp | $ZSTD -d$local_d"
47     ./datagen $1 $local_p > tmp
48     < tmp $MD5SUM > tmp.md5.1
49     $ZSTD --ultra -v$local_c -c tmp | $ZSTD -d$local_d | $MD5SUM > tmp.md5.2
50     $DIFF -q tmp.md5.1 tmp.md5.2
51 }
52
53 truncateLastByte() {
54     dd bs=1 count=$(($(wc -c < "$1") - 1)) if="$1"
55 }
56
57 println() {
58     printf '%b\n' "${*}"
59 }
60
61
62 SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
63 PRGDIR="$SCRIPT_DIR/../programs"
64 TESTDIR="$SCRIPT_DIR/../tests"
65 UNAME=$(uname)
66
67 detectedTerminal=false
68 if [ -t 0 ] && [ -t 1 ]
69 then
70     detectedTerminal=true
71 fi
72 isTerminal=${isTerminal:-$detectedTerminal}
73
74 isWindows=false
75 INTOVOID="/dev/null"
76 case "$UNAME" in
77   GNU) DEVDEVICE="/dev/random" ;;
78   *) DEVDEVICE="/dev/zero" ;;
79 esac
80 case "$OS" in
81   Windows*)
82     isWindows=true
83     INTOVOID="NUL"
84     DEVDEVICE="NUL"
85     ;;
86 esac
87
88 case "$UNAME" in
89   Darwin) MD5SUM="md5 -r" ;;
90   FreeBSD) MD5SUM="gmd5sum" ;;
91   OpenBSD) MD5SUM="md5" ;;
92   *) MD5SUM="md5sum" ;;
93 esac
94
95 DIFF="diff"
96 case "$UNAME" in
97   SunOS) DIFF="gdiff" ;;
98 esac
99
100 println "\nStarting playTests.sh isWindows=$isWindows ZSTD='$ZSTD'"
101
102 [ -n "$ZSTD" ] || die "ZSTD variable must be defined!"
103
104 if echo hello | $ZSTD -v -T2 2>&1 > $INTOVOID | grep -q 'multi-threading is disabled'
105 then
106     hasMT=""
107 else
108     hasMT="true"
109 fi
110
111
112 println "\n===>  simple tests "
113
114 ./datagen > tmp
115 println "test : basic compression "
116 $ZSTD -f tmp                      # trivial compression case, creates tmp.zst
117 println "test : basic decompression"
118 $ZSTD -df tmp.zst                 # trivial decompression case (overwrites tmp)
119 println "test : too large compression level => auto-fix"
120 $ZSTD -99 -f tmp  # too large compression level, automatic sized down
121 $ZSTD -5000000000 -f tmp && die "too large numeric value : must fail"
122 println "test : --fast aka negative compression levels"
123 $ZSTD --fast -f tmp  # == -1
124 $ZSTD --fast=3 -f tmp  # == -3
125 $ZSTD --fast=200000 -f tmp  # too low compression level, automatic fixed
126 $ZSTD --fast=5000000000 -f tmp && die "too large numeric value : must fail"
127 $ZSTD -c --fast=0 tmp > $INTOVOID && die "--fast must not accept value 0"
128 println "test : too large numeric argument"
129 $ZSTD --fast=9999999999 -f tmp  && die "should have refused numeric value"
130 println "test : set compression level with environment variable ZSTD_CLEVEL"
131 ZSTD_CLEVEL=12  $ZSTD -f tmp # positive compression level
132 ZSTD_CLEVEL=-12 $ZSTD -f tmp # negative compression level
133 ZSTD_CLEVEL=+12 $ZSTD -f tmp # valid: verbose '+' sign
134 ZSTD_CLEVEL=''  $ZSTD -f tmp # empty env var, warn and revert to default setting
135 ZSTD_CLEVEL=-   $ZSTD -f tmp # malformed env var, warn and revert to default setting
136 ZSTD_CLEVEL=a   $ZSTD -f tmp # malformed env var, warn and revert to default setting
137 ZSTD_CLEVEL=+a  $ZSTD -f tmp # malformed env var, warn and revert to default setting
138 ZSTD_CLEVEL=3a7 $ZSTD -f tmp # malformed env var, warn and revert to default setting
139 ZSTD_CLEVEL=50000000000  $ZSTD -f tmp # numeric value too large, warn and revert to default setting
140 println "test : override ZSTD_CLEVEL with command line option"
141 ZSTD_CLEVEL=12  $ZSTD --fast=3 -f tmp # overridden by command line option
142 println "test : compress to stdout"
143 $ZSTD tmp -c > tmpCompressed
144 $ZSTD tmp --stdout > tmpCompressed       # long command format
145 println "test : compress to named file"
146 rm tmpCompressed
147 $ZSTD tmp -o tmpCompressed
148 test -f tmpCompressed   # file must be created
149 println "test : -o must be followed by filename (must fail)"
150 $ZSTD tmp -of tmpCompressed && die "-o must be followed by filename "
151 println "test : force write, correct order"
152 $ZSTD tmp -fo tmpCompressed
153 println "test : forgotten argument"
154 cp tmp tmp2
155 $ZSTD tmp2 -fo && die "-o must be followed by filename "
156 println "test : implied stdout when input is stdin"
157 println bob | $ZSTD | $ZSTD -d
158 if [ "$isTerminal" = true ]; then
159 println "test : compressed data to terminal"
160 println bob | $ZSTD && die "should have refused : compressed data to terminal"
161 println "test : compressed data from terminal (a hang here is a test fail, zstd is wrongly waiting on data from terminal)"
162 $ZSTD -d > $INTOVOID && die "should have refused : compressed data from terminal"
163 fi
164 println "test : null-length file roundtrip"
165 println -n '' | $ZSTD - --stdout | $ZSTD -d --stdout
166 println "test : ensure small file doesn't add 3-bytes null block"
167 ./datagen -g1 > tmp1
168 $ZSTD tmp1 -c | wc -c | grep "14"
169 $ZSTD < tmp1  | wc -c | grep "14"
170 println "test : decompress file with wrong suffix (must fail)"
171 $ZSTD -d tmpCompressed && die "wrong suffix error not detected!"
172 $ZSTD -df tmp && die "should have refused : wrong extension"
173 println "test : decompress into stdout"
174 $ZSTD -d tmpCompressed -c > tmpResult    # decompression using stdout
175 $ZSTD --decompress tmpCompressed -c > tmpResult
176 $ZSTD --decompress tmpCompressed --stdout > tmpResult
177 println "test : decompress from stdin into stdout"
178 $ZSTD -dc   < tmp.zst > $INTOVOID   # combine decompression, stdin & stdout
179 $ZSTD -dc - < tmp.zst > $INTOVOID
180 $ZSTD -d    < tmp.zst > $INTOVOID   # implicit stdout when stdin is used
181 $ZSTD -d  - < tmp.zst > $INTOVOID
182 println "test : impose memory limitation (must fail)"
183 $ZSTD -d -f tmp.zst -M2K -c > $INTOVOID && die "decompression needs more memory than allowed"
184 $ZSTD -d -f tmp.zst --memlimit=2K -c > $INTOVOID && die "decompression needs more memory than allowed"  # long command
185 $ZSTD -d -f tmp.zst --memory=2K -c > $INTOVOID && die "decompression needs more memory than allowed"  # long command
186 $ZSTD -d -f tmp.zst --memlimit-decompress=2K -c > $INTOVOID && die "decompression needs more memory than allowed"  # long command
187 println "test : overwrite protection"
188 $ZSTD -q tmp && die "overwrite check failed!"
189 println "test : force overwrite"
190 $ZSTD -q -f tmp
191 $ZSTD -q --force tmp
192 println "test : overwrite readonly file"
193 rm -f tmpro tmpro.zst
194 println foo > tmpro.zst
195 println foo > tmpro
196 chmod 400 tmpro.zst
197 $ZSTD -q tmpro && die "should have refused to overwrite read-only file"
198 $ZSTD -q -f tmpro
199 println "test: --no-progress flag"
200 $ZSTD tmpro -c --no-progress | $ZSTD -d -f -o "$INTOVOID" --no-progress
201 $ZSTD tmpro -cv --no-progress | $ZSTD -dv -f -o "$INTOVOID" --no-progress
202 rm -f tmpro tmpro.zst
203 println "test: overwrite input file (must fail)"
204 $ZSTD tmp -fo tmp && die "zstd compression overwrote the input file"
205 $ZSTD tmp.zst -dfo tmp.zst && die "zstd decompression overwrote the input file"
206 println "test: detect that input file does not exist"
207 $ZSTD nothere && die "zstd hasn't detected that input file does not exist"
208 println "test: --[no-]compress-literals"
209 $ZSTD tmp -c --no-compress-literals -1       | $ZSTD -t
210 $ZSTD tmp -c --no-compress-literals --fast=1 | $ZSTD -t
211 $ZSTD tmp -c --no-compress-literals -19      | $ZSTD -t
212 $ZSTD tmp -c --compress-literals    -1       | $ZSTD -t
213 $ZSTD tmp -c --compress-literals    --fast=1 | $ZSTD -t
214 $ZSTD tmp -c --compress-literals    -19      | $ZSTD -t
215 $ZSTD -b --fast=1 -i0e1 tmp --compress-literals
216 $ZSTD -b --fast=1 -i0e1 tmp --no-compress-literals
217
218 println "test: --exclude-compressed flag"
219 rm -rf precompressedFilterTestDir
220 mkdir -p precompressedFilterTestDir
221 ./datagen $size > precompressedFilterTestDir/input.5
222 ./datagen $size > precompressedFilterTestDir/input.6
223 $ZSTD --exclude-compressed --long --rm -r precompressedFilterTestDir
224 sleep 5
225 ./datagen $size > precompressedFilterTestDir/input.7
226 ./datagen $size > precompressedFilterTestDir/input.8
227 $ZSTD --exclude-compressed --long --rm -r precompressedFilterTestDir
228 test ! -f precompressedFilterTestDir/input.5.zst.zst
229 test ! -f precompressedFilterTestDir/input.6.zst.zst
230 file1timestamp=`date -r precompressedFilterTestDir/input.5.zst +%s`
231 file2timestamp=`date -r precompressedFilterTestDir/input.7.zst +%s`
232 if [[ $file2timestamp -ge $file1timestamp ]]; then
233   println "Test is successful. input.5.zst is precompressed and therefore not compressed/modified again."
234 else
235   println "Test is not successful"
236 fi
237 #File Extension check.
238 ./datagen $size > precompressedFilterTestDir/input.zstbar
239 $ZSTD --exclude-compressed --long --rm -r precompressedFilterTestDir
240 #ZSTD should compress input.zstbar
241 test -f precompressedFilterTestDir/input.zstbar.zst
242 #Check without the --exclude-compressed flag
243 $ZSTD --long --rm -r precompressedFilterTestDir
244 #Files should get compressed again without the --exclude-compressed flag.
245 test -f precompressedFilterTestDir/input.5.zst.zst
246 test -f precompressedFilterTestDir/input.6.zst.zst
247 println "Test completed"
248
249 println "test : file removal"
250 $ZSTD -f --rm tmp
251 test ! -f tmp  # tmp should no longer be present
252 $ZSTD -f -d --rm tmp.zst
253 test ! -f tmp.zst   # tmp.zst should no longer be present
254 println "test : should quietly not remove non-regular file"
255 println hello > tmp
256 $ZSTD tmp -f -o "$DEVDEVICE" 2>tmplog > "$INTOVOID"
257 grep -v "Refusing to remove non-regular file" tmplog
258 rm -f tmplog
259 $ZSTD tmp -f -o "$INTOVOID" 2>&1 | grep -v "Refusing to remove non-regular file"
260 println "test : --rm on stdin"
261 println a | $ZSTD --rm > $INTOVOID   # --rm should remain silent
262 rm tmp
263 $ZSTD -f tmp && die "tmp not present : should have failed"
264 test ! -f tmp.zst  # tmp.zst should not be created
265 println "test : -d -f do not delete destination when source is not present"
266 touch tmp    # create destination file
267 $ZSTD -d -f tmp.zst && die "attempt to decompress a non existing file"
268 test -f tmp  # destination file should still be present
269 println "test : -f do not delete destination when source is not present"
270 rm tmp         # erase source file
271 touch tmp.zst  # create destination file
272 $ZSTD -f tmp && die "attempt to compress a non existing file"
273 test -f tmp.zst  # destination file should still be present
274 rm -rf tmp*  # may also erase tmp* directory from previous failed run
275
276 println "\n===> decompression only tests "
277 head -c 1048576 /dev/zero > tmp
278 $ZSTD -d -o tmp1 "$TESTDIR/golden-decompression/rle-first-block.zst"
279 $DIFF -s tmp1 tmp
280 rm tmp*
281
282 println "test : compress multiple files"
283 println hello > tmp1
284 println world > tmp2
285 $ZSTD tmp1 tmp2 -o "$INTOVOID" -f
286 $ZSTD tmp1 tmp2 -c | $ZSTD -t
287 $ZSTD tmp1 tmp2 -o tmp.zst
288 test ! -f tmp1.zst
289 test ! -f tmp2.zst
290 $ZSTD tmp1 tmp2
291 $ZSTD -t tmp1.zst tmp2.zst
292 $ZSTD -dc tmp1.zst tmp2.zst
293 $ZSTD tmp1.zst tmp2.zst -o "$INTOVOID" -f
294 $ZSTD -d tmp1.zst tmp2.zst -o tmp
295 touch tmpexists
296 $ZSTD tmp1 tmp2 -f -o tmpexists
297 $ZSTD tmp1 tmp2 -o tmpexists && die "should have refused to overwrite"
298 # Bug: PR #972
299 if [ "$?" -eq 139 ]; then
300   die "should not have segfaulted"
301 fi
302 rm tmp*
303
304 println "test : compress multiple files into an output directory, --output-dir-flat"
305 println henlo > tmp1
306 mkdir tmpInputTestDir
307 mkdir tmpInputTestDir/we
308 mkdir tmpInputTestDir/we/must
309 mkdir tmpInputTestDir/we/must/go
310 mkdir tmpInputTestDir/we/must/go/deeper
311 println cool > tmpInputTestDir/we/must/go/deeper/tmp2
312 mkdir tmpOutDir
313 $ZSTD tmp1 tmpInputTestDir/we/must/go/deeper/tmp2 --output-dir-flat tmpOutDir
314 test -f tmpOutDir/tmp1.zst
315 test -f tmpOutDir/tmp2.zst
316 println "test : decompress multiple files into an output directory, --output-dir-flat"
317 mkdir tmpOutDirDecomp
318 $ZSTD tmpOutDir -r -d --output-dir-flat tmpOutDirDecomp
319 test -f tmpOutDirDecomp/tmp2
320 test -f tmpOutDirDecomp/tmp1
321 rm -f tmpOutDirDecomp/*
322 $ZSTD tmpOutDir -r -d --output-dir-flat=tmpOutDirDecomp
323 test -f tmpOutDirDecomp/tmp2
324 test -f tmpOutDirDecomp/tmp1
325 rm -rf tmp*
326
327 println "\n===>  Advanced compression parameters "
328 println "Hello world!" | $ZSTD --zstd=windowLog=21,      - -o tmp.zst && die "wrong parameters not detected!"
329 println "Hello world!" | $ZSTD --zstd=windowLo=21        - -o tmp.zst && die "wrong parameters not detected!"
330 println "Hello world!" | $ZSTD --zstd=windowLog=21,slog  - -o tmp.zst && die "wrong parameters not detected!"
331 println "Hello world!" | $ZSTD --zstd=strategy=10        - -o tmp.zst && die "parameter out of bound not detected!"  # > btultra2 : does not exist
332 test ! -f tmp.zst  # tmp.zst should not be created
333 roundTripTest -g512K
334 roundTripTest -g512K " --zstd=mml=3,tlen=48,strat=6"
335 roundTripTest -g512K " --zstd=strat=6,wlog=23,clog=23,hlog=22,slog=6"
336 roundTripTest -g512K " --zstd=windowLog=23,chainLog=23,hashLog=22,searchLog=6,minMatch=3,targetLength=48,strategy=6"
337 roundTripTest -g512K " --single-thread --long --zstd=ldmHashLog=20,ldmMinMatch=64,ldmBucketSizeLog=1,ldmHashRateLog=7"
338 roundTripTest -g512K " --single-thread --long --zstd=lhlog=20,lmml=64,lblog=1,lhrlog=7"
339 roundTripTest -g64K  "19 --zstd=strat=9"   # btultra2
340
341
342 println "\n===>  Pass-Through mode "
343 println "Hello world 1!" | $ZSTD -df
344 println "Hello world 2!" | $ZSTD -dcf
345 println "Hello world 3!" > tmp1
346 $ZSTD -dcf tmp1
347
348
349 println "\n===>  frame concatenation "
350
351 println "hello " > hello.tmp
352 println "world!" > world.tmp
353 cat hello.tmp world.tmp > helloworld.tmp
354 $ZSTD -c hello.tmp > hello.zstd
355 $ZSTD -c world.tmp > world.zstd
356 cat hello.zstd world.zstd > helloworld.zstd
357 $ZSTD -dc helloworld.zstd > result.tmp
358 cat result.tmp
359 $DIFF helloworld.tmp result.tmp
360 println "frame concatenation without checksum"
361 $ZSTD -c hello.tmp > hello.zstd --no-check
362 $ZSTD -c world.tmp > world.zstd --no-check
363 cat hello.zstd world.zstd > helloworld.zstd
364 $ZSTD -dc helloworld.zstd > result.tmp
365 $DIFF helloworld.tmp result.tmp
366 println "testing zstdcat symlink"
367 ln -sf $ZSTD zstdcat
368 ./zstdcat helloworld.zstd > result.tmp
369 $DIFF helloworld.tmp result.tmp
370 ln -s helloworld.zstd helloworld.link.zstd
371 ./zstdcat helloworld.link.zstd > result.tmp
372 $DIFF helloworld.tmp result.tmp
373 rm zstdcat
374 rm result.tmp
375 println "testing zcat symlink"
376 ln -sf $ZSTD zcat
377 ./zcat helloworld.zstd > result.tmp
378 $DIFF helloworld.tmp result.tmp
379 ./zcat helloworld.link.zstd > result.tmp
380 $DIFF helloworld.tmp result.tmp
381 rm zcat
382 rm ./*.tmp ./*.zstd
383 println "frame concatenation tests completed"
384
385
386 if [ "$isWindows" = false ] && [ "$UNAME" != 'SunOS' ] && [ "$UNAME" != "OpenBSD" ] ; then
387 println "\n**** flush write error test **** "
388
389 println "println foo | $ZSTD > /dev/full"
390 println foo | $ZSTD > /dev/full && die "write error not detected!"
391 println "println foo | $ZSTD | $ZSTD -d > /dev/full"
392 println foo | $ZSTD | $ZSTD -d > /dev/full && die "write error not detected!"
393
394 fi
395
396
397 if [ "$isWindows" = false ] && [ "$UNAME" != 'SunOS' ] ; then
398
399 println "\n===>  symbolic link test "
400
401 rm -f hello.tmp world.tmp world2.tmp hello.tmp.zst world.tmp.zst
402 println "hello world" > hello.tmp
403 ln -s hello.tmp world.tmp
404 ln -s hello.tmp world2.tmp
405 $ZSTD world.tmp hello.tmp || true
406 test -f hello.tmp.zst  # regular file should have been compressed!
407 test ! -f world.tmp.zst  # symbolic link should not have been compressed!
408 $ZSTD world.tmp || true
409 test ! -f world.tmp.zst  # symbolic link should not have been compressed!
410 $ZSTD world.tmp world2.tmp || true
411 test ! -f world.tmp.zst  # symbolic link should not have been compressed!
412 test ! -f world2.tmp.zst  # symbolic link should not have been compressed!
413 $ZSTD world.tmp hello.tmp -f
414 test -f world.tmp.zst  # symbolic link should have been compressed with --force
415 rm -f hello.tmp world.tmp world2.tmp hello.tmp.zst world.tmp.zst
416
417 fi
418
419
420 println "\n===>  test sparse file support "
421
422 ./datagen -g5M  -P100 > tmpSparse
423 $ZSTD tmpSparse -c | $ZSTD -dv -o tmpSparseRegen
424 $DIFF -s tmpSparse tmpSparseRegen
425 $ZSTD tmpSparse -c | $ZSTD -dv --sparse -c > tmpOutSparse
426 $DIFF -s tmpSparse tmpOutSparse
427 $ZSTD tmpSparse -c | $ZSTD -dv --no-sparse -c > tmpOutNoSparse
428 $DIFF -s tmpSparse tmpOutNoSparse
429 ls -ls tmpSparse*  # look at file size and block size on disk
430 ./datagen -s1 -g1200007 -P100 | $ZSTD | $ZSTD -dv --sparse -c > tmpSparseOdd   # Odd size file (to not finish on an exact nb of blocks)
431 ./datagen -s1 -g1200007 -P100 | $DIFF -s - tmpSparseOdd
432 ls -ls tmpSparseOdd  # look at file size and block size on disk
433 println "\n Sparse Compatibility with Console :"
434 println "Hello World 1 !" | $ZSTD | $ZSTD -d -c
435 println "Hello World 2 !" | $ZSTD | $ZSTD -d | cat
436 println "\n Sparse Compatibility with Append :"
437 ./datagen -P100 -g1M > tmpSparse1M
438 cat tmpSparse1M tmpSparse1M > tmpSparse2M
439 $ZSTD -v -f tmpSparse1M -o tmpSparseCompressed
440 $ZSTD -d -v -f tmpSparseCompressed -o tmpSparseRegenerated
441 $ZSTD -d -v -f tmpSparseCompressed -c >> tmpSparseRegenerated
442 ls -ls tmpSparse*  # look at file size and block size on disk
443 $DIFF tmpSparse2M tmpSparseRegenerated
444 rm tmpSparse*
445
446
447 println "\n===>  multiple files tests "
448
449 ./datagen -s1        > tmp1 2> $INTOVOID
450 ./datagen -s2 -g100K > tmp2 2> $INTOVOID
451 ./datagen -s3 -g1M   > tmp3 2> $INTOVOID
452 println "compress tmp* : "
453 $ZSTD -f tmp*
454 ls -ls tmp*
455 rm tmp1 tmp2 tmp3
456 println "decompress tmp* : "
457 $ZSTD -df ./*.zst
458 ls -ls tmp*
459 println "compress tmp* into stdout > tmpall : "
460 $ZSTD -c tmp1 tmp2 tmp3 > tmpall
461 ls -ls tmp*  # check size of tmpall (should be tmp1.zst + tmp2.zst + tmp3.zst)
462 println "decompress tmpall* into stdout > tmpdec : "
463 cp tmpall tmpall2
464 $ZSTD -dc tmpall* > tmpdec
465 ls -ls tmp* # check size of tmpdec (should be 2*(tmp1 + tmp2 + tmp3))
466 println "compress multiple files including a missing one (notHere) : "
467 $ZSTD -f tmp1 notHere tmp2 && die "missing file not detected!"
468
469 println "\n===>  stream-size mode"
470
471 ./datagen -g11000 > tmp
472 println "test : basic file compression vs sized streaming compression"
473 file_size=$($ZSTD -14 -f tmp -o tmp.zst && wc -c < tmp.zst)
474 stream_size=$(cat tmp | $ZSTD -14 --stream-size=11000 | wc -c)
475 if [ "$stream_size" -gt "$file_size" ]; then
476   die "hinted compression larger than expected"
477 fi
478 println "test : sized streaming compression and decompression"
479 cat tmp | $ZSTD -14 -f tmp -o --stream-size=11000 tmp.zst
480 $ZSTD -df tmp.zst -o tmp_decompress
481 cmp tmp tmp_decompress || die "difference between original and decompressed file"
482 println "test : incorrect stream size"
483 cat tmp | $ZSTD -14 -f -o tmp.zst --stream-size=11001 && die "should fail with incorrect stream size"
484
485
486 println "\n===>  size-hint mode"
487
488 ./datagen -g11000 > tmp
489 ./datagen -g11000 > tmp2
490 ./datagen > tmpDict
491 println "test : basic file compression vs hinted streaming compression"
492 file_size=$($ZSTD -14 -f tmp -o tmp.zst && wc -c < tmp.zst)
493 stream_size=$(cat tmp | $ZSTD -14 --size-hint=11000 | wc -c)
494 if [ "$stream_size" -ge "$file_size" ]; then
495   die "hinted compression larger than expected"
496 fi
497 println "test : hinted streaming compression and decompression"
498 cat tmp | $ZSTD -14 -f -o tmp.zst --size-hint=11000
499 $ZSTD -df tmp.zst -o tmp_decompress
500 cmp tmp tmp_decompress || die "difference between original and decompressed file"
501 println "test : hinted streaming compression with dictionary"
502 cat tmp | $ZSTD -14 -f -D tmpDict --size-hint=11000 | $ZSTD -t -D tmpDict
503 println "test : multiple file compression with hints and dictionary"
504 $ZSTD -14 -f -D tmpDict --size-hint=11000 tmp tmp2
505 $ZSTD -14 -f -o tmp1_.zst -D tmpDict --size-hint=11000 tmp
506 $ZSTD -14 -f -o tmp2_.zst -D tmpDict --size-hint=11000 tmp2
507 cmp tmp.zst tmp1_.zst || die "first file's output differs"
508 cmp tmp2.zst tmp2_.zst || die "second file's output differs"
509 println "test : incorrect hinted stream sizes"
510 cat tmp | $ZSTD -14 -f --size-hint=11050 | $ZSTD -t  # slightly too high
511 cat tmp | $ZSTD -14 -f --size-hint=10950 | $ZSTD -t  # slightly too low
512 cat tmp | $ZSTD -14 -f --size-hint=22000 | $ZSTD -t  # considerably too high
513 cat tmp | $ZSTD -14 -f --size-hint=5500  | $ZSTD -t  # considerably too low
514
515
516 println "\n===>  dictionary tests "
517
518 println "- test with raw dict (content only) "
519 ./datagen > tmpDict
520 ./datagen -g1M | $MD5SUM > tmp1
521 ./datagen -g1M | $ZSTD -D tmpDict | $ZSTD -D tmpDict -dvq | $MD5SUM > tmp2
522 $DIFF -q tmp1 tmp2
523 println "- Create first dictionary "
524 TESTFILE="$PRGDIR"/zstdcli.c
525 $ZSTD --train "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpDict
526 cp "$TESTFILE" tmp
527 println "- Test dictionary compression with tmpDict as an input file and dictionary"
528 $ZSTD -f tmpDict -D tmpDict && die "compression error not detected!"
529 println "- Dictionary compression roundtrip"
530 $ZSTD -f tmp -D tmpDict
531 $ZSTD -d tmp.zst -D tmpDict -fo result
532 $DIFF "$TESTFILE" result
533 println "- Dictionary compression with btlazy2 strategy"
534 $ZSTD -f tmp -D tmpDict --zstd=strategy=6
535 $ZSTD -d tmp.zst -D tmpDict -fo result
536 $DIFF "$TESTFILE" result
537 if [ -n "$hasMT" ]
538 then
539     println "- Test dictionary compression with multithreading "
540     ./datagen -g5M | $ZSTD -T2 -D tmpDict | $ZSTD -t -D tmpDict   # fails with v1.3.2
541 fi
542 println "- Create second (different) dictionary "
543 $ZSTD --train "$TESTDIR"/*.c "$PRGDIR"/*.c "$PRGDIR"/*.h -o tmpDictC
544 $ZSTD -d tmp.zst -D tmpDictC -fo result && die "wrong dictionary not detected!"
545 println "- Create dictionary with short dictID"
546 $ZSTD --train "$TESTDIR"/*.c "$PRGDIR"/*.c --dictID=1 -o tmpDict1
547 cmp tmpDict tmpDict1 && die "dictionaries should have different ID !"
548 println "- Create dictionary with wrong dictID parameter order (must fail)"
549 $ZSTD --train "$TESTDIR"/*.c "$PRGDIR"/*.c --dictID -o 1 tmpDict1 && die "wrong order : --dictID must be followed by argument "
550 println "- Create dictionary with size limit"
551 $ZSTD --train "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpDict2 --maxdict=4K -v
552 println "- Create dictionary with small size limit"
553 $ZSTD --train "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpDict3 --maxdict=1K -v
554 println "- Create dictionary with wrong parameter order (must fail)"
555 $ZSTD --train "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpDict3 --maxdict -v 4K && die "wrong order : --maxdict must be followed by argument "
556 println "- Compress without dictID"
557 $ZSTD -f tmp -D tmpDict1 --no-dictID
558 $ZSTD -d tmp.zst -D tmpDict -fo result
559 $DIFF "$TESTFILE" result
560 println "- Compress with wrong argument order (must fail)"
561 $ZSTD tmp -Df tmpDict1 -c > $INTOVOID && die "-D must be followed by dictionary name "
562 println "- Compress multiple files with dictionary"
563 rm -rf dirTestDict
564 mkdir dirTestDict
565 cp "$TESTDIR"/*.c dirTestDict
566 cp "$PRGDIR"/*.c dirTestDict
567 cp "$PRGDIR"/*.h dirTestDict
568 $MD5SUM dirTestDict/* > tmph1
569 $ZSTD -f --rm dirTestDict/* -D tmpDictC
570 $ZSTD -d --rm dirTestDict/*.zst -D tmpDictC  # note : use internal checksum by default
571 case "$UNAME" in
572   Darwin) println "md5sum -c not supported on OS-X : test skipped" ;;  # not compatible with OS-X's md5
573   *) $MD5SUM -c tmph1 ;;
574 esac
575 rm -rf dirTestDict
576 println "- dictionary builder on bogus input"
577 println "Hello World" > tmp
578 $ZSTD --train-legacy -q tmp && die "Dictionary training should fail : not enough input source"
579 ./datagen -P0 -g10M > tmp
580 $ZSTD --train-legacy -q tmp && die "Dictionary training should fail : source is pure noise"
581 println "- Test -o before --train"
582 rm -f tmpDict dictionary
583 $ZSTD -o tmpDict --train "$TESTDIR"/*.c "$PRGDIR"/*.c
584 test -f tmpDict
585 $ZSTD --train "$TESTDIR"/*.c "$PRGDIR"/*.c
586 test -f dictionary
587 println "- Test dictionary training fails"
588 echo "000000000000000000000000000000000" > tmpz
589 $ZSTD --train tmpz tmpz tmpz tmpz tmpz tmpz tmpz tmpz tmpz && die "Dictionary training should fail : source is all zeros"
590 if [ -n "$hasMT" ]
591 then
592   $ZSTD --train -T0 tmpz tmpz tmpz tmpz tmpz tmpz tmpz tmpz tmpz && die "Dictionary training should fail : source is all zeros"
593   println "- Create dictionary with multithreading enabled"
594   $ZSTD --train -T0 "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpDict
595 fi
596 rm tmp* dictionary
597
598
599 println "\n===>  fastCover dictionary builder : advanced options "
600 TESTFILE="$PRGDIR"/zstdcli.c
601 ./datagen > tmpDict
602 println "- Create first dictionary"
603 $ZSTD --train-fastcover=k=46,d=8,f=15,split=80 "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpDict
604 cp "$TESTFILE" tmp
605 $ZSTD -f tmp -D tmpDict
606 $ZSTD -d tmp.zst -D tmpDict -fo result
607 $DIFF "$TESTFILE" result
608 println "- Create second (different) dictionary"
609 $ZSTD --train-fastcover=k=56,d=8 "$TESTDIR"/*.c "$PRGDIR"/*.c "$PRGDIR"/*.h -o tmpDictC
610 $ZSTD -d tmp.zst -D tmpDictC -fo result && die "wrong dictionary not detected!"
611 $ZSTD --train-fastcover=k=56,d=8 && die "Create dictionary without input file"
612 println "- Create dictionary with short dictID"
613 $ZSTD --train-fastcover=k=46,d=8,f=15,split=80 "$TESTDIR"/*.c "$PRGDIR"/*.c --dictID=1 -o tmpDict1
614 cmp tmpDict tmpDict1 && die "dictionaries should have different ID !"
615 println "- Create dictionaries with shrink-dict flag enabled"
616 $ZSTD --train-fastcover=steps=1,shrink "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpShrinkDict
617 $ZSTD --train-fastcover=steps=1,shrink=1 "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpShrinkDict1
618 $ZSTD --train-fastcover=steps=1,shrink=5 "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpShrinkDict2
619 println "- Create dictionary with size limit"
620 $ZSTD --train-fastcover=steps=1 "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpDict2 --maxdict=4K
621 println "- Create dictionary using all samples for both training and testing"
622 $ZSTD --train-fastcover=k=56,d=8,split=100 -r "$TESTDIR"/*.c "$PRGDIR"/*.c
623 println "- Create dictionary using f=16"
624 $ZSTD --train-fastcover=k=56,d=8,f=16 -r "$TESTDIR"/*.c "$PRGDIR"/*.c
625 $ZSTD --train-fastcover=k=56,d=8,accel=15 -r "$TESTDIR"/*.c "$PRGDIR"/*.c && die "Created dictionary using accel=15"
626 println "- Create dictionary using accel=2"
627 $ZSTD --train-fastcover=k=56,d=8,accel=2 -r "$TESTDIR"/*.c "$PRGDIR"/*.c
628 println "- Create dictionary using accel=10"
629 $ZSTD --train-fastcover=k=56,d=8,accel=10 -r "$TESTDIR"/*.c "$PRGDIR"/*.c
630 println "- Create dictionary with multithreading"
631 $ZSTD --train-fastcover -T4 -r "$TESTDIR"/*.c "$PRGDIR"/*.c
632 println "- Test -o before --train-fastcover"
633 rm -f tmpDict dictionary
634 $ZSTD -o tmpDict --train-fastcover=k=56,d=8 "$TESTDIR"/*.c "$PRGDIR"/*.c
635 test -f tmpDict
636 $ZSTD --train-fastcover=k=56,d=8 "$TESTDIR"/*.c "$PRGDIR"/*.c
637 test -f dictionary
638 rm tmp* dictionary
639
640
641 println "\n===>  legacy dictionary builder "
642
643 TESTFILE="$PRGDIR"/zstdcli.c
644 ./datagen > tmpDict
645 println "- Create first dictionary"
646 $ZSTD --train-legacy=selectivity=8 "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpDict
647 cp "$TESTFILE" tmp
648 $ZSTD -f tmp -D tmpDict
649 $ZSTD -d tmp.zst -D tmpDict -fo result
650 $DIFF "$TESTFILE" result
651 $ZSTD --train-legacy=s=8 && die "Create dictionary without input files (should error)"
652 println "- Create second (different) dictionary"
653 $ZSTD --train-legacy=s=5 "$TESTDIR"/*.c "$PRGDIR"/*.c "$PRGDIR"/*.h -o tmpDictC
654 $ZSTD -d tmp.zst -D tmpDictC -fo result && die "wrong dictionary not detected!"
655 println "- Create dictionary with short dictID"
656 $ZSTD --train-legacy -s5 "$TESTDIR"/*.c "$PRGDIR"/*.c --dictID=1 -o tmpDict1
657 cmp tmpDict tmpDict1 && die "dictionaries should have different ID !"
658 println "- Create dictionary with size limit"
659 $ZSTD --train-legacy -s9 "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpDict2 --maxdict=4K
660 println "- Test -o before --train-legacy"
661 rm -f tmpDict dictionary
662 $ZSTD -o tmpDict --train-legacy "$TESTDIR"/*.c "$PRGDIR"/*.c
663 test -f tmpDict
664 $ZSTD --train-legacy "$TESTDIR"/*.c "$PRGDIR"/*.c
665 test -f dictionary
666 rm tmp* dictionary
667
668
669 println "\n===>  integrity tests "
670
671 println "test one file (tmp1.zst) "
672 ./datagen > tmp1
673 $ZSTD tmp1
674 $ZSTD -t tmp1.zst
675 $ZSTD --test tmp1.zst
676 println "test multiple files (*.zst) "
677 $ZSTD -t ./*.zst
678 println "test bad files (*) "
679 $ZSTD -t ./* && die "bad files not detected !"
680 $ZSTD -t tmp1 && die "bad file not detected !"
681 cp tmp1 tmp2.zst
682 $ZSTD -t tmp2.zst && die "bad file not detected !"
683 ./datagen -g0 > tmp3
684 $ZSTD -t tmp3 && die "bad file not detected !"   # detects 0-sized files as bad
685 println "test --rm and --test combined "
686 $ZSTD -t --rm tmp1.zst
687 test -f tmp1.zst   # check file is still present
688 split -b16384 tmp1.zst tmpSplit.
689 $ZSTD -t tmpSplit.* && die "bad file not detected !"
690 ./datagen | $ZSTD -c | $ZSTD -t
691
692
693
694 println "\n===>  golden files tests "
695
696 $ZSTD -t -r "$TESTDIR/golden-compression"
697 $ZSTD -c -r "$TESTDIR/golden-compression" | $ZSTD -t
698
699
700 println "\n===>  benchmark mode tests "
701
702 println "bench one file"
703 ./datagen > tmp1
704 $ZSTD -bi0 tmp1
705 println "bench multiple levels"
706 $ZSTD -i0b0e3 tmp1
707 println "bench negative level"
708 $ZSTD -bi0 --fast tmp1
709 println "with recursive and quiet modes"
710 $ZSTD -rqi0b1e2 tmp1
711 println "benchmark decompression only"
712 $ZSTD -f tmp1
713 $ZSTD -b -d -i0 tmp1.zst
714
715 println "\n===>  zstd compatibility tests "
716
717 ./datagen > tmp
718 rm -f tmp.zst
719 $ZSTD --format=zstd -f tmp
720 test -f tmp.zst
721
722 println "\n===>  gzip compatibility tests "
723
724 GZIPMODE=1
725 $ZSTD --format=gzip -V || GZIPMODE=0
726 if [ $GZIPMODE -eq 1 ]; then
727     println "gzip support detected"
728     GZIPEXE=1
729     gzip -V || GZIPEXE=0
730     if [ $GZIPEXE -eq 1 ]; then
731         ./datagen > tmp
732         $ZSTD --format=gzip -f tmp
733         gzip -t -v tmp.gz
734         gzip -f tmp
735         $ZSTD -d -f -v tmp.gz
736         rm tmp*
737     else
738         println "gzip binary not detected"
739     fi
740 else
741     println "gzip mode not supported"
742 fi
743
744
745 println "\n===>  gzip frame tests "
746
747 if [ $GZIPMODE -eq 1 ]; then
748     ./datagen > tmp
749     $ZSTD -f --format=gzip tmp
750     $ZSTD -f tmp
751     cat tmp.gz tmp.zst tmp.gz tmp.zst | $ZSTD -d -f -o tmp
752     truncateLastByte tmp.gz | $ZSTD -t > $INTOVOID && die "incomplete frame not detected !"
753     rm tmp*
754 else
755     println "gzip mode not supported"
756 fi
757
758 if [ $GZIPMODE -eq 1 ]; then
759     ./datagen > tmp
760     rm -f tmp.zst
761     $ZSTD --format=gzip --format=zstd -f tmp
762     test -f tmp.zst
763 fi
764
765 println "\n===>  xz compatibility tests "
766
767 LZMAMODE=1
768 $ZSTD --format=xz -V || LZMAMODE=0
769 if [ $LZMAMODE -eq 1 ]; then
770     println "xz support detected"
771     XZEXE=1
772     xz -Q -V && lzma -Q -V || XZEXE=0
773     if [ $XZEXE -eq 1 ]; then
774         println "Testing zstd xz and lzma support"
775         ./datagen > tmp
776         $ZSTD --format=lzma -f tmp
777         $ZSTD --format=xz -f tmp
778         xz -Q -t -v tmp.xz
779         xz -Q -t -v tmp.lzma
780         xz -Q -f -k tmp
781         lzma -Q -f -k --lzma1 tmp
782         $ZSTD -d -f -v tmp.xz
783         $ZSTD -d -f -v tmp.lzma
784         rm tmp*
785         println "Creating symlinks"
786         ln -s $ZSTD ./xz
787         ln -s $ZSTD ./unxz
788         ln -s $ZSTD ./lzma
789         ln -s $ZSTD ./unlzma
790         println "Testing xz and lzma symlinks"
791         ./datagen > tmp
792         ./xz tmp
793         xz -Q -d tmp.xz
794         ./lzma tmp
795         lzma -Q -d tmp.lzma
796         println "Testing unxz and unlzma symlinks"
797         xz -Q tmp
798         ./xz -d tmp.xz
799         lzma -Q tmp
800         ./lzma -d tmp.lzma
801         rm xz unxz lzma unlzma
802         rm tmp*
803     else
804         println "xz binary not detected"
805     fi
806 else
807     println "xz mode not supported"
808 fi
809
810
811 println "\n===>  xz frame tests "
812
813 if [ $LZMAMODE -eq 1 ]; then
814     ./datagen > tmp
815     $ZSTD -f --format=xz tmp
816     $ZSTD -f --format=lzma tmp
817     $ZSTD -f tmp
818     cat tmp.xz tmp.lzma tmp.zst tmp.lzma tmp.xz tmp.zst | $ZSTD -d -f -o tmp
819     truncateLastByte tmp.xz | $ZSTD -t > $INTOVOID && die "incomplete frame not detected !"
820     truncateLastByte tmp.lzma | $ZSTD -t > $INTOVOID && die "incomplete frame not detected !"
821     rm tmp*
822 else
823     println "xz mode not supported"
824 fi
825
826 println "\n===>  lz4 compatibility tests "
827
828 LZ4MODE=1
829 $ZSTD --format=lz4 -V || LZ4MODE=0
830 if [ $LZ4MODE -eq 1 ]; then
831     println "lz4 support detected"
832     LZ4EXE=1
833     lz4 -V || LZ4EXE=0
834     if [ $LZ4EXE -eq 1 ]; then
835         ./datagen > tmp
836         $ZSTD --format=lz4 -f tmp
837         lz4 -t -v tmp.lz4
838         lz4 -f tmp
839         $ZSTD -d -f -v tmp.lz4
840         rm tmp*
841     else
842         println "lz4 binary not detected"
843     fi
844 else
845     println "lz4 mode not supported"
846 fi
847
848
849 println "\n===>  lz4 frame tests "
850
851 if [ $LZ4MODE -eq 1 ]; then
852     ./datagen > tmp
853     $ZSTD -f --format=lz4 tmp
854     $ZSTD -f tmp
855     cat tmp.lz4 tmp.zst tmp.lz4 tmp.zst | $ZSTD -d -f -o tmp
856     truncateLastByte tmp.lz4 | $ZSTD -t > $INTOVOID && die "incomplete frame not detected !"
857     rm tmp*
858 else
859     println "lz4 mode not supported"
860 fi
861
862 println "\n===> suffix list test"
863
864 ! $ZSTD -d tmp.abc 2> tmplg
865
866 if [ $GZIPMODE -ne 1 ]; then
867     grep ".gz" tmplg > $INTOVOID && die "Unsupported suffix listed"
868 fi
869
870 if [ $LZMAMODE -ne 1 ]; then
871     grep ".lzma" tmplg > $INTOVOID && die "Unsupported suffix listed"
872     grep ".xz" tmplg > $INTOVOID && die "Unsupported suffix listed"
873 fi
874
875 if [ $LZ4MODE -ne 1 ]; then
876     grep ".lz4" tmplg > $INTOVOID && die "Unsupported suffix listed"
877 fi
878
879 println "\n===>  tar extension tests "
880
881 rm -f tmp tmp.tar tmp.tzst tmp.tgz tmp.txz tmp.tlz4
882
883 ./datagen > tmp
884 tar cf tmp.tar tmp
885 $ZSTD tmp.tar -o tmp.tzst
886 rm tmp.tar
887 $ZSTD -d tmp.tzst
888 [ -e tmp.tar ] || die ".tzst failed to decompress to .tar!"
889 rm -f tmp.tar tmp.tzst
890
891 if [ $GZIPMODE -eq 1 ]; then
892     tar czf tmp.tgz tmp
893     $ZSTD -d tmp.tgz
894     [ -e tmp.tar ] || die ".tgz failed to decompress to .tar!"
895     rm -f tmp.tar tmp.tgz
896 fi
897
898 if [ $LZMAMODE -eq 1 ]; then
899     tar c tmp | $ZSTD --format=xz > tmp.txz
900     $ZSTD -d tmp.txz
901     [ -e tmp.tar ] || die ".txz failed to decompress to .tar!"
902     rm -f tmp.tar tmp.txz
903 fi
904
905 if [ $LZ4MODE -eq 1 ]; then
906     tar c tmp | $ZSTD --format=lz4 > tmp.tlz4
907     $ZSTD -d tmp.tlz4
908     [ -e tmp.tar ] || die ".tlz4 failed to decompress to .tar!"
909     rm -f tmp.tar tmp.tlz4
910 fi
911
912 touch tmp.t tmp.tz tmp.tzs
913 ! $ZSTD -d tmp.t
914 ! $ZSTD -d tmp.tz
915 ! $ZSTD -d tmp.tzs
916
917 exit
918
919 println "\n===>  zstd round-trip tests "
920
921 roundTripTest
922 roundTripTest -g15K       # TableID==3
923 roundTripTest -g127K      # TableID==2
924 roundTripTest -g255K      # TableID==1
925 roundTripTest -g522K      # TableID==0
926 roundTripTest -g519K 6    # greedy, hash chain
927 roundTripTest -g517K 16   # btlazy2
928 roundTripTest -g516K 19   # btopt
929
930 fileRoundTripTest -g500K
931
932 println "\n===>  zstd long distance matching round-trip tests "
933 roundTripTest -g0 "2 --single-thread --long"
934 roundTripTest -g1000K "1 --single-thread --long"
935 roundTripTest -g517K "6 --single-thread --long"
936 roundTripTest -g516K "16 --single-thread --long"
937 roundTripTest -g518K "19 --single-thread --long"
938 fileRoundTripTest -g5M "3 --single-thread --long"
939
940
941 roundTripTest -g96K "5 --single-thread"
942 if [ -n "$hasMT" ]
943 then
944     println "\n===>  zstdmt round-trip tests "
945     roundTripTest -g4M "1 -T0"
946     roundTripTest -g8M "3 -T2"
947     roundTripTest -g8000K "2 --threads=2"
948     fileRoundTripTest -g4M "19 -T2 -B1M"
949
950     println "\n===>  zstdmt long distance matching round-trip tests "
951     roundTripTest -g8M "3 --long=24 -T2"
952
953     println "\n===>  ovLog tests "
954     ./datagen -g2MB > tmp
955     refSize=$($ZSTD tmp -6 -c --zstd=wlog=18         | wc -c)
956     ov9Size=$($ZSTD tmp -6 -c --zstd=wlog=18,ovlog=9 | wc -c)
957     ov1Size=$($ZSTD tmp -6 -c --zstd=wlog=18,ovlog=1 | wc -c)
958     if [ "$refSize" -eq "$ov9Size" ]; then
959         echo ov9Size should be different from refSize
960         exit 1
961     fi
962     if [ "$refSize" -eq "$ov1Size" ]; then
963         echo ov1Size should be different from refSize
964         exit 1
965     fi
966     if [ "$ov9Size" -ge "$ov1Size" ]; then
967         echo ov9Size="$ov9Size" should be smaller than ov1Size="$ov1Size"
968         exit 1
969     fi
970
971 else
972     println "\n===>  no multithreading, skipping zstdmt tests "
973 fi
974
975 rm tmp*
976
977 println "\n===>  zstd --list/-l single frame tests "
978 ./datagen > tmp1
979 ./datagen > tmp2
980 ./datagen > tmp3
981 $ZSTD tmp*
982 $ZSTD -l ./*.zst
983 $ZSTD -lv ./*.zst | grep "Decompressed Size:"  # check that decompressed size is present in header
984 $ZSTD --list ./*.zst
985 $ZSTD --list -v ./*.zst
986
987 println "\n===>  zstd --list/-l multiple frame tests "
988 cat tmp1.zst tmp2.zst > tmp12.zst
989 cat tmp12.zst tmp3.zst > tmp123.zst
990 $ZSTD -l ./*.zst
991 $ZSTD -lv ./*.zst
992
993 println "\n===>  zstd --list/-l error detection tests "
994 $ZSTD -l tmp1 tmp1.zst && die "-l must fail on non-zstd file"
995 $ZSTD --list tmp* && die "-l must fail on non-zstd file"
996 $ZSTD -lv tmp1* && die "-l must fail on non-zstd file"
997 $ZSTD --list -v tmp2 tmp12.zst && die "-l must fail on non-zstd file"
998
999 println "test : detect truncated compressed file "
1000 TEST_DATA_FILE=truncatable-input.txt
1001 FULL_COMPRESSED_FILE=${TEST_DATA_FILE}.zst
1002 TRUNCATED_COMPRESSED_FILE=truncated-input.txt.zst
1003 ./datagen -g50000 > $TEST_DATA_FILE
1004 $ZSTD -f $TEST_DATA_FILE -o $FULL_COMPRESSED_FILE
1005 dd bs=1 count=100 if=$FULL_COMPRESSED_FILE of=$TRUNCATED_COMPRESSED_FILE
1006 $ZSTD --list $TRUNCATED_COMPRESSED_FILE && die "-l must fail on truncated file"
1007
1008 rm $TEST_DATA_FILE
1009 rm $FULL_COMPRESSED_FILE
1010 rm $TRUNCATED_COMPRESSED_FILE
1011
1012 println "\n===>  zstd --list/-l errors when presented with stdin / no files"
1013 $ZSTD -l && die "-l must fail on empty list of files"
1014 $ZSTD -l - && die "-l does not work on stdin"
1015 $ZSTD -l < tmp1.zst && die "-l does not work on stdin"
1016 $ZSTD -l - < tmp1.zst && die "-l does not work on stdin"
1017 $ZSTD -l - tmp1.zst && die "-l does not work on stdin"
1018 $ZSTD -l - tmp1.zst < tmp1.zst && die "-l does not work on stdin"
1019 $ZSTD -l tmp1.zst < tmp2.zst # this will check tmp1.zst, but not tmp2.zst, which is not an error : zstd simply doesn't read stdin in this case. It must not error just because stdin is not a tty
1020
1021 println "\n===>  zstd --list/-l test with null files "
1022 ./datagen -g0 > tmp5
1023 $ZSTD tmp5
1024 $ZSTD -l tmp5.zst
1025 $ZSTD -l tmp5* && die "-l must fail on non-zstd file"
1026 $ZSTD -lv tmp5.zst | grep "Decompressed Size: 0.00 KB (0 B)"  # check that 0 size is present in header
1027 $ZSTD -lv tmp5* && die "-l must fail on non-zstd file"
1028
1029 println "\n===>  zstd --list/-l test with no content size field "
1030 ./datagen -g513K | $ZSTD > tmp6.zst
1031 $ZSTD -l tmp6.zst
1032 $ZSTD -lv tmp6.zst | grep "Decompressed Size:"  && die "Field :Decompressed Size: should not be available in this compressed file"
1033
1034 println "\n===>   zstd --list/-l test with no checksum "
1035 $ZSTD -f --no-check tmp1
1036 $ZSTD -l tmp1.zst
1037 $ZSTD -lv tmp1.zst
1038
1039 rm tmp*
1040
1041
1042 println "\n===>   zstd long distance matching tests "
1043 roundTripTest -g0 " --single-thread --long"
1044 roundTripTest -g9M "2 --single-thread --long"
1045 # Test parameter parsing
1046 roundTripTest -g1M -P50 "1 --single-thread --long=29" " --memory=512MB"
1047 roundTripTest -g1M -P50 "1 --single-thread --long=29 --zstd=wlog=28" " --memory=256MB"
1048 roundTripTest -g1M -P50 "1 --single-thread --long=29" " --long=28 --memory=512MB"
1049 roundTripTest -g1M -P50 "1 --single-thread --long=29" " --zstd=wlog=28 --memory=512MB"
1050
1051
1052 if [ -n "$hasMT" ]
1053 then
1054     println "\n===>   adaptive mode "
1055     roundTripTest -g270000000 " --adapt"
1056     roundTripTest -g27000000 " --adapt=min=1,max=4"
1057     println "===>   test: --adapt must fail on incoherent bounds "
1058     ./datagen > tmp
1059     $ZSTD -f -vv --adapt=min=10,max=9 tmp && die "--adapt must fail on incoherent bounds"
1060
1061     println "\n===>   rsyncable mode "
1062     roundTripTest -g10M " --rsyncable"
1063     roundTripTest -g10M " --rsyncable -B100K"
1064     println "===>   test: --rsyncable must fail with --single-thread"
1065     $ZSTD -f -vv --rsyncable --single-thread tmp && die "--rsyncable must fail with --single-thread"
1066 fi
1067
1068
1069 if [ "$1" != "--test-large-data" ]; then
1070     println "Skipping large data tests"
1071     exit 0
1072 fi
1073
1074
1075 #############################################################################
1076
1077 println "\n===>   large files tests "
1078
1079 roundTripTest -g270000000 1
1080 roundTripTest -g250000000 2
1081 roundTripTest -g230000000 3
1082
1083 roundTripTest -g140000000 -P60 4
1084 roundTripTest -g130000000 -P62 5
1085 roundTripTest -g120000000 -P65 6
1086
1087 roundTripTest -g70000000 -P70 7
1088 roundTripTest -g60000000 -P71 8
1089 roundTripTest -g50000000 -P73 9
1090
1091 roundTripTest -g35000000 -P75 10
1092 roundTripTest -g30000000 -P76 11
1093 roundTripTest -g25000000 -P78 12
1094
1095 roundTripTest -g18000013 -P80 13
1096 roundTripTest -g18000014 -P80 14
1097 roundTripTest -g18000015 -P81 15
1098 roundTripTest -g18000016 -P84 16
1099 roundTripTest -g18000017 -P88 17
1100 roundTripTest -g18000018 -P94 18
1101 roundTripTest -g18000019 -P96 19
1102
1103 roundTripTest -g5000000000 -P99 1
1104 roundTripTest -g1700000000 -P0 "1 --zstd=strategy=6"   # ensure btlazy2 can survive an overflow rescale
1105
1106 fileRoundTripTest -g4193M -P99 1
1107
1108
1109 println "\n===>   zstd long, long distance matching round-trip tests "
1110 roundTripTest -g270000000 "1 --single-thread --long"
1111 roundTripTest -g130000000 -P60 "5 --single-thread --long"
1112 roundTripTest -g35000000 -P70 "8 --single-thread --long"
1113 roundTripTest -g18000001 -P80  "18 --single-thread --long"
1114 # Test large window logs
1115 roundTripTest -g700M -P50 "1 --single-thread --long=29"
1116 roundTripTest -g600M -P50 "1 --single-thread --long --zstd=wlog=29,clog=28"
1117
1118
1119 if [ -n "$hasMT" ]
1120 then
1121     println "\n===>   zstdmt long round-trip tests "
1122     roundTripTest -g80000000 -P99 "19 -T2" " "
1123     roundTripTest -g5000000000 -P99 "1 -T2" " "
1124     roundTripTest -g500000000 -P97 "1 -T999" " "
1125     fileRoundTripTest -g4103M -P98 " -T0" " "
1126     roundTripTest -g400000000 -P97 "1 --long=24 -T2" " "
1127     # Exposes the bug in https://github.com/facebook/zstd/pull/1678
1128     # This test fails on 4 different travis builds at the time of writing
1129     # because it needs to allocate 8 GB of memory.
1130     # roundTripTest -g10G -P99 "1 -T1 --long=31 --zstd=clog=27 --fast=1000"
1131 else
1132     println "\n**** no multithreading, skipping zstdmt tests **** "
1133 fi
1134
1135
1136 println "\n===>  cover dictionary builder : advanced options "
1137
1138 TESTFILE="$PRGDIR"/zstdcli.c
1139 ./datagen > tmpDict
1140 println "- Create first dictionary"
1141 $ZSTD --train-cover=k=46,d=8,split=80 "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpDict
1142 cp "$TESTFILE" tmp
1143 $ZSTD -f tmp -D tmpDict
1144 $ZSTD -d tmp.zst -D tmpDict -fo result
1145 $DIFF "$TESTFILE" result
1146 $ZSTD --train-cover=k=56,d=8 && die "Create dictionary without input file (should error)"
1147 println "- Create second (different) dictionary"
1148 $ZSTD --train-cover=k=56,d=8 "$TESTDIR"/*.c "$PRGDIR"/*.c "$PRGDIR"/*.h -o tmpDictC
1149 $ZSTD -d tmp.zst -D tmpDictC -fo result && die "wrong dictionary not detected!"
1150 println "- Create dictionary using shrink-dict flag"
1151 $ZSTD --train-cover=steps=256,shrink "$TESTDIR"/*.c "$PRGDIR"/*.c --dictID=1 -o tmpShrinkDict
1152 $ZSTD --train-cover=steps=256,shrink=1 "$TESTDIR"/*.c "$PRGDIR"/*.c --dictID=1 -o tmpShrinkDict1
1153 $ZSTD --train-cover=steps=256,shrink=5 "$TESTDIR"/*.c "$PRGDIR"/*.c --dictID=1 -o tmpShrinkDict2
1154 println "- Create dictionary with short dictID"
1155 $ZSTD --train-cover=k=46,d=8,split=80 "$TESTDIR"/*.c "$PRGDIR"/*.c --dictID=1 -o tmpDict1
1156 cmp tmpDict tmpDict1 && die "dictionaries should have different ID !"
1157 println "- Create dictionary with size limit"
1158 $ZSTD --train-cover=steps=8 "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpDict2 --maxdict=4K
1159 println "- Compare size of dictionary from 90% training samples with 80% training samples"
1160 $ZSTD --train-cover=split=90 -r "$TESTDIR"/*.c "$PRGDIR"/*.c
1161 $ZSTD --train-cover=split=80 -r "$TESTDIR"/*.c "$PRGDIR"/*.c
1162 println "- Create dictionary using all samples for both training and testing"
1163 $ZSTD --train-cover=split=100 -r "$TESTDIR"/*.c "$PRGDIR"/*.c
1164 println "- Test -o before --train-cover"
1165 rm -f tmpDict dictionary
1166 $ZSTD -o tmpDict --train-cover "$TESTDIR"/*.c "$PRGDIR"/*.c
1167 test -f tmpDict
1168 $ZSTD --train-cover "$TESTDIR"/*.c "$PRGDIR"/*.c
1169 test -f dictionary
1170 rm -f tmp* dictionary
1171
1172
1173 if [ "$isWindows" = false ] ; then
1174
1175 println "\n===>  zstd fifo named pipe test "
1176 head -c 10 /dev/zero > tmp_original
1177 mkfifo named_pipe
1178 head -c 10 /dev/zero > named_pipe &
1179 $ZSTD named_pipe -o tmp_compressed
1180 $ZSTD -d -o tmp_decompressed tmp_compressed
1181 $DIFF -s tmp_original tmp_decompressed
1182 rm -rf tmp*
1183 rm -rf named_pipe
1184
1185 fi
1186
1187 rm -f tmp*