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) ;
56 ok(7, $dbh->{compare} == undef) ;
57 ok(8, $dbh->{prefix} == undef) ;
60 $dbh->{flags} = 3000 ;
61 ok(9, $dbh->{flags} == 3000) ;
63 $dbh->{cachesize} = 9000 ;
64 ok(10, $dbh->{cachesize} == 9000);
67 ok(11, $dbh->{psize} == 400) ;
70 ok(12, $dbh->{lorder} == 65) ;
72 $dbh->{minkeypage} = 123 ;
73 ok(13, $dbh->{minkeypage} == 123) ;
75 $dbh->{maxkeypage} = 1234 ;
76 ok(14, $dbh->{maxkeypage} == 1234 );
78 $dbh->{compare} = 1234 ;
79 ok(15, $dbh->{compare} == 1234) ;
81 $dbh->{prefix} = 1234 ;
82 ok(16, $dbh->{prefix} == 1234 );
84 # Check that an invalid entry is caught both for store & fetch
85 eval '$dbh->{fred} = 1234' ;
86 ok(17, $@ =~ /^DB_File::BTREEINFO::STORE - Unknown element 'fred' at/ ) ;
87 eval '$q = $dbh->{fred}' ;
88 ok(18, $@ =~ /^DB_File::BTREEINFO::FETCH - Unknown element 'fred' at/ ) ;
90 # Now check the interface to BTREE
92 ok(19, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE )) ;
94 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
95 $blksize,$blocks) = stat($Dfile);
96 ok(20, ($mode & 0777) == ($^O eq 'os2' ? 0666 : 0640) );
98 while (($key,$value) = each(%h)) {
103 $h{'goner1'} = 'snork';
106 ok(22, $h{'abc'} eq 'ABC' );
107 ok(23, ! defined $h{'jimmy'} ) ;
108 ok(24, ! exists $h{'jimmy'} ) ;
109 ok(25, defined $h{'abc'} ) ;
112 $h{'jkl','mno'} = "JKL\034MNO";
113 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
117 $X->STORE('b', 'B') ;
130 $h{'goner2'} = 'snork';
134 # IMPORTANT - $X must be undefined before the untie otherwise the
135 # underlying DB close routine will not get called.
140 # tie to the same file again
141 ok(26, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE)) ;
143 # Modify an entry from the previous tie
164 $h{'goner3'} = 'snork';
167 $X->DELETE('goner3');
170 @values = values(%h);
172 ok(27, $#keys == 29 && $#values == 29) ;
175 while (($key,$value) = each(%h)) {
176 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
178 $i++ if $key eq $value;
184 @keys = ('blurfl', keys(%h), 'dyick');
185 ok(29, $#keys == 31) ;
187 #Check that the keys can be retrieved in order
189 my @c = sort lexical @b ;
190 ok(30, ArrayCompare(\@b, \@c)) ;
193 ok(31, $h{'foo'} eq '' ) ;
196 ok(32, $h{''} eq 'bar' );
198 # check cache overflow and numeric keys and contents
200 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
201 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
204 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
205 $blksize,$blocks) = stat($Dfile);
208 @h{0..200} = 200..400;
210 ok(35, join(':',200..400) eq join(':',@foo) );
212 # Now check all the non-tie specific stuff
215 # Check R_NOOVERWRITE flag will make put fail when attempting to overwrite
216 # an existing record.
218 $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
219 ok(36, $status == 1 );
221 # check that the value of the key 'x' has not been changed by the
223 ok(37, $h{'x'} eq 'X' );
226 $status = $X->put('key', 'value') ;
227 ok(38, $status == 0 );
229 #check that previous put can be retrieved
231 $status = $X->get('key', $value) ;
232 ok(39, $status == 0 );
233 ok(40, $value eq 'value' );
235 # Attempting to delete an existing key should work
237 $status = $X->del('q') ;
238 ok(41, $status == 0 );
239 $status = $X->del('') ;
240 ok(42, $status == 0 );
242 # Make sure that the key deleted, cannot be retrieved
244 ok(43, $h{'q'} eq undef) ;
245 ok(44, $h{''} eq undef) ;
251 ok(45, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE ));
253 # Attempting to delete a non-existant key should fail
255 $status = $X->del('joe') ;
256 ok(46, $status == 1 );
258 # Check the get interface
260 # First a non-existing key
261 $status = $X->get('aaaa', $value) ;
262 ok(47, $status == 1 );
264 # Next an existing key
265 $status = $X->get('a', $value) ;
266 ok(48, $status == 0 );
267 ok(49, $value eq 'A' );
272 # use seq to find an approximate match
275 $status = $X->seq($key, $value, R_CURSOR) ;
276 ok(50, $status == 0 );
277 ok(51, $key eq 'key' );
278 ok(52, $value eq 'value' );
280 # seq when the key does not match
283 $status = $X->seq($key, $value, R_CURSOR) ;
284 ok(53, $status == 1 );
287 # use seq to set the cursor, then delete the record @ the cursor.
291 $status = $X->seq($key, $value, R_CURSOR) ;
292 ok(54, $status == 0 );
293 ok(55, $key eq 'x' );
294 ok(56, $value eq 'X' );
295 $status = $X->del(0, R_CURSOR) ;
296 ok(57, $status == 0 );
297 $status = $X->get('x', $value) ;
298 ok(58, $status == 1 );
300 # ditto, but use put to replace the key/value pair.
303 $status = $X->seq($key, $value, R_CURSOR) ;
304 ok(59, $status == 0 );
305 ok(60, $key eq 'y' );
306 ok(61, $value eq 'Y' );
308 $key = "replace key" ;
309 $value = "replace value" ;
310 $status = $X->put($key, $value, R_CURSOR) ;
311 ok(62, $status == 0 );
312 ok(63, $key eq 'replace key' );
313 ok(64, $value eq 'replace value' );
314 $status = $X->get('y', $value) ;
315 ok(65, $status == 1 );
317 # use seq to walk forwards through a file
319 $status = $X->seq($key, $value, R_FIRST) ;
320 ok(66, $status == 0 );
324 while (($status = $X->seq($key, $value, R_NEXT)) == 0)
326 ($ok = 0), last if ($previous cmp $key) == 1 ;
329 ok(67, $status == 1 );
332 # use seq to walk backwards through a file
333 $status = $X->seq($key, $value, R_LAST) ;
334 ok(69, $status == 0 );
338 while (($status = $X->seq($key, $value, R_PREV)) == 0)
340 ($ok = 0), last if ($previous cmp $key) == -1 ;
341 #print "key = [$key] value = [$value]\n" ;
344 ok(70, $status == 1 );
348 # check seq FIRST/LAST
354 ok(72, $status == 0 );
361 ok(73, $status != 0 );
369 # Now try an in memory file
370 ok(74, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE ));
372 # fd with an in memory file should return failure
374 ok(75, $status == -1 );
381 my $bt = new DB_File::BTREEINFO ;
382 $bt->{flags} = R_DUP ;
383 ok(76, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ;
385 $hh{'Wall'} = 'Larry' ;
386 $hh{'Wall'} = 'Stone' ; # Note the duplicate key
387 $hh{'Wall'} = 'Brick' ; # Note the duplicate key
388 $hh{'Wall'} = 'Brick' ; # Note the duplicate key and value
389 $hh{'Smith'} = 'John' ;
390 $hh{'mouse'} = 'mickey' ;
392 # first work in scalar context
393 ok(77, scalar $YY->get_dup('Unknown') == 0 );
394 ok(78, scalar $YY->get_dup('Smith') == 1 );
395 ok(79, scalar $YY->get_dup('Wall') == 4 );
397 # now in list context
398 my @unknown = $YY->get_dup('Unknown') ;
399 ok(80, "@unknown" eq "" );
401 my @smith = $YY->get_dup('Smith') ;
402 ok(81, "@smith" eq "John" );
405 my @wall = $YY->get_dup('Wall') ;
407 @wall{@wall} = @wall ;
408 ok(82, (@wall == 4 && $wall{'Larry'} && $wall{'Stone'} && $wall{'Brick'}) );
412 my %unknown = $YY->get_dup('Unknown', 1) ;
413 ok(83, keys %unknown == 0 );
415 my %smith = $YY->get_dup('Smith', 1) ;
416 ok(84, keys %smith == 1 && $smith{'John'}) ;
418 my %wall = $YY->get_dup('Wall', 1) ;
419 ok(85, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1
420 && $wall{'Brick'} == 2);
427 # test multiple callbacks
432 $dbh1 = TIEHASH DB_File::BTREEINFO ;
433 $dbh1->{compare} = sub { $_[0] <=> $_[1] } ;
435 $dbh2 = TIEHASH DB_File::BTREEINFO ;
436 $dbh2->{compare} = sub { $_[0] cmp $_[1] } ;
438 $dbh3 = TIEHASH 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 ;
450 @srt_2 = sort { $a cmp $b } @Keys ;
451 @srt_3 = sort { length $a <=> length $b } @Keys ;
465 return 0 if @$a != @$b ;
467 foreach (1 .. length @$a)
469 return 0 unless $$a[$_] eq $$b[$_] ;
475 ok(86, ArrayCompare (\@srt_1, [keys %h]) );
476 ok(87, ArrayCompare (\@srt_2, [keys %g]) );
477 ok(88, ArrayCompare (\@srt_3, [keys %k]) );
482 unlink $Dfile1, $Dfile2, $Dfile3 ;
487 ok(89, tie(%h, 'DB_File', $Dfile1, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
489 { $h{$_} = $_ * 100 }
491 # check that there are 10 elements in the hash
493 while (($key,$value) = each(%h)) {
503 while (($key,$value) = each(%h)) {