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") ;
459 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
464 my($fk, $sk, $fv, $sv) = @_ ;
466 $fetch_key eq $fk && $store_key eq $sk &&
467 $fetch_value eq $fv && $store_value eq $sv &&
471 ok(79, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
473 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
474 $db->filter_store_key (sub { $store_key = $_ }) ;
475 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
476 $db->filter_store_value (sub { $store_value = $_ }) ;
482 ok(80, checkOutput( "", 0, "", "joe")) ;
484 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
485 ok(81, $h[0] eq "joe");
487 ok(82, checkOutput( "", 0, "joe", "")) ;
489 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
490 ok(83, $db->FIRSTKEY() == 0) ;
492 ok(84, checkOutput( 0, "", "", "")) ;
494 # replace the filters, but remember the previous set
495 my ($old_fk) = $db->filter_fetch_key
496 (sub { ++ $_ ; $fetch_key = $_ }) ;
497 my ($old_sk) = $db->filter_store_key
498 (sub { $_ *= 2 ; $store_key = $_ }) ;
499 my ($old_fv) = $db->filter_fetch_value
500 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
501 my ($old_sv) = $db->filter_store_value
502 (sub { s/o/x/g; $store_value = $_ }) ;
504 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
507 ok(85, checkOutput( "", 2, "", "Jxe")) ;
509 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
510 ok(86, $h[1] eq "[Jxe]");
512 ok(87, checkOutput( "", 2, "[Jxe]", "")) ;
514 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
515 ok(88, $db->FIRSTKEY() == 1) ;
517 ok(89, checkOutput( 1, "", "", "")) ;
519 # put the original filters back
520 $db->filter_fetch_key ($old_fk);
521 $db->filter_store_key ($old_sk);
522 $db->filter_fetch_value ($old_fv);
523 $db->filter_store_value ($old_sv);
525 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
527 ok(90, checkOutput( "", 0, "", "joe")) ;
529 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
530 ok(91, $h[0] eq "joe");
531 ok(92, checkOutput( "", 0, "joe", "")) ;
533 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
534 ok(93, $db->FIRSTKEY() == 0) ;
535 ok(94, checkOutput( 0, "", "", "")) ;
538 $db->filter_fetch_key (undef);
539 $db->filter_store_key (undef);
540 $db->filter_fetch_value (undef);
541 $db->filter_store_value (undef);
543 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
545 ok(95, checkOutput( "", "", "", "")) ;
547 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
548 ok(96, $h[0] eq "joe");
549 ok(97, checkOutput( "", "", "", "")) ;
551 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
552 ok(98, $db->FIRSTKEY() == 0) ;
553 ok(99, checkOutput( "", "", "", "")) ;
561 # DBM Filter with a closure
567 ok(100, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
577 return sub { ++$count ;
579 $result{$name} = "$name - $count: [@kept]" ;
583 $db->filter_store_key(Closure("store key")) ;
584 $db->filter_store_value(Closure("store value")) ;
585 $db->filter_fetch_key(Closure("fetch key")) ;
586 $db->filter_fetch_value(Closure("fetch value")) ;
591 ok(101, $result{"store key"} eq "store key - 1: [0]");
592 ok(102, $result{"store value"} eq "store value - 1: [joe]");
593 ok(103, ! defined $result{"fetch key"} );
594 ok(104, ! defined $result{"fetch value"} );
595 ok(105, $_ eq "original") ;
597 ok(106, $db->FIRSTKEY() == 0 ) ;
598 ok(107, $result{"store key"} eq "store key - 1: [0]");
599 ok(108, $result{"store value"} eq "store value - 1: [joe]");
600 ok(109, $result{"fetch key"} eq "fetch key - 1: [0]");
601 ok(110, ! defined $result{"fetch value"} );
602 ok(111, $_ eq "original") ;
605 ok(112, $result{"store key"} eq "store key - 2: [0 7]");
606 ok(113, $result{"store value"} eq "store value - 2: [joe john]");
607 ok(114, $result{"fetch key"} eq "fetch key - 1: [0]");
608 ok(115, ! defined $result{"fetch value"} );
609 ok(116, $_ eq "original") ;
611 ok(117, $h[0] eq "joe");
612 ok(118, $result{"store key"} eq "store key - 3: [0 7 0]");
613 ok(119, $result{"store value"} eq "store value - 2: [joe john]");
614 ok(120, $result{"fetch key"} eq "fetch key - 1: [0]");
615 ok(121, $result{"fetch value"} eq "fetch value - 1: [joe]");
616 ok(122, $_ eq "original") ;
624 # DBM Filter recursion detection
629 ok(123, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
631 $db->filter_store_key (sub { $_ = $h[0] }) ;
633 eval '$h[1] = 1234' ;
634 ok(124, $@ =~ /^recursion detected in filter_store_key at/ );