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 / 01_gen.t
CommitLineData
b5aed31e
AMS
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;
12print $o ? "ok $N\n" : "not ok $N\n";
13$N++;
14
15# 3-4 create
16$a[0] = 'rec0';
17check_contents("rec0");
18
19# 5-8 append
20$a[1] = 'rec1';
21check_contents("rec0", "rec1");
22$a[2] = 'rec2';
23check_contents("rec0", "rec1", "rec2");
24
25# 9-14 same-length alterations
26$a[0] = 'new0';
27check_contents("new0", "rec1", "rec2");
28$a[1] = 'new1';
29check_contents("new0", "new1", "rec2");
30$a[2] = 'new2';
31check_contents("new0", "new1", "new2");
32
33# 15-24 lengthening alterations
34$a[0] = 'long0';
35check_contents("long0", "new1", "new2");
36$a[1] = 'long1';
37check_contents("long0", "long1", "new2");
38$a[2] = 'long2';
39check_contents("long0", "long1", "long2");
40$a[1] = 'longer1';
41check_contents("long0", "longer1", "long2");
42$a[0] = 'longer0';
43check_contents("longer0", "longer1", "long2");
44
45# 25-34 shortening alterations, including truncation
46$a[0] = 'short0';
47check_contents("short0", "longer1", "long2");
48$a[1] = 'short1';
49check_contents("short0", "short1", "long2");
50$a[2] = 'short2';
51check_contents("short0", "short1", "short2");
52$a[1] = 'sh1';
53check_contents("short0", "sh1", "short2");
54$a[0] = 'sh0';
55check_contents("sh0", "sh1", "short2");
56
57# file with holes
58$a[4] = 'rec4';
59check_contents("sh0", "sh1", "short2", "", "rec4");
60$a[3] = 'rec3';
61check_contents("sh0", "sh1", "short2", "rec3", "rec4");
62
63
64# try inserting a record into the middle of an empty file
65
66
67sub check_contents {
68 my @c = @_;
69 my $x = join $/, @c, '';
70 local *FH;
71 my $open = open FH, "< $file";
72 my $a;
73 { local $/; $a = <FH> }
74 print (($open && $a eq $x) ? "ok $N\n" : "not ok $N # file @c\n");
75 $N++;
76
77 # now check FETCH:
78 my $good = 1;
79 for (0.. $#c) {
80 $good = 0 unless $a[$_] eq "$c[$_]\n";
81 }
82 print (($open && $good) ? "ok $N\n" : "not ok $N # fetch @c\n");
83 $N++;
84}
85
86END {
87 1 while unlink $file;
88}
89