[shell changes from patch from perl5.003_16 to perl5.003_17]
[p5sagit/p5-mst-13.2.git] / t / op / overload.t
CommitLineData
748a9306 1#!./perl
2
d1f8c7a4 3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
748a9306 6}
7
d1f8c7a4 8use Config;
9
748a9306 10package Oscalar;
4633a7c4 11use overload (
748a9306 12 # Anonymous subroutines:
4633a7c4 13'+' => sub {new Oscalar $ {$_[0]}+$_[1]},
748a9306 14'-' => sub {new Oscalar
15 $_[2]? $_[1]-${$_[0]} : ${$_[0]}-$_[1]},
16'<=>' => sub {new Oscalar
17 $_[2]? $_[1]-${$_[0]} : ${$_[0]}-$_[1]},
18'cmp' => sub {new Oscalar
19 $_[2]? ($_[1] cmp ${$_[0]}) : (${$_[0]} cmp $_[1])},
20'*' => sub {new Oscalar ${$_[0]}*$_[1]},
21'/' => sub {new Oscalar
22 $_[2]? $_[1]/${$_[0]} :
23 ${$_[0]}/$_[1]},
24'%' => sub {new Oscalar
25 $_[2]? $_[1]%${$_[0]} : ${$_[0]}%$_[1]},
26'**' => sub {new Oscalar
27 $_[2]? $_[1]**${$_[0]} : ${$_[0]}-$_[1]},
28
29qw(
30"" stringify
310+ numify) # Order of arguments unsignificant
32);
33
34sub new {
35 my $foo = $_[1];
36 bless \$foo;
37}
38
39sub stringify { "${$_[0]}" }
40sub numify { 0 + "${$_[0]}" } # Not needed, additional overhead
41 # comparing to direct compilation based on
42 # stringify
43
44package main;
45
46$test = 0;
47$| = 1;
48print "1..",&last,"\n";
49
50sub test {
51 $test++; if (shift) {print "ok $test\n";1} else {print "not ok $test\n";0}
52}
53
54$a = new Oscalar "087";
55$b= "$a";
56
4633a7c4 57# All test numbers in comments are off by 1.
58# So much for hard-wiring them in :-)
748a9306 59test ($b eq $a); # 2
60test ($b eq "087"); # 3
61test (ref $a eq "Oscalar"); # 4
62test ($a eq $a); # 5
63test ($a eq "087"); # 6
64
65$c = $a + 7;
66
67test (ref $c eq "Oscalar"); # 7
68test (!($c eq $a)); # 8
69test ($c eq "94"); # 9
70
71$b=$a;
72
73test (ref $a eq "Oscalar"); # 10
74
75$b++;
76
77test (ref $b eq "Oscalar"); # 11
78test ( $a eq "087"); # 12
79test ( $b eq "88"); # 13
80test (ref $a eq "Oscalar"); # 14
81
82$c=$b;
83$c-=$a;
84
85test (ref $c eq "Oscalar"); # 15
86test ( $a eq "087"); # 16
87test ( $c eq "1"); # 17
88test (ref $a eq "Oscalar"); # 18
89
90$b=1;
91$b+=$a;
92
93test (ref $b eq "Oscalar"); # 19
94test ( $a eq "087"); # 20
95test ( $b eq "88"); # 21
96test (ref $a eq "Oscalar"); # 22
97
4633a7c4 98eval q[ package Oscalar; use overload ('++' => sub { $ {$_[0]}++;$_[0] } ) ];
748a9306 99
100$b=$a;
101
102test (ref $a eq "Oscalar"); # 23
103
104$b++;
105
106test (ref $b eq "Oscalar"); # 24
107test ( $a eq "087"); # 25
108test ( $b eq "88"); # 26
109test (ref $a eq "Oscalar"); # 27
110
111package Oscalar;
112$dummy=bless \$dummy; # Now cache of method should be reloaded
113package main;
114
115$b=$a;
116$b++;
117
118test (ref $b eq "Oscalar"); # 28
119test ( $a eq "087"); # 29
120test ( $b eq "88"); # 30
121test (ref $a eq "Oscalar"); # 31
122
123
4633a7c4 124eval q[package Oscalar; use overload ('++' => sub { $ {$_[0]} += 2; $_[0] } ) ];
748a9306 125
126$b=$a;
127
128test (ref $a eq "Oscalar"); # 32
129
130$b++;
131
132test (ref $b eq "Oscalar"); # 33
133test ( $a eq "087"); # 34
134test ( $b eq "88"); # 35
135test (ref $a eq "Oscalar"); # 36
136
137package Oscalar;
138$dummy=bless \$dummy; # Now cache of method should be reloaded
139package main;
140
141$b++;
142
143test (ref $b eq "Oscalar"); # 37
144test ( $a eq "087"); # 38
145test ( $b eq "90"); # 39
146test (ref $a eq "Oscalar"); # 40
147
148$b=$a;
149$b++;
150
151test (ref $b eq "Oscalar"); # 41
152test ( $a eq "087"); # 42
153test ( $b eq "89"); # 43
154test (ref $a eq "Oscalar"); # 44
155
156
157test ($b? 1:0); # 45
158
4633a7c4 159eval q[ package Oscalar; use overload ('=' => sub {$main::copies++;
160 package Oscalar;
161 local $new=$ {$_[0]};
162 bless \$new } ) ];
748a9306 163
164$b=new Oscalar "$a";
165
166test (ref $b eq "Oscalar"); # 46
167test ( $a eq "087"); # 47
168test ( $b eq "087"); # 48
169test (ref $a eq "Oscalar"); # 49
170
171$b++;
172
173test (ref $b eq "Oscalar"); # 50
174test ( $a eq "087"); # 51
175test ( $b eq "89"); # 52
176test (ref $a eq "Oscalar"); # 53
177test ($copies == 0); # 54
178
179$b+=1;
180
181test (ref $b eq "Oscalar"); # 55
182test ( $a eq "087"); # 56
183test ( $b eq "90"); # 57
184test (ref $a eq "Oscalar"); # 58
185test ($copies == 0); # 59
186
187$b=$a;
188$b+=1;
189
190test (ref $b eq "Oscalar"); # 60
191test ( $a eq "087"); # 61
192test ( $b eq "88"); # 62
193test (ref $a eq "Oscalar"); # 63
194test ($copies == 0); # 64
195
196$b=$a;
197$b++;
198
199test (ref $b eq "Oscalar") || print ref $b,"=ref(b)\n"; # 65
200test ( $a eq "087"); # 66
201test ( $b eq "89"); # 67
202test (ref $a eq "Oscalar"); # 68
203test ($copies == 1); # 69
204
4633a7c4 205eval q[package Oscalar; use overload ('+=' => sub {$ {$_[0]} += 3*$_[1];
206 $_[0] } ) ];
748a9306 207$c=new Oscalar; # Cause rehash
208
209$b=$a;
210$b+=1;
211
212test (ref $b eq "Oscalar"); # 70
213test ( $a eq "087"); # 71
214test ( $b eq "90"); # 72
215test (ref $a eq "Oscalar"); # 73
216test ($copies == 2); # 74
217
218$b+=$b;
219
220test (ref $b eq "Oscalar"); # 75
221test ( $b eq "360"); # 76
222test ($copies == 2); # 77
223$b=-$b;
224
225test (ref $b eq "Oscalar"); # 78
226test ( $b eq "-360"); # 79
227test ($copies == 2); # 80
228
229$b=abs($b);
230
231test (ref $b eq "Oscalar"); # 81
232test ( $b eq "360"); # 82
233test ($copies == 2); # 83
234
235$b=abs($b);
236
237test (ref $b eq "Oscalar"); # 84
238test ( $b eq "360"); # 85
239test ($copies == 2); # 86
240
4633a7c4 241eval q[package Oscalar;
242 use overload ('x' => sub {new Oscalar ( $_[2] ? "_.$_[1]._" x $ {$_[0]}
243 : "_.${$_[0]}._" x $_[1])}) ];
748a9306 244
245$a=new Oscalar "yy";
246$a x= 3;
247test ($a eq "_.yy.__.yy.__.yy._"); # 87
248
4633a7c4 249eval q[package Oscalar;
250 use overload ('.' => sub {new Oscalar ( $_[2] ?
251 "_.$_[1].__.$ {$_[0]}._"
252 : "_.$ {$_[0]}.__.$_[1]._")}) ];
748a9306 253
254$a=new Oscalar "xx";
255
256test ("b${a}c" eq "_._.b.__.xx._.__.c._"); # 88
257
258# Here we test blessing to a package updates hash
259
4633a7c4 260eval "package Oscalar; no overload '.'";
748a9306 261
262test ("b${a}" eq "_.b.__.xx._"); # 89
263$x="1";
264bless \$x, Oscalar;
4633a7c4 265test ("b${a}c" eq "bxxc"); # 90
748a9306 266new Oscalar 1;
4633a7c4 267test ("b${a}c" eq "bxxc"); # 91
748a9306 268
4633a7c4 269# Last test is number 90.
270sub last {90}