Bind op fix.
[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\n# Unsigned arithmetic is not sane\n";
55   exit 0;
56 }
57
58 my $st_t = 4*4;                 # We try 4 initializers and 4 reporters
59
60 my $num = 0;
61 $num += 10**$_ - 4**$_ for 1.. $max_chain;
62 $num *= $st_t;
63 print "1..$num\n";              # In fact 15 times more subsubtests...
64
65 my $max_uv = ~0;
66 my $max_iv = int($max_uv/2);
67 my $zero = 0;
68
69 my $l_uv = length $max_uv;
70 my $l_iv = length $max_iv;
71
72 # Hope: the first digits are good
73 my $larger_than_uv = substr 97 x 100, 0, $l_uv;
74 my $smaller_than_iv = substr 12 x 100, 0, $l_iv;
75 my $yet_smaller_than_iv = substr 97 x 100, 0, ($l_iv - 1);
76
77 my @list = (1, $yet_smaller_than_iv, $smaller_than_iv, $max_iv, $max_iv + 1,
78             $max_uv, $max_uv + 1);
79 unshift @list, (reverse map -$_, @list), 0; # 15 elts
80 @list = map "$_", @list; # Normalize
81
82 # print "@list\n";
83
84
85 my @opnames = split //, "-+UINPuinp";
86
87 # @list = map { 2->($_), 3->($_), 4->($_), 5->($_),  } @list; # Prepare input
88
89 #print "@list\n";
90 #print "'@ops'\n";
91
92 my $test = 1;
93 my $nok;
94 for my $num_chain (1..$max_chain) {
95   my @ops = map [split //], grep /[4-9]/,
96     map { sprintf "%0${num_chain}d", $_ }  0 .. 10**$num_chain - 1;
97
98   #@ops = ([]) unless $num_chain;
99   #@ops = ([6, 4]);
100
101   # print "'@ops'\n";
102   for my $op (@ops) {
103     for my $first (2..5) {
104       for my $last (2..5) {
105         $nok = 0;
106         my @otherops = grep $_ <= 3, @$op;
107         my @curops = ($op,\@otherops);
108
109         for my $num (@list) {
110           my $inpt;
111           my @ans;
112
113           for my $short (0, 1) {
114             # undef $inpt;      # Forget all we had - some bugs were masked
115
116             $inpt = $num;       # Try to not contaminate $num...
117             $inpt = "$inpt";
118             if ($first == 2) {
119               $inpt = $max_uv & $inpt; # U 2
120             } elsif ($first == 3) {
121               use integer; $inpt += $zero; # I 3
122             } elsif ($first == 4) {
123               $inpt += $zero;   # N 4
124             } else {
125               $inpt = "$inpt";  # P 5
126             }
127
128             # Saves 20% of time - not with this logic:
129             #my $tmp = $inpt;
130             #my $tmp1 = $num;
131             #next if $num_chain > 1
132             #  and "$tmp" ne "$tmp1"; # Already the coercion gives problems...
133
134             for my $curop (@{$curops[$short]}) {
135               if ($curop < 5) {
136                 if ($curop < 3) {
137                   if ($curop == 0) {
138                     --$inpt;    # - 0
139                   } elsif ($curop == 1) {
140                     ++$inpt;    # + 1
141                   } else {
142                     $inpt = $max_uv & $inpt; # U 2
143                   }
144                 } elsif ($curop == 3) {
145                   use integer; $inpt += $zero;
146                 } else {
147                   $inpt += $zero; # N 4
148                 }
149               } elsif ($curop < 8) {
150                 if ($curop == 5) {
151                   $inpt = "$inpt"; # P 5
152                 } elsif ($curop == 6) {
153                   $max_uv & $inpt; # u 6
154                 } else {
155                   use integer; $inpt + $zero;
156                 }
157               } elsif ($curop == 8) {
158                 $inpt + $zero;  # n 8
159               } else {
160                 $inpt . "";     # p 9
161               }
162             }
163
164             if ($last == 2) {
165               $inpt = sprintf "%u", $inpt; # U 2
166             } elsif ($last == 3) {
167               $inpt = sprintf "%d", $inpt; # I 3
168             } elsif ($last == 4) {
169               $inpt = sprintf "%g", $inpt; # N 4
170             } else {
171               $inpt = "$inpt";  # P 5
172             }
173             push @ans, $inpt;
174           }
175           $nok++,
176             print "# '$ans[0]' ne '$ans[1]',\t$num\t=> @opnames[$first,@{$curops[0]},$last] vs @opnames[$first,@{$curops[1]},$last]\n"
177               if $ans[0] ne $ans[1];
178         }
179         print "not " if $nok;
180         print "ok $test\n";
181         #print $txt if $nok;
182         $test++;
183       }
184     }
185   }
186 }