Commit | Line | Data |
84d4ea48 |
1 | #!./perl |
2 | |
be6228f7 |
3 | # This tests the behavior of sort() under the different 'use sort' forms. |
4 | # Algorithm by John P. Linderman. |
5 | |
be6228f7 |
6 | my ($BigWidth, $BigEnough, $RootWidth, $ItemFormat, @TestSizes, $WellSoaked); |
7 | |
84d4ea48 |
8 | BEGIN { |
9 | chdir 't' if -d 't'; |
be6228f7 |
10 | @INC = qw(../lib); |
11 | $BigWidth = 6; # Digits in $BigEnough-1 |
12 | $BigEnough = 10**$BigWidth; # Largest array we'll attempt |
13 | $RootWidth = int(($BigWidth+1)/2); # Digits in sqrt($BigEnough-1) |
14 | $ItemFormat = "%0${RootWidth}d%0${BigWidth}d"; # Array item format |
15 | @TestSizes = (0, 1, 2); # Small special cases |
16 | # Testing all the way up to $BigEnough takes too long |
17 | # for casual testing. There are some cutoffs (~256) |
18 | # in pp_sort that should be tested, but 10_000 is ample. |
19 | $WellSoaked = 10_000; # <= $BigEnough |
20 | for (my $ts = 3; $ts < $WellSoaked; $ts *= 10**(1/3)) { |
21 | push(@TestSizes, int($ts)); # about 3 per decade |
22 | } |
84d4ea48 |
23 | } |
24 | |
be6bd645 |
25 | use strict; |
26 | use warnings; |
27 | |
be6228f7 |
28 | use Test::More tests => @TestSizes * 2 # sort() tests |
29 | * 4 # number of pragmas to test |
30 | + 1 # extra test for qsort instability |
31 | + 3; # tests for sort::current |
84d4ea48 |
32 | |
be6228f7 |
33 | # Generate array of specified size for testing sort. |
34 | # |
35 | # We ensure repeated items, where possible, by drawing the $size items |
36 | # from a pool of size sqrt($size). Each randomly chosen item is |
37 | # tagged with the item index, so we can detect original input order, |
38 | # and reconstruct the original array order. |
39 | |
40 | sub genarray { |
41 | my $size = int(shift); # fractions not welcome |
42 | my ($items, $i); |
43 | my @a; |
44 | |
45 | if ($size < 0) { $size = 0; } # avoid complexity with sqrt |
46 | elsif ($size > $BigEnough) { $size = $BigEnough; } |
47 | $#a = $size - 1; # preallocate array |
48 | $items = int(sqrt($size)); # number of distinct items |
49 | for ($i = 0; $i < $size; ++$i) { |
50 | $a[$i] = sprintf($ItemFormat, int($items * rand()), $i); |
51 | } |
52 | return \@a; |
53 | } |
54 | |
55 | |
56 | # Check for correct order (including stability) |
57 | |
58 | sub checkorder { |
59 | my $aref = shift; |
60 | my $status = ''; # so far, so good |
28e9118f |
61 | my ($i, $disorder); |
be6228f7 |
62 | |
63 | for ($i = 0; $i < $#$aref; ++$i) { |
28e9118f |
64 | # Equality shouldn't happen, but catch it in the contents check |
65 | next if ($aref->[$i] le $aref->[$i+1]); |
66 | $disorder = (substr($aref->[$i], 0, $RootWidth) eq |
67 | substr($aref->[$i+1], 0, $RootWidth)) ? |
68 | "Instability" : "Disorder"; |
69 | # Keep checking if merely unstable... disorder is much worse. |
70 | $status = |
71 | "$disorder at element $i between $aref->[$i] and $aref->[$i+1]"; |
72 | last unless ($disorder eq "Instability"); |
be6228f7 |
73 | } |
74 | return $status; |
75 | } |
76 | |
77 | |
78 | # Verify that the two array refs reference identical arrays |
79 | |
80 | sub checkequal { |
81 | my ($aref, $bref) = @_; |
82 | my $status = ''; |
83 | my $i; |
84 | |
85 | if (@$aref != @$bref) { |
86 | $status = "Sizes differ: " . @$aref . " vs " . @$bref; |
87 | } else { |
88 | for ($i = 0; $i < @$aref; ++$i) { |
89 | next if ($aref->[$i] eq $bref->[$i]); |
90 | $status = "Element $i differs: $aref->[$i] vs $bref->[$i]"; |
91 | last; |
92 | } |
93 | } |
94 | return $status; |
95 | } |
96 | |
97 | |
98 | # Test sort on arrays of various sizes (set up in @TestSizes) |
99 | |
100 | sub main { |
101 | my ($expect_unstable) = @_; |
102 | my ($ts, $unsorted, @sorted, $status); |
103 | my $unstable_num = 0; |
104 | |
105 | foreach $ts (@TestSizes) { |
106 | $unsorted = genarray($ts); |
107 | # Sort only on item portion of each element. |
108 | # There will typically be many repeated items, |
109 | # and their order had better be preserved. |
110 | @sorted = sort { substr($a, 0, $RootWidth) |
111 | cmp |
112 | substr($b, 0, $RootWidth) } @$unsorted; |
113 | $status = checkorder(\@sorted); |
114 | # Put the items back into the original order. |
115 | # The contents of the arrays had better be identical. |
116 | if ($expect_unstable && $status =~ /^Instability/) { |
117 | $status = ''; |
118 | ++$unstable_num; |
119 | } |
120 | is($status, '', "order ok for size $ts"); |
121 | @sorted = sort { substr($a, $RootWidth) |
122 | cmp |
123 | substr($b, $RootWidth) } @sorted; |
124 | $status = checkequal(\@sorted, $unsorted); |
125 | is($status, '', "contents ok for size $ts"); |
126 | } |
28e9118f |
127 | # P5P: The following test (#58) has been observed failing on |
128 | # a solaris 2.8 platform. Failure doesn't mean that sort is |
129 | # misbehaving, it is just exhibiting an exceedingly unlikely |
130 | # pattern of breaking ties. If you see no other failures, |
131 | # it should be perfectly safe to install. |
be6228f7 |
132 | if ($expect_unstable) { |
133 | ok($unstable_num > 0, 'Instability ok'); |
134 | } |
84d4ea48 |
135 | } |
136 | |
be6228f7 |
137 | # Test with no pragma still loaded -- stability expected (this is a mergesort) |
138 | main(0); |
139 | |
140 | # XXX We're using this eval "..." trick to force recompilation, |
141 | # to ensure that the correct pragma is enabled when main() is run. |
142 | # Currently 'use sort' modifies $^H{SORT} at compile-time, but |
143 | # pp_sort() fetches its value at run-time : thus the lexical scoping |
144 | # of %^H is of no utility. |
145 | # The order of those evals is important. |
146 | |
147 | eval q{ |
148 | use sort qw(_qsort); |
149 | is(sort::current(), 'quicksort', 'sort::current for _qsort'); |
150 | main(1); |
151 | }; |
152 | die $@ if $@; |
153 | |
154 | eval q{ |
155 | use sort qw(_mergesort); |
156 | is(sort::current(), 'mergesort', 'sort::current for _mergesort'); |
157 | main(0); |
158 | }; |
159 | die $@ if $@; |
84d4ea48 |
160 | |
be6228f7 |
161 | eval q{ |
162 | use sort qw(_qsort stable); |
163 | is(sort::current(), 'quicksort stable', 'sort::current for _qsort stable'); |
164 | main(0); |
165 | }; |
166 | die $@ if $@; |