perl 5.003_07: t/lib/anydbm.t t/lib/db-btree.t t/lib/db-hash.t t/lib/db-recno.t t...
[p5sagit/p5-mst-13.2.git] / t / lib / db-recno.t
CommitLineData
f6b705ef 1#!./perl -w
a0d0e21e 2
3BEGIN {
4 @INC = '../lib';
5 require Config; import Config;
6 if ($Config{'extensions'} !~ /\bDB_File\b/) {
7 print "1..0\n";
8 exit 0;
9 }
10}
11
12use DB_File;
13use Fcntl;
55d68b4a 14use strict ;
15use vars qw($dbh $Dfile) ;
a0d0e21e 16
55d68b4a 17sub ok
18{
19 my $no = shift ;
20 my $result = shift ;
a0d0e21e 21
55d68b4a 22 print "not " unless $result ;
23 print "ok $no\n" ;
24}
25
f6b705ef 26print "1..47\n";
55d68b4a 27
28my $Dfile = "recno.tmp";
29unlink $Dfile ;
a0d0e21e 30
31umask(0);
32
33# Check the interface to RECNOINFO
34
55d68b4a 35my $dbh = new DB_File::RECNOINFO ;
f6b705ef 36$^W = 0 ;
55d68b4a 37ok(1, $dbh->{bval} == undef ) ;
38ok(2, $dbh->{cachesize} == undef) ;
39ok(3, $dbh->{psize} == undef) ;
40ok(4, $dbh->{flags} == undef) ;
41ok(5, $dbh->{lorder} == undef);
42ok(6, $dbh->{reclen} == undef);
43ok(7, $dbh->{bfname} eq undef);
f6b705ef 44$^W = 0 ;
a0d0e21e 45
46$dbh->{bval} = 3000 ;
f6b705ef 47ok(8, $dbh->{bval} == 3000 );
a0d0e21e 48
49$dbh->{cachesize} = 9000 ;
f6b705ef 50ok(9, $dbh->{cachesize} == 9000 );
a0d0e21e 51
52$dbh->{psize} = 400 ;
f6b705ef 53ok(10, $dbh->{psize} == 400 );
a0d0e21e 54
55$dbh->{flags} = 65 ;
f6b705ef 56ok(11, $dbh->{flags} == 65 );
a0d0e21e 57
58$dbh->{lorder} = 123 ;
f6b705ef 59ok(12, $dbh->{lorder} == 123 );
a0d0e21e 60
61$dbh->{reclen} = 1234 ;
f6b705ef 62ok(13, $dbh->{reclen} == 1234 );
a0d0e21e 63
64$dbh->{bfname} = 1234 ;
f6b705ef 65ok(14, $dbh->{bfname} == 1234 );
a0d0e21e 66
67
68# Check that an invalid entry is caught both for store & fetch
69eval '$dbh->{fred} = 1234' ;
f6b705ef 70ok(15, $@ =~ /^DB_File::RECNOINFO::STORE - Unknown element 'fred' at/ );
55d68b4a 71eval 'my $q = $dbh->{fred}' ;
f6b705ef 72ok(16, $@ =~ /^DB_File::RECNOINFO::FETCH - Unknown element 'fred' at/ );
a0d0e21e 73
74# Now check the interface to RECNOINFO
75
55d68b4a 76my $X ;
77my @h ;
78ok(17, $X = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
a0d0e21e 79
053b5721 80ok(18, ( (stat($Dfile))[2] & 0777) == ($^O eq 'os2' ? 0666 : 0640)) ;
a0d0e21e 81
55d68b4a 82#my $l = @h ;
83my $l = $X->length ;
f6b705ef 84ok(19, !$l );
a0d0e21e 85
55d68b4a 86my @data = qw( a b c d ever f g h i j k longername m n o p) ;
a0d0e21e 87
88$h[0] = shift @data ;
f6b705ef 89ok(20, $h[0] eq 'a' );
a0d0e21e 90
55d68b4a 91my $ i;
a0d0e21e 92foreach (@data)
93 { $h[++$i] = $_ }
94
95unshift (@data, 'a') ;
96
f6b705ef 97ok(21, defined $h[1] );
98ok(22, ! defined $h[16] );
99ok(23, $X->length == @data );
a0d0e21e 100
101
102# Overwrite an entry & check fetch it
103$h[3] = 'replaced' ;
104$data[3] = 'replaced' ;
f6b705ef 105ok(24, $h[3] eq 'replaced' );
a0d0e21e 106
107#PUSH
55d68b4a 108my @push_data = qw(added to the end) ;
f6b705ef 109#my push (@h, @push_data) ;
a0d0e21e 110$X->push(@push_data) ;
111push (@data, @push_data) ;
f6b705ef 112ok(25, $h[++$i] eq 'added' );
113ok(26, $h[++$i] eq 'to' );
114ok(27, $h[++$i] eq 'the' );
115ok(28, $h[++$i] eq 'end' );
a0d0e21e 116
117# POP
f6b705ef 118my $popped = pop (@data) ;
119#my $value = pop(@h) ;
55d68b4a 120my $value = $X->pop ;
f6b705ef 121ok(29, $value eq $popped) ;
a0d0e21e 122
123# SHIFT
124#$value = shift @h
125$value = $X->shift ;
f6b705ef 126my $shifted = shift @data ;
127ok(30, $value eq $shifted );
a0d0e21e 128
129# UNSHIFT
130
131# empty list
132$X->unshift ;
f6b705ef 133ok(31, $X->length == @data );
a0d0e21e 134
55d68b4a 135my @new_data = qw(add this to the start of the array) ;
a0d0e21e 136#unshift @h, @new_data ;
137$X->unshift (@new_data) ;
138unshift (@data, @new_data) ;
f6b705ef 139ok(32, $X->length == @data );
140ok(33, $h[0] eq "add") ;
141ok(34, $h[1] eq "this") ;
142ok(35, $h[2] eq "to") ;
143ok(36, $h[3] eq "the") ;
144ok(37, $h[4] eq "start") ;
145ok(38, $h[5] eq "of") ;
146ok(39, $h[6] eq "the") ;
147ok(40, $h[7] eq "array") ;
148ok(41, $h[8] eq $data[8]) ;
a0d0e21e 149
150# SPLICE
151
152# Now both arrays should be identical
153
55d68b4a 154my $ok = 1 ;
155my $j = 0 ;
a0d0e21e 156foreach (@data)
157{
158 $ok = 0, last if $_ ne $h[$j ++] ;
159}
f6b705ef 160ok(42, $ok );
a0d0e21e 161
55d68b4a 162# Neagtive subscripts
163
164# get the last element of the array
f6b705ef 165ok(43, $h[-1] eq $data[-1] );
166ok(44, $h[-1] eq $h[$X->length -1] );
55d68b4a 167
168# get the first element using a negative subscript
169eval '$h[ - ( $X->length)] = "abcd"' ;
f6b705ef 170ok(45, $@ eq "" );
171ok(46, $h[0] eq "abcd" );
55d68b4a 172
173# now try to read before the start of the array
174eval '$h[ - (1 + $X->length)] = 1234' ;
f6b705ef 175ok(47, $@ =~ '^Modification of non-creatable array value attempted' );
55d68b4a 176
a0d0e21e 177# IMPORTANT - $X must be undefined before the untie otherwise the
178# underlying DB close routine will not get called.
179undef $X ;
180untie(@h);
181
182unlink $Dfile;
183
184exit ;