4 @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, $dbh->{flags} == 0) ;
50 ok(2, $dbh->{cachesize} == 0) ;
51 ok(3, $dbh->{psize} == 0) ;
52 ok(4, $dbh->{lorder} == 0) ;
53 ok(5, $dbh->{minkeypage} == 0) ;
54 ok(6, $dbh->{maxkeypage} == 0) ;
57 ok(7, $dbh->{compare} == undef) ;
58 ok(8, $dbh->{prefix} == undef) ;
61 $dbh->{flags} = 3000 ;
62 ok(9, $dbh->{flags} == 3000) ;
64 $dbh->{cachesize} = 9000 ;
65 ok(10, $dbh->{cachesize} == 9000);
68 ok(11, $dbh->{psize} == 400) ;
71 ok(12, $dbh->{lorder} == 65) ;
73 $dbh->{minkeypage} = 123 ;
74 ok(13, $dbh->{minkeypage} == 123) ;
76 $dbh->{maxkeypage} = 1234 ;
77 ok(14, $dbh->{maxkeypage} == 1234 );
79 $dbh->{compare} = 1234 ;
80 ok(15, $dbh->{compare} == 1234) ;
82 $dbh->{prefix} = 1234 ;
83 ok(16, $dbh->{prefix} == 1234 );
85 # Check that an invalid entry is caught both for store & fetch
86 eval '$dbh->{fred} = 1234' ;
87 ok(17, $@ =~ /^DB_File::BTREEINFO::STORE - Unknown element 'fred' at/ ) ;
88 eval '$q = $dbh->{fred}' ;
89 ok(18, $@ =~ /^DB_File::BTREEINFO::FETCH - Unknown element 'fred' at/ ) ;
91 # Now check the interface to BTREE
93 ok(19, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE )) ;
95 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
96 $blksize,$blocks) = stat($Dfile);
97 ok(20, ($mode & 0777) == ($^O eq 'os2' ? 0666 : 0640) || $^O eq 'amigaos');
99 while (($key,$value) = each(%h)) {
104 $h{'goner1'} = 'snork';
107 ok(22, $h{'abc'} eq 'ABC' );
108 ok(23, ! defined $h{'jimmy'} ) ;
109 ok(24, ! exists $h{'jimmy'} ) ;
110 ok(25, defined $h{'abc'} ) ;
113 $h{'jkl','mno'} = "JKL\034MNO";
114 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
118 $X->STORE('b', 'B') ;
131 $h{'goner2'} = 'snork';
135 # IMPORTANT - $X must be undefined before the untie otherwise the
136 # underlying DB close routine will not get called.
141 # tie to the same file again
142 ok(26, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE)) ;
144 # Modify an entry from the previous tie
165 $h{'goner3'} = 'snork';
168 $X->DELETE('goner3');
171 @values = values(%h);
173 ok(27, $#keys == 29 && $#values == 29) ;
176 while (($key,$value) = each(%h)) {
177 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
179 $i++ if $key eq $value;
185 @keys = ('blurfl', keys(%h), 'dyick');
186 ok(29, $#keys == 31) ;
188 #Check that the keys can be retrieved in order
190 my @c = sort lexical @b ;
191 ok(30, ArrayCompare(\@b, \@c)) ;
194 ok(31, $h{'foo'} eq '' ) ;
197 ok(32, $h{''} eq 'bar' );
199 # check cache overflow and numeric keys and contents
201 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
202 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
205 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
206 $blksize,$blocks) = stat($Dfile);
209 @h{0..200} = 200..400;
211 ok(35, join(':',200..400) eq join(':',@foo) );
213 # Now check all the non-tie specific stuff
216 # Check R_NOOVERWRITE flag will make put fail when attempting to overwrite
217 # an existing record.
219 $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
220 ok(36, $status == 1 );
222 # check that the value of the key 'x' has not been changed by the
224 ok(37, $h{'x'} eq 'X' );
227 $status = $X->put('key', 'value') ;
228 ok(38, $status == 0 );
230 #check that previous put can be retrieved
232 $status = $X->get('key', $value) ;
233 ok(39, $status == 0 );
234 ok(40, $value eq 'value' );
236 # Attempting to delete an existing key should work
238 $status = $X->del('q') ;
239 ok(41, $status == 0 );
240 $status = $X->del('') ;
241 ok(42, $status == 0 );
243 # Make sure that the key deleted, cannot be retrieved
244 ok(43, ! defined $h{'q'}) ;
245 ok(44, ! defined $h{''}) ;
250 ok(45, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE ));
252 # Attempting to delete a non-existant key should fail
254 $status = $X->del('joe') ;
255 ok(46, $status == 1 );
257 # Check the get interface
259 # First a non-existing key
260 $status = $X->get('aaaa', $value) ;
261 ok(47, $status == 1 );
263 # Next an existing key
264 $status = $X->get('a', $value) ;
265 ok(48, $status == 0 );
266 ok(49, $value eq 'A' );
271 # use seq to find an approximate match
274 $status = $X->seq($key, $value, R_CURSOR) ;
275 ok(50, $status == 0 );
276 ok(51, $key eq 'key' );
277 ok(52, $value eq 'value' );
279 # seq when the key does not match
282 $status = $X->seq($key, $value, R_CURSOR) ;
283 ok(53, $status == 1 );
286 # use seq to set the cursor, then delete the record @ the cursor.
290 $status = $X->seq($key, $value, R_CURSOR) ;
291 ok(54, $status == 0 );
292 ok(55, $key eq 'x' );
293 ok(56, $value eq 'X' );
294 $status = $X->del(0, R_CURSOR) ;
295 ok(57, $status == 0 );
296 $status = $X->get('x', $value) ;
297 ok(58, $status == 1 );
299 # ditto, but use put to replace the key/value pair.
302 $status = $X->seq($key, $value, R_CURSOR) ;
303 ok(59, $status == 0 );
304 ok(60, $key eq 'y' );
305 ok(61, $value eq 'Y' );
307 $key = "replace key" ;
308 $value = "replace value" ;
309 $status = $X->put($key, $value, R_CURSOR) ;
310 ok(62, $status == 0 );
311 ok(63, $key eq 'replace key' );
312 ok(64, $value eq 'replace value' );
313 $status = $X->get('y', $value) ;
314 ok(65, $status == 1 );
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)) {