Update Changes.
[p5sagit/p5-mst-13.2.git] / lib / Tie / File / t / 04_splice.t
CommitLineData
b5aed31e 1#!/usr/bin/perl
2#
3# Check SPLICE function's effect on the file
4# (07_rv_splice.t checks its return value)
5#
6# Each call to 'check_contents' actually performs two tests.
7# First, it calls the tied object's own 'check_integrity' method,
8# which makes sure that the contents of the read cache and offset tables
9# accurately reflect the contents of the file.
10# Then, it checks the actual contents of the file against the expected
11# contents.
12
b5aed31e 13my $file = "tf$$.txt";
14my $data = "rec0$/rec1$/rec2$/";
15
7b6b3db1 16print "1..101\n";
17
18init_file($data);
b5aed31e 19
20my $N = 1;
21use Tie::File;
22print "ok $N\n"; $N++; # partial credit just for showing up
23
24my $o = tie @a, 'Tie::File', $file;
25print $o ? "ok $N\n" : "not ok $N\n";
26$N++;
27
28my $n;
29
30# (3-22) splicing at the beginning
b5aed31e 31splice(@a, 0, 0, "rec4");
32check_contents("rec4$/$data");
33splice(@a, 0, 1, "rec5"); # same length
34check_contents("rec5$/$data");
35splice(@a, 0, 1, "record5"); # longer
36check_contents("record5$/$data");
37
38splice(@a, 0, 1, "r5"); # shorter
39check_contents("r5$/$data");
40splice(@a, 0, 1); # removal
41check_contents("$data");
42splice(@a, 0, 0); # no-op
43check_contents("$data");
44splice(@a, 0, 0, 'r7', 'rec8'); # insert more than one
45check_contents("r7$/rec8$/$data");
46splice(@a, 0, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
47check_contents("rec7$/record8$/rec9$/$data");
48
49splice(@a, 0, 3, 'record9', 'rec10'); # delete more than insert
50check_contents("record9$/rec10$/$data");
51splice(@a, 0, 2); # delete more than one
52check_contents("$data");
53
54
55# (23-42) splicing in the middle
56splice(@a, 1, 0, "rec4");
57check_contents("rec0$/rec4$/rec1$/rec2$/");
58splice(@a, 1, 1, "rec5"); # same length
59check_contents("rec0$/rec5$/rec1$/rec2$/");
60splice(@a, 1, 1, "record5"); # longer
61check_contents("rec0$/record5$/rec1$/rec2$/");
62
63splice(@a, 1, 1, "r5"); # shorter
64check_contents("rec0$/r5$/rec1$/rec2$/");
65splice(@a, 1, 1); # removal
66check_contents("$data");
67splice(@a, 1, 0); # no-op
68check_contents("$data");
69splice(@a, 1, 0, 'r7', 'rec8'); # insert more than one
70check_contents("rec0$/r7$/rec8$/rec1$/rec2$/");
71splice(@a, 1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
72check_contents("rec0$/rec7$/record8$/rec9$/rec1$/rec2$/");
73
74splice(@a, 1, 3, 'record9', 'rec10'); # delete more than insert
75check_contents("rec0$/record9$/rec10$/rec1$/rec2$/");
76splice(@a, 1, 2); # delete more than one
77check_contents("$data");
78
79# (43-62) splicing at the end
80splice(@a, 3, 0, "rec4");
81check_contents("$ {data}rec4$/");
82splice(@a, 3, 1, "rec5"); # same length
83check_contents("$ {data}rec5$/");
84splice(@a, 3, 1, "record5"); # longer
85check_contents("$ {data}record5$/");
86
87splice(@a, 3, 1, "r5"); # shorter
88check_contents("$ {data}r5$/");
89splice(@a, 3, 1); # removal
90check_contents("$data");
91splice(@a, 3, 0); # no-op
92check_contents("$data");
93splice(@a, 3, 0, 'r7', 'rec8'); # insert more than one
94check_contents("$ {data}r7$/rec8$/");
95splice(@a, 3, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
96check_contents("$ {data}rec7$/record8$/rec9$/");
97
98splice(@a, 3, 3, 'record9', 'rec10'); # delete more than insert
99check_contents("$ {data}record9$/rec10$/");
100splice(@a, 3, 2); # delete more than one
101check_contents("$data");
102
103# (63-82) splicing with negative subscript
104splice(@a, -1, 0, "rec4");
105check_contents("rec0$/rec1$/rec4$/rec2$/");
106splice(@a, -1, 1, "rec5"); # same length
107check_contents("rec0$/rec1$/rec4$/rec5$/");
108splice(@a, -1, 1, "record5"); # longer
109check_contents("rec0$/rec1$/rec4$/record5$/");
110
111splice(@a, -1, 1, "r5"); # shorter
112check_contents("rec0$/rec1$/rec4$/r5$/");
113splice(@a, -1, 1); # removal
114check_contents("rec0$/rec1$/rec4$/");
115splice(@a, -1, 0); # no-op
116check_contents("rec0$/rec1$/rec4$/");
117splice(@a, -1, 0, 'r7', 'rec8'); # insert more than one
118check_contents("rec0$/rec1$/r7$/rec8$/rec4$/");
119splice(@a, -1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
120check_contents("rec0$/rec1$/r7$/rec8$/rec7$/record8$/rec9$/");
121
122splice(@a, -3, 3, 'record9', 'rec10'); # delete more than insert
123check_contents("rec0$/rec1$/r7$/rec8$/record9$/rec10$/");
124splice(@a, -4, 3); # delete more than one
125check_contents("rec0$/rec1$/rec10$/");
126
127# (83-84) scrub it all out
128splice(@a, 0, 3);
129check_contents("");
130
131# (85-86) put some back in
132splice(@a, 0, 0, "rec0", "rec1");
133check_contents("rec0$/rec1$/");
134
135# (87-88) what if we remove too many records?
136splice(@a, 0, 17);
137check_contents("");
138
51efdd02 139# (89-92) In the past, splicing past the end was not correctly detected
140# (1.14)
141splice(@a, 89, 3);
142check_contents("");
143splice(@a, @a, 3);
144check_contents("");
145
146# (93-96) Also we did not emulate splice's freaky behavior when inserting
147# past the end of the array (1.14)
148splice(@a, 89, 0, "I", "like", "pie");
149check_contents("I$/like$/pie$/");
150splice(@a, 89, 0, "pie pie pie");
151check_contents("I$/like$/pie$/pie pie pie$/");
152
153# (97) Splicing with too large a negative number should be fatal
154# This test ignored because it causes 5.6.1 and 5.7.2 to dump core
155# NOT MY FAULT
fa408a35 156if ($] < 5.006 || $] > 5.007003) {
51efdd02 157 eval { splice(@a, -7, 0) };
158 print $@ =~ /^Modification of non-creatable array value attempted, subscript -7/
159 ? "ok $N\n" : "not ok $N \# \$\@ was '$@'\n";
160} else {
fa408a35 161 print "ok $N \# skipped (5.6.0 through 5.7.3 dump core here.)\n";
51efdd02 162}
163$N++;
164
7b6b3db1 165# (98-101) Test default arguments
166splice @a, 0, 0, (0..11);
167splice @a, 4;
168check_contents("0$/1$/2$/3$/");
169splice @a;
170check_contents("");
51efdd02 171
172
b5aed31e 173sub init_file {
174 my $data = shift;
175 open F, "> $file" or die $!;
1768807e 176 binmode F;
b5aed31e 177 print F $data;
178 close F;
179}
180
7b6b3db1 181use POSIX 'SEEK_SET';
b5aed31e 182sub check_contents {
183 my $x = shift;
b5aed31e 184 my $integrity = $o->_check_integrity($file, $ENV{INTEGRITY});
7b6b3db1 185 local *FH = $o->{fh};
186 seek FH, 0, SEEK_SET;
b5aed31e 187 print $integrity ? "ok $N\n" : "not ok $N\n";
188 $N++;
b5aed31e 189 my $a;
190 { local $/; $a = <FH> }
7b6b3db1 191 $a = "" unless defined $a;
192 if ($a eq $x) {
193 print "ok $N\n";
194 } else {
195 s{$/}{\\n}g for $a, $x;
196 print "not ok $N\n# expected <$x>, got <$a>\n";
197 }
b5aed31e 198 $N++;
199}
200
201END {
7b6b3db1 202 undef $o;
203 untie @a;
b5aed31e 204 1 while unlink $file;
205}
206