Upgrade to Tie::File 0.16.
[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
5print "1..38\n";
6
7my $N = 1;
8use Tie::File;
9print "ok $N\n"; $N++;
10
11my $o = tie @a, 'Tie::File', $file, recsep => 'blah';
12print $o ? "ok $N\n" : "not ok $N\n";
13$N++;
14
15
16# 3-4 create
17$a[0] = 'rec0';
18check_contents("rec0");
19
20# 5-8 append
21$a[1] = 'rec1';
22check_contents("rec0", "rec1");
23$a[2] = 'rec2';
24check_contents("rec0", "rec1", "rec2");
25
26# 9-14 same-length alterations
27$a[0] = 'new0';
28check_contents("new0", "rec1", "rec2");
29$a[1] = 'new1';
30check_contents("new0", "new1", "rec2");
31$a[2] = 'new2';
32check_contents("new0", "new1", "new2");
33
34# 15-24 lengthening alterations
35$a[0] = 'long0';
36check_contents("long0", "new1", "new2");
37$a[1] = 'long1';
38check_contents("long0", "long1", "new2");
39$a[2] = 'long2';
40check_contents("long0", "long1", "long2");
41$a[1] = 'longer1';
42check_contents("long0", "longer1", "long2");
43$a[0] = 'longer0';
44check_contents("longer0", "longer1", "long2");
45
46# 25-34 shortening alterations, including truncation
47$a[0] = 'short0';
48check_contents("short0", "longer1", "long2");
49$a[1] = 'short1';
50check_contents("short0", "short1", "long2");
51$a[2] = 'short2';
52check_contents("short0", "short1", "short2");
53$a[1] = 'sh1';
54check_contents("short0", "sh1", "short2");
55$a[0] = 'sh0';
56check_contents("sh0", "sh1", "short2");
57
58# file with holes
59$a[4] = 'rec4';
60check_contents("sh0", "sh1", "short2", "", "rec4");
61$a[3] = 'rec3';
62check_contents("sh0", "sh1", "short2", "rec3", "rec4");
63
64
65# try inserting a record into the middle of an empty file
66
7b6b3db1 67use POSIX 'SEEK_SET';
b5aed31e 68sub check_contents {
69 my @c = @_;
70 my $x = join 'blah', @c, '';
7b6b3db1 71 local *FH = $o->{fh};
72 seek FH, 0, SEEK_SET;
b5aed31e 73 my $a;
74 { local $/; $a = <FH> }
7b6b3db1 75
76 $a = "" unless defined $a;
77 if ($a eq $x) {
78 print "ok $N\n";
79 } else {
80 s{$/}{\\n}g for $a, $x;
81 print "not ok $N\n# expected <$x>, got <$a>\n";
82 }
b5aed31e 83 $N++;
84
85 # now check FETCH:
86 my $good = 1;
87 for (0.. $#c) {
7b6b3db1 88 unless ($a[$_] eq "$c[$_]blah") {
89 $msg = "expected $c[$_]blah, got $a[$_]";
90 $msg =~ s{$/}{\\n}g;
91 $good = 0;
92 }
b5aed31e 93 }
7b6b3db1 94 print $good ? "ok $N\n" : "not ok $N # fetch @c\n";
b5aed31e 95 $N++;
96}
97
98END {
7b6b3db1 99 undef $o;
100 untie @a;
b5aed31e 101 1 while unlink $file;
102}
103