3 my $file = "tf09-$$.txt";
11 print "ok $N\n"; $N++;
14 my $o = tie @a, 'Tie::File', $file,
15 recsep => $RECSEP, autochomp => 0, autodefer => 0;
16 print $o ? "ok $N\n" : "not ok $N\n";
22 check_contents("rec0");
26 check_contents("rec0", "rec1");
28 check_contents("rec0", "rec1", "rec2");
30 # 9-14 same-length alterations
32 check_contents("new0", "rec1", "rec2");
34 check_contents("new0", "new1", "rec2");
36 check_contents("new0", "new1", "new2");
38 # 15-24 lengthening alterations
40 check_contents("long0", "new1", "new2");
42 check_contents("long0", "long1", "new2");
44 check_contents("long0", "long1", "long2");
46 check_contents("long0", "longer1", "long2");
48 check_contents("longer0", "longer1", "long2");
50 # 25-34 shortening alterations, including truncation
52 check_contents("short0", "longer1", "long2");
54 check_contents("short0", "short1", "long2");
56 check_contents("short0", "short1", "short2");
58 check_contents("short0", "sh1", "short2");
60 check_contents("sh0", "sh1", "short2");
62 # (35-38) file with holes
64 check_contents("sh0", "sh1", "short2", "", "rec4");
66 check_contents("sh0", "sh1", "short2", "rec3", "rec4");
68 # (39-40) zero out file
72 # (41-42) insert into the middle of an empty file
74 check_contents("", "", "", "rec3");
76 # (43-47) 20020326 You thought there would be a bug in STORE where if
77 # a cached record was false, STORE wouldn't see it at all. Yup, there is,
78 # and adding the appropriate defined() test fixes the problem.
79 undef $o; untie @a; 1 while unlink $file;
81 $o = tie @a, 'Tie::File', $file,
82 recsep => $RECSEP, autochomp => 0, autodefer => 0;
83 print $o ? "ok $N\n" : "not ok $N\n";
86 my $z = $a[1]; # caches "0"
88 check_contents("", "", "oops");
90 check_contents("", "bah", "oops");
93 # (48-56) 20020331 Make sure we correctly handle the case where the final
94 # record of the file is not properly terminated, Through version 0.90,
95 # we would mangle the file.
96 my $badrec = "Malformed";
97 $: = $RECSEP = Tie::File::_default_recsep();
99 if (setup_badly_terminated_file(3)) {
100 $o = tie @a, 'Tie::File', $file,
101 recsep => $RECSEP, autochomp => 0, autodefer => 0
102 or die "Couldn't tie file: $!";
104 print $z eq "$badrec$:" ? "ok $N\n" :
105 "not ok $N \# got $z, expected $badrec\n";
108 check_contents($badrec, "next");
111 if (setup_badly_terminated_file(2)) {
112 $o = tie @a, 'Tie::File', $file,
113 recsep => $RECSEP, autochomp => 0, autodefer => 0
114 or die "Couldn't tie file: $!";
115 splice @a, 1, 0, "x", "y";
116 check_contents($badrec, "x", "y");
119 if (setup_badly_terminated_file(4)) {
120 $o = tie @a, 'Tie::File', $file,
121 recsep => $RECSEP, autochomp => 0, autodefer => 0
122 or die "Couldn't tie file: $!";
123 my @r = splice @a, 0, 1, "x", "y";
125 print $n == 1 ? "ok $N\n" : "not ok $N \# expected 1 elt, got $n\n";
127 print $r[0] eq "$badrec$:" ? "ok $N\n"
128 : "not ok $N \# expected <$badrec>, got <$r[0]>\n";
130 check_contents("x", "y");
133 # (57-58) 20020402 The modification would have failed if $\ were set wrong.
135 if (setup_badly_terminated_file(2)) {
136 $o = tie @a, 'Tie::File', $file,
137 recsep => $RECSEP, autochomp => 0, autodefer => 0
138 or die "Couldn't tie file: $!";
139 { local $\ = "I hate \$\\.";
142 check_contents($badrec);
145 # (59) 20030527 Tom Christiansen pointed out that FETCH returns the wrong
146 # data on the final record of an unterminated file if the file is opened
147 # in read-only mode. Note that the $#a is necessary here.
148 # There's special-case code to fix the final record when it is read normally.
149 # But the $#a forces it to be read from the cache, which skips the
151 $badrec = "world${RECSEP}hello";
152 if (setup_badly_terminated_file(1)) {
153 tie(@a, "Tie::File", $file, mode => O_RDONLY, recsep => $RECSEP)
154 or die "Couldn't tie file: $!";
157 print $z eq "hello" ? "ok $N\n" :
158 "not ok $N \# got $z, expected hello\n";
162 sub setup_badly_terminated_file {
164 open F, '>', $file or die "Couldn't open $file: $!";
168 unless (-s $file == length $badrec) {
170 print "ok $N \# skipped - can't create improperly terminated file\n";
179 use POSIX 'SEEK_SET';
182 my $x = join $RECSEP, @c, '';
183 local *FH = $o->{fh};
184 seek FH, 0, SEEK_SET;
186 { local $/; $a = <FH> }
188 $a = "" unless defined $a;
192 my $msg = "# expected <$x>, got <$a>";
194 print "not ok $N $msg\n";
201 unless ($a[$_] eq "$c[$_]$RECSEP") {
202 $msg = "expected $c[$_]$RECSEP, got $a[$_]";
207 print $good ? "ok $N\n" : "not ok $N # fetch $msg\n";
223 1 while unlink $file;