This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to Tie::File 0.13 (Win32 fixes.)
[perl5.git] / lib / Tie / File / 10_splice_rs.t
CommitLineData
b5aed31e
AMS
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
13my $file = "tf$$.txt";
14my $data = "rec0blahrec1blahrec2blah";
15
16print "1..88\n";
17
18my $N = 1;
19use Tie::File;
20print "ok $N\n"; $N++; # partial credit just for showing up
21
22my $o = tie @a, 'Tie::File', $file, recsep => 'blah';
23print $o ? "ok $N\n" : "not ok $N\n";
24$N++;
25
26my $n;
27
28# (3-22) splicing at the beginning
29init_file($data);
30
31splice(@a, 0, 0, "rec4");
32check_contents("rec4blah$data");
33splice(@a, 0, 1, "rec5"); # same length
34check_contents("rec5blah$data");
35splice(@a, 0, 1, "record5"); # longer
36check_contents("record5blah$data");
37
38splice(@a, 0, 1, "r5"); # shorter
39check_contents("r5blah$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("r7blahrec8blah$data");
46splice(@a, 0, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
47check_contents("rec7blahrecord8blahrec9blah$data");
48
49splice(@a, 0, 3, 'record9', 'rec10'); # delete more than insert
50check_contents("record9blahrec10blah$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("rec0blahrec4blahrec1blahrec2blah");
58splice(@a, 1, 1, "rec5"); # same length
59check_contents("rec0blahrec5blahrec1blahrec2blah");
60splice(@a, 1, 1, "record5"); # longer
61check_contents("rec0blahrecord5blahrec1blahrec2blah");
62
63splice(@a, 1, 1, "r5"); # shorter
64check_contents("rec0blahr5blahrec1blahrec2blah");
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("rec0blahr7blahrec8blahrec1blahrec2blah");
71splice(@a, 1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
72check_contents("rec0blahrec7blahrecord8blahrec9blahrec1blahrec2blah");
73
74splice(@a, 1, 3, 'record9', 'rec10'); # delete more than insert
75check_contents("rec0blahrecord9blahrec10blahrec1blahrec2blah");
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}rec4blah");
82splice(@a, 3, 1, "rec5"); # same length
83check_contents("$ {data}rec5blah");
84splice(@a, 3, 1, "record5"); # longer
85check_contents("$ {data}record5blah");
86
87splice(@a, 3, 1, "r5"); # shorter
88check_contents("$ {data}r5blah");
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}r7blahrec8blah");
95splice(@a, 3, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
96check_contents("$ {data}rec7blahrecord8blahrec9blah");
97
98splice(@a, 3, 3, 'record9', 'rec10'); # delete more than insert
99check_contents("$ {data}record9blahrec10blah");
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("rec0blahrec1blahrec4blahrec2blah");
106splice(@a, -1, 1, "rec5"); # same length
107check_contents("rec0blahrec1blahrec4blahrec5blah");
108splice(@a, -1, 1, "record5"); # longer
109check_contents("rec0blahrec1blahrec4blahrecord5blah");
110
111splice(@a, -1, 1, "r5"); # shorter
112check_contents("rec0blahrec1blahrec4blahr5blah");
113splice(@a, -1, 1); # removal
114check_contents("rec0blahrec1blahrec4blah");
115splice(@a, -1, 0); # no-op
116check_contents("rec0blahrec1blahrec4blah");
117splice(@a, -1, 0, 'r7', 'rec8'); # insert more than one
118check_contents("rec0blahrec1blahr7blahrec8blahrec4blah");
119splice(@a, -1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
120check_contents("rec0blahrec1blahr7blahrec8blahrec7blahrecord8blahrec9blah");
121
122splice(@a, -3, 3, 'record9', 'rec10'); # delete more than insert
123check_contents("rec0blahrec1blahr7blahrec8blahrecord9blahrec10blah");
124splice(@a, -4, 3); # delete more than one
125check_contents("rec0blahrec1blahrec10blah");
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("rec0blahrec1blah");
134
135# (87-88) what if we remove too many records?
136splice(@a, 0, 17);
137check_contents("");
138
139sub init_file {
140 my $data = shift;
141 open F, "> $file" or die $!;
1768807e 142 binmode F;
b5aed31e
AMS
143 print F $data;
144 close F;
145}
146
147sub check_contents {
148 my $x = shift;
149 local *FH;
150 my $integrity = $o->_check_integrity($file, $ENV{INTEGRITY});
151 print $integrity ? "ok $N\n" : "not ok $N\n";
152 $N++;
153 my $open = open FH, "< $file";
154 my $a;
155 { local $/; $a = <FH> }
156 print (($open && $a eq $x) ? "ok $N\n" : "not ok $N\n");
157 $N++;
158}
159
160END {
161 1 while unlink $file;
162}
163