| 1 | #!./perl |
| 2 | |
| 3 | # $RCSfile: sort.t,v $$Revision: 4.1 $$Date: 92/08/07 18:28:24 $ |
| 4 | |
| 5 | print "1..21\n"; |
| 6 | |
| 7 | sub backwards { $a lt $b ? 1 : $a gt $b ? -1 : 0 } |
| 8 | |
| 9 | my $upperfirst = 'A' lt 'a'; |
| 10 | |
| 11 | # Beware: in future this may become hairier because of possible |
| 12 | # collation complications: qw(A a B c) can be sorted at least as |
| 13 | # any of the following |
| 14 | # |
| 15 | # A a B b |
| 16 | # A B a b |
| 17 | # a b A B |
| 18 | # a A b B |
| 19 | # |
| 20 | # All the above orders make sense. |
| 21 | # |
| 22 | # That said, EBCDIC sorts all small letters first, as opposed |
| 23 | # to ASCII which sorts all big letters first. |
| 24 | |
| 25 | @harry = ('dog','cat','x','Cain','Abel'); |
| 26 | @george = ('gone','chased','yz','punished','Axed'); |
| 27 | |
| 28 | $x = join('', sort @harry); |
| 29 | $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain'; |
| 30 | print "# 1: x = '$x', expected = '$expected'\n"; |
| 31 | print ($x eq $expected ? "ok 1\n" : "not ok 1\n"); |
| 32 | |
| 33 | $x = join('', sort( backwards @harry)); |
| 34 | $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; |
| 35 | print "# 2: x = '$x', expected = '$expected'\n"; |
| 36 | print ($x eq $expected ? "ok 2\n" : "not ok 2\n"); |
| 37 | |
| 38 | $x = join('', sort @george, 'to', @harry); |
| 39 | $expected = $upperfirst ? |
| 40 | 'AbelAxedCaincatchaseddoggonepunishedtoxyz' : |
| 41 | 'catchaseddoggonepunishedtoxyzAbelAxedCain' ; |
| 42 | print "# 3: x = '$x', expected = '$expected'\n"; |
| 43 | print ($x eq $expected ?"ok 3\n":"not ok 3\n"); |
| 44 | |
| 45 | @a = (); |
| 46 | @b = reverse @a; |
| 47 | print ("@b" eq "" ? "ok 4\n" : "not ok 4 (@b)\n"); |
| 48 | |
| 49 | @a = (1); |
| 50 | @b = reverse @a; |
| 51 | print ("@b" eq "1" ? "ok 5\n" : "not ok 5 (@b)\n"); |
| 52 | |
| 53 | @a = (1,2); |
| 54 | @b = reverse @a; |
| 55 | print ("@b" eq "2 1" ? "ok 6\n" : "not ok 6 (@b)\n"); |
| 56 | |
| 57 | @a = (1,2,3); |
| 58 | @b = reverse @a; |
| 59 | print ("@b" eq "3 2 1" ? "ok 7\n" : "not ok 7 (@b)\n"); |
| 60 | |
| 61 | @a = (1,2,3,4); |
| 62 | @b = reverse @a; |
| 63 | print ("@b" eq "4 3 2 1" ? "ok 8\n" : "not ok 8 (@b)\n"); |
| 64 | |
| 65 | @a = (10,2,3,4); |
| 66 | @b = sort {$a <=> $b;} @a; |
| 67 | print ("@b" eq "2 3 4 10" ? "ok 9\n" : "not ok 9 (@b)\n"); |
| 68 | |
| 69 | $sub = 'backwards'; |
| 70 | $x = join('', sort $sub @harry); |
| 71 | $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; |
| 72 | print "# 10: x = $x, expected = '$expected'\n"; |
| 73 | print ($x eq $expected ? "ok 10\n" : "not ok 10\n"); |
| 74 | |
| 75 | # literals, combinations |
| 76 | |
| 77 | @b = sort (4,1,3,2); |
| 78 | print ("@b" eq '1 2 3 4' ? "ok 11\n" : "not ok 11\n"); |
| 79 | print "# x = '@b'\n"; |
| 80 | |
| 81 | @b = sort grep { $_ } (4,1,3,2); |
| 82 | print ("@b" eq '1 2 3 4' ? "ok 12\n" : "not ok 12\n"); |
| 83 | print "# x = '@b'\n"; |
| 84 | |
| 85 | @b = sort map { $_ } (4,1,3,2); |
| 86 | print ("@b" eq '1 2 3 4' ? "ok 13\n" : "not ok 13\n"); |
| 87 | print "# x = '@b'\n"; |
| 88 | |
| 89 | @b = sort reverse (4,1,3,2); |
| 90 | print ("@b" eq '1 2 3 4' ? "ok 14\n" : "not ok 14\n"); |
| 91 | print "# x = '@b'\n"; |
| 92 | |
| 93 | $^W = 0; |
| 94 | # redefining sort sub inside the sort sub should fail |
| 95 | sub twoface { *twoface = sub { $a <=> $b }; &twoface } |
| 96 | eval { @b = sort twoface 4,1,3,2 }; |
| 97 | print ($@ =~ /redefine active sort/ ? "ok 15\n" : "not ok 15\n"); |
| 98 | |
| 99 | # redefining sort subs outside the sort should not fail |
| 100 | eval { *twoface = sub { &backwards } }; |
| 101 | print $@ ? "not ok 16\n" : "ok 16\n"; |
| 102 | |
| 103 | eval { @b = sort twoface 4,1,3,2 }; |
| 104 | print ("@b" eq '4 3 2 1' ? "ok 17\n" : "not ok 17 |@b|\n"); |
| 105 | |
| 106 | *twoface = sub { *twoface = *backwards; $a <=> $b }; |
| 107 | eval { @b = sort twoface 4,1 }; |
| 108 | print ($@ =~ /redefine active sort/ ? "ok 18\n" : "not ok 18\n"); |
| 109 | |
| 110 | *twoface = sub { |
| 111 | eval 'sub twoface { $a <=> $b }'; |
| 112 | die($@ =~ /redefine active sort/ ? "ok 19\n" : "not ok 19\n"); |
| 113 | $a <=> $b; |
| 114 | }; |
| 115 | eval { @b = sort twoface 4,1 }; |
| 116 | print $@ ? "$@" : "not ok 19\n"; |
| 117 | |
| 118 | eval <<'CODE'; |
| 119 | my @result = sort main'backwards 'one', 'two'; |
| 120 | CODE |
| 121 | print $@ ? "not ok 20\n# $@" : "ok 20\n"; |
| 122 | |
| 123 | eval <<'CODE'; |
| 124 | # "sort 'one', 'two'" should not try to parse "'one" as a sort sub |
| 125 | my @result = sort 'one', 'two'; |
| 126 | CODE |
| 127 | print $@ ? "not ok 21\n# $@" : "ok 21\n"; |