1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
#!./perl
BEGIN {
chdir 't' if -d 't';
@INC = '../lib';
require Config;
if ($Config::Config{'uvsize'} != 8) {
print "1..0 # Skip -- Perl configured with 32-bit ints\n";
exit 0;
}
}
$| = 1;
use Test::More 'tests' => 100;
my $ii = 36028797018963971; # 2^55 + 3
### Tests with numerifying large positive int
{ package Oobj;
use overload '0+' => sub { ${$_[0]} += 1; $ii },
'fallback' => 1;
}
my $oo = bless(\do{my $x = 0}, 'Oobj');
my $cnt = 1;
is("$oo", "$ii", '0+ overload with stringification');
is($$oo, $cnt++, 'overload called once');
is($oo>>3, $ii>>3, '0+ overload with bit shift right');
is($$oo, $cnt++, 'overload called once');
is($oo<<2, $ii<<2, '0+ overload with bit shift left');
is($$oo, $cnt++, 'overload called once');
is($oo|0xFF00, $ii|0xFF00, '0+ overload with bitwise or');
is($$oo, $cnt++, 'overload called once');
is($oo&0xFF03, $ii&0xFF03, '0+ overload with bitwise and');
is($$oo, $cnt++, 'overload called once');
ok($oo == $ii, '0+ overload with equality');
is($$oo, $cnt++, 'overload called once');
is(int($oo), $ii, '0+ overload with int()');
is($$oo, $cnt++, 'overload called once');
is(abs($oo), $ii, '0+ overload with abs()');
is($$oo, $cnt++, 'overload called once');
is(-$oo, -$ii, '0+ overload with unary minus');
is($$oo, $cnt++, 'overload called once');
is(0+$oo, $ii, '0+ overload with addition');
is($$oo, $cnt++, 'overload called once');
is($oo+0, $ii, '0+ overload with addition');
is($$oo, $cnt++, 'overload called once');
is($oo+$oo, 2*$ii, '0+ overload with addition');
$cnt++;
is($$oo, $cnt++, 'overload called once');
is(0-$oo, -$ii, '0+ overload with subtraction');
is($$oo, $cnt++, 'overload called once');
is($oo-99, $ii-99, '0+ overload with subtraction');
is($$oo, $cnt++, 'overload called once');
is(2*$oo, 2*$ii, '0+ overload with multiplication');
is($$oo, $cnt++, 'overload called once');
is($oo*3, 3*$ii, '0+ overload with multiplication');
is($$oo, $cnt++, 'overload called once');
is($oo/1, $ii, '0+ overload with division');
is($$oo, $cnt++, 'overload called once');
is($ii/$oo, 1, '0+ overload with division');
is($$oo, $cnt++, 'overload called once');
is($oo%100, $ii%100, '0+ overload with modulo');
is($$oo, $cnt++, 'overload called once');
is($ii%$oo, 0, '0+ overload with modulo');
is($$oo, $cnt++, 'overload called once');
is($oo**1, $ii, '0+ overload with exponentiation');
is($$oo, $cnt++, 'overload called once');
### Tests with numerifying large negative int
{ package Oobj2;
use overload '0+' => sub { ${$_[0]} += 1; -$ii },
'fallback' => 1;
}
$oo = bless(\do{my $x = 0}, 'Oobj2');
$cnt = 1;
is(int($oo), -$ii, '0+ overload with int()');
is($$oo, $cnt++, 'overload called once');
is(abs($oo), $ii, '0+ overload with abs()');
is($$oo, $cnt++, 'overload called once');
is(-$oo, $ii, '0+ overload with unary -');
is($$oo, $cnt++, 'overload called once');
is(0+$oo, -$ii, '0+ overload with addition');
is($$oo, $cnt++, 'overload called once');
is($oo+0, -$ii, '0+ overload with addition');
is($$oo, $cnt++, 'overload called once');
is($oo+$oo, -2*$ii, '0+ overload with addition');
$cnt++;
is($$oo, $cnt++, 'overload called once');
is(0-$oo, $ii, '0+ overload with subtraction');
is($$oo, $cnt++, 'overload called once');
is(2*$oo, -2*$ii, '0+ overload with multiplication');
is($$oo, $cnt++, 'overload called once');
is($oo*3, -3*$ii, '0+ overload with multiplication');
is($$oo, $cnt++, 'overload called once');
is($oo/1, -$ii, '0+ overload with division');
is($$oo, $cnt++, 'overload called once');
is($ii/$oo, -1, '0+ overload with division');
is($$oo, $cnt++, 'overload called once');
is($oo%100, (-$ii)%100, '0+ overload with modulo');
is($$oo, $cnt++, 'overload called once');
is($ii%$oo, 0, '0+ overload with modulo');
is($$oo, $cnt++, 'overload called once');
is($oo**1, -$ii, '0+ overload with exponentiation');
is($$oo, $cnt++, 'overload called once');
### Tests with overloading but no fallback
{ package Oobj3;
use overload
'int' => sub { ${$_[0]} += 1; $ii },
'abs' => sub { ${$_[0]} += 1; $ii },
'neg' => sub { ${$_[0]} += 1; -$ii },
'+' => sub {
${$_[0]} += 1;
my $res = (ref($_[0]) eq __PACKAGE__) ? $ii : $_[0];
$res += (ref($_[1]) eq __PACKAGE__) ? $ii : $_[1];
},
'-' => sub {
${$_[0]} += 1;
my ($l, $r) = ($_[2]) ? (1, 0) : (0, 1);
my $res = (ref($_[$l]) eq __PACKAGE__) ? $ii : $_[$l];
$res -= (ref($_[$r]) eq __PACKAGE__) ? $ii : $_[$r];
},
'*' => sub {
${$_[0]} += 1;
my $res = (ref($_[0]) eq __PACKAGE__) ? $ii : $_[0];
$res *= (ref($_[1]) eq __PACKAGE__) ? $ii : $_[1];
},
'/' => sub {
${$_[0]} += 1;
my ($l, $r) = ($_[2]) ? (1, 0) : (0, 1);
my $res = (ref($_[$l]) eq __PACKAGE__) ? $ii+1 : $_[$l];
$res /= (ref($_[$r]) eq __PACKAGE__) ? $ii+1 : $_[$r];
},
'%' => sub {
${$_[0]} += 1;
my ($l, $r) = ($_[2]) ? (1, 0) : (0, 1);
my $res = (ref($_[$l]) eq __PACKAGE__) ? $ii : $_[$l];
$res %= (ref($_[$r]) eq __PACKAGE__) ? $ii : $_[$r];
},
'**' => sub {
${$_[0]} += 1;
my ($l, $r) = ($_[2]) ? (1, 0) : (0, 1);
my $res = (ref($_[$l]) eq __PACKAGE__) ? $ii : $_[$l];
$res **= (ref($_[$r]) eq __PACKAGE__) ? $ii : $_[$r];
},
}
$oo = bless(\do{my $x = 0}, 'Oobj3');
$cnt = 1;
is(int($oo), $ii, 'int() overload');
is($$oo, $cnt++, 'overload called once');
is(abs($oo), $ii, 'abs() overload');
is($$oo, $cnt++, 'overload called once');
is(-$oo, -$ii, 'neg overload');
is($$oo, $cnt++, 'overload called once');
is(0+$oo, $ii, '+ overload');
is($$oo, $cnt++, 'overload called once');
is($oo+0, $ii, '+ overload');
is($$oo, $cnt++, 'overload called once');
is($oo+$oo, 2*$ii, '+ overload');
is($$oo, $cnt++, 'overload called once');
is(0-$oo, -$ii, '- overload');
is($$oo, $cnt++, 'overload called once');
is($oo-99, $ii-99, '- overload');
is($$oo, $cnt++, 'overload called once');
is($oo*2, 2*$ii, '* overload');
is($$oo, $cnt++, 'overload called once');
is(-3*$oo, -3*$ii, '* overload');
is($$oo, $cnt++, 'overload called once');
is($oo/2, ($ii+1)/2, '/ overload');
is($$oo, $cnt++, 'overload called once');
is(($ii+1)/$oo, 1, '/ overload');
is($$oo, $cnt++, 'overload called once');
is($oo%100, $ii%100, '% overload');
is($$oo, $cnt++, 'overload called once');
is($ii%$oo, 0, '% overload');
is($$oo, $cnt++, 'overload called once');
is($oo**1, $ii, '** overload');
is($$oo, $cnt++, 'overload called once');
# EOF
|