2 # @(#)stests 8.1 (Berkeley) 6/6/93
3 # $NetBSD: stests,v 1.14 2001/05/14 21:31:38 jdolecek Exp $
5 # This code is derived from software contributed to Berkeley by
8 #Latest version. My sort passes all tests because I wrote it.
9 #We differ only on 25E and 25H.
10 #(I found at least one bug in constructing test 25, and was driven
11 #to rewrite field parsing to clarify it.)
13 #In 25E, -k2.3,2.1b, the fields are not necessarily out of order.
14 #Even if they were, it would be legal (11752-3), although certainly
15 #justification for warning.
17 #On 25H, your answer is as defensible as mine. (Our suggestion
21 # Tests for the Unix sort utility
22 # Test Posix features except for locale.
23 # Test some nonstandard features if present.
25 # Other tests should be made for files too big to fit in memory.
27 dict
=/usr
/share
/dict
/words
29 # Initialize switches for nonstandard features.
30 # Use parenthesized settings for supported features.
32 o
=: # officially obsolescent features: +1 -2, misplaced -o (o=)
33 g
=: # -g numeric sort including e-format numbers (g=)
34 M
=: # -M sort by month names (M=)
35 s
=: # -s stable, do not compare raw bytes on equal keys (s=)
36 y
= # -y user-specified memory size (y=-y10000)
37 S
= # -S non-stable sort
39 # Detect what features are supported, assuming bad options cause
40 # errors. Set switches accordingly.
43 Note
: some
sort(1) implementations
(for example GNU
sort) may not pass these
44 tests
- this suite was made to fit
sort(1) using
(non-stable
) radixsort
(3),
45 sort(1) using other algorithm may give different results
, though still
50 echo Obsolescent and nonstandard features recognized
, if any
:
51 if sort +0 </dev
/null
2>/dev
/null
; then
55 o
="not_tested +POS/-POS"
57 if sort /dev
/null
-o xx
2>/dev
/null
; then
60 if sort -g </dev
/null
2>/dev
/null
; then
62 echo ' -g g-format numbers';
66 if sort -M </dev
/null
2>/dev
/null
; then
72 if sort -s </dev
/null
2>/dev
/null
; then
74 echo ' -s stable sort';
78 if sort -S </dev
/null
2>/dev
/null
; then
80 echo ' -S non-stable sort';
82 if sort -y10000 </dev
/null
2>/dev
/null
; then
86 if sort -z10000 </dev
/null
2>/dev
/null
; then
87 echo ' -z size (not exercised)';
89 if sort -T.
</dev
/null
2>/dev
/null
; then
90 echo ' -T tempdir (not exercised)';
93 TEST
= # major sequence number of test
95 trap "rm -f in in1 out xx -k fields; exit" 0 1 2 13 15
97 # some tests would fail if non-C locale is used
98 unset LANG LC_ALL LC_CTYPE
100 # xsort testno options
101 # Sort file "in" with specified options.
102 # Compare with file "out" if that is supplied,
103 # otherwise make plausibility checks on output
105 # "sum" must be dumb; insensitive to the
106 # order of lines within a file.
107 # System V sum is suitable; sum -5 is the v10 equivalent.
113 if sort $S "$@" in >xx
&& sort -c $S "$@" xx
117 cmp xx out
>/dev
/null
&& return 0
118 echo "$TEST$X comparison failed - sort $@"
120 test "`cksum -o2 <in`" = "`cksum -o2 <xx`" && return 0
121 echo "$TEST$X checksum failed - sort $@"
124 echo "$TEST$X failed - sort $@"
129 # linecount testno file count
130 # declares the given "testno" to be in error if number of
131 # lines in "file" differs from "count"
133 awk 'END{ if(NR!='$3') print "'$TEST$1' failed" }' $2
136 # print info about skipped test
138 echo "$TEST$X skipped - flag '$1' not supported"
143 #---------------------------------------------------------------
144 TEST
=01; echo $TEST # -c status, checksum
145 # obsolescent features go together
152 sort $S -c in 2>/dev
/null
&& echo ${TEST}A failed
154 xsort B ||
'"cksum"' is probably unsuitable
- see comments
156 $o sort $S +0 in -o in ||
echo ${TEST}C failed
158 #---------------------------------------------------------------
159 TEST
=02; echo $TEST # output from -c
165 sort -c $S -r in >out
2>xx
&& echo ${TEST}A failed
166 test -s out
&& echo ${TEST}B failed
167 test -s xx
&& echo option
-c is noisy
"(probably legal)"
168 test -s xx ||
echo option
-c is quiet
"(legal, not classical)"
170 #---------------------------------------------------------------
171 TEST
=03; echo $TEST # -n
178 0010.000000000000000000000000000000000001
192 0010.000000000000000000000000000000000001
197 #---------------------------------------------------------------
198 TEST
=04; echo $TEST # -b without fields, piping, -c status return
207 cat in |
sort $S |
cat >xx
208 cmp xx out
>/dev
/null ||
echo ${TEST}B failed
210 sort $S in |
sort -c $S -r 2>/dev
/null
&& echo ${TEST}C failed
212 #---------------------------------------------------------------
213 TEST
=05; echo $TEST # fields, reverse fields, -c status return
235 xsort F
-k 2,2 -k 1,1 -k 3
237 sort -c $S -k 2 in 2>/dev
/null
&& echo ${TEST}G failed
239 #---------------------------------------------------------------
240 TEST
=06; echo $TEST # -t
247 $o xsort A
-t : -r +0
249 $o xsort B
-t : +0 -1
255 #---------------------------------------------------------------
256 TEST
=07; echo $TEST # -t, character positions in fields
257 # -t: as 1 arg is not strictly conforming, but classical
267 $o xsort A
-b -t: +1.1
271 xsort C
-b -t: -k 2.2
275 #---------------------------------------------------------------
276 TEST
=08; echo $TEST # space and tab as -t characters
286 xsort B
-t ' ' -k2.1
,2.0
296 xsort D
-t ' ' -k2.1
,2.0
314 #---------------------------------------------------------------
315 TEST
=09; echo $TEST # alphabetic as -t character
323 xsort
"" -tX -k2 -k1r,1
325 #---------------------------------------------------------------
326 TEST
=10; echo $TEST # -m
354 sort $S -m in in1
>xx
355 cmp xx out
>/dev
/null ||
echo $TEST failed
357 #---------------------------------------------------------------
358 TEST
=11; echo $TEST # multiple files, -o overwites input, -m, -mu
366 sort $S -o xx
in in in in in in in in in in in in in in in in in
368 sort $S -o in -mu in in in in in in in in in in in in in in in in in
370 sort $S -o in -m in in in in in in in in in in in in in in in in in
372 cmp in xx
>/dev
/null ||
echo ${TEST}C failed
374 #---------------------------------------------------------------
375 TEST
=12; echo $TEST # does -mu pick the first among equals?
393 xsort A
-mudf ||
echo "(other behavior is legal, not classical)"
395 xsort B
-mudf -k1 ||
echo "(other behavior is legal, not classical)"
397 #---------------------------------------------------------------
398 TEST
=13; echo $TEST # long records (>8000 bytes, keys >16000), -r
401 for(i=1; i<=12; i++) x = x x
402 for(i=15; i<=25; i++) print x i
406 for(i=1; i<=12; i++) x = x x
407 for(i=25; i>=15; i--) print x i
414 #---------------------------------------------------------------
415 TEST
=14; echo $TEST "(3 long parts)"
416 awk 'BEGIN { for(i=0; i<100000; i++) print rand() }' |
grep -v e
>in
419 xsort A
; echo $TEST "(part A done)"
421 xsort B
-n; echo $TEST "(part B done)"
423 # next test is unclean: xx is a hidden side-effect of xsort
426 $0 < x { print "test '${TEST}C
' failed"; exit }
427 $0 "" != x { print >"out"; x = $0 }
432 #---------------------------------------------------------------
433 TEST
=15; echo $TEST "(long)" # force intermediate files if possible
434 awk 'BEGIN { for(i=0; i<20000; i++) print rand() }' >in
439 sort $S -r in |
awk '$0 "x" != x { print ; x = $0 "x" }' >out
443 #---------------------------------------------------------------
444 TEST
=16; echo $TEST # -nr, -nm, file name -
445 awk 'BEGIN { for(i=-100; i<=100; i+=2) printf "%.10d\n", i }' >in
447 awk 'BEGIN { for(i=-99; i<=100; i+=2) print i }' |
sort $S -nr in - >xx
448 awk '$0+0 != 101-NR { print "'${TEST}A
' failed"; exit }' xx
450 awk 'BEGIN { for(i=-99; i<=100; i+=2) print i }' |
sort $S -mn in - >xx
451 awk '$0+0 != -101+NR { print "'${TEST}B
' failed"; exit }' xx
453 #---------------------------------------------------------------
454 TEST
=17; echo $TEST # -d, fields without end, modifier override
470 $o xsort A
-df +0 +0d
472 xsort B
-df -k 1 -k 1d
474 #---------------------------------------------------------------
475 TEST
=18; echo $TEST # -u on key only
491 sort $S -u -k 1,1 in >xx
494 #---------------------------------------------------------------
495 TEST
=19; echo $TEST # -i, -d, -f
497 run
(i
,j
){ for( ; i
<=j
; i
++) printf("%.3o %c\n",i
,i
); }
498 main
(){ run
(0, 011); /* 012=='\n' */
504 run
(i
,j
){ for( ; i
<=j
; i
++) printf("%.3o %c\n",i
,i
); }
516 run
(i
,j
){ for( ; i
<=j
; i
++) printf("%.3o %c\n",i
,i
); }
517 main
(){ run
(0, 010); /* 011=='\t', 012=='\n' */
535 run
(i
,j
){ for( ; i
<=j
; i
++) printf("%.3o %c\n",i
,i
); }
539 for(i
='A'; i
<='Z'; i
++)
540 printf("%.3o %c\n%.3o %c\n",i
,i
,i
+040,i
+040);
550 #---------------------------------------------------------------
551 TEST
=20; echo $TEST # -d, -f, -b applies only to fields
571 #---------------------------------------------------------------
572 TEST
=21; echo $TEST # behavior of null bytes
574 main
() { printf("%cb\n%ca\n",0,0); }
579 cmp in xx
>/dev
/null
&& echo ${TEST}A failed
580 test "`wc -c <in`" = "`wc -c <xx`" ||
echo ${TEST}B failed
583 #---------------------------------------------------------------
584 TEST
=22; echo $TEST # field limits
598 xsort
"" -r -k1,1 -k2n
600 #---------------------------------------------------------------
601 TEST
=23; echo $TEST # empty file
603 sort $S -o xx
</dev
/null
604 cmp xx
/dev
/null
2>/dev
/null ||
echo ${TEST}A failed
606 sort $S -c </dev
/null ||
echo ${TEST}B failed
608 sort $S -cu </dev
/null ||
echo ${TEST}C failed
610 #---------------------------------------------------------------
611 TEST
=24; echo $TEST # many fields
635 xsort
"" -t: -k9 -k8 -k7 -k6 -k5 -k4 -k3 -k2 -k1
637 #---------------------------------------------------------------
638 TEST
=25; echo $TEST # variously specified alpha fields
639 # numbers give the correct orderings
641 01:04:19:01:16:01:21:01 a
642 02:03:13:15:13:19:15:02 a
643 03:02:07:09:07:13:09:03 a
644 04:01:01:03:01:07:03:04 a
645 05:08:20:16:17:02:20:05 aa
646 06:07:14:18:14:20:14:06 aa
647 07:06:08:10:08:14:08:07 aa
648 08:05:02:04:02:08:02:08 aa
649 09:16:22:02:22:04:24:13 b
650 10:15:16:20:19:22:18:14 b
651 11:14:10:12:10:16:12:15 b
652 12:13:04:06:04:10:06:16 b
653 13:24:24:22:24:06:22:21 bb
654 14:23:18:24:21:24:16:22 bb
655 15:22:12:14:12:18:10:23 bb
656 16:21:06:08:06:12:04:24 bb
657 17:12:21:21:18:03:19:09 ab
658 18:11:15:19:15:21:13:10 ab
659 19:10:09:11:09:15:07:11 ab
660 20:09:03:05:03:09:01:12 ab
661 21:20:23:17:23:05:23:17 ba
662 22:19:17:23:20:23:17:18 ba
663 23:18:11:13:11:17:11:19 ba
664 24:17:05:07:05:11:05:20 ba
666 sort $S -k2b -k2 in >xx
&&
667 sort -c -t: -k2n xx
2>/dev
/null ||
echo ${TEST}A failed
668 sort $S -k2,2.1b
-k2 in >xx
&&
669 sort -c -t: -k3n xx
2>/dev
/null ||
echo ${TEST}B failed
670 sort $S -k2.3
-k2 in >xx
&&
671 sort -c -t: -k4n xx
2>/dev
/null ||
echo ${TEST}C failed
672 sort $S -k2b,2.3 -k2 in >xx
&&
673 sort -c -t: -k5n xx
2>/dev
/null ||
echo ${TEST}D failed
674 sort $S -k2.3
,2.1b
-k2 in >xx
&&
675 sort -c -t: -k6n xx
2>/dev
/null ||
echo ${TEST}E failed
676 sort $S -k2,2.1b
-k2r in >xx
&&
677 sort -c -t: -k7n xx
2>/dev
/null ||
echo ${TEST}F failed
678 sort $S -b -k2,2 -k2 in >xx
&&
679 sort -c -t: -k8n xx
2>/dev
/null ||
echo ${TEST}G failed
680 sort $S -b -k2,2b
-k2 in >xx
&& # perhaps same as G
681 sort -c -t: -k3n xx
2>/dev
/null ||
echo ${TEST}H failed\
682 "(standard is not clear on this)"
684 #---------------------------------------------------------------
685 TEST
=26; echo $TEST # empty fields, out of bounds fields
696 xsort
"" -k2.2
,2.1 -k2.3
,2.4
698 #---------------------------------------------------------------
699 TEST
=27; echo $TEST # displaced -o
702 $o sort -S /dev
/null
-o out ||
$o echo ${TEST}B failed
703 $o test -f out ||
$o echo ${TEST}C failed
705 #---------------------------------------------------------------
706 TEST
=28; echo $TEST # apparently nonmonotone field specs
714 $o xsort A
+1 -0.3
+1.4 -1.5
716 xsort B
-k2,1.3 -k2.5
,2.5
718 #---------------------------------------------------------------
719 TEST
=29; echo $TEST # determination of end of option list
725 sort $S -- -k </dev
/null
>xx ||
echo ${TEST}A argument failed
726 cmp xx
-k ||
echo ${TEST}A comparison failed
728 sort $S - -c </dev
/null
2>/dev
/null
&& echo ${TEST}B failed
730 #---------------------------------------------------------------
731 TEST
=30; echo $TEST # missing newline
732 awk 'BEGIN{ printf "%s", "x"}' |
sort >xx
733 wc -c <xx |
awk '$1!=2{ print "'${TEST}' failed" }'
735 #---------------------------------------------------------------
736 TEST
=31; echo $TEST # -M, multiple fields
782 $M xsort
"" -k3n -k1M -k2n
784 #---------------------------------------------------------------
785 TEST
=32; echo $TEST # -M case insensitivity, -r
801 #---------------------------------------------------------------
802 TEST
=33; echo $TEST # -g
824 #---------------------------------------------------------------
825 TEST
=34; echo $TEST # -g wide operands
827 .99999999999999999999
828 099999999999999999999e-21
829 099999999999999999999e-19
833 099999999999999999999e-21
834 .99999999999999999999
836 099999999999999999999e-19
843 .99999999999999999999
844 099999999999999999999e-19
845 099999999999999999999e-21
850 #---------------------------------------------------------------
851 TEST
=35; echo $TEST #-g, -u with different fp reps
873 sort $S -gu in >xx
&& $g sort -c $S -gu xx ||
echo ${TEST}B failed
880 #---------------------------------------------------------------
881 TEST
=36; echo $TEST # -s
901 #---------------------------------------------------------------
902 TEST
=37; echo $TEST # -s, multiple files
918 $s sort -smru -k1,1 in in in1 in1
>xx
919 $s cmp xx out
>/dev
/null ||
echo $TEST failed
- sort -smru -k1,1
921 #---------------------------------------------------------------
922 TEST
=38; echo $TEST # -s
926 for(j=1; j<=i; j++) {
932 $s sort -m -s -k1,1n
in in1
>out
935 func stop() { print "'$TEST' comparison failed - sort -m -s -k1,1n"; exit }
936 $1!=last1 { if(count!=last1 || $2!=2) stop();
938 $1==last1 && $2!=last2 { if(count!=last1 || $2!=1) stop();
940 { count++; last1 = $1; last2 = $2 }
942 #---------------------------------------------------------------
943 TEST
=39; echo $TEST # sort already sorted dictionary
944 sort -f $dict |
cmp -s - $dict ||
echo $TEST failed
- sort -f $dict
946 #---------------------------------------------------------------
947 TEST
=40; echo "$TEST (long)" # long lines test
949 line1
="abcdefghijklmnopqrstabcdefghijklmnopqrstabcdefghijklmnopqrstabcdefghij"
950 line2
="bcdefghijklmnopqrstabcdefghijklmnopqrstabcdefghijklmnopqrstabcdefghijk"
954 # generate two out-of-order long lines (roughly 70 KB each), and try sort
958 line1 = "'"${line1}"'"
959 line2 = "'"${line2}"'"
960 for(i=0; i<10; i++) {
973 # generate already sorted file using $dict, only append space and
974 # long string to each line to make lines longer than usual
975 # use only first 200 lines (sort file approx. 14MB)
983 /^/ { print $1 " " line; if (idx++ > 200) exit 0 }' > in
987 #---------------------------------------------------------------
988 TEST
=41; echo $TEST # sort -f empty item; previously bombed
989 cat /dev
/null |
sort -f
991 #---------------------------------------------------------------
992 TEST
=42; echo $TEST # sort -H $dict; previously bombed
993 sort -H -f $dict |
cmp -s - $dict ||
echo $TEST failed
- sort -H -f $dict