5 require Config; import Config;
6 if ($Config{'extensions'} !~ /\bDB_File\b/) {
22 print "not " unless $result ;
26 $Dfile = "dbbtree.tmp";
31 # Check the interface to BTREEINFO
33 my $dbh = new DB_File::BTREEINFO ;
35 ok(1, $dbh->{flags} == undef) ;
36 ok(2, $dbh->{cachesize} == undef) ;
37 ok(3, $dbh->{psize} == undef) ;
38 ok(4, $dbh->{lorder} == undef) ;
39 ok(5, $dbh->{minkeypage} == undef) ;
40 ok(6, $dbh->{maxkeypage} == undef) ;
41 ok(7, $dbh->{compare} == undef) ;
42 ok(8, $dbh->{prefix} == undef) ;
45 $dbh->{flags} = 3000 ;
46 ok(9, $dbh->{flags} == 3000) ;
48 $dbh->{cachesize} = 9000 ;
49 ok(10, $dbh->{cachesize} == 9000);
52 ok(11, $dbh->{psize} == 400) ;
55 ok(12, $dbh->{lorder} == 65) ;
57 $dbh->{minkeypage} = 123 ;
58 ok(13, $dbh->{minkeypage} == 123) ;
60 $dbh->{maxkeypage} = 1234 ;
61 ok(14, $dbh->{maxkeypage} == 1234 );
63 $dbh->{compare} = 1234 ;
64 ok(15, $dbh->{compare} == 1234) ;
66 $dbh->{prefix} = 1234 ;
67 ok(16, $dbh->{prefix} == 1234 );
69 # Check that an invalid entry is caught both for store & fetch
70 eval '$dbh->{fred} = 1234' ;
71 ok(17, $@ =~ /^DB_File::BTREEINFO::STORE - Unknown element 'fred' at/ ) ;
72 eval '$q = $dbh->{fred}' ;
73 ok(18, $@ =~ /^DB_File::BTREEINFO::FETCH - Unknown element 'fred' at/ ) ;
75 # Now check the interface to BTREE
77 ok(19, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE )) ;
79 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
80 $blksize,$blocks) = stat($Dfile);
81 ok(20, ($mode & 0777) == ($^O eq 'os2' ? 0666 : 0640) );
83 while (($key,$value) = each(%h)) {
88 $h{'goner1'} = 'snork';
91 ok(22, $h{'abc'} eq 'ABC' );
92 ok(23, ! defined $h{'jimmy'} ) ;
93 ok(24, ! exists $h{'jimmy'} ) ;
94 ok(25, defined $h{'abc'} ) ;
97 $h{'jkl','mno'} = "JKL\034MNO";
98 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
102 $X->STORE('b', 'B') ;
115 $h{'goner2'} = 'snork';
119 # IMPORTANT - $X must be undefined before the untie otherwise the
120 # underlying DB close routine will not get called.
125 # tie to the same file again
126 ok(26, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE)) ;
128 # Modify an entry from the previous tie
149 $h{'goner3'} = 'snork';
152 $X->DELETE('goner3');
155 @values = values(%h);
157 ok(27, $#keys == 29 && $#values == 29) ;
160 while (($key,$value) = each(%h)) {
161 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
163 $i++ if $key eq $value;
169 @keys = ('blurfl', keys(%h), 'dyick');
170 ok(29, $#keys == 31) ;
172 #Check that the keys can be retrieved in order
176 ($ok = 0), last if defined $previous && $previous gt $_ ;
182 ok(31, $h{'foo'} eq '' ) ;
185 ok(32, $h{''} eq 'bar' );
187 # check cache overflow and numeric keys and contents
189 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
190 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
193 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
194 $blksize,$blocks) = stat($Dfile);
197 @h{0..200} = 200..400;
199 ok(35, join(':',200..400) eq join(':',@foo) );
201 # Now check all the non-tie specific stuff
204 # Check R_NOOVERWRITE flag will make put fail when attempting to overwrite
205 # an existing record.
207 $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
208 ok(36, $status == 1 );
210 # check that the value of the key 'x' has not been changed by the
212 ok(37, $h{'x'} eq 'X' );
215 $status = $X->put('key', 'value') ;
216 ok(38, $status == 0 );
218 #check that previous put can be retrieved
220 $status = $X->get('key', $value) ;
221 ok(39, $status == 0 );
222 ok(40, $value eq 'value' );
224 # Attempting to delete an existing key should work
226 $status = $X->del('q') ;
227 ok(41, $status == 0 );
228 $status = $X->del('') ;
229 ok(42, $status == 0 );
231 # Make sure that the key deleted, cannot be retrieved
233 ok(43, $h{'q'} eq undef) ;
234 ok(44, $h{''} eq undef) ;
240 ok(45, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE ));
242 # Attempting to delete a non-existant key should fail
244 $status = $X->del('joe') ;
245 ok(46, $status == 1 );
247 # Check the get interface
249 # First a non-existing key
250 $status = $X->get('aaaa', $value) ;
251 ok(47, $status == 1 );
253 # Next an existing key
254 $status = $X->get('a', $value) ;
255 ok(48, $status == 0 );
256 ok(49, $value eq 'A' );
261 # use seq to find an approximate match
264 $status = $X->seq($key, $value, R_CURSOR) ;
265 ok(50, $status == 0 );
266 ok(51, $key eq 'key' );
267 ok(52, $value eq 'value' );
269 # seq when the key does not match
272 $status = $X->seq($key, $value, R_CURSOR) ;
273 ok(53, $status == 1 );
276 # use seq to set the cursor, then delete the record @ the cursor.
280 $status = $X->seq($key, $value, R_CURSOR) ;
281 ok(54, $status == 0 );
282 ok(55, $key eq 'x' );
283 ok(56, $value eq 'X' );
284 $status = $X->del(0, R_CURSOR) ;
285 ok(57, $status == 0 );
286 $status = $X->get('x', $value) ;
287 ok(58, $status == 1 );
289 # ditto, but use put to replace the key/value pair.
292 $status = $X->seq($key, $value, R_CURSOR) ;
293 ok(59, $status == 0 );
294 ok(60, $key eq 'y' );
295 ok(61, $value eq 'Y' );
297 $key = "replace key" ;
298 $value = "replace value" ;
299 $status = $X->put($key, $value, R_CURSOR) ;
300 ok(62, $status == 0 );
301 ok(63, $key eq 'replace key' );
302 ok(64, $value eq 'replace value' );
303 $status = $X->get('y', $value) ;
304 ok(65, $status == 1 );
306 # use seq to walk forwards through a file
308 $status = $X->seq($key, $value, R_FIRST) ;
309 ok(66, $status == 0 );
313 while (($status = $X->seq($key, $value, R_NEXT)) == 0)
315 ($ok = 0), last if ($previous cmp $key) == 1 ;
318 ok(67, $status == 1 );
321 # use seq to walk backwards through a file
322 $status = $X->seq($key, $value, R_LAST) ;
323 ok(69, $status == 0 );
327 while (($status = $X->seq($key, $value, R_PREV)) == 0)
329 ($ok = 0), last if ($previous cmp $key) == -1 ;
330 #print "key = [$key] value = [$value]\n" ;
333 ok(70, $status == 1 );
337 # check seq FIRST/LAST
343 ok(72, $status == 0 );
350 ok(73, $status != 0 );
358 # Now try an in memory file
359 ok(74, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE ));
361 # fd with an in memory file should return failure
363 ok(75, $status == -1 );
370 my $bt = new DB_File::BTREEINFO ;
371 $bt->{flags} = R_DUP ;
372 ok(76, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ;
374 $hh{'Wall'} = 'Larry' ;
375 $hh{'Wall'} = 'Stone' ; # Note the duplicate key
376 $hh{'Wall'} = 'Brick' ; # Note the duplicate key
377 $hh{'Wall'} = 'Brick' ; # Note the duplicate key and value
378 $hh{'Smith'} = 'John' ;
379 $hh{'mouse'} = 'mickey' ;
381 # first work in scalar context
382 ok(77, scalar $YY->get_dup('Unknown') == 0 );
383 ok(78, scalar $YY->get_dup('Smith') == 1 );
384 ok(79, scalar $YY->get_dup('Wall') == 4 );
386 # now in list context
387 my @unknown = $YY->get_dup('Unknown') ;
388 ok(80, "@unknown" eq "" );
390 my @smith = $YY->get_dup('Smith') ;
391 ok(81, "@smith" eq "John" );
394 my @wall = $YY->get_dup('Wall') ;
396 @wall{@wall} = @wall ;
397 ok(82, (@wall == 4 && $wall{'Larry'} && $wall{'Stone'} && $wall{'Brick'}) );
401 my %unknown = $YY->get_dup('Unknown', 1) ;
402 ok(83, keys %unknown == 0 );
404 my %smith = $YY->get_dup('Smith', 1) ;
405 ok(84, keys %smith == 1 && $smith{'John'}) ;
407 my %wall = $YY->get_dup('Wall', 1) ;
408 ok(85, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1
409 && $wall{'Brick'} == 2);
416 # test multiple callbacks
421 $dbh1 = TIEHASH DB_File::BTREEINFO ;
422 $dbh1->{compare} = sub { $_[0] <=> $_[1] } ;
424 $dbh2 = TIEHASH DB_File::BTREEINFO ;
425 $dbh2->{compare} = sub { $_[0] cmp $_[1] } ;
427 $dbh3 = TIEHASH DB_File::BTREEINFO ;
428 $dbh3->{compare} = sub { length $_[0] <=> length $_[1] } ;
431 tie(%h, 'DB_File',$Dfile1, O_RDWR|O_CREAT, 0640, $dbh1 ) ;
432 tie(%g, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) ;
433 tie(%k, 'DB_File',$Dfile3, O_RDWR|O_CREAT, 0640, $dbh3 ) ;
435 @Keys = qw( 0123 12 -1234 9 987654321 def ) ;
437 @srt_1 = sort { $a <=> $b } @Keys ;
439 @srt_2 = sort { $a cmp $b } @Keys ;
440 @srt_3 = sort { length $a <=> length $b } @Keys ;
443 $^W = 0 ; $h{$_} = 1 ; $^W = 1 ;
452 return 0 if @$a != @$b ;
454 foreach (1 .. length @$a)
456 return 0 unless $$a[$_] eq $$b[$_] ;
462 ok(86, ArrayCompare (\@srt_1, [keys %h]) );
463 ok(87, ArrayCompare (\@srt_2, [keys %g]) );
464 ok(88, ArrayCompare (\@srt_3, [keys %k]) );
469 unlink $Dfile1, $Dfile2, $Dfile3 ;
474 ok(89, tie(%h, 'DB_File', $Dfile1, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
476 { $h{$_} = $_ * 100 }
478 # check that there are 10 elements in the hash
480 while (($key,$value) = each(%h)) {
490 while (($key,$value) = each(%h)) {