Fix label on C<for(;;)> statement
[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 ;
25268f15 15use vars qw($dbh $Dfile $bad_ones) ;
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" ;
6250ba0a 24
25 return $result ;
26}
27
28sub bad_one
29{
25268f15 30 print STDERR <<EOM unless $bad_ones++ ;
31#
6250ba0a 32# Some older versions of Berkeley DB will fail tests 51, 53 and 55.
33#
34# You can safely ignore the errors if you're never going to use the
35# broken functionality (recno databases with a modified bval).
36# Otherwise you'll have to upgrade your DB library.
37#
38# If you want to upgrade Berkeley DB, the most recent version is 1.85.
39# Check out http://www.bostic.com/db for more details.
40#
41EOM
55d68b4a 42}
43
36477c24 44print "1..55\n";
55d68b4a 45
46my $Dfile = "recno.tmp";
47unlink $Dfile ;
a0d0e21e 48
49umask(0);
50
51# Check the interface to RECNOINFO
52
55d68b4a 53my $dbh = new DB_File::RECNOINFO ;
3fe9a6f1 54ok(1, ! defined $dbh->{bval}) ;
55ok(2, ! defined $dbh->{cachesize}) ;
56ok(3, ! defined $dbh->{psize}) ;
57ok(4, ! defined $dbh->{flags}) ;
58ok(5, ! defined $dbh->{lorder}) ;
59ok(6, ! defined $dbh->{reclen}) ;
60ok(7, ! defined $dbh->{bfname}) ;
a0d0e21e 61
62$dbh->{bval} = 3000 ;
f6b705ef 63ok(8, $dbh->{bval} == 3000 );
a0d0e21e 64
65$dbh->{cachesize} = 9000 ;
f6b705ef 66ok(9, $dbh->{cachesize} == 9000 );
a0d0e21e 67
68$dbh->{psize} = 400 ;
f6b705ef 69ok(10, $dbh->{psize} == 400 );
a0d0e21e 70
71$dbh->{flags} = 65 ;
f6b705ef 72ok(11, $dbh->{flags} == 65 );
a0d0e21e 73
74$dbh->{lorder} = 123 ;
f6b705ef 75ok(12, $dbh->{lorder} == 123 );
a0d0e21e 76
77$dbh->{reclen} = 1234 ;
f6b705ef 78ok(13, $dbh->{reclen} == 1234 );
a0d0e21e 79
80$dbh->{bfname} = 1234 ;
f6b705ef 81ok(14, $dbh->{bfname} == 1234 );
a0d0e21e 82
83
84# Check that an invalid entry is caught both for store & fetch
85eval '$dbh->{fred} = 1234' ;
f6b705ef 86ok(15, $@ =~ /^DB_File::RECNOINFO::STORE - Unknown element 'fred' at/ );
55d68b4a 87eval 'my $q = $dbh->{fred}' ;
f6b705ef 88ok(16, $@ =~ /^DB_File::RECNOINFO::FETCH - Unknown element 'fred' at/ );
a0d0e21e 89
90# Now check the interface to RECNOINFO
91
55d68b4a 92my $X ;
93my @h ;
94ok(17, $X = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
a0d0e21e 95
b971f6e4 96ok(18, ((stat($Dfile))[2] & 0777) == ($^O eq 'os2' ? 0666 : 0640)
97 || $^O eq 'amigaos') ;
a0d0e21e 98
55d68b4a 99#my $l = @h ;
100my $l = $X->length ;
f6b705ef 101ok(19, !$l );
a0d0e21e 102
55d68b4a 103my @data = qw( a b c d ever f g h i j k longername m n o p) ;
a0d0e21e 104
105$h[0] = shift @data ;
f6b705ef 106ok(20, $h[0] eq 'a' );
a0d0e21e 107
55d68b4a 108my $ i;
a0d0e21e 109foreach (@data)
110 { $h[++$i] = $_ }
111
112unshift (@data, 'a') ;
113
f6b705ef 114ok(21, defined $h[1] );
115ok(22, ! defined $h[16] );
116ok(23, $X->length == @data );
a0d0e21e 117
118
119# Overwrite an entry & check fetch it
120$h[3] = 'replaced' ;
121$data[3] = 'replaced' ;
f6b705ef 122ok(24, $h[3] eq 'replaced' );
a0d0e21e 123
124#PUSH
55d68b4a 125my @push_data = qw(added to the end) ;
f6b705ef 126#my push (@h, @push_data) ;
a0d0e21e 127$X->push(@push_data) ;
128push (@data, @push_data) ;
f6b705ef 129ok(25, $h[++$i] eq 'added' );
130ok(26, $h[++$i] eq 'to' );
131ok(27, $h[++$i] eq 'the' );
132ok(28, $h[++$i] eq 'end' );
a0d0e21e 133
134# POP
f6b705ef 135my $popped = pop (@data) ;
136#my $value = pop(@h) ;
55d68b4a 137my $value = $X->pop ;
f6b705ef 138ok(29, $value eq $popped) ;
a0d0e21e 139
140# SHIFT
141#$value = shift @h
142$value = $X->shift ;
f6b705ef 143my $shifted = shift @data ;
144ok(30, $value eq $shifted );
a0d0e21e 145
146# UNSHIFT
147
148# empty list
149$X->unshift ;
f6b705ef 150ok(31, $X->length == @data );
a0d0e21e 151
55d68b4a 152my @new_data = qw(add this to the start of the array) ;
a0d0e21e 153#unshift @h, @new_data ;
154$X->unshift (@new_data) ;
155unshift (@data, @new_data) ;
f6b705ef 156ok(32, $X->length == @data );
157ok(33, $h[0] eq "add") ;
158ok(34, $h[1] eq "this") ;
159ok(35, $h[2] eq "to") ;
160ok(36, $h[3] eq "the") ;
161ok(37, $h[4] eq "start") ;
162ok(38, $h[5] eq "of") ;
163ok(39, $h[6] eq "the") ;
164ok(40, $h[7] eq "array") ;
165ok(41, $h[8] eq $data[8]) ;
a0d0e21e 166
167# SPLICE
168
169# Now both arrays should be identical
170
55d68b4a 171my $ok = 1 ;
172my $j = 0 ;
a0d0e21e 173foreach (@data)
174{
175 $ok = 0, last if $_ ne $h[$j ++] ;
176}
f6b705ef 177ok(42, $ok );
a0d0e21e 178
55d68b4a 179# Neagtive subscripts
180
181# get the last element of the array
f6b705ef 182ok(43, $h[-1] eq $data[-1] );
183ok(44, $h[-1] eq $h[$X->length -1] );
55d68b4a 184
185# get the first element using a negative subscript
186eval '$h[ - ( $X->length)] = "abcd"' ;
f6b705ef 187ok(45, $@ eq "" );
188ok(46, $h[0] eq "abcd" );
55d68b4a 189
190# now try to read before the start of the array
191eval '$h[ - (1 + $X->length)] = 1234' ;
f6b705ef 192ok(47, $@ =~ '^Modification of non-creatable array value attempted' );
55d68b4a 193
a0d0e21e 194# IMPORTANT - $X must be undefined before the untie otherwise the
195# underlying DB close routine will not get called.
196undef $X ;
197untie(@h);
198
199unlink $Dfile;
200
36477c24 201{
202 # Check bval defaults to \n
203
204 my @h = () ;
205 my $dbh = new DB_File::RECNOINFO ;
206 ok(48, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
207 $h[0] = "abc" ;
208 $h[1] = "def" ;
209 $h[3] = "ghi" ;
210 untie @h ;
211 my $x = `cat $Dfile` ;
36477c24 212 unlink $Dfile;
6250ba0a 213 ok(49, $x eq "abc\ndef\n\nghi\n") ;
36477c24 214}
215
216{
217 # Change bval
218
219 my @h = () ;
220 my $dbh = new DB_File::RECNOINFO ;
221 $dbh->{bval} = "-" ;
222 ok(50, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
223 $h[0] = "abc" ;
224 $h[1] = "def" ;
225 $h[3] = "ghi" ;
226 untie @h ;
227 my $x = `cat $Dfile` ;
36477c24 228 unlink $Dfile;
6250ba0a 229 my $ok = ($x eq "abc-def--ghi-") ;
230 bad_one() unless $ok ;
231 ok(51, $ok) ;
36477c24 232}
233
234{
235 # Check R_FIXEDLEN with default bval (space)
236
237 my @h = () ;
238 my $dbh = new DB_File::RECNOINFO ;
239 $dbh->{flags} = R_FIXEDLEN ;
240 $dbh->{reclen} = 5 ;
241 ok(52, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
242 $h[0] = "abc" ;
243 $h[1] = "def" ;
244 $h[3] = "ghi" ;
245 untie @h ;
246 my $x = `cat $Dfile` ;
36477c24 247 unlink $Dfile;
6250ba0a 248 my $ok = ($x eq "abc def ghi ") ;
249 bad_one() unless $ok ;
250 ok(53, $ok) ;
36477c24 251}
252
253{
254 # Check R_FIXEDLEN with user-defined bval
255
256 my @h = () ;
257 my $dbh = new DB_File::RECNOINFO ;
258 $dbh->{flags} = R_FIXEDLEN ;
259 $dbh->{bval} = "-" ;
260 $dbh->{reclen} = 5 ;
261 ok(54, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
262 $h[0] = "abc" ;
263 $h[1] = "def" ;
264 $h[3] = "ghi" ;
265 untie @h ;
266 my $x = `cat $Dfile` ;
36477c24 267 unlink $Dfile;
6250ba0a 268 my $ok = ($x eq "abc--def-------ghi--") ;
269 bad_one() unless $ok ;
270 ok(55, $ok) ;
36477c24 271}
272
a0d0e21e 273exit ;