summaryrefslogtreecommitdiff
path: root/dist/Tie-File/t/05_size.t
blob: a8df07b42e1d092de079f6466c2f4c898275996e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/perl
#
# Check FETCHSIZE and SETSIZE functions
# PUSH POP SHIFT UNSHIFT
#

use strict;
use warnings;

use POSIX 'SEEK_SET';

my $file = "tf05-$$.txt";
my ($o, $n);

print "1..16\n";

my $N = 1;
use Tie::File;
print "ok $N\n"; $N++;

# 2-3 FETCHSIZE 0-length file
open F, '>', $file or die $!;
binmode F;
close F;

my @a;
$o = tie @a, 'Tie::File', $file;
print $o ? "ok $N\n" : "not ok $N\n";
$N++;

$: = $o->{recsep};

$n = @a;
print $n == 0 ? "ok $N\n" : "not ok $N # $n, s/b 0\n";
$N++;

# Reset everything
undef $o;
untie @a;

my $data = "rec0$:rec1$:rec2$:";
open F, '>', $file or die $!;
binmode F;
print F $data;
close F;

$o = tie @a, 'Tie::File', $file;
print $o ? "ok $N\n" : "not ok $N\n";
$N++;

# 4-5 FETCHSIZE positive-length file
$n = @a;
print $n == 3 ? "ok $N\n" : "not ok $N # $n, s/b 0\n";
$N++;

# STORESIZE
# (6-7) Make it longer:
populate();
$#a = 4;
check_contents("$data$:$:");

# (8-9) Make it longer again:
populate();
$#a = 6;
check_contents("$data$:$:$:$:");

# (10-11) Make it shorter:
populate();
$#a = 4;
check_contents("$data$:$:");

# (12-13) Make it shorter again:
populate();
$#a = 2;
check_contents($data);

# (14-15) Get rid of it completely:
populate();
$#a = -1;
check_contents('');

# (16) 20020324 I have an idea that shortening the array will not
# expunge a cached record at the end if one is present.
$o->defer;
$a[3] = "record";
my $r = $a[3];
$#a = -1;
$r = $a[3];
print (! defined $r ? "ok $N\n" : "not ok $N \# was <$r>; should be UNDEF\n");
# Turns out not to be the case---STORESIZE explicitly removes them later
# 20020326 Well, but happily, this test did fail today.

# In the past, there was a bug in STORESIZE that it didn't correctly
# remove deleted records from the cache.  This wasn't detected
# because these tests were all done with an empty cache.  populate()
# will ensure that the cache is fully populated.
sub populate {
  my $z;
  $z = $a[$_] for 0 .. $#a;
}

sub check_contents {
  my $x = shift;
  local *FH = $o->{fh};
  seek FH, 0, SEEK_SET;
  my $a;
  { local $/; $a = <FH> }
  $a = "" unless defined $a;
  if ($a eq $x) {
    print "ok $N\n";
  } else {
    ctrlfix($a, $x);
    print "not ok $N\n# expected <$x>, got <$a>\n";
  }
  $N++;
  my $integrity = $o->_check_integrity($file, $ENV{INTEGRITY});
  print $integrity ? "ok $N\n" : "not ok $N \# integrity\n";
  $N++;
}


sub ctrlfix {
  for (@_) {
    s/\n/\\n/g;
    s/\r/\\r/g;
  }
}

END {
  undef $o;
  untie @a;
  1 while unlink $file;
}