Commit | Line | Data |
---|---|---|
a687059c LW |
1 | #!./perl |
2 | ||
79072805 | 3 | # $RCSfile: sort.t,v $$Revision: 4.1 $$Date: 92/08/07 18:28:24 $ |
a687059c | 4 | |
15f0808c | 5 | print "1..21\n"; |
a687059c | 6 | |
2f52a358 | 7 | sub backwards { $a lt $b ? 1 : $a gt $b ? -1 : 0 } |
a687059c LW |
8 | |
9 | @harry = ('dog','cat','x','Cain','Abel'); | |
2f52a358 | 10 | @george = ('gone','chased','yz','punished','Axed'); |
a687059c LW |
11 | |
12 | $x = join('', sort @harry); | |
13 | print ($x eq 'AbelCaincatdogx' ? "ok 1\n" : "not ok 1\n"); | |
2f52a358 | 14 | print "# x = '$x'\n"; |
a687059c | 15 | |
a0d0e21e | 16 | $x = join('', sort( backwards @harry)); |
a687059c | 17 | print ($x eq 'xdogcatCainAbel' ? "ok 2\n" : "not ok 2\n"); |
2f52a358 | 18 | print "# x = '$x'\n"; |
a687059c LW |
19 | |
20 | $x = join('', sort @george, 'to', @harry); | |
2f52a358 JH |
21 | print ($x eq 'AbelAxedCaincatchaseddoggonepunishedtoxyz'?"ok 3\n":"not ok 3\n"); |
22 | print "# x = '$x'\n"; | |
03a14243 LW |
23 | |
24 | @a = (); | |
25 | @b = reverse @a; | |
26 | print ("@b" eq "" ? "ok 4\n" : "not ok 4 (@b)\n"); | |
27 | ||
28 | @a = (1); | |
29 | @b = reverse @a; | |
30 | print ("@b" eq "1" ? "ok 5\n" : "not ok 5 (@b)\n"); | |
31 | ||
32 | @a = (1,2); | |
33 | @b = reverse @a; | |
34 | print ("@b" eq "2 1" ? "ok 6\n" : "not ok 6 (@b)\n"); | |
35 | ||
36 | @a = (1,2,3); | |
37 | @b = reverse @a; | |
38 | print ("@b" eq "3 2 1" ? "ok 7\n" : "not ok 7 (@b)\n"); | |
39 | ||
40 | @a = (1,2,3,4); | |
41 | @b = reverse @a; | |
42 | print ("@b" eq "4 3 2 1" ? "ok 8\n" : "not ok 8 (@b)\n"); | |
55204971 LW |
43 | |
44 | @a = (10,2,3,4); | |
45 | @b = sort {$a <=> $b;} @a; | |
46 | print ("@b" eq "2 3 4 10" ? "ok 9\n" : "not ok 9 (@b)\n"); | |
988174c1 | 47 | |
463ee0b2 | 48 | $sub = 'backwards'; |
988174c1 LW |
49 | $x = join('', sort $sub @harry); |
50 | print ($x eq 'xdogcatCainAbel' ? "ok 10\n" : "not ok 10\n"); | |
51 | ||
cd5de442 GS |
52 | # literals, combinations |
53 | ||
54 | @b = sort (4,1,3,2); | |
55 | print ("@b" eq '1 2 3 4' ? "ok 11\n" : "not ok 11\n"); | |
56 | print "# x = '@b'\n"; | |
57 | ||
58 | @b = sort grep { $_ } (4,1,3,2); | |
59 | print ("@b" eq '1 2 3 4' ? "ok 12\n" : "not ok 12\n"); | |
60 | print "# x = '@b'\n"; | |
61 | ||
62 | @b = sort map { $_ } (4,1,3,2); | |
63 | print ("@b" eq '1 2 3 4' ? "ok 13\n" : "not ok 13\n"); | |
64 | print "# x = '@b'\n"; | |
65 | ||
66 | @b = sort reverse (4,1,3,2); | |
67 | print ("@b" eq '1 2 3 4' ? "ok 14\n" : "not ok 14\n"); | |
68 | print "# x = '@b'\n"; | |
7bac28a0 | 69 | |
70 | $^W = 0; | |
71 | # redefining sort sub inside the sort sub should fail | |
72 | sub twoface { *twoface = sub { $a <=> $b }; &twoface } | |
73 | eval { @b = sort twoface 4,1,3,2 }; | |
74 | print ($@ =~ /redefine active sort/ ? "ok 15\n" : "not ok 15\n"); | |
75 | ||
76 | # redefining sort subs outside the sort should not fail | |
77 | eval { *twoface = sub { &backwards } }; | |
78 | print $@ ? "not ok 16\n" : "ok 16\n"; | |
79 | ||
80 | eval { @b = sort twoface 4,1,3,2 }; | |
81 | print ("@b" eq '4 3 2 1' ? "ok 17\n" : "not ok 17 |@b|\n"); | |
82 | ||
83 | *twoface = sub { *twoface = *backwards; $a <=> $b }; | |
84 | eval { @b = sort twoface 4,1 }; | |
85 | print ($@ =~ /redefine active sort/ ? "ok 18\n" : "not ok 18\n"); | |
86 | ||
87 | *twoface = sub { | |
88 | eval 'sub twoface { $a <=> $b }'; | |
89 | die($@ =~ /redefine active sort/ ? "ok 19\n" : "not ok 19\n"); | |
90 | $a <=> $b; | |
91 | }; | |
92 | eval { @b = sort twoface 4,1 }; | |
93 | print $@ ? "$@" : "not ok 19\n"; | |
15f0808c GS |
94 | |
95 | eval <<'CODE'; | |
96 | my @result = sort main'backwards 'one', 'two'; | |
97 | CODE | |
98 | print $@ ? "not ok 20\n# $@" : "ok 20\n"; | |
99 | ||
100 | eval <<'CODE'; | |
101 | # "sort 'one', 'two'" should not try to parse "'one" as a sort sub | |
102 | my @result = sort 'one', 'two'; | |
103 | CODE | |
104 | print $@ ? "not ok 21\n# $@" : "ok 21\n"; |