This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
propagate failures in DESTROY() as (optional) warnings
[perl5.git] / t / op / sort.t
CommitLineData
a687059c
LW
1#!./perl
2
79072805 3# $RCSfile: sort.t,v $$Revision: 4.1 $$Date: 92/08/07 18:28:24 $
a687059c 4
15f0808c 5print "1..21\n";
a687059c 6
2f52a358 7sub backwards { $a lt $b ? 1 : $a gt $b ? -1 : 0 }
a687059c 8
9d116dd7
JH
9my $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
a687059c 25@harry = ('dog','cat','x','Cain','Abel');
2f52a358 26@george = ('gone','chased','yz','punished','Axed');
a687059c
LW
27
28$x = join('', sort @harry);
9d116dd7
JH
29$expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
30print "# 1: x = '$x', expected = '$expected'\n";
31print ($x eq $expected ? "ok 1\n" : "not ok 1\n");
a687059c 32
a0d0e21e 33$x = join('', sort( backwards @harry));
9d116dd7
JH
34$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
35print "# 2: x = '$x', expected = '$expected'\n";
36print ($x eq $expected ? "ok 2\n" : "not ok 2\n");
a687059c
LW
37
38$x = join('', sort @george, 'to', @harry);
9d116dd7
JH
39$expected = $upperfirst ?
40 'AbelAxedCaincatchaseddoggonepunishedtoxyz' :
41 'catchaseddoggonepunishedtoxyzAbelAxedCain' ;
42print "# 3: x = '$x', expected = '$expected'\n";
43print ($x eq $expected ?"ok 3\n":"not ok 3\n");
03a14243
LW
44
45@a = ();
46@b = reverse @a;
47print ("@b" eq "" ? "ok 4\n" : "not ok 4 (@b)\n");
48
49@a = (1);
50@b = reverse @a;
51print ("@b" eq "1" ? "ok 5\n" : "not ok 5 (@b)\n");
52
53@a = (1,2);
54@b = reverse @a;
55print ("@b" eq "2 1" ? "ok 6\n" : "not ok 6 (@b)\n");
56
57@a = (1,2,3);
58@b = reverse @a;
59print ("@b" eq "3 2 1" ? "ok 7\n" : "not ok 7 (@b)\n");
60
61@a = (1,2,3,4);
62@b = reverse @a;
63print ("@b" eq "4 3 2 1" ? "ok 8\n" : "not ok 8 (@b)\n");
55204971
LW
64
65@a = (10,2,3,4);
66@b = sort {$a <=> $b;} @a;
67print ("@b" eq "2 3 4 10" ? "ok 9\n" : "not ok 9 (@b)\n");
988174c1 68
463ee0b2 69$sub = 'backwards';
988174c1 70$x = join('', sort $sub @harry);
9d116dd7
JH
71$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
72print "# 10: x = $x, expected = '$expected'\n";
73print ($x eq $expected ? "ok 10\n" : "not ok 10\n");
988174c1 74
cd5de442
GS
75# literals, combinations
76
77@b = sort (4,1,3,2);
78print ("@b" eq '1 2 3 4' ? "ok 11\n" : "not ok 11\n");
79print "# x = '@b'\n";
80
81@b = sort grep { $_ } (4,1,3,2);
82print ("@b" eq '1 2 3 4' ? "ok 12\n" : "not ok 12\n");
83print "# x = '@b'\n";
84
85@b = sort map { $_ } (4,1,3,2);
86print ("@b" eq '1 2 3 4' ? "ok 13\n" : "not ok 13\n");
87print "# x = '@b'\n";
88
89@b = sort reverse (4,1,3,2);
90print ("@b" eq '1 2 3 4' ? "ok 14\n" : "not ok 14\n");
91print "# x = '@b'\n";
7bac28a0
PP
92
93$^W = 0;
94# redefining sort sub inside the sort sub should fail
95sub twoface { *twoface = sub { $a <=> $b }; &twoface }
96eval { @b = sort twoface 4,1,3,2 };
97print ($@ =~ /redefine active sort/ ? "ok 15\n" : "not ok 15\n");
98
99# redefining sort subs outside the sort should not fail
100eval { *twoface = sub { &backwards } };
101print $@ ? "not ok 16\n" : "ok 16\n";
102
103eval { @b = sort twoface 4,1,3,2 };
104print ("@b" eq '4 3 2 1' ? "ok 17\n" : "not ok 17 |@b|\n");
105
106*twoface = sub { *twoface = *backwards; $a <=> $b };
107eval { @b = sort twoface 4,1 };
108print ($@ =~ /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 };
115eval { @b = sort twoface 4,1 };
116print $@ ? "$@" : "not ok 19\n";
15f0808c
GS
117
118eval <<'CODE';
119 my @result = sort main'backwards 'one', 'two';
120CODE
121print $@ ? "not ok 20\n# $@" : "ok 20\n";
122
123eval <<'CODE';
124 # "sort 'one', 'two'" should not try to parse "'one" as a sort sub
125 my @result = sort 'one', 'two';
126CODE
127print $@ ? "not ok 21\n# $@" : "ok 21\n";