4 unshift @INC, '../lib' if -d '../lib' ;
5 require Config; import Config;
6 if ($Config{'extensions'} !~ /\bDB_File\b/) {
15 use vars qw($dbh $Dfile $bad_ones $FA) ;
17 # full tied array support started in Perl 5.004_57
18 # Double check to see if it is available.
21 sub try::TIEARRAY { bless [], "try" }
22 sub try::FETCHSIZE { $FA = 1 }
35 print "not " unless $result ;
43 print STDERR <<EOM unless $bad_ones++ ;
45 # Some older versions of Berkeley DB version 1 will fail tests 51,
48 # You can safely ignore the errors if you're never going to use the
49 # broken functionality (recno databases with a modified bval).
50 # Otherwise you'll have to upgrade your DB library.
52 # If you want to use Berkeley DB version 1, then 1.85 and 1.86 are the
53 # last versions that were released. Berkeley DB version 2 is continually
54 # being updated -- Check out http://www.sleepycat.com/ for more details.
61 my $Dfile = "recno.tmp";
66 # Check the interface to RECNOINFO
68 my $dbh = new DB_File::RECNOINFO ;
69 ok(1, ! defined $dbh->{bval}) ;
70 ok(2, ! defined $dbh->{cachesize}) ;
71 ok(3, ! defined $dbh->{psize}) ;
72 ok(4, ! defined $dbh->{flags}) ;
73 ok(5, ! defined $dbh->{lorder}) ;
74 ok(6, ! defined $dbh->{reclen}) ;
75 ok(7, ! defined $dbh->{bfname}) ;
78 ok(8, $dbh->{bval} == 3000 );
80 $dbh->{cachesize} = 9000 ;
81 ok(9, $dbh->{cachesize} == 9000 );
84 ok(10, $dbh->{psize} == 400 );
87 ok(11, $dbh->{flags} == 65 );
89 $dbh->{lorder} = 123 ;
90 ok(12, $dbh->{lorder} == 123 );
92 $dbh->{reclen} = 1234 ;
93 ok(13, $dbh->{reclen} == 1234 );
95 $dbh->{bfname} = 1234 ;
96 ok(14, $dbh->{bfname} == 1234 );
99 # Check that an invalid entry is caught both for store & fetch
100 eval '$dbh->{fred} = 1234' ;
101 ok(15, $@ =~ /^DB_File::RECNOINFO::STORE - Unknown element 'fred' at/ );
102 eval 'my $q = $dbh->{fred}' ;
103 ok(16, $@ =~ /^DB_File::RECNOINFO::FETCH - Unknown element 'fred' at/ );
105 # Now check the interface to RECNOINFO
109 ok(17, $X = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
111 ok(18, ((stat($Dfile))[2] & 0777) == ($^O eq 'os2' ? 0666 : 0640)
112 || $^O eq 'MSWin32' || $^O eq 'amigaos') ;
116 ok(19, ($FA ? @h == 0 : !$l) );
118 my @data = qw( a b c d ever f g h i j k longername m n o p) ;
120 $h[0] = shift @data ;
121 ok(20, $h[0] eq 'a' );
127 unshift (@data, 'a') ;
129 ok(21, defined $h[1] );
130 ok(22, ! defined $h[16] );
131 ok(23, $FA ? @h == @data : $X->length == @data );
134 # Overwrite an entry & check fetch it
136 $data[3] = 'replaced' ;
137 ok(24, $h[3] eq 'replaced' );
140 my @push_data = qw(added to the end) ;
141 ($FA ? push(@h, @push_data) : $X->push(@push_data)) ;
142 push (@data, @push_data) ;
143 ok(25, $h[++$i] eq 'added' );
144 ok(26, $h[++$i] eq 'to' );
145 ok(27, $h[++$i] eq 'the' );
146 ok(28, $h[++$i] eq 'end' );
149 my $popped = pop (@data) ;
150 my $value = ($FA ? pop @h : $X->pop) ;
151 ok(29, $value eq $popped) ;
154 $value = ($FA ? shift @h : $X->shift) ;
155 my $shifted = shift @data ;
156 ok(30, $value eq $shifted );
161 ($FA ? unshift @h : $X->unshift) ;
162 ok(31, ($FA ? @h == @data : $X->length == @data ));
164 my @new_data = qw(add this to the start of the array) ;
165 $FA ? unshift (@h, @new_data) : $X->unshift (@new_data) ;
166 unshift (@data, @new_data) ;
167 ok(32, $FA ? @h == @data : $X->length == @data );
168 ok(33, $h[0] eq "add") ;
169 ok(34, $h[1] eq "this") ;
170 ok(35, $h[2] eq "to") ;
171 ok(36, $h[3] eq "the") ;
172 ok(37, $h[4] eq "start") ;
173 ok(38, $h[5] eq "of") ;
174 ok(39, $h[6] eq "the") ;
175 ok(40, $h[7] eq "array") ;
176 ok(41, $h[8] eq $data[8]) ;
180 # Now both arrays should be identical
186 $ok = 0, last if $_ ne $h[$j ++] ;
190 # Neagtive subscripts
192 # get the last element of the array
193 ok(43, $h[-1] eq $data[-1] );
194 ok(44, $h[-1] eq $h[ ($FA ? @h : $X->length) -1] );
196 # get the first element using a negative subscript
197 eval '$h[ - ( $FA ? @h : $X->length)] = "abcd"' ;
199 ok(46, $h[0] eq "abcd" );
201 # now try to read before the start of the array
202 eval '$h[ - (1 + ($FA ? @h : $X->length))] = 1234' ;
203 ok(47, $@ =~ '^Modification of non-creatable array value attempted' );
205 # IMPORTANT - $X must be undefined before the untie otherwise the
206 # underlying DB close routine will not get called.
216 open(CAT,$file) || die "Cannot open $file:$!";
224 # Check bval defaults to \n
227 my $dbh = new DB_File::RECNOINFO ;
228 ok(48, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
233 my $x = docat($Dfile) ;
235 ok(49, $x eq "abc\ndef\n\nghi\n") ;
242 my $dbh = new DB_File::RECNOINFO ;
244 ok(50, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
249 my $x = docat($Dfile) ;
251 my $ok = ($x eq "abc-def--ghi-") ;
252 bad_one() unless $ok ;
257 # Check R_FIXEDLEN with default bval (space)
260 my $dbh = new DB_File::RECNOINFO ;
261 $dbh->{flags} = R_FIXEDLEN ;
263 ok(52, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
268 my $x = docat($Dfile) ;
270 my $ok = ($x eq "abc def ghi ") ;
271 bad_one() unless $ok ;
276 # Check R_FIXEDLEN with user-defined bval
279 my $dbh = new DB_File::RECNOINFO ;
280 $dbh->{flags} = R_FIXEDLEN ;
283 ok(54, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
288 my $x = docat($Dfile) ;
290 my $ok = ($x eq "abc--def-------ghi--") ;
291 bad_one() unless $ok ;
296 # check that attempting to tie an associative array to a DB_RECNO will fail
298 my $filename = "xyz" ;
300 eval { tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO ; } ;
301 ok(56, $@ =~ /^DB_File can only tie an array to a DB_RECNO database/) ;
312 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
318 use vars qw( @ISA @EXPORT) ;
323 @EXPORT = @DB_File::EXPORT ;
329 $self->SUPER::STORE($key, $value * 2) ;
335 $self->SUPER::FETCH($key) - 1 ;
342 $self->SUPER::put($key, $value * 3) ;
347 $self->SUPER::get($_[0], $_[1]) ;
355 my $value = $self->FETCH($key) ;
356 return "[[$value]]" ;
364 BEGIN { push @INC, '.'; }
366 main::ok(57, $@ eq "") ;
370 $X = tie(@h, "SubDB","recno.tmp", O_RDWR|O_CREAT, 0640, $DB_RECNO );
373 main::ok(58, $@ eq "") ;
375 my $ret = eval '$h[3] = 3 ; return $h[3] ' ;
376 main::ok(59, $@ eq "") ;
377 main::ok(60, $ret == 5) ;
380 $ret = eval '$X->put(1, 4) ; $X->get(1, $value) ; return $value' ;
381 main::ok(61, $@ eq "") ;
382 main::ok(62, $ret == 10) ;
384 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
385 main::ok(63, $@ eq "" ) ;
386 main::ok(64, $ret == 1) ;
388 $ret = eval '$X->A_new_method(1) ' ;
389 main::ok(65, $@ eq "") ;
390 main::ok(66, $ret eq "[[11]]") ;
394 unlink "SubDB.pm", "recno.tmp" ;
403 ok(67, $self = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
408 ok(68, $FA ? $#h == 3 : $self->length() == 4) ;
411 my $x = docat($Dfile) ;
412 ok(69, $x eq "abc\ndef\nghi\njkl\n") ;
414 # $# sets array to same length
415 ok(70, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
419 { $self->STORESIZE(4) }
420 ok(71, $FA ? $#h == 3 : $self->length() == 4) ;
424 ok(72, $x eq "abc\ndef\nghi\njkl\n") ;
426 # $# sets array to bigger
427 ok(73, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
431 { $self->STORESIZE(7) }
432 ok(74, $FA ? $#h == 6 : $self->length() == 7) ;
436 ok(75, $x eq "abc\ndef\nghi\njkl\n\n\n\n") ;
438 # $# sets array smaller
439 ok(76, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
443 { $self->STORESIZE(3) }
444 ok(77, $FA ? $#h == 2 : $self->length() == 3) ;
448 ok(78, $x eq "abc\ndef\nghi\n") ;