This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add Tie::File 0.12 from MJD.
[perl5.git] / lib / Tie / File / 09_gen_rs.t
1 #!/usr/bin/perl
2
3 my $file = "tf$$.txt";
4
5 print "1..38\n";
6
7 my $N = 1;
8 use Tie::File;
9 print "ok $N\n"; $N++;
10
11 my $o = tie @a, 'Tie::File', $file, recsep => 'blah';
12 print $o ? "ok $N\n" : "not ok $N\n";
13 $N++;
14
15
16 # 3-4 create
17 $a[0] = 'rec0';
18 check_contents("rec0");
19
20 # 5-8 append
21 $a[1] = 'rec1';
22 check_contents("rec0", "rec1");
23 $a[2] = 'rec2';
24 check_contents("rec0", "rec1", "rec2");
25
26 # 9-14 same-length alterations
27 $a[0] = 'new0';
28 check_contents("new0", "rec1", "rec2");
29 $a[1] = 'new1';
30 check_contents("new0", "new1", "rec2");
31 $a[2] = 'new2';
32 check_contents("new0", "new1", "new2");
33
34 # 15-24 lengthening alterations
35 $a[0] = 'long0';
36 check_contents("long0", "new1", "new2");
37 $a[1] = 'long1';
38 check_contents("long0", "long1", "new2");
39 $a[2] = 'long2';
40 check_contents("long0", "long1", "long2");
41 $a[1] = 'longer1';
42 check_contents("long0", "longer1", "long2");
43 $a[0] = 'longer0';
44 check_contents("longer0", "longer1", "long2");
45
46 # 25-34 shortening alterations, including truncation
47 $a[0] = 'short0';
48 check_contents("short0", "longer1", "long2");
49 $a[1] = 'short1';
50 check_contents("short0", "short1", "long2");
51 $a[2] = 'short2';
52 check_contents("short0", "short1", "short2");
53 $a[1] = 'sh1';
54 check_contents("short0", "sh1", "short2");
55 $a[0] = 'sh0';
56 check_contents("sh0", "sh1", "short2");
57
58 # file with holes
59 $a[4] = 'rec4';
60 check_contents("sh0", "sh1", "short2", "", "rec4");
61 $a[3] = 'rec3';
62 check_contents("sh0", "sh1", "short2", "rec3", "rec4");
63
64
65 # try inserting a record into the middle of an empty file
66
67
68 sub check_contents {
69   my @c = @_;
70   my $x = join 'blah', @c, '';
71   local *FH;
72   my $open = open FH, "< $file";
73   my $a;
74   { local $/; $a = <FH> }
75   print (($open && $a eq $x) ? "ok $N\n" : "not ok $N # file @c\n");
76   $N++;
77
78   # now check FETCH:
79   my $good = 1;
80   for (0.. $#c) {
81     $good = 0 unless $a[$_] eq "$c[$_]blah";
82   }
83   print (($open && $good) ? "ok $N\n" : "not ok $N # fetch @c\n");
84   $N++;
85 }
86
87 END {
88   1 while unlink $file;
89 }
90