pod2html: try to be EOL agnostic.
[p5sagit/p5-mst-13.2.git] / lib / sort.t
CommitLineData
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 6my ($BigWidth, $BigEnough, $RootWidth, $ItemFormat, @TestSizes, $WellSoaked);
7
84d4ea48 8BEGIN {
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 25use strict;
26use warnings;
27
be6228f7 28use 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
40sub 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
58sub checkorder {
59 my $aref = shift;
60 my $status = ''; # so far, so good
61 my $i;
62
63 for ($i = 0; $i < $#$aref; ++$i) {
64 next if ($aref->[$i] lt $aref->[$i+1]);
65 $status = (substr($aref->[$i], 0, $RootWidth) eq
66 substr($aref->[$i+1], 0, $RootWidth)) ?
67 "Instability" : "Disorder";
68 $status .= " at element $i between $aref->[$i] and $aref->[$i+1]";
69 last;
70 }
71 return $status;
72}
73
74
75# Verify that the two array refs reference identical arrays
76
77sub checkequal {
78 my ($aref, $bref) = @_;
79 my $status = '';
80 my $i;
81
82 if (@$aref != @$bref) {
83 $status = "Sizes differ: " . @$aref . " vs " . @$bref;
84 } else {
85 for ($i = 0; $i < @$aref; ++$i) {
86 next if ($aref->[$i] eq $bref->[$i]);
87 $status = "Element $i differs: $aref->[$i] vs $bref->[$i]";
88 last;
89 }
90 }
91 return $status;
92}
93
94
95# Test sort on arrays of various sizes (set up in @TestSizes)
96
97sub main {
98 my ($expect_unstable) = @_;
99 my ($ts, $unsorted, @sorted, $status);
100 my $unstable_num = 0;
101
102 foreach $ts (@TestSizes) {
103 $unsorted = genarray($ts);
104 # Sort only on item portion of each element.
105 # There will typically be many repeated items,
106 # and their order had better be preserved.
107 @sorted = sort { substr($a, 0, $RootWidth)
108 cmp
109 substr($b, 0, $RootWidth) } @$unsorted;
110 $status = checkorder(\@sorted);
111 # Put the items back into the original order.
112 # The contents of the arrays had better be identical.
113 if ($expect_unstable && $status =~ /^Instability/) {
114 $status = '';
115 ++$unstable_num;
116 }
117 is($status, '', "order ok for size $ts");
118 @sorted = sort { substr($a, $RootWidth)
119 cmp
120 substr($b, $RootWidth) } @sorted;
121 $status = checkequal(\@sorted, $unsorted);
122 is($status, '', "contents ok for size $ts");
123 }
124 if ($expect_unstable) {
125 ok($unstable_num > 0, 'Instability ok');
126 }
84d4ea48 127}
128
be6228f7 129# Test with no pragma still loaded -- stability expected (this is a mergesort)
130main(0);
131
132# XXX We're using this eval "..." trick to force recompilation,
133# to ensure that the correct pragma is enabled when main() is run.
134# Currently 'use sort' modifies $^H{SORT} at compile-time, but
135# pp_sort() fetches its value at run-time : thus the lexical scoping
136# of %^H is of no utility.
137# The order of those evals is important.
138
139eval q{
140 use sort qw(_qsort);
141 is(sort::current(), 'quicksort', 'sort::current for _qsort');
142 main(1);
143};
144die $@ if $@;
145
146eval q{
147 use sort qw(_mergesort);
148 is(sort::current(), 'mergesort', 'sort::current for _mergesort');
149 main(0);
150};
151die $@ if $@;
84d4ea48 152
be6228f7 153eval q{
154 use sort qw(_qsort stable);
155 is(sort::current(), 'quicksort stable', 'sort::current for _qsort stable');
156 main(0);
157};
158die $@ if $@;