Integrate macperl patches #16926 and #16938;
[p5sagit/p5-mst-13.2.git] / lib / Tie / File / t / 09_gen_rs.t
1 #!/usr/bin/perl
2
3 my $file = "tf$$.txt";
4
5 print "1..58\n";
6
7 my $N = 1;
8 use Tie::File;
9 print "ok $N\n"; $N++;
10
11 $RECSEP = 'blah';
12 my $o = tie @a, 'Tie::File', $file, 
13     recsep => $RECSEP, autochomp => 0, autodefer => 0;
14 print $o ? "ok $N\n" : "not ok $N\n";
15 $N++;
16
17
18 # 3-4 create
19 $a[0] = 'rec0';
20 check_contents("rec0");
21
22 # 5-8 append
23 $a[1] = 'rec1';
24 check_contents("rec0", "rec1");
25 $a[2] = 'rec2';
26 check_contents("rec0", "rec1", "rec2");
27
28 # 9-14 same-length alterations
29 $a[0] = 'new0';
30 check_contents("new0", "rec1", "rec2");
31 $a[1] = 'new1';
32 check_contents("new0", "new1", "rec2");
33 $a[2] = 'new2';
34 check_contents("new0", "new1", "new2");
35
36 # 15-24 lengthening alterations
37 $a[0] = 'long0';
38 check_contents("long0", "new1", "new2");
39 $a[1] = 'long1';
40 check_contents("long0", "long1", "new2");
41 $a[2] = 'long2';
42 check_contents("long0", "long1", "long2");
43 $a[1] = 'longer1';
44 check_contents("long0", "longer1", "long2");
45 $a[0] = 'longer0';
46 check_contents("longer0", "longer1", "long2");
47
48 # 25-34 shortening alterations, including truncation
49 $a[0] = 'short0';
50 check_contents("short0", "longer1", "long2");
51 $a[1] = 'short1';
52 check_contents("short0", "short1", "long2");
53 $a[2] = 'short2';
54 check_contents("short0", "short1", "short2");
55 $a[1] = 'sh1';
56 check_contents("short0", "sh1", "short2");
57 $a[0] = 'sh0';
58 check_contents("sh0", "sh1", "short2");
59
60 # (35-38) file with holes
61 $a[4] = 'rec4';
62 check_contents("sh0", "sh1", "short2", "", "rec4");
63 $a[3] = 'rec3';
64 check_contents("sh0", "sh1", "short2", "rec3", "rec4");
65
66 # (39-40) zero out file
67 @a = ();
68 check_contents();
69
70 # (41-42) insert into the middle of an empty file
71 $a[3] = "rec3";
72 check_contents("", "", "", "rec3");
73
74 # (43-47) 20020326 You thought there would be a bug in STORE where if
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.
77 undef $o;  untie @a;  1 while unlink $file;
78 $RECSEP = '0';
79 $o = tie @a, 'Tie::File', $file, 
80     recsep => $RECSEP, autochomp => 0, autodefer => 0;
81 print $o ? "ok $N\n" : "not ok $N\n";
82 $N++;
83 $#a = 2;
84 my $z = $a[1];                  # caches "0"
85 $a[2] = "oops";
86 check_contents("", "", "oops");
87 $a[1] = "bah";
88 check_contents("", "bah", "oops");
89 undef $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.
94 my $badrec = "Malformed";
95 $: = $RECSEP = Tie::File::_default_recsep();
96 # (48-50)
97 if (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");
107   undef $o; untie @a;
108 }
109 # (51-52)
110 if (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");
116   undef $o; untie @a;
117 }
118 # (53-56)
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";
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");
131   undef $o; untie @a;
132 }
133
134 # (57-58) 20020402 The modifiaction would have failed if $\ were set wrong.
135 # I hate $\.
136 if (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);
144   undef $o; untie @a;
145 }
146
147 sub 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 }
162
163
164 use POSIX 'SEEK_SET';
165 sub check_contents {
166   my @c = @_;
167   my $x = join $RECSEP, @c, '';
168   local *FH = $o->{fh};
169   seek FH, 0, SEEK_SET;
170   my $a;
171   { local $/; $a = <FH> }
172
173   $a = "" unless defined $a;
174   if ($a eq $x) {
175     print "ok $N\n";
176   } else {
177     my $msg = "# expected <$x>, got <$a>";
178     ctrlfix($msg);
179     print "not ok $N $msg\n";
180   }
181   $N++;
182
183   # now check FETCH:
184   my $good = 1;
185   for (0.. $#c) {
186     unless ($a[$_] eq "$c[$_]$RECSEP") {
187       $msg = "expected $c[$_]$RECSEP, got $a[$_]";
188       ctrlfix($msg);
189       $good = 0;
190     }
191   }
192   print $good ? "ok $N\n" : "not ok $N # fetch $msg\n";
193   $N++;
194 }
195
196
197 sub ctrlfix {
198   for (@_) {
199     s/\n/\\n/g;
200     s/\r/\\r/g;
201   }
202 }
203
204
205 END {
206   undef $o;
207   untie @a;
208   1 while unlink $file;
209 }
210