1#!/usr/bin/perl
2#
3# Tests for various caching errors
4#
5
6my $file = "tf20-$$.txt";
7$: = Tie::File::_default_recsep();
8my $data = join $:, "record0" .. "record9", "";
9my $V = $ENV{INTEGRITY};        # Verbose integrity checking?
10
11print "1..111\n";
12
13my $N = 1;
14use Tie::File;
15print "ok $N\n"; $N++;
16
17open F, '>', $file or die $!;
18binmode F;
19print F $data;
20close F;
21
22# Limit cache size to 30 bytes
23my $MAX = 30;
24#  -- that's enough space for 3 records, but not 4, on both \n and \r\n systems
25my $o = tie @a, 'Tie::File', $file, memory => $MAX, autodefer => 0;
26print $o ? "ok $N\n" : "not ok $N\n";
27$N++;
28
29# (3-5) Let's see if data was properly expired from the cache
30my @z = @a;                     # force cache to contain all ten records
31# It should now contain only the *last* three records, 7, 8, and 9
32{
33  my $x = "7 8 9";
34  my $a = join " ", sort $o->{cache}->ckeys;
35  if ($a eq $x) { print "ok $N\n" }
36  else { print "not ok $N # cache keys were <$a>; expected <$x>\n" }
37  $N++;
38}
39check();
40
41# Here we redo *all* the splice tests, with populate()
42# calls before each one, to make sure that splice() does not botch the cache.
43
44# (6-25) splicing at the beginning
45splice(@a, 0, 0, "rec4");
46check();
47splice(@a, 0, 1, "rec5");       # same length
48check();
49splice(@a, 0, 1, "record5");    # longer
50check();
51splice(@a, 0, 1, "r5");         # shorter
52check();
53splice(@a, 0, 1);               # removal
54check();
55splice(@a, 0, 0);               # no-op
56check();
57
58splice(@a, 0, 0, 'r7', 'rec8'); # insert more than one
59check();
60splice(@a, 0, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
61check();
62splice(@a, 0, 3, 'record9', 'rec10'); # delete more than insert
63check();
64splice(@a, 0, 2);               # delete more than one
65check();
66
67
68# (26-45) splicing in the middle
69splice(@a, 1, 0, "rec4");
70check();
71splice(@a, 1, 1, "rec5");       # same length
72check();
73splice(@a, 1, 1, "record5");    # longer
74check();
75splice(@a, 1, 1, "r5");         # shorter
76check();
77splice(@a, 1, 1);               # removal
78check();
79splice(@a, 1, 0);               # no-op
80check();
81
82splice(@a, 1, 0, 'r7', 'rec8'); # insert more than one
83check();
84splice(@a, 1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
85check();
86splice(@a, 1, 3, 'record9', 'rec10'); # delete more than insert
87check();
88splice(@a, 1, 2);               # delete more than one
89check();
90
91# (46-65) splicing at the end
92splice(@a, 3, 0, "rec4");
93check();
94splice(@a, 3, 1, "rec5");       # same length
95check();
96splice(@a, 3, 1, "record5");    # longer
97check();
98splice(@a, 3, 1, "r5");         # shorter
99check();
100splice(@a, 3, 1);               # removal
101check();
102splice(@a, 3, 0);               # no-op
103check();
104
105splice(@a, 3, 0, 'r7', 'rec8'); # insert more than one
106check();
107splice(@a, 3, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
108check();
109splice(@a, 3, 3, 'record9', 'rec10'); # delete more than insert
110check();
111splice(@a, 3, 2);               # delete more than one
112check();
113
114# (66-85) splicing with negative subscript
115splice(@a, -1, 0, "rec4");
116check();
117splice(@a, -1, 1, "rec5");       # same length
118check();
119splice(@a, -1, 1, "record5");    # longer
120check();
121splice(@a, -1, 1, "r5");         # shorter
122check();
123splice(@a, -1, 1);               # removal
124check();
125splice(@a, -1, 0);               # no-op
126check();
127
128splice(@a, -1, 0, 'r7', 'rec8'); # insert more than one
129check();
130splice(@a, -1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
131check();
132splice(@a, -3, 3, 'record9', 'rec10'); # delete more than insert
133check();
134splice(@a, -4, 3);               # delete more than one
135check();
136
137# (86-87) scrub it all out
138splice(@a, 0, 3);
139check();
140
141# (88-89) put some back in
142splice(@a, 0, 0, "rec0", "rec1");
143check();
144
145# (90-91) what if we remove too many records?
146splice(@a, 0, 17);
147check();
148
149# (92-95) In the past, splicing past the end was not correctly detected
150# (1.14)
151splice(@a, 89, 3);
152check();
153splice(@a, @a, 3);
154check();
155
156# (96-99) Also we did not emulate splice's freaky behavior when inserting
157# past the end of the array (1.14)
158splice(@a, 89, 0, "I", "like", "pie");
159check();
160splice(@a, 89, 0, "pie pie pie");
161check();
162
163# (100-105) Test default arguments
164splice @a, 0, 0, (0..11);
165check();
166splice @a, 4;
167check();
168splice @a;
169check();
170
171# (106-111) One last set of tests.  I don't know what state the cache
172# is in now.  But if I read any three records, those three records are
173# what should be in the cache, and nothing else.
174@a = "record0" .. "record9";
175check(); # In 0.18 #107 fails here--STORE was not flushing the cache when
176         # replacing an old cached record with a longer one
177for (5, 6, 1) { my $z = $a[$_] }
178{
179  my $x = "5 6 1";
180  my $a = join " ", $o->{cache}->_produce_lru;
181  if ($a eq $x) { print "ok $N\n" }
182  else { print "not ok $N # LRU was <$a>; expected <$x>\n" }
183  $N++;
184  $x = "1 5 6";
185  $a = join " ", sort $o->{cache}->ckeys;
186  if ($a eq $x) { print "ok $N\n" }
187  else { print "not ok $N # cache keys were <$a>; expected <$x>\n" }
188  $N++;
189}
190check();
191
192
193sub init_file {
194  my $data = shift;
195  open F, '>', $file or die $!;
196  binmode F;
197  print F $data;
198  close F;
199}
200
201sub check {
202  my $integrity = $o->_check_integrity($file, $ENV{INTEGRITY});
203  print $integrity ? "ok $N\n" : "not ok $N\n";
204  $N++;
205
206  my $b = $o->{cache}->bytes;
207  print $b <= $MAX
208    ? "ok $N\n"
209    : "not ok $N # $b bytes cached, should be <= $MAX\n";
210  $N++;
211}
212
213
214sub ctrlfix {
215  for (@_) {
216    s/\n/\\n/g;
217    s/\r/\\r/g;
218  }
219}
220
221END {
222  undef $o;
223  untie @a;
224  1 while unlink $file;
225}
226
227
228
229