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