3 # SUSv3 compliant sort tests.
4 # Public Domain, David Leonard 2022
8 # name cmd expected ./input stdin
9 testing
"" "tsort" "a\n" "" "a a\n"
10 testing
"" "tsort -" "a\n" "" "a a\n"
11 testing
"" "tsort input" "a\n" "a a\n" ""
12 testing
"tsort input (w/o eol)" "tsort input" "a\n" "a a" ""
13 testing
"" "tsort /dev/null" "" "" ""
15 testing
"tsort empty" tsort "" "" ""
16 testing
"tsort blank" tsort "" "" "\n"
17 testing
"tsort blanks" tsort "" "" "\n\n \t\n "
19 # simple inputs having exactly one solution
20 testing
"tsort 1-edge" tsort "a\nb\n" "" "a b\n"
21 testing
"tsort 2-edge" tsort "a\nb\nc\n" "" "a b b c\n"
24 # The following test helper accommodates future variable output because, as
25 # tsort is allowed to emit any total ordering that satisfies its input,
26 # should the implementation changes, these tests will remain valid.
28 # The idea is to verify that:
29 # - each input word is present EXACTLY ONCE in tsort's output
30 # - for each input pair 'a b', the occurrence of 'a' APPEARS BEFORE 'b'
31 # - the exit code is 0
39 echo "echo \"$args\" | tsort >actual"
41 echo "$args" |
tsort >actual
43 if [ $ec -ne 0 ]; then
44 fail
"tsort exit $ec, expected 0"
46 while [ $# -ne 0 ]; do
49 aline
=$
(grep -nxF "$a" <actual | cut
-d: -f1)
50 bline
=$
(grep -nxF "$b" <actual | cut
-d: -f1)
52 "") fail
"word $a missing from output ($args)";;
53 *" "*) fail
"word $a duplicated ($args)";;
56 "") fail
"word $b missing from output ($args)";;
57 *" "*) fail
"word $b duplicated ($args)";;
59 if [ $aline -gt $bline ]; then
60 fail
"$a appears after $b ($args)"
63 if [ $fail ] && [ $VERBOSE ]; then
64 echo "exit $ec, actual:"
71 # Test that erroneous input causes an unsuccessful exit code
72 # we don't test the output error message
76 echo "$*" |
tsort >/dev
/null
2>/dev
/null
78 if [ $ec -eq 0 ]; then
79 fail
"$name: unexpected exit 0 ($*)"
85 [ $VERBOSE ] && echo "ERROR: $*"
91 FAILCOUNT
=$
(($FAILCOUNT + 1))
98 tsort_test
"tsort empty2"
99 tsort_test
"tsort singleton" a a
100 tsort_test
"tsort simple" a b b c
101 tsort_test
"tsort 2singleton" a a b b
102 tsort_test
"tsort medium" a b a b b c
103 tsort_test
"tsort std.example" a b c c d e g g f g e f h h
104 tsort_test
"tsort prefixes" a aa aa aaa aaaa aaaaa a aaaaa
106 tsort_test_err
"tsort odd" a
107 tsort_test_err
"tsort odd2" a b c
108 tsort_test_err
"tsort cycle" a b b a