1 # $NetBSD: t_sort.sh,v 1.1 2012/03/17 16:33:15 jruoho Exp $
3 # Copyright (c) 2008, 2009, 2010 The NetBSD Foundation, Inc.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 # POSSIBILITY OF SUCH DAMAGE.
31 atf_set
"descr" "Basic functionality test"
49 atf_check
-o file:expout
sort in
52 atf_test_case empty_file
55 atf_set
"descr" "Tests sorting an empty file"
60 atf_check
-o empty
sort -S empty
61 atf_check
sort -S -c empty
62 atf_check
sort -S -c -u empty
65 atf_test_case end_of_options
68 atf_set
"descr" "Determination of end of option list"
73 atf_check
-o file:-k -x "sort -S -- -k </dev/null"
74 atf_check
-s not-exit
:1 -e ignore
-x "sort -S - -c </dev/null"
77 atf_test_case missing_newline
78 missing_newline_head
()
80 atf_set
"descr" "Tests with missing new line in input file"
82 missing_newline_body
()
85 atf_check
-o inline
:'x\n' sort in
88 atf_test_case null_bytes
91 atf_set
"descr" "Tests the behavior of null bytes"
95 printf '\0b\n\0a\n' >in
96 atf_check
-o inline
:'\0a\n\0b\n' sort -S in
99 atf_test_case long_records
102 atf_set
"descr" "Tests long lines and keys"
107 for(i=1; i<=12; i++) x = x x
108 for(i=15; i<=25; i++) print x i
112 for(i=1; i<=12; i++) x = x x
113 for(i=25; i>=15; i--) print x i
116 atf_check
-o file:out
sort -r in
117 atf_check
-o file:out
sort -k 1,1r
-k 1 in
120 atf_test_case long_file
123 atf_set
"descr" "Tests with a long file to try to force intermediate" \
128 awk 'BEGIN { for(i=0; i<20000; i++) print rand() }' >in
129 sort -S -r in |
awk '$0 "x" != x { print ; x = $0 "x" }' >out
130 atf_check
-o file:out
sort -u -r in
133 atf_test_case any_char
136 atf_set
"descr" "Tests with files containing non-printable/extended" \
141 atf_check
-o file:$
(atf_get_srcdir
)/d_any_char_dflag_out.txt \
142 sort -d -k 2 $
(atf_get_srcdir
)/d_any_char_in.txt
144 atf_check
-o file:$
(atf_get_srcdir
)/d_any_char_fflag_out.txt \
145 sort -f -k 2 $
(atf_get_srcdir
)/d_any_char_in.txt
147 atf_check
-o file:$
(atf_get_srcdir
)/d_any_char_iflag_out.txt \
148 sort -i -k 2 $
(atf_get_srcdir
)/d_any_char_in.txt
154 atf_set
"descr" "Tests the -b flag"
163 atf_check
-o file:in sort -b in
164 atf_check
-o file:in -x "sort -b <in"
165 atf_check
-s exit:1 -o ignore
-e ignore
-x "sort in | sort -c -r"
171 atf_set
"descr" "Tests the -c flag"
180 atf_check
-s exit:1 -e ignore
sort -S -c in
183 atf_test_case kflag_one_field
184 kflag_one_field_head
()
186 atf_set
"descr" "Tests the -k flag with one field"
188 kflag_one_field_body
()
204 atf_check
-o file:expout
sort -k2.1
in
207 atf_test_case kflag_two_fields
208 kflag_two_fields_head
()
210 atf_set
"descr" "Tests the -k flag with two fields"
212 kflag_two_fields_body
()
227 atf_check
-o file:expout
sort -k2.1
,2.0 in
230 atf_test_case kflag_many_fields
231 kflag_many_fields_head
()
233 atf_set
"descr" "Tests the -k flag with many fields"
235 kflag_many_fields_body
()
261 atf_check
-o file:out
sort -t: -k9 -k8 -k7 -k6 -k5 -k4 -k3 \
265 atf_test_case kflag_outofbounds
266 kflag_outofbounds_head
()
268 atf_set
"descr" "Tests the -k flag with out of bounds fields"
270 kflag_outofbounds_body
()
281 atf_check
-o file:in sort -k2.2
,2.1 -k2.3
,2.4 in
284 atf_test_case kflag_nonmonotone
285 kflag_nonmonotone_head
()
287 atf_set
"descr" "Tests the -k flag with apparently nonmonotone" \
290 kflag_nonmonotone_body
()
298 atf_check
-o file:in sort -k2,1.3 -k2.5
,2.5 in
301 atf_test_case kflag_limits
304 atf_set
"descr" "Tests the -k flag field limits"
322 atf_check
-o file:out
sort -r -k1,1 -k2n in
325 atf_test_case kflag_alpha
328 atf_set
"descr" "Tests the -k flag with various alpha fields"
333 01:04:19:01:16:01:21:01 a
334 02:03:13:15:13:19:15:02 a
335 03:02:07:09:07:13:09:03 a
336 04:01:01:03:01:07:03:04 a
337 05:08:20:16:17:02:20:05 aa
338 06:07:14:18:14:20:14:06 aa
339 07:06:08:10:08:14:08:07 aa
340 08:05:02:04:02:08:02:08 aa
341 09:16:22:02:22:04:24:13 b
342 10:15:16:20:19:22:18:14 b
343 11:14:10:12:10:16:12:15 b
344 12:13:04:06:04:10:06:16 b
345 13:24:24:22:24:06:22:21 bb
346 14:23:18:24:21:24:16:22 bb
347 15:22:12:14:12:18:10:23 bb
348 16:21:06:08:06:12:04:24 bb
349 17:12:21:21:18:03:19:09 ab
350 18:11:15:19:15:21:13:10 ab
351 19:10:09:11:09:15:07:11 ab
352 20:09:03:05:03:09:01:12 ab
353 21:20:23:17:23:05:23:17 ba
354 22:19:17:23:20:23:17:18 ba
355 23:18:11:13:11:17:11:19 ba
356 24:17:05:07:05:11:05:20 ba
359 atf_check
-x "sort -S -k2b -k2 in >xx"
360 atf_check
-e ignore
sort -c -t: -k2n xx
362 atf_check
-x "sort -S -k2,2.1b -k2 in >xx"
363 atf_check
-e ignore
sort -c -t: -k3n xx
365 atf_check
-x "sort -S -k2.3 -k2 in >xx"
366 atf_check
-e ignore
sort -c -t: -k4n xx
368 atf_check
-x "sort -S -k2b,2.3 -k2 in >xx"
369 atf_check
-e ignore
sort -c -t: -k5n xx
371 atf_check
-x "sort -S -k2.3,2.1b -k2 in >xx"
372 atf_check
-e ignore
sort -c -t: -k6n xx
374 atf_check
-x "sort -S -k2,2.1b -k2r in >xx"
375 atf_check
-e ignore
sort -c -t: -k7n xx
377 atf_check
-x "sort -S -b -k2,2 -k2 in >xx"
378 atf_check
-e ignore
sort -c -t: -k8n xx
380 # XXX This test is broken. The standard is not clear on the behavior.
381 #atf_check -x "sort -S -b -k2,2b -k2 in >xx"
382 #atf_check -e ignore sort -c -t: -k3n xx
385 atf_test_case kflag_no_end
388 atf_set
"descr" "Tests the -k flag with a field without end"
408 atf_check
-o file:out
sort -df -k 1 -k 1d
<in
414 atf_set
"descr" "Tests the -m flag"
445 atf_check
-o file:out
sort -S -m in1 in2
448 atf_test_case mflag_uflag
451 atf_set
"descr" "Tests the -m flag together with -u"
462 atf_check
-o file:in sort -m -u in
465 atf_test_case mflag_uflag_first
466 mflag_uflag_first_head
()
468 atf_set
"descr" "Tests that the -m flag together with -u picks the" \
471 mflag_uflag_first_body
()
491 atf_check
-o file:out
sort -mudf in
492 atf_check
-o file:out
sort -mudf -k1 in
498 atf_set
"descr" "Tests the -n flag"
508 0010.000000000000000000000000000000000001
523 0010.000000000000000000000000000000000001
526 atf_check
-o file:expout
sort -n in
529 atf_test_case nflag_rflag
532 atf_set
"descr" "Tests the -n and -r flag combination"
548 atf_check
-o file:expout
sort -rn in
554 atf_set
"descr" "Tests the -o flag"
569 atf_check
sort -u -o in in
578 atf_check
-o file:expout
cat in
581 atf_test_case oflag_displaced
582 oflag_displaced_head
()
584 atf_set
"descr" "Tests the -o flag after the file names"
586 oflag_displaced_body
()
588 atf_check
sort -S /dev
/null
-o out
589 test -f out || atf_fail
"File not created"
595 atf_set
"descr" "Tests the -r flag"
613 atf_check
-o file:expout
sort -r in
619 atf_set
"descr" "Tests the -s flag"
641 atf_check
-o file:out
sort -s -k1,1 in
644 atf_test_case sflag_many_files
645 sflag_many_files_head
()
647 atf_set
"descr" "Tests the -s flag with multiple files"
649 sflag_many_files_body
()
668 atf_check
-o file:out
sort -smru -k1,1 in1 in1 in2 in2
674 atf_set
"descr" "Tests the -t flag"
683 atf_check
-o file:in sort -t : -r +0 in
684 atf_check
-o file:in sort -t : +0 -1 in
685 atf_check
-o file:in sort -t : -r -k 1 in
686 atf_check
-o file:in sort -t : -k 1,1 in
689 atf_test_case tflag_alphabetic
690 tflag_alphabetic_head
()
692 atf_set
"descr" "Tests the -t flag with a character as the delimiter"
694 tflag_alphabetic_body
()
702 atf_check
-o file:in sort -tX -k2 -k1r,1 in
705 atf_test_case tflag_char_pos
706 tflag_char_pos_head
()
708 atf_set
"descr" "Tests the -t flag with character positions in fields"
710 tflag_char_pos_body
()
722 atf_check
-o file:out
sort -b -t: +1.1 in
723 atf_check
-o file:out
sort -t: +1.1r
in
724 atf_check
-o file:out
sort -b -t: -k 2.2 in
725 atf_check
-o file:out
sort -t: -k 2.2r
in
728 atf_test_case tflag_whitespace
729 tflag_whitespace_head
()
731 atf_set
"descr" "Tests the -t flag with spaces and tabs as the" \
734 tflag_whitespace_body
()
742 atf_check
-o file:in sort -t ' ' -k2,2 in
743 atf_check
-o file:in sort -t ' ' -k2.1
,2.0 in
751 atf_check
-o file:out
sort -t ' ' -k2,2 in
752 atf_check
-o file:out
sort -t ' ' -k2.1
,2.0 in
760 atf_check
-o file:out
sort -S -k2 in
768 atf_check
-o file:out
sort -S -k2b in
774 atf_set
"descr" "Tests the -u flag"
791 atf_check
-o file:expout
sort -u in
794 atf_test_case uflag_rflag
797 atf_set
"descr" "Tests the -u and -r flag combination"
814 atf_check
-o file:expout
sort -ru in
817 atf_test_case plus_one
820 atf_set
"descr" "Tests +- addressing: +1 should become -k2.1"
838 atf_check
-o file:expout
sort +1 in
841 atf_test_case plus_one_minus_two
842 plus_one_minus_two_head
()
844 atf_set
"descr" "Tests +- addressing: +1 -2 should become -k2.1,2.0"
846 plus_one_minus_two_body
()
862 atf_check
-o file:expout
sort +1 -2 in
865 atf_test_case plus_zero
868 atf_set
"descr" "Tests +- addressing: '-- +0' raised a '-k1.1: No" \
869 "such file or directory' error"
873 echo 'good contents' >.
/+0
875 atf_check
-o file:+0 sort -- +0
878 atf_test_case plus_nonmonotone
879 plus_nonmonotone_head
()
881 atf_set
"descr" "Tests += addressing: apparently nonmonotone field" \
884 plus_nonmonotone_body
()
892 atf_check
-o file:in sort +1 -0.3
+1.4 -1.5
in
895 atf_test_case plus_as_path
898 atf_set
"descr" "Tests +- addressing: 'file +0' raised a '-k1.1: No" \
899 "such file or directory' error"
903 echo 'good contents' >.
/+0
904 echo 'more contents' >in
907 atf_check
-o file:expout
sort in +0
910 atf_test_case plus_bad_tempfile
911 plus_bad_tempfile_head
()
913 atf_set
"descr" "Tests +- addressing: intermediate wrong behavior" \
914 "that raised a '+0: No such file or directory' error"
916 plus_bad_tempfile_body
()
918 echo 'good contents' >in
919 atf_check
-o file:in sort -T /tmp
+0 in
922 atf_test_case plus_rflag_invalid
923 plus_rflag_invalid_head
()
925 atf_set
"descr" "Tests +- addressing: invalid record delimiter"
927 plus_rflag_invalid_body
()
936 atf_check
-o inline
:'x a n g+x a n h+z b m f+y c o e+' \
940 atf_test_case plus_tflag
943 atf_set
"descr" "Tests +- addressing: using -T caused a 'No such file" \
944 "or directory' error"
949 yes |
sed 200000q |
sort -T + >/dev
/null || atf_fail
"program failed"
952 atf_test_case plus_no_end
955 atf_set
"descr" "Tests +- addressing: field without end"
975 atf_check
-o file:out
sort -df +0 +0d
in
978 atf_init_test_cases
()
980 atf_add_test_case basic
981 atf_add_test_case empty_file
982 atf_add_test_case end_of_options
983 atf_add_test_case missing_newline
984 atf_add_test_case null_bytes
985 atf_add_test_case long_records
986 atf_add_test_case long_file
987 atf_add_test_case any_char
988 atf_add_test_case bflag
989 atf_add_test_case cflag
990 atf_add_test_case kflag_one_field
991 atf_add_test_case kflag_two_fields
992 atf_add_test_case kflag_many_fields
993 atf_add_test_case kflag_outofbounds
994 atf_add_test_case kflag_nonmonotone
995 atf_add_test_case kflag_limits
996 atf_add_test_case kflag_alpha
997 atf_add_test_case kflag_no_end
998 atf_add_test_case mflag
999 atf_add_test_case mflag_uflag
1000 atf_add_test_case mflag_uflag_first
1001 atf_add_test_case nflag
1002 atf_add_test_case nflag_rflag
1003 atf_add_test_case oflag
1004 atf_add_test_case oflag_displaced
1005 atf_add_test_case rflag
1006 atf_add_test_case sflag
1007 atf_add_test_case sflag_many_files
1008 atf_add_test_case tflag
1009 atf_add_test_case tflag_alphabetic
1010 atf_add_test_case tflag_char_pos
1011 atf_add_test_case tflag_whitespace
1012 atf_add_test_case uflag
1013 atf_add_test_case uflag_rflag
1014 atf_add_test_case plus_one
1015 atf_add_test_case plus_one_minus_two
1016 atf_add_test_case plus_zero
1017 atf_add_test_case plus_nonmonotone
1018 atf_add_test_case plus_as_path
1019 atf_add_test_case plus_bad_tempfile
1020 atf_add_test_case plus_rflag_invalid
1021 atf_add_test_case plus_tflag
1022 atf_add_test_case plus_no_end