Integrate macperl patches #16926 and #16938;
[p5sagit/p5-mst-13.2.git] / lib / Tie / File / t / 09_gen_rs.t
CommitLineData
b5aed31e 1#!/usr/bin/perl
2
3my $file = "tf$$.txt";
4
bf919750 5print "1..58\n";
b5aed31e 6
7my $N = 1;
8use Tie::File;
9print "ok $N\n"; $N++;
10
6fc0ea7e 11$RECSEP = 'blah';
12my $o = tie @a, 'Tie::File', $file,
13 recsep => $RECSEP, autochomp => 0, autodefer => 0;
b5aed31e 14print $o ? "ok $N\n" : "not ok $N\n";
15$N++;
16
17
18# 3-4 create
19$a[0] = 'rec0';
20check_contents("rec0");
21
22# 5-8 append
23$a[1] = 'rec1';
24check_contents("rec0", "rec1");
25$a[2] = 'rec2';
26check_contents("rec0", "rec1", "rec2");
27
28# 9-14 same-length alterations
29$a[0] = 'new0';
30check_contents("new0", "rec1", "rec2");
31$a[1] = 'new1';
32check_contents("new0", "new1", "rec2");
33$a[2] = 'new2';
34check_contents("new0", "new1", "new2");
35
36# 15-24 lengthening alterations
37$a[0] = 'long0';
38check_contents("long0", "new1", "new2");
39$a[1] = 'long1';
40check_contents("long0", "long1", "new2");
41$a[2] = 'long2';
42check_contents("long0", "long1", "long2");
43$a[1] = 'longer1';
44check_contents("long0", "longer1", "long2");
45$a[0] = 'longer0';
46check_contents("longer0", "longer1", "long2");
47
48# 25-34 shortening alterations, including truncation
49$a[0] = 'short0';
50check_contents("short0", "longer1", "long2");
51$a[1] = 'short1';
52check_contents("short0", "short1", "long2");
53$a[2] = 'short2';
54check_contents("short0", "short1", "short2");
55$a[1] = 'sh1';
56check_contents("short0", "sh1", "short2");
57$a[0] = 'sh0';
58check_contents("sh0", "sh1", "short2");
59
27531ffb 60# (35-38) file with holes
b5aed31e 61$a[4] = 'rec4';
62check_contents("sh0", "sh1", "short2", "", "rec4");
63$a[3] = 'rec3';
64check_contents("sh0", "sh1", "short2", "rec3", "rec4");
65
27531ffb 66# (39-40) zero out file
6fc0ea7e 67@a = ();
68check_contents();
69
27531ffb 70# (41-42) insert into the middle of an empty file
6fc0ea7e 71$a[3] = "rec3";
72check_contents("", "", "", "rec3");
73
27531ffb 74# (43-47) 20020326 You thought there would be a bug in STORE where if
6fc0ea7e 75# a cached record was false, STORE wouldn't see it at all. Yup, there is,
76# and adding the appropriate defined() test fixes the problem.
77undef $o; untie @a; 1 while unlink $file;
78$RECSEP = '0';
79$o = tie @a, 'Tie::File', $file,
80 recsep => $RECSEP, autochomp => 0, autodefer => 0;
81print $o ? "ok $N\n" : "not ok $N\n";
82$N++;
83$#a = 2;
84my $z = $a[1]; # caches "0"
85$a[2] = "oops";
86check_contents("", "", "oops");
87$a[1] = "bah";
88check_contents("", "bah", "oops");
27531ffb 89undef $o; untie @a;
90
91# (48-56) 20020331 Make sure we correctly handle the case where the final
92# record of the file is not properly terminated, Through version 0.90,
93# we would mangle the file.
94my $badrec = "Malformed";
95$: = $RECSEP = Tie::File::_default_recsep();
96# (48-50)
97if (setup_badly_terminated_file(3)) {
98 $o = tie @a, 'Tie::File', $file,
99 recsep => $RECSEP, autochomp => 0, autodefer => 0
100 or die "Couldn't tie file: $!";
101 my $z = $a[0];
102 print $z eq "$badrec$:" ? "ok $N\n" :
103 "not ok $N \# got $z, expected $badrec\n";
104 $N++;
105 push @a, "next";
106 check_contents($badrec, "next");
d5201bd2 107 undef $o; untie @a;
27531ffb 108}
109# (51-52)
110if (setup_badly_terminated_file(2)) {
111 $o = tie @a, 'Tie::File', $file,
112 recsep => $RECSEP, autochomp => 0, autodefer => 0
113 or die "Couldn't tie file: $!";
114 splice @a, 1, 0, "x", "y";
115 check_contents($badrec, "x", "y");
d5201bd2 116 undef $o; untie @a;
27531ffb 117}
118# (53-56)
119if (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";
124 my $n = @r;
125 print $n == 1 ? "ok $N\n" : "not ok $N \# expected 1 elt, got $n\n";
126 $N++;
127 print $r[0] eq "$badrec$:" ? "ok $N\n"
128 : "not ok $N \# expected <$badrec>, got <$r[0]>\n";
129 $N++;
130 check_contents("x", "y");
d5201bd2 131 undef $o; untie @a;
27531ffb 132}
133
bf919750 134# (57-58) 20020402 The modifiaction would have failed if $\ were set wrong.
135# I hate $\.
136if (setup_badly_terminated_file(2)) {
137 $o = tie @a, 'Tie::File', $file,
138 recsep => $RECSEP, autochomp => 0, autodefer => 0
139 or die "Couldn't tie file: $!";
140 { local $\ = "I hate \$\\.";
141 my $z = $a[0];
142 }
143 check_contents($badrec);
d5201bd2 144 undef $o; untie @a;
bf919750 145}
146
27531ffb 147sub setup_badly_terminated_file {
148 my $NTESTS = shift;
149 open F, "> $file" or die "Couldn't open $file: $!";
150 binmode F;
151 print F $badrec;
152 close F;
153 unless (-s $file == length $badrec) {
154 for (1 .. $NTESTS) {
155 print "ok $N \# skipped - can't create improperly terminated file\n";
156 $N++;
157 }
158 return;
159 }
160 return 1;
161}
b5aed31e 162
b5aed31e 163
7b6b3db1 164use POSIX 'SEEK_SET';
b5aed31e 165sub check_contents {
166 my @c = @_;
6fc0ea7e 167 my $x = join $RECSEP, @c, '';
7b6b3db1 168 local *FH = $o->{fh};
169 seek FH, 0, SEEK_SET;
b5aed31e 170 my $a;
171 { local $/; $a = <FH> }
7b6b3db1 172
173 $a = "" unless defined $a;
174 if ($a eq $x) {
175 print "ok $N\n";
176 } else {
b3fe5a4c 177 my $msg = "# expected <$x>, got <$a>";
178 ctrlfix($msg);
179 print "not ok $N $msg\n";
7b6b3db1 180 }
b5aed31e 181 $N++;
182
183 # now check FETCH:
184 my $good = 1;
185 for (0.. $#c) {
6fc0ea7e 186 unless ($a[$_] eq "$c[$_]$RECSEP") {
187 $msg = "expected $c[$_]$RECSEP, got $a[$_]";
b3fe5a4c 188 ctrlfix($msg);
7b6b3db1 189 $good = 0;
190 }
b5aed31e 191 }
6fc0ea7e 192 print $good ? "ok $N\n" : "not ok $N # fetch $msg\n";
b5aed31e 193 $N++;
194}
195
b3fe5a4c 196
197sub ctrlfix {
198 for (@_) {
199 s/\n/\\n/g;
200 s/\r/\\r/g;
201 }
202}
203
204
b5aed31e 205END {
7b6b3db1 206 undef $o;
207 untie @a;
b5aed31e 208 1 while unlink $file;
209}
210