4 unshift @INC, '../lib' if -d '../lib' ;
5 require Config; import Config;
6 if ($Config{'extensions'} !~ /\bDB_File\b/) {
22 print "not " unless $result ;
28 my(@a) = unpack ("C*", $a) ;
29 my(@b) = unpack ("C*", $b) ;
31 my $len = (@a > @b ? @b : @a) ;
34 foreach $i ( 0 .. $len -1) {
35 return $a[$i] - $b[$i] if $a[$i] != $b[$i] ;
41 $Dfile = "dbbtree.tmp";
46 # Check the interface to BTREEINFO
48 my $dbh = new DB_File::BTREEINFO ;
49 ok(1, ! defined $dbh->{flags}) ;
50 ok(2, ! defined $dbh->{cachesize}) ;
51 ok(3, ! defined $dbh->{psize}) ;
52 ok(4, ! defined $dbh->{lorder}) ;
53 ok(5, ! defined $dbh->{minkeypage}) ;
54 ok(6, ! defined $dbh->{maxkeypage}) ;
55 ok(7, ! defined $dbh->{compare}) ;
56 ok(8, ! defined $dbh->{prefix}) ;
58 $dbh->{flags} = 3000 ;
59 ok(9, $dbh->{flags} == 3000) ;
61 $dbh->{cachesize} = 9000 ;
62 ok(10, $dbh->{cachesize} == 9000);
65 ok(11, $dbh->{psize} == 400) ;
68 ok(12, $dbh->{lorder} == 65) ;
70 $dbh->{minkeypage} = 123 ;
71 ok(13, $dbh->{minkeypage} == 123) ;
73 $dbh->{maxkeypage} = 1234 ;
74 ok(14, $dbh->{maxkeypage} == 1234 );
76 $dbh->{compare} = 1234 ;
77 ok(15, $dbh->{compare} == 1234) ;
79 $dbh->{prefix} = 1234 ;
80 ok(16, $dbh->{prefix} == 1234 );
82 # Check that an invalid entry is caught both for store & fetch
83 eval '$dbh->{fred} = 1234' ;
84 ok(17, $@ =~ /^DB_File::BTREEINFO::STORE - Unknown element 'fred' at/ ) ;
85 eval '$q = $dbh->{fred}' ;
86 ok(18, $@ =~ /^DB_File::BTREEINFO::FETCH - Unknown element 'fred' at/ ) ;
88 # Now check the interface to BTREE
90 ok(19, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE )) ;
92 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
93 $blksize,$blocks) = stat($Dfile);
94 ok(20, ($mode & 0777) == ($^O eq 'os2' ? 0666 : 0640) || $^O eq 'amigaos' || $^O eq 'MSWin32');
96 while (($key,$value) = each(%h)) {
101 $h{'goner1'} = 'snork';
104 ok(22, $h{'abc'} eq 'ABC' );
105 ok(23, ! defined $h{'jimmy'} ) ;
106 ok(24, ! exists $h{'jimmy'} ) ;
107 ok(25, defined $h{'abc'} ) ;
110 $h{'jkl','mno'} = "JKL\034MNO";
111 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
115 $X->STORE('b', 'B') ;
128 $h{'goner2'} = 'snork';
132 # IMPORTANT - $X must be undefined before the untie otherwise the
133 # underlying DB close routine will not get called.
138 # tie to the same file again
139 ok(26, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE)) ;
141 # Modify an entry from the previous tie
162 $h{'goner3'} = 'snork';
165 $X->DELETE('goner3');
168 @values = values(%h);
170 ok(27, $#keys == 29 && $#values == 29) ;
173 while (($key,$value) = each(%h)) {
174 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
176 $i++ if $key eq $value;
182 @keys = ('blurfl', keys(%h), 'dyick');
183 ok(29, $#keys == 31) ;
185 #Check that the keys can be retrieved in order
187 my @c = sort lexical @b ;
188 ok(30, ArrayCompare(\@b, \@c)) ;
191 ok(31, $h{'foo'} eq '' ) ;
194 #ok(32, $h{''} eq 'bar' );
197 # check cache overflow and numeric keys and contents
199 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
200 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
203 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
204 $blksize,$blocks) = stat($Dfile);
207 @h{0..200} = 200..400;
209 ok(35, join(':',200..400) eq join(':',@foo) );
211 # Now check all the non-tie specific stuff
214 # Check R_NOOVERWRITE flag will make put fail when attempting to overwrite
215 # an existing record.
217 $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
218 ok(36, $status == 1 );
220 # check that the value of the key 'x' has not been changed by the
222 ok(37, $h{'x'} eq 'X' );
225 $status = $X->put('key', 'value') ;
226 ok(38, $status == 0 );
228 #check that previous put can be retrieved
230 $status = $X->get('key', $value) ;
231 ok(39, $status == 0 );
232 ok(40, $value eq 'value' );
234 # Attempting to delete an existing key should work
236 $status = $X->del('q') ;
237 ok(41, $status == 0 );
238 #$status = $X->del('') ;
239 #ok(42, $status == 0 );
242 # Make sure that the key deleted, cannot be retrieved
243 ok(43, ! defined $h{'q'}) ;
244 ok(44, ! defined $h{''}) ;
249 ok(45, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE ));
251 # Attempting to delete a non-existant key should fail
253 $status = $X->del('joe') ;
254 ok(46, $status == 1 );
256 # Check the get interface
258 # First a non-existing key
259 $status = $X->get('aaaa', $value) ;
260 ok(47, $status == 1 );
262 # Next an existing key
263 $status = $X->get('a', $value) ;
264 ok(48, $status == 0 );
265 ok(49, $value eq 'A' );
270 # use seq to find an approximate match
273 $status = $X->seq($key, $value, R_CURSOR) ;
274 ok(50, $status == 0 );
275 ok(51, $key eq 'key' );
276 ok(52, $value eq 'value' );
278 # seq when the key does not match
281 $status = $X->seq($key, $value, R_CURSOR) ;
282 ok(53, $status == 1 );
285 # use seq to set the cursor, then delete the record @ the cursor.
289 $status = $X->seq($key, $value, R_CURSOR) ;
290 ok(54, $status == 0 );
291 ok(55, $key eq 'x' );
292 ok(56, $value eq 'X' );
293 $status = $X->del(0, R_CURSOR) ;
294 ok(57, $status == 0 );
295 $status = $X->get('x', $value) ;
296 ok(58, $status == 1 );
298 # ditto, but use put to replace the key/value pair.
301 $status = $X->seq($key, $value, R_CURSOR) ;
302 ok(59, $status == 0 );
303 ok(60, $key eq 'y' );
304 ok(61, $value eq 'Y' );
306 $key = "replace key" ;
307 $value = "replace value" ;
308 $status = $X->put($key, $value, R_CURSOR) ;
309 ok(62, $status == 0 );
310 ok(63, $key eq 'replace key' );
311 ok(64, $value eq 'replace value' );
312 $status = $X->get('y', $value) ;
313 ok(65, 1) ; # hard-wire to always pass. the previous test ($status == 1)
314 # only worked because of a bug in 1.85/6
316 # use seq to walk forwards through a file
318 $status = $X->seq($key, $value, R_FIRST) ;
319 ok(66, $status == 0 );
323 while (($status = $X->seq($key, $value, R_NEXT)) == 0)
325 ($ok = 0), last if ($previous cmp $key) == 1 ;
328 ok(67, $status == 1 );
331 # use seq to walk backwards through a file
332 $status = $X->seq($key, $value, R_LAST) ;
333 ok(69, $status == 0 );
337 while (($status = $X->seq($key, $value, R_PREV)) == 0)
339 ($ok = 0), last if ($previous cmp $key) == -1 ;
340 #print "key = [$key] value = [$value]\n" ;
343 ok(70, $status == 1 );
347 # check seq FIRST/LAST
353 ok(72, $status == 0 );
360 ok(73, $status != 0 );
368 # Now try an in memory file
369 ok(74, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE ));
371 # fd with an in memory file should return failure
373 ok(75, $status == -1 );
380 my $bt = new DB_File::BTREEINFO ;
381 $bt->{flags} = R_DUP ;
382 ok(76, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ;
384 $hh{'Wall'} = 'Larry' ;
385 $hh{'Wall'} = 'Stone' ; # Note the duplicate key
386 $hh{'Wall'} = 'Brick' ; # Note the duplicate key
387 $hh{'Wall'} = 'Brick' ; # Note the duplicate key and value
388 $hh{'Smith'} = 'John' ;
389 $hh{'mouse'} = 'mickey' ;
391 # first work in scalar context
392 ok(77, scalar $YY->get_dup('Unknown') == 0 );
393 ok(78, scalar $YY->get_dup('Smith') == 1 );
394 ok(79, scalar $YY->get_dup('Wall') == 4 );
396 # now in list context
397 my @unknown = $YY->get_dup('Unknown') ;
398 ok(80, "@unknown" eq "" );
400 my @smith = $YY->get_dup('Smith') ;
401 ok(81, "@smith" eq "John" );
404 my @wall = $YY->get_dup('Wall') ;
406 @wall{@wall} = @wall ;
407 ok(82, (@wall == 4 && $wall{'Larry'} && $wall{'Stone'} && $wall{'Brick'}) );
411 my %unknown = $YY->get_dup('Unknown', 1) ;
412 ok(83, keys %unknown == 0 );
414 my %smith = $YY->get_dup('Smith', 1) ;
415 ok(84, keys %smith == 1 && $smith{'John'}) ;
417 my %wall = $YY->get_dup('Wall', 1) ;
418 ok(85, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1
419 && $wall{'Brick'} == 2);
426 # test multiple callbacks
431 $dbh1 = new DB_File::BTREEINFO ;
433 $dbh1->{compare} = sub { $_[0] <=> $_[1] } ; }
435 $dbh2 = new DB_File::BTREEINFO ;
436 $dbh2->{compare} = sub { $_[0] cmp $_[1] } ;
438 $dbh3 = new DB_File::BTREEINFO ;
439 $dbh3->{compare} = sub { length $_[0] <=> length $_[1] } ;
442 tie(%h, 'DB_File',$Dfile1, O_RDWR|O_CREAT, 0640, $dbh1 ) ;
443 tie(%g, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) ;
444 tie(%k, 'DB_File',$Dfile3, O_RDWR|O_CREAT, 0640, $dbh3 ) ;
446 @Keys = qw( 0123 12 -1234 9 987654321 def ) ;
448 @srt_1 = sort { $a <=> $b } @Keys ; }
449 @srt_2 = sort { $a cmp $b } @Keys ;
450 @srt_3 = sort { length $a <=> length $b } @Keys ;
463 return 0 if @$a != @$b ;
465 foreach (1 .. length @$a)
467 return 0 unless $$a[$_] eq $$b[$_] ;
473 ok(86, ArrayCompare (\@srt_1, [keys %h]) );
474 ok(87, ArrayCompare (\@srt_2, [keys %g]) );
475 ok(88, ArrayCompare (\@srt_3, [keys %k]) );
480 unlink $Dfile1, $Dfile2, $Dfile3 ;
485 ok(89, tie(%h, 'DB_File', $Dfile1, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
487 { $h{$_} = $_ * 100 }
489 # check that there are 10 elements in the hash
491 while (($key,$value) = each(%h)) {
501 while (($key,$value) = each(%h)) {
510 # check that attempting to tie an array to a DB_BTREE will fail
512 my $filename = "xyz" ;
514 eval { tie @x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE ; } ;
515 ok(92, $@ =~ /^DB_File can only tie an associative array to a DB_BTREE database/) ;
526 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
532 use vars qw( @ISA @EXPORT) ;
537 @EXPORT = @DB_File::EXPORT ;
543 $self->SUPER::STORE($key, $value * 2) ;
549 $self->SUPER::FETCH($key) - 1 ;
556 $self->SUPER::put($key, $value * 3) ;
561 $self->SUPER::get($_[0], $_[1]) ;
569 my $value = $self->FETCH($key) ;
570 return "[[$value]]" ;
578 BEGIN { push @INC, '.'; }
580 main::ok(93, $@ eq "") ;
584 $X = tie(%h, "SubDB","dbbtree.tmp", O_RDWR|O_CREAT, 0640, $DB_BTREE );
587 main::ok(94, $@ eq "") ;
589 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
590 main::ok(95, $@ eq "") ;
591 main::ok(96, $ret == 5) ;
594 $ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ;
595 main::ok(97, $@ eq "") ;
596 main::ok(98, $ret == 10) ;
598 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
599 main::ok(99, $@ eq "" ) ;
600 main::ok(100, $ret == 1) ;
602 $ret = eval '$X->A_new_method("joe") ' ;
603 main::ok(101, $@ eq "") ;
604 main::ok(102, $ret eq "[[11]]") ;
608 unlink "SubDB.pm", "dbbtree.tmp" ;