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, ! 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');
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' );
196 # check cache overflow and numeric keys and contents
198 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
199 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
202 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
203 $blksize,$blocks) = stat($Dfile);
206 @h{0..200} = 200..400;
208 ok(35, join(':',200..400) eq join(':',@foo) );
210 # Now check all the non-tie specific stuff
213 # Check R_NOOVERWRITE flag will make put fail when attempting to overwrite
214 # an existing record.
216 $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
217 ok(36, $status == 1 );
219 # check that the value of the key 'x' has not been changed by the
221 ok(37, $h{'x'} eq 'X' );
224 $status = $X->put('key', 'value') ;
225 ok(38, $status == 0 );
227 #check that previous put can be retrieved
229 $status = $X->get('key', $value) ;
230 ok(39, $status == 0 );
231 ok(40, $value eq 'value' );
233 # Attempting to delete an existing key should work
235 $status = $X->del('q') ;
236 ok(41, $status == 0 );
237 $status = $X->del('') ;
238 ok(42, $status == 0 );
240 # Make sure that the key deleted, cannot be retrieved
241 ok(43, ! defined $h{'q'}) ;
242 ok(44, ! defined $h{''}) ;
247 ok(45, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE ));
249 # Attempting to delete a non-existant key should fail
251 $status = $X->del('joe') ;
252 ok(46, $status == 1 );
254 # Check the get interface
256 # First a non-existing key
257 $status = $X->get('aaaa', $value) ;
258 ok(47, $status == 1 );
260 # Next an existing key
261 $status = $X->get('a', $value) ;
262 ok(48, $status == 0 );
263 ok(49, $value eq 'A' );
268 # use seq to find an approximate match
271 $status = $X->seq($key, $value, R_CURSOR) ;
272 ok(50, $status == 0 );
273 ok(51, $key eq 'key' );
274 ok(52, $value eq 'value' );
276 # seq when the key does not match
279 $status = $X->seq($key, $value, R_CURSOR) ;
280 ok(53, $status == 1 );
283 # use seq to set the cursor, then delete the record @ the cursor.
287 $status = $X->seq($key, $value, R_CURSOR) ;
288 ok(54, $status == 0 );
289 ok(55, $key eq 'x' );
290 ok(56, $value eq 'X' );
291 $status = $X->del(0, R_CURSOR) ;
292 ok(57, $status == 0 );
293 $status = $X->get('x', $value) ;
294 ok(58, $status == 1 );
296 # ditto, but use put to replace the key/value pair.
299 $status = $X->seq($key, $value, R_CURSOR) ;
300 ok(59, $status == 0 );
301 ok(60, $key eq 'y' );
302 ok(61, $value eq 'Y' );
304 $key = "replace key" ;
305 $value = "replace value" ;
306 $status = $X->put($key, $value, R_CURSOR) ;
307 ok(62, $status == 0 );
308 ok(63, $key eq 'replace key' );
309 ok(64, $value eq 'replace value' );
310 $status = $X->get('y', $value) ;
311 ok(65, $status == 1 );
313 # use seq to walk forwards through a file
315 $status = $X->seq($key, $value, R_FIRST) ;
316 ok(66, $status == 0 );
320 while (($status = $X->seq($key, $value, R_NEXT)) == 0)
322 ($ok = 0), last if ($previous cmp $key) == 1 ;
325 ok(67, $status == 1 );
328 # use seq to walk backwards through a file
329 $status = $X->seq($key, $value, R_LAST) ;
330 ok(69, $status == 0 );
334 while (($status = $X->seq($key, $value, R_PREV)) == 0)
336 ($ok = 0), last if ($previous cmp $key) == -1 ;
337 #print "key = [$key] value = [$value]\n" ;
340 ok(70, $status == 1 );
344 # check seq FIRST/LAST
350 ok(72, $status == 0 );
357 ok(73, $status != 0 );
365 # Now try an in memory file
366 ok(74, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE ));
368 # fd with an in memory file should return failure
370 ok(75, $status == -1 );
377 my $bt = new DB_File::BTREEINFO ;
378 $bt->{flags} = R_DUP ;
379 ok(76, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ;
381 $hh{'Wall'} = 'Larry' ;
382 $hh{'Wall'} = 'Stone' ; # Note the duplicate key
383 $hh{'Wall'} = 'Brick' ; # Note the duplicate key
384 $hh{'Wall'} = 'Brick' ; # Note the duplicate key and value
385 $hh{'Smith'} = 'John' ;
386 $hh{'mouse'} = 'mickey' ;
388 # first work in scalar context
389 ok(77, scalar $YY->get_dup('Unknown') == 0 );
390 ok(78, scalar $YY->get_dup('Smith') == 1 );
391 ok(79, scalar $YY->get_dup('Wall') == 4 );
393 # now in list context
394 my @unknown = $YY->get_dup('Unknown') ;
395 ok(80, "@unknown" eq "" );
397 my @smith = $YY->get_dup('Smith') ;
398 ok(81, "@smith" eq "John" );
401 my @wall = $YY->get_dup('Wall') ;
403 @wall{@wall} = @wall ;
404 ok(82, (@wall == 4 && $wall{'Larry'} && $wall{'Stone'} && $wall{'Brick'}) );
408 my %unknown = $YY->get_dup('Unknown', 1) ;
409 ok(83, keys %unknown == 0 );
411 my %smith = $YY->get_dup('Smith', 1) ;
412 ok(84, keys %smith == 1 && $smith{'John'}) ;
414 my %wall = $YY->get_dup('Wall', 1) ;
415 ok(85, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1
416 && $wall{'Brick'} == 2);
423 # test multiple callbacks
428 $dbh1 = new DB_File::BTREEINFO ;
430 $dbh1->{compare} = sub { $_[0] <=> $_[1] } ; }
432 $dbh2 = new DB_File::BTREEINFO ;
433 $dbh2->{compare} = sub { $_[0] cmp $_[1] } ;
435 $dbh3 = new DB_File::BTREEINFO ;
436 $dbh3->{compare} = sub { length $_[0] <=> length $_[1] } ;
439 tie(%h, 'DB_File',$Dfile1, O_RDWR|O_CREAT, 0640, $dbh1 ) ;
440 tie(%g, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) ;
441 tie(%k, 'DB_File',$Dfile3, O_RDWR|O_CREAT, 0640, $dbh3 ) ;
443 @Keys = qw( 0123 12 -1234 9 987654321 def ) ;
445 @srt_1 = sort { $a <=> $b } @Keys ; }
446 @srt_2 = sort { $a cmp $b } @Keys ;
447 @srt_3 = sort { length $a <=> length $b } @Keys ;
460 return 0 if @$a != @$b ;
462 foreach (1 .. length @$a)
464 return 0 unless $$a[$_] eq $$b[$_] ;
470 ok(86, ArrayCompare (\@srt_1, [keys %h]) );
471 ok(87, ArrayCompare (\@srt_2, [keys %g]) );
472 ok(88, ArrayCompare (\@srt_3, [keys %k]) );
477 unlink $Dfile1, $Dfile2, $Dfile3 ;
482 ok(89, tie(%h, 'DB_File', $Dfile1, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
484 { $h{$_} = $_ * 100 }
486 # check that there are 10 elements in the hash
488 while (($key,$value) = each(%h)) {
498 while (($key,$value) = each(%h)) {