xref: /openbsd/gnu/usr.bin/perl/dist/Tie-File/t/19_cache.t (revision 771fbea0)
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6#
7# Tests for various caching errors
8#
9
10$|=1;
11my $file = "tf19-$$.txt";
12$: = Tie::File::_default_recsep();
13my $data = join $:, "rec0" .. "rec9", "";
14my $V = $ENV{INTEGRITY};        # Verbose integrity checking?
15
16print "1..55\n";
17
18my $N = 1;
19use Tie::File;
20print "ok $N\n"; $N++;
21
22open F, '>', $file or die $!;
23binmode F;
24print F $data;
25close F;
26
27my @a;
28my $o = tie @a, 'Tie::File', $file;
29print $o ? "ok $N\n" : "not ok $N\n";
30$N++;
31
32# (3) Through 0.18, this 'splice' call would corrupt the cache.
33my @z = @a;                     # force cache to contain all ten records
34splice @a, 0, 0, "x";
35print $o->_check_integrity($file, $V) ? "ok $N\n" : "not ok $N\n";
36$N++;
37
38# Here we redo *all* the splice tests, with populate()
39# calls before each one, to make sure that splice() does not botch the cache.
40
41# (4-14) splicing at the beginning
42check();
43splice(@a, 0, 0, "rec4");
44check();
45splice(@a, 0, 1, "rec5");       # same length
46check();
47splice(@a, 0, 1, "record5");    # longer
48check();
49splice(@a, 0, 1, "r5");         # shorter
50check();
51splice(@a, 0, 1);               # removal
52check();
53splice(@a, 0, 0);               # no-op
54check();
55
56splice(@a, 0, 0, 'r7', 'rec8'); # insert more than one
57check();
58splice(@a, 0, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
59check();
60splice(@a, 0, 3, 'record9', 'rec10'); # delete more than insert
61check();
62splice(@a, 0, 2);               # delete more than one
63check();
64
65
66# (15-24) splicing in the middle
67splice(@a, 1, 0, "rec4");
68check();
69splice(@a, 1, 1, "rec5");       # same length
70check();
71splice(@a, 1, 1, "record5");    # longer
72check();
73splice(@a, 1, 1, "r5");         # shorter
74check();
75splice(@a, 1, 1);               # removal
76check();
77splice(@a, 1, 0);               # no-op
78check();
79
80splice(@a, 1, 0, 'r7', 'rec8'); # insert more than one
81check();
82splice(@a, 1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
83check();
84splice(@a, 1, 3, 'record9', 'rec10'); # delete more than insert
85check();
86splice(@a, 1, 2);               # delete more than one
87check();
88
89# (25-34) splicing at the end
90splice(@a, 3, 0, "rec4");
91check();
92splice(@a, 3, 1, "rec5");       # same length
93check();
94splice(@a, 3, 1, "record5");    # longer
95check();
96splice(@a, 3, 1, "r5");         # shorter
97check();
98splice(@a, 3, 1);               # removal
99check();
100splice(@a, 3, 0);               # no-op
101check();
102
103splice(@a, 3, 0, 'r7', 'rec8'); # insert more than one
104check();
105splice(@a, 3, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
106check();
107splice(@a, 3, 3, 'record9', 'rec10'); # delete more than insert
108check();
109splice(@a, 3, 2);               # delete more than one
110check();
111
112# (35-44) splicing with negative subscript
113splice(@a, -1, 0, "rec4");
114check();
115splice(@a, -1, 1, "rec5");       # same length
116check();
117splice(@a, -1, 1, "record5");    # longer
118check();
119splice(@a, -1, 1, "r5");         # shorter
120check();
121splice(@a, -1, 1);               # removal
122check();
123splice(@a, -1, 0);               # no-op
124check();
125
126splice(@a, -1, 0, 'r7', 'rec8'); # insert more than one
127check();
128splice(@a, -1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
129check();
130splice(@a, -3, 3, 'record9', 'rec10'); # delete more than insert
131check();
132splice(@a, -4, 3);               # delete more than one
133check();
134
135# (45) scrub it all out
136splice(@a, 0, 3);
137check();
138
139# (46) put some back in
140splice(@a, 0, 0, "rec0", "rec1");
141check();
142
143# (47) what if we remove too many records?
144splice(@a, 0, 17);
145check();
146
147# (48-49) In the past, splicing past the end was not correctly detected
148# (1.14)
149splice(@a, 89, 3);
150check();
151splice(@a, @a, 3);
152check();
153
154# (50-51) Also we did not emulate splice's freaky behavior when inserting
155# past the end of the array (1.14)
156splice(@a, 89, 0, "I", "like", "pie");
157check();
158splice(@a, 89, 0, "pie pie pie");
159check();
160
161# (52-54) Test default arguments
162splice @a, 0, 0, (0..11);
163check();
164splice @a, 4;
165check();
166splice @a;
167check();
168
169# (55) This was broken on 20030507 when you moved the cache management
170# stuff out of _oadjust back into _splice without also putting it back
171# into _store.
172@a = (0..11);
173check();
174
175sub init_file {
176  my $data = shift;
177  open F, '>', $file or die $!;
178  binmode F;
179  print F $data;
180  close F;
181}
182
183sub check {
184  my $integrity = $o->_check_integrity($file, $ENV{INTEGRITY});
185  print $integrity ? "ok $N\n" : "not ok $N\n";
186  $N++;
187  repopulate();
188}
189
190
191sub ctrlfix {
192  for (@_) {
193    s/\n/\\n/g;
194    s/\r/\\r/g;
195  }
196}
197
198sub repopulate {
199  $o->{cache}->empty;
200  my @z = @a;                   # refill the cache with correct data
201}
202
203END {
204  undef $o;
205  untie @a;
206  1 while unlink $file;
207}
208
209
210
211