Commit | Line | Data |
---|---|---|
b5aed31e | 1 | #!/usr/bin/perl |
b3fe5a4c | 2 | |
b5aed31e AMS |
3 | # |
4 | # Check SPLICE function's effect on the file | |
5 | # (07_rv_splice.t checks its return value) | |
6 | # | |
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 | |
12 | # contents. | |
13 | ||
27531ffb | 14 | |
6ae23f41 | 15 | $| = 1; |
b5aed31e | 16 | my $file = "tf$$.txt"; |
b3fe5a4c AMS |
17 | $: = Tie::File::_default_recsep(); |
18 | my $data = "rec0$:rec1$:rec2$:"; | |
bf919750 | 19 | print "1..118\n"; |
7b6b3db1 JH |
20 | |
21 | init_file($data); | |
b5aed31e AMS |
22 | |
23 | my $N = 1; | |
24 | use Tie::File; | |
25 | print "ok $N\n"; $N++; # partial credit just for showing up | |
26 | ||
27 | my $o = tie @a, 'Tie::File', $file; | |
28 | print $o ? "ok $N\n" : "not ok $N\n"; | |
29 | $N++; | |
30 | ||
b3fe5a4c | 31 | $: = $o->{recsep}; |
b5aed31e AMS |
32 | my $n; |
33 | ||
34 | # (3-22) splicing at the beginning | |
b5aed31e | 35 | splice(@a, 0, 0, "rec4"); |
b3fe5a4c | 36 | check_contents("rec4$:$data"); |
b5aed31e | 37 | splice(@a, 0, 1, "rec5"); # same length |
b3fe5a4c | 38 | check_contents("rec5$:$data"); |
b5aed31e | 39 | splice(@a, 0, 1, "record5"); # longer |
b3fe5a4c | 40 | check_contents("record5$:$data"); |
b5aed31e AMS |
41 | |
42 | splice(@a, 0, 1, "r5"); # shorter | |
b3fe5a4c | 43 | check_contents("r5$:$data"); |
b5aed31e AMS |
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 | |
b3fe5a4c | 49 | check_contents("r7$:rec8$:$data"); |
b5aed31e | 50 | splice(@a, 0, 2, 'rec7', 'record8', 'rec9'); # insert more than delete |
b3fe5a4c | 51 | check_contents("rec7$:record8$:rec9$:$data"); |
b5aed31e AMS |
52 | |
53 | splice(@a, 0, 3, 'record9', 'rec10'); # delete more than insert | |
b3fe5a4c | 54 | check_contents("record9$:rec10$:$data"); |
b5aed31e AMS |
55 | splice(@a, 0, 2); # delete more than one |
56 | check_contents("$data"); | |
57 | ||
58 | ||
59 | # (23-42) splicing in the middle | |
60 | splice(@a, 1, 0, "rec4"); | |
b3fe5a4c | 61 | check_contents("rec0$:rec4$:rec1$:rec2$:"); |
b5aed31e | 62 | splice(@a, 1, 1, "rec5"); # same length |
b3fe5a4c | 63 | check_contents("rec0$:rec5$:rec1$:rec2$:"); |
b5aed31e | 64 | splice(@a, 1, 1, "record5"); # longer |
b3fe5a4c | 65 | check_contents("rec0$:record5$:rec1$:rec2$:"); |
b5aed31e AMS |
66 | |
67 | splice(@a, 1, 1, "r5"); # shorter | |
b3fe5a4c | 68 | check_contents("rec0$:r5$:rec1$:rec2$:"); |
b5aed31e AMS |
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 | |
b3fe5a4c | 74 | check_contents("rec0$:r7$:rec8$:rec1$:rec2$:"); |
b5aed31e | 75 | splice(@a, 1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete |
b3fe5a4c | 76 | check_contents("rec0$:rec7$:record8$:rec9$:rec1$:rec2$:"); |
b5aed31e AMS |
77 | |
78 | splice(@a, 1, 3, 'record9', 'rec10'); # delete more than insert | |
b3fe5a4c | 79 | check_contents("rec0$:record9$:rec10$:rec1$:rec2$:"); |
b5aed31e AMS |
80 | splice(@a, 1, 2); # delete more than one |
81 | check_contents("$data"); | |
82 | ||
83 | # (43-62) splicing at the end | |
84 | splice(@a, 3, 0, "rec4"); | |
b3fe5a4c | 85 | check_contents("$ {data}rec4$:"); |
b5aed31e | 86 | splice(@a, 3, 1, "rec5"); # same length |
b3fe5a4c | 87 | check_contents("$ {data}rec5$:"); |
b5aed31e | 88 | splice(@a, 3, 1, "record5"); # longer |
b3fe5a4c | 89 | check_contents("$ {data}record5$:"); |
b5aed31e AMS |
90 | |
91 | splice(@a, 3, 1, "r5"); # shorter | |
b3fe5a4c | 92 | check_contents("$ {data}r5$:"); |
b5aed31e AMS |
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 | |
b3fe5a4c | 98 | check_contents("$ {data}r7$:rec8$:"); |
b5aed31e | 99 | splice(@a, 3, 2, 'rec7', 'record8', 'rec9'); # insert more than delete |
b3fe5a4c | 100 | check_contents("$ {data}rec7$:record8$:rec9$:"); |
b5aed31e AMS |
101 | |
102 | splice(@a, 3, 3, 'record9', 'rec10'); # delete more than insert | |
b3fe5a4c | 103 | check_contents("$ {data}record9$:rec10$:"); |
b5aed31e AMS |
104 | splice(@a, 3, 2); # delete more than one |
105 | check_contents("$data"); | |
106 | ||
107 | # (63-82) splicing with negative subscript | |
108 | splice(@a, -1, 0, "rec4"); | |
b3fe5a4c | 109 | check_contents("rec0$:rec1$:rec4$:rec2$:"); |
b5aed31e | 110 | splice(@a, -1, 1, "rec5"); # same length |
b3fe5a4c | 111 | check_contents("rec0$:rec1$:rec4$:rec5$:"); |
b5aed31e | 112 | splice(@a, -1, 1, "record5"); # longer |
b3fe5a4c | 113 | check_contents("rec0$:rec1$:rec4$:record5$:"); |
b5aed31e AMS |
114 | |
115 | splice(@a, -1, 1, "r5"); # shorter | |
b3fe5a4c | 116 | check_contents("rec0$:rec1$:rec4$:r5$:"); |
b5aed31e | 117 | splice(@a, -1, 1); # removal |
b3fe5a4c | 118 | check_contents("rec0$:rec1$:rec4$:"); |
b5aed31e | 119 | splice(@a, -1, 0); # no-op |
b3fe5a4c | 120 | check_contents("rec0$:rec1$:rec4$:"); |
b5aed31e | 121 | splice(@a, -1, 0, 'r7', 'rec8'); # insert more than one |
b3fe5a4c | 122 | check_contents("rec0$:rec1$:r7$:rec8$:rec4$:"); |
b5aed31e | 123 | splice(@a, -1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete |
b3fe5a4c | 124 | check_contents("rec0$:rec1$:r7$:rec8$:rec7$:record8$:rec9$:"); |
b5aed31e AMS |
125 | |
126 | splice(@a, -3, 3, 'record9', 'rec10'); # delete more than insert | |
b3fe5a4c | 127 | check_contents("rec0$:rec1$:r7$:rec8$:record9$:rec10$:"); |
b5aed31e | 128 | splice(@a, -4, 3); # delete more than one |
b3fe5a4c | 129 | check_contents("rec0$:rec1$:rec10$:"); |
b5aed31e AMS |
130 | |
131 | # (83-84) scrub it all out | |
132 | splice(@a, 0, 3); | |
133 | check_contents(""); | |
134 | ||
135 | # (85-86) put some back in | |
136 | splice(@a, 0, 0, "rec0", "rec1"); | |
b3fe5a4c | 137 | check_contents("rec0$:rec1$:"); |
b5aed31e AMS |
138 | |
139 | # (87-88) what if we remove too many records? | |
140 | splice(@a, 0, 17); | |
141 | check_contents(""); | |
142 | ||
51efdd02 AMS |
143 | # (89-92) In the past, splicing past the end was not correctly detected |
144 | # (1.14) | |
145 | splice(@a, 89, 3); | |
146 | check_contents(""); | |
147 | splice(@a, @a, 3); | |
148 | check_contents(""); | |
149 | ||
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"); | |
b3fe5a4c | 153 | check_contents("I$:like$:pie$:"); |
51efdd02 | 154 | splice(@a, 89, 0, "pie pie pie"); |
b3fe5a4c | 155 | check_contents("I$:like$:pie$:pie pie pie$:"); |
51efdd02 AMS |
156 | |
157 | # (97) Splicing with too large a negative number should be fatal | |
cf8feb78 MJD |
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) | |
51efdd02 | 160 | # NOT MY FAULT |
0bf62e3b | 161 | if ($] > 5.007003) { |
51efdd02 AMS |
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"; | |
165 | } else { | |
0bf62e3b | 166 | print "ok $N \# skipped (versions through 5.7.3 dump core here.)\n"; |
51efdd02 AMS |
167 | } |
168 | $N++; | |
169 | ||
7b6b3db1 JH |
170 | # (98-101) Test default arguments |
171 | splice @a, 0, 0, (0..11); | |
172 | splice @a, 4; | |
b3fe5a4c | 173 | check_contents("0$:1$:2$:3$:"); |
7b6b3db1 JH |
174 | splice @a; |
175 | check_contents(""); | |
27531ffb JH |
176 | |
177 | # (102-103) I think there's a bug here---it will fail to clear the EOF flag | |
178 | @a = (0..11); | |
179 | splice @a, -1, 1000; | |
180 | check_contents("0$:1$:2$:3$:4$:5$:6$:7$:8$:9$:10$:"); | |
bf919750 | 181 | |
27531ffb JH |
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.) | |
185 | # 20020331 | |
186 | { | |
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)}; | |
190 | local $^W = 1; | |
191 | @a = (1); | |
192 | splice @a, 1, 0, undef, undef, undef; | |
193 | print $good ? "ok $N\n" : "not ok $N # $warn\n"; | |
194 | $N++; $good = 1; | |
195 | print defined($a[2]) ? "ok $N\n" : "not ok $N\n"; | |
196 | $N++; $good = 1; | |
197 | my @r = splice @a, 2; | |
198 | print defined($r[0]) ? "ok $N\n" : "not ok $N\n"; | |
199 | $N++; $good = 1; | |
200 | } | |
51efdd02 | 201 | |
bf919750 JH |
202 | # (107-118) splice with negative length was treated wrong |
203 | # 20020402 Reported by Juerd Waalboer | |
204 | @a = (0..8) ; | |
205 | splice @a, 0, -3; | |
206 | check_contents("6$:7$:8$:"); | |
207 | @a = (0..8) ; | |
208 | splice @a, 1, -3; | |
209 | check_contents("0$:6$:7$:8$:"); | |
210 | @a = (0..8) ; | |
211 | splice @a, 7, -3; | |
212 | check_contents("0$:1$:2$:3$:4$:5$:6$:7$:8$:"); | |
213 | @a = (0..2) ; | |
214 | splice @a, 0, -3; | |
215 | check_contents("0$:1$:2$:"); | |
216 | @a = (0..2) ; | |
217 | splice @a, 1, -3; | |
218 | check_contents("0$:1$:2$:"); | |
219 | @a = (0..2) ; | |
220 | splice @a, 7, -3; | |
221 | check_contents("0$:1$:2$:"); | |
222 | ||
b5aed31e AMS |
223 | sub init_file { |
224 | my $data = shift; | |
1ae6ead9 | 225 | open F, '>', $file or die $!; |
1768807e | 226 | binmode F; |
b5aed31e AMS |
227 | print F $data; |
228 | close F; | |
229 | } | |
230 | ||
7b6b3db1 | 231 | use POSIX 'SEEK_SET'; |
b5aed31e AMS |
232 | sub check_contents { |
233 | my $x = shift; | |
b5aed31e | 234 | my $integrity = $o->_check_integrity($file, $ENV{INTEGRITY}); |
7b6b3db1 JH |
235 | local *FH = $o->{fh}; |
236 | seek FH, 0, SEEK_SET; | |
b5aed31e AMS |
237 | print $integrity ? "ok $N\n" : "not ok $N\n"; |
238 | $N++; | |
b5aed31e AMS |
239 | my $a; |
240 | { local $/; $a = <FH> } | |
7b6b3db1 JH |
241 | $a = "" unless defined $a; |
242 | if ($a eq $x) { | |
243 | print "ok $N\n"; | |
244 | } else { | |
b3fe5a4c | 245 | ctrlfix($a, $x); |
7b6b3db1 JH |
246 | print "not ok $N\n# expected <$x>, got <$a>\n"; |
247 | } | |
b5aed31e AMS |
248 | $N++; |
249 | } | |
250 | ||
b3fe5a4c AMS |
251 | |
252 | sub ctrlfix { | |
253 | for (@_) { | |
254 | s/\n/\\n/g; | |
255 | s/\r/\\r/g; | |
256 | } | |
257 | } | |
258 | ||
b5aed31e | 259 | END { |
7b6b3db1 JH |
260 | undef $o; |
261 | untie @a; | |
b5aed31e AMS |
262 | 1 while unlink $file; |
263 | } | |
264 |