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