3 # $RCSfile: sort.t,v $$Revision: 4.1 $$Date: 92/08/07 18:28:24 $
7 sub backwards { $a lt $b ? 1 : $a gt $b ? -1 : 0 }
9 my $upperfirst = 'A' lt 'a';
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
20 # All the above orders make sense.
22 # That said, EBCDIC sorts all small letters first, as opposed
23 # to ASCII which sorts all big letters first.
25 @harry = ('dog','cat','x','Cain','Abel');
26 @george = ('gone','chased','yz','punished','Axed');
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");
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");
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");
47 print ("@b" eq "" ? "ok 4\n" : "not ok 4 (@b)\n");
51 print ("@b" eq "1" ? "ok 5\n" : "not ok 5 (@b)\n");
55 print ("@b" eq "2 1" ? "ok 6\n" : "not ok 6 (@b)\n");
59 print ("@b" eq "3 2 1" ? "ok 7\n" : "not ok 7 (@b)\n");
63 print ("@b" eq "4 3 2 1" ? "ok 8\n" : "not ok 8 (@b)\n");
66 @b = sort {$a <=> $b;} @a;
67 print ("@b" eq "2 3 4 10" ? "ok 9\n" : "not ok 9 (@b)\n");
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");
75 # literals, combinations
78 print ("@b" eq '1 2 3 4' ? "ok 11\n" : "not ok 11\n");
81 @b = sort grep { $_ } (4,1,3,2);
82 print ("@b" eq '1 2 3 4' ? "ok 12\n" : "not ok 12\n");
85 @b = sort map { $_ } (4,1,3,2);
86 print ("@b" eq '1 2 3 4' ? "ok 13\n" : "not ok 13\n");
89 @b = sort reverse (4,1,3,2);
90 print ("@b" eq '1 2 3 4' ? "ok 14\n" : "not ok 14\n");
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");
99 # redefining sort subs outside the sort should not fail
100 eval { *twoface = sub { &backwards } };
101 print $@ ? "not ok 16\n" : "ok 16\n";
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");
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");
111 eval 'sub twoface { $a <=> $b }';
112 die($@ =~ /redefine active sort/ ? "ok 19\n" : "not ok 19\n");
115 eval { @b = sort twoface 4,1 };
116 print $@ ? "$@" : "not ok 19\n";
119 my @result = sort main'backwards 'one', 'two';
121 print $@ ? "not ok 20\n# $@" : "ok 20\n";
124 # "sort 'one', 'two'" should not try to parse "'one" as a sort sub
125 my @result = sort 'one', 'two';
127 print $@ ? "not ok 21\n# $@" : "ok 21\n";