tiearray tweaks
[p5sagit/p5-mst-13.2.git] / t / op / tiearray.t
CommitLineData
93965878 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7
8my %seen;
9
10package Implement;
11
12sub TIEARRAY
13{
14 $seen{'TIEARRAY'}++;
15 my ($class,@val) = @_;
16 return bless \@val,$class;
17}
18
19sub STORESIZE
20{
21 $seen{'STORESIZE'}++;
22 my ($ob,$sz) = @_;
23 return @$ob = $sz;
24}
25
26sub EXTEND
27{
28 $seen{'EXTEND'}++;
29 my ($ob,$sz) = @_;
30 return @$ob = $sz;
31}
32
33sub FETCHSIZE
34{
35 $seen{'FETCHSIZE'}++;
36 my ($ob) = @_;
37 return @$ob-1;
38}
39
40sub FETCH
41{
42 $seen{'FETCH'}++;
43 my ($ob,$id) = @_;
44 return $ob->[$id];
45}
46
47sub STORE
48{
49 $seen{'STORE'}++;
50 my ($ob,$id,$val) = @_;
51 $ob->[$id] = $val;
52}
53
54sub UNSHIFT
55{
56 $seen{'UNSHIFT'}++;
57 $ob = shift;
58 unshift(@$ob,@_);
59}
60
61sub PUSH
62{
63 $seen{'PUSH'}++;
64 my $ob = shift;;
65 push(@$ob,@_);
66}
67
68sub CLEAR
69{
70 $seen{'CLEAR'}++;
71}
72
73sub POP
74{
75 $seen{'POP'}++;
76 my ($ob) = @_;
77 return pop(@$ob);
78}
79
80sub SHIFT
81{
82 $seen{'SHIFT'}++;
83 my ($ob) = @_;
84 return shift(@$ob);
85}
86
87sub SPLICE
88{
89 $seen{'SPLICE'}++;
90 my $ob = shift;
91 my $off = @_ ? shift : 0;
92 my $len = @_ ? shift : @$ob-1;
93 return splice(@$ob,$off,$len,@_);
94}
95
96package main;
97
98print "1..23\n";
99my $test = 1;
100
101{my @ary;
102
103{ my $ob = tie @ary,'Implement',3,2,1;
104 print "not " unless $ob;
105 print "ok ", $test++,"\n";
106 print "not " unless tied(@ary) == $ob;
107 print "ok ", $test++,"\n";
108}
109
110
111print "not " unless @ary == 3;
112print "ok ", $test++,"\n";
113
114print "not " unless $#ary == 2;
115print "ok ", $test++,"\n";
116
117print "not " unless join(':',@ary) eq '3:2:1';
118print "ok ", $test++,"\n";
119
120print "not " unless $seen{'FETCH'} >= 3;
121print "ok ", $test++,"\n";
122
123@ary = (1,2,3);
124
125print "not " unless $seen{'STORE'} >= 3;
126print "ok ", $test++,"\n";
127
128print "not " unless join(':',@ary) eq '1:2:3';
129print "ok ", $test++,"\n";
130
131print "not " unless pop(@ary) == 3;
132print "ok ", $test++,"\n";
133print "not " unless $seen{'POP'} == 1;
134print "ok ", $test++,"\n";
135print "not " unless join(':',@ary) eq '1:2';
136print "ok ", $test++,"\n";
137
138push(@ary,4);
139print "not " unless $seen{'PUSH'} == 1;
140print "ok ", $test++,"\n";
141print "not " unless join(':',@ary) eq '1:2:4';
142print "ok ", $test++,"\n";
143
144my @x = splice(@ary,1,1,7);
145
146
147print "not " unless $seen{'SPLICE'} == 1;
148print "ok ", $test++,"\n";
149
150print "not " unless @x == 1;
151print "ok ", $test++,"\n";
152print "not " unless $x[0] == 2;
153print "ok ", $test++,"\n";
154print "not " unless join(':',@ary) eq '1:7:4';
155print "ok ", $test++,"\n";
156
157
158
159print "not " unless shift(@ary) == 1;
160print "ok ", $test++,"\n";
161print "not " unless $seen{'SHIFT'} == 1;
162print "ok ", $test++,"\n";
163print "not " unless join(':',@ary) eq '7:4';
164print "ok ", $test++,"\n";
165
166
167unshift(@ary,5);
168print "not " unless $seen{'UNSHIFT'} == 1;
169print "ok ", $test++,"\n";
170print "not " unless join(':',@ary) eq '5:7:4';
171print "ok ", $test++,"\n";
172
173@ary = split(/:/,'1:2:3');
174print "not " unless join(':',@ary) eq '1:2:3';
175print "ok ", $test++,"\n";
176
1393e206 177# untie @ary;
93965878 178
179}
180
181
182
183