4 # Check SPLICE function's effect on the file
5 # (07_rv_splice.t checks its return value)
7 # Each call to 'check_contents' actually performs two tests.
8 # First, it calls the tied object's own 'check_integrity' method,
9 # which makes sure that the contents of the read cache and offset tables
10 # accurately reflect the contents of the file.
11 # Then, it checks the actual contents of the file against the expected
16 my $file = "tf$$.txt";
17 $: = Tie::File::_default_recsep();
18 my $data = "rec0$:rec1$:rec2$:";
25 print "ok $N\n"; $N++; # partial credit just for showing up
27 my $o = tie @a, 'Tie::File', $file;
28 print $o ? "ok $N\n" : "not ok $N\n";
34 # (3-22) splicing at the beginning
35 splice(@a, 0, 0, "rec4");
36 check_contents("rec4$:$data");
37 splice(@a, 0, 1, "rec5"); # same length
38 check_contents("rec5$:$data");
39 splice(@a, 0, 1, "record5"); # longer
40 check_contents("record5$:$data");
42 splice(@a, 0, 1, "r5"); # shorter
43 check_contents("r5$:$data");
44 splice(@a, 0, 1); # removal
45 check_contents("$data");
46 splice(@a, 0, 0); # no-op
47 check_contents("$data");
48 splice(@a, 0, 0, 'r7', 'rec8'); # insert more than one
49 check_contents("r7$:rec8$:$data");
50 splice(@a, 0, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
51 check_contents("rec7$:record8$:rec9$:$data");
53 splice(@a, 0, 3, 'record9', 'rec10'); # delete more than insert
54 check_contents("record9$:rec10$:$data");
55 splice(@a, 0, 2); # delete more than one
56 check_contents("$data");
59 # (23-42) splicing in the middle
60 splice(@a, 1, 0, "rec4");
61 check_contents("rec0$:rec4$:rec1$:rec2$:");
62 splice(@a, 1, 1, "rec5"); # same length
63 check_contents("rec0$:rec5$:rec1$:rec2$:");
64 splice(@a, 1, 1, "record5"); # longer
65 check_contents("rec0$:record5$:rec1$:rec2$:");
67 splice(@a, 1, 1, "r5"); # shorter
68 check_contents("rec0$:r5$:rec1$:rec2$:");
69 splice(@a, 1, 1); # removal
70 check_contents("$data");
71 splice(@a, 1, 0); # no-op
72 check_contents("$data");
73 splice(@a, 1, 0, 'r7', 'rec8'); # insert more than one
74 check_contents("rec0$:r7$:rec8$:rec1$:rec2$:");
75 splice(@a, 1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
76 check_contents("rec0$:rec7$:record8$:rec9$:rec1$:rec2$:");
78 splice(@a, 1, 3, 'record9', 'rec10'); # delete more than insert
79 check_contents("rec0$:record9$:rec10$:rec1$:rec2$:");
80 splice(@a, 1, 2); # delete more than one
81 check_contents("$data");
83 # (43-62) splicing at the end
84 splice(@a, 3, 0, "rec4");
85 check_contents("$ {data}rec4$:");
86 splice(@a, 3, 1, "rec5"); # same length
87 check_contents("$ {data}rec5$:");
88 splice(@a, 3, 1, "record5"); # longer
89 check_contents("$ {data}record5$:");
91 splice(@a, 3, 1, "r5"); # shorter
92 check_contents("$ {data}r5$:");
93 splice(@a, 3, 1); # removal
94 check_contents("$data");
95 splice(@a, 3, 0); # no-op
96 check_contents("$data");
97 splice(@a, 3, 0, 'r7', 'rec8'); # insert more than one
98 check_contents("$ {data}r7$:rec8$:");
99 splice(@a, 3, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
100 check_contents("$ {data}rec7$:record8$:rec9$:");
102 splice(@a, 3, 3, 'record9', 'rec10'); # delete more than insert
103 check_contents("$ {data}record9$:rec10$:");
104 splice(@a, 3, 2); # delete more than one
105 check_contents("$data");
107 # (63-82) splicing with negative subscript
108 splice(@a, -1, 0, "rec4");
109 check_contents("rec0$:rec1$:rec4$:rec2$:");
110 splice(@a, -1, 1, "rec5"); # same length
111 check_contents("rec0$:rec1$:rec4$:rec5$:");
112 splice(@a, -1, 1, "record5"); # longer
113 check_contents("rec0$:rec1$:rec4$:record5$:");
115 splice(@a, -1, 1, "r5"); # shorter
116 check_contents("rec0$:rec1$:rec4$:r5$:");
117 splice(@a, -1, 1); # removal
118 check_contents("rec0$:rec1$:rec4$:");
119 splice(@a, -1, 0); # no-op
120 check_contents("rec0$:rec1$:rec4$:");
121 splice(@a, -1, 0, 'r7', 'rec8'); # insert more than one
122 check_contents("rec0$:rec1$:r7$:rec8$:rec4$:");
123 splice(@a, -1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
124 check_contents("rec0$:rec1$:r7$:rec8$:rec7$:record8$:rec9$:");
126 splice(@a, -3, 3, 'record9', 'rec10'); # delete more than insert
127 check_contents("rec0$:rec1$:r7$:rec8$:record9$:rec10$:");
128 splice(@a, -4, 3); # delete more than one
129 check_contents("rec0$:rec1$:rec10$:");
131 # (83-84) scrub it all out
135 # (85-86) put some back in
136 splice(@a, 0, 0, "rec0", "rec1");
137 check_contents("rec0$:rec1$:");
139 # (87-88) what if we remove too many records?
143 # (89-92) In the past, splicing past the end was not correctly detected
150 # (93-96) Also we did not emulate splice's freaky behavior when inserting
151 # past the end of the array (1.14)
152 splice(@a, 89, 0, "I", "like", "pie");
153 check_contents("I$:like$:pie$:");
154 splice(@a, 89, 0, "pie pie pie");
155 check_contents("I$:like$:pie$:pie pie pie$:");
157 # (97) Splicing with too large a negative number should be fatal
158 # This test ignored because it causes 5.6.1 and 5.7.3 to dump core
159 # It also garbles the stack under 5.005_03 (20020401)
162 eval { splice(@a, -7, 0) };
163 print $@ =~ /^Modification of non-creatable array value attempted, subscript -7/
164 ? "ok $N\n" : "not ok $N \# \$\@ was '$@'\n";
166 print "ok $N \# skipped (versions through 5.7.3 dump core here.)\n";
170 # (98-101) Test default arguments
171 splice @a, 0, 0, (0..11);
173 check_contents("0$:1$:2$:3$:");
177 # (102-103) I think there's a bug here---it will fail to clear the EOF flag
180 check_contents("0$:1$:2$:3$:4$:5$:6$:7$:8$:9$:10$:");
182 # (104-106) make sure that undefs are treated correctly---they should
183 # be converted to empty records, and should not raise any warnings.
184 # (Some of these failed in 0.90. The change to _fixrec fixed them.)
187 my $good = 1; my $warn;
188 # If any of these raise warnings, we have a problem.
189 local $SIG{__WARN__} = sub { $good = 0; $warn = shift(); ctrlfix($warn)};
192 splice @a, 1, 0, undef, undef, undef;
193 print $good ? "ok $N\n" : "not ok $N # $warn\n";
195 print defined($a[2]) ? "ok $N\n" : "not ok $N\n";
197 my @r = splice @a, 2;
198 print defined($r[0]) ? "ok $N\n" : "not ok $N\n";
202 # (107-118) splice with negative length was treated wrong
203 # 20020402 Reported by Juerd Waalboer
206 check_contents("6$:7$:8$:");
209 check_contents("0$:6$:7$:8$:");
212 check_contents("0$:1$:2$:3$:4$:5$:6$:7$:8$:");
215 check_contents("0$:1$:2$:");
218 check_contents("0$:1$:2$:");
221 check_contents("0$:1$:2$:");
225 open F, "> $file" or die $!;
231 use POSIX 'SEEK_SET';
234 my $integrity = $o->_check_integrity($file, $ENV{INTEGRITY});
235 local *FH = $o->{fh};
236 seek FH, 0, SEEK_SET;
237 print $integrity ? "ok $N\n" : "not ok $N\n";
240 { local $/; $a = <FH> }
241 $a = "" unless defined $a;
246 print "not ok $N\n# expected <$x>, got <$a>\n";
262 1 while unlink $file;