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