3db280bbfd179dac89ef3ddd078560ccf8edabcd
[p5sagit/p5-mst-13.2.git] / t / op / numconvert.t
1 #!./perl
2
3 #
4 # test the conversion operators
5 #
6 # Notations:
7 #
8 # "N p i N vs N N":  Apply op-N, then op-p, then op-i, then reporter-N
9 # Compare with application of op-N, then reporter-N
10 # Right below are descriptions of different ops and reporters.
11
12 # We do not use these subroutines any more, sub overhead makes a "switch"
13 # solution better:
14
15 # obviously, 0, 1 and 2, 3 are destructive.  (XXXX 64-bit? 4 destructive too)
16
17 # *0 = sub {--$_[0]};           # -
18 # *1 = sub {++$_[0]};           # +
19
20 # # Converters
21 # *2 = sub { $_[0] = $max_uv & $_[0]}; # U
22 # *3 = sub { use integer; $_[0] += $zero}; # I
23 # *4 = sub { $_[0] += $zero};   # N
24 # *5 = sub { $_[0] = "$_[0]" }; # P
25
26 # # Side effects
27 # *6 = sub { $max_uv & $_[0]};  # u
28 # *7 = sub { use integer; $_[0] + $zero};       # i
29 # *8 = sub { $_[0] + $zero};    # n
30 # *9 = sub { $_[0] . "" };      # p
31
32 # # Reporters
33 # sub a2 { sprintf "%u", $_[0] }        # U
34 # sub a3 { sprintf "%d", $_[0] }        # I
35 # sub a4 { sprintf "%g", $_[0] }        # N
36 # sub a5 { "$_[0]" }            # P
37
38 BEGIN {
39     chdir 't' if -d 't';
40     @INC = '../lib';
41 }
42
43 use strict 'vars';
44
45 my $max_chain = $ENV{PERL_TEST_NUMCONVERTS} || 2;
46
47 # Bulk out if unsigned type is hopelessly wrong:
48 my $max_uv1 = ~0;
49 my $max_uv2 = sprintf "%u", $max_uv1 ** 6; # 6 is an arbitrary number here
50 my $big_iv = do {use integer; $max_uv1 * 16}; # 16 is an arbitrary number here
51
52 print "# max_uv1 = $max_uv1, max_uv2 = $max_uv2, big_iv = $big_iv\n";
53 if ($max_uv1 ne $max_uv2 or $big_iv > $max_uv1) {
54   print "1..0 # skipped: unsigned perl arithmetic is not sane";
55   eval { require Config; import Config };
56   use vars qw(%Config);
57   if ($Config{d_quad} eq 'define') {
58       print " (common in 64-bit platforms)";
59   }
60   print "\n";
61   exit 0;
62 }
63
64 my $st_t = 4*4;                 # We try 4 initializers and 4 reporters
65
66 my $num = 0;
67 $num += 10**$_ - 4**$_ for 1.. $max_chain;
68 $num *= $st_t;
69 print "1..$num\n";              # In fact 15 times more subsubtests...
70
71 my $max_uv = ~0;
72 my $max_iv = int($max_uv/2);
73 my $zero = 0;
74
75 my $l_uv = length $max_uv;
76 my $l_iv = length $max_iv;
77
78 # Hope: the first digits are good
79 my $larger_than_uv = substr 97 x 100, 0, $l_uv;
80 my $smaller_than_iv = substr 12 x 100, 0, $l_iv;
81 my $yet_smaller_than_iv = substr 97 x 100, 0, ($l_iv - 1);
82
83 my @list = (1, $yet_smaller_than_iv, $smaller_than_iv, $max_iv, $max_iv + 1,
84             $max_uv, $max_uv + 1);
85 unshift @list, (reverse map -$_, @list), 0; # 15 elts
86 @list = map "$_", @list; # Normalize
87
88 print "# @list\n";
89
90 # need to special case ++ for max_uv, as ++ "magic" on a string gives
91 # another string, whereas ++ magic on a string used as a number gives
92 # a number. Not a problem when NV preserves UV, but if it doesn't then
93 # stringification of the latter gives something in e notation.
94
95 my $max_uv_pp = "$max_uv"; $max_uv_pp++;
96 my $max_uv_p1 = "$max_uv"; $max_uv_p1+=0; $max_uv_p1++;
97
98 my @opnames = split //, "-+UINPuinp";
99
100 # @list = map { 2->($_), 3->($_), 4->($_), 5->($_),  } @list; # Prepare input
101
102 #print "@list\n";
103 #print "'@ops'\n";
104
105 my $test = 1;
106 my $nok;
107 for my $num_chain (1..$max_chain) {
108   my @ops = map [split //], grep /[4-9]/,
109     map { sprintf "%0${num_chain}d", $_ }  0 .. 10**$num_chain - 1;
110
111   #@ops = ([]) unless $num_chain;
112   #@ops = ([6, 4]);
113
114   # print "'@ops'\n";
115   for my $op (@ops) {
116     for my $first (2..5) {
117       for my $last (2..5) {
118         $nok = 0;
119         my @otherops = grep $_ <= 3, @$op;
120         my @curops = ($op,\@otherops);
121
122         for my $num (@list) {
123           my $inpt;
124           my @ans;
125
126           for my $short (0, 1) {
127             # undef $inpt;      # Forget all we had - some bugs were masked
128
129             $inpt = $num;       # Try to not contaminate $num...
130             $inpt = "$inpt";
131             if ($first == 2) {
132               $inpt = $max_uv & $inpt; # U 2
133             } elsif ($first == 3) {
134               use integer; $inpt += $zero; # I 3
135             } elsif ($first == 4) {
136               $inpt += $zero;   # N 4
137             } else {
138               $inpt = "$inpt";  # P 5
139             }
140
141             # Saves 20% of time - not with this logic:
142             #my $tmp = $inpt;
143             #my $tmp1 = $num;
144             #next if $num_chain > 1
145             #  and "$tmp" ne "$tmp1"; # Already the coercion gives problems...
146
147             for my $curop (@{$curops[$short]}) {
148               if ($curop < 5) {
149                 if ($curop < 3) {
150                   if ($curop == 0) {
151                     --$inpt;    # - 0
152                   } elsif ($curop == 1) {
153                     ++$inpt;    # + 1
154                   } else {
155                     $inpt = $max_uv & $inpt; # U 2
156                   }
157                 } elsif ($curop == 3) {
158                   use integer; $inpt += $zero;
159                 } else {
160                   $inpt += $zero; # N 4
161                 }
162               } elsif ($curop < 8) {
163                 if ($curop == 5) {
164                   $inpt = "$inpt"; # P 5
165                 } elsif ($curop == 6) {
166                   $max_uv & $inpt; # u 6
167                 } else {
168                   use integer; $inpt + $zero;
169                 }
170               } elsif ($curop == 8) {
171                 $inpt + $zero;  # n 8
172               } else {
173                 $inpt . "";     # p 9
174               }
175             }
176
177             if ($last == 2) {
178               $inpt = sprintf "%u", $inpt; # U 2
179             } elsif ($last == 3) {
180               $inpt = sprintf "%d", $inpt; # I 3
181             } elsif ($last == 4) {
182               $inpt = sprintf "%g", $inpt; # N 4
183             } else {
184               $inpt = "$inpt";  # P 5
185             }
186             push @ans, $inpt;
187           }
188           if ($ans[0] ne $ans[1]) {
189             print "# '$ans[0]' ne '$ans[1]',\t$num\t=> @opnames[$first,@{$curops[0]},$last] vs @opnames[$first,@{$curops[1]},$last]\n";
190             # XXX ought to check that "+" was in the list of opnames
191             if ((($ans[0] eq $max_uv_pp) and ($ans[1] eq $max_uv_p1))
192                 or (($ans[1] eq $max_uv_pp) and ($ans[0] eq $max_uv_p1))) {
193               # string ++ versus numeric ++. Tolerate this little
194               # bit of insanity
195               print "# ok, as string ++ of max_uv is \"$max_uv_pp\", numeric is $max_uv_p1\n"
196             } else {
197               $nok++,
198             }
199           }
200         }
201         print "not " if $nok;
202         print "ok $test\n";
203         #print $txt if $nok;
204         $test++;
205       }
206     }
207   }
208 }