Integrate mainline
[p5sagit/p5-mst-13.2.git] / ext / DB_File / t / db-btree.t
CommitLineData
f6b705ef 1#!./perl -w
a0d0e21e 2
77fd2717 3BEGIN {
4 unless(grep /blib/, @INC) {
5 chdir 't' if -d 't';
6 @INC = '../lib' if -d '../lib';
7 }
8}
9
bb50757b 10use warnings;
11use strict;
77fd2717 12use Config;
13
a0d0e21e 14BEGIN {
77fd2717 15 if(-d "lib" && -f "TEST") {
16 if ($Config{'extensions'} !~ /\bDB_File\b/ ) {
bb50757b 17 print "1..0 # Skip: DB_File was not built\n";
77fd2717 18 exit 0;
19 }
a0d0e21e 20 }
80a5d8e7 21 if ($^O eq 'darwin'
22 && $Config{db_version_major} == 1
23 && $Config{db_version_minor} == 0
24 && $Config{db_version_patch} == 0) {
25 warn <<EOM;
26#
27# This test is known to crash in Mac OS X versions 10.1.3 (or earlier)
28# because of the buggy Berkeley DB version included with the OS.
29#
30EOM
31 }
a0d0e21e 32}
33
34use DB_File;
35use Fcntl;
36
0bf2e707 37print "1..163\n";
f6b705ef 38
39sub ok
40{
41 my $no = shift ;
42 my $result = shift ;
43
44 print "not " unless $result ;
45 print "ok $no\n" ;
46}
a0d0e21e 47
55497cff 48sub lexical
49{
50 my(@a) = unpack ("C*", $a) ;
51 my(@b) = unpack ("C*", $b) ;
52
53 my $len = (@a > @b ? @b : @a) ;
54 my $i = 0 ;
55
56 foreach $i ( 0 .. $len -1) {
57 return $a[$i] - $b[$i] if $a[$i] != $b[$i] ;
58 }
59
60 return @a - @b ;
61}
62
2c2d71f5 63{
64 package Redirect ;
65 use Symbol ;
66
67 sub new
68 {
69 my $class = shift ;
70 my $filename = shift ;
71 my $fh = gensym ;
72 open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
73 my $real_stdout = select($fh) ;
74 return bless [$fh, $real_stdout ] ;
75
76 }
77 sub DESTROY
78 {
79 my $self = shift ;
80 close $self->[0] ;
81 select($self->[1]) ;
82 }
83}
84
85sub docat
86{
87 my $file = shift;
77fd2717 88 local $/ = undef ;
2c2d71f5 89 open(CAT,$file) || die "Cannot open $file: $!";
77fd2717 90 my $result = <CAT>;
2c2d71f5 91 close(CAT);
77fd2717 92 $result = normalise($result) ;
93 return $result ;
2c2d71f5 94}
95
96sub docat_del
97{
98 my $file = shift;
77fd2717 99 my $result = docat($file);
2c2d71f5 100 unlink $file ;
77fd2717 101 return $result ;
2c2d71f5 102}
103
77fd2717 104sub normalise
105{
106 my $data = shift ;
107 $data =~ s#\r\n#\n#g
108 if $^O eq 'cygwin' ;
109
110 return $data ;
111}
112
113
2c2d71f5 114
3245f058 115my $db185mode = ($DB_File::db_version == 1 && ! $DB_File::db_185_compat) ;
116my $null_keys_allowed = ($DB_File::db_ver < 2.004010
117 || $DB_File::db_ver >= 3.1 );
039d031f 118
9fe6733a 119my $Dfile = "dbbtree.tmp";
a0d0e21e 120unlink $Dfile;
121
122umask(0);
123
124# Check the interface to BTREEINFO
125
f6b705ef 126my $dbh = new DB_File::BTREEINFO ;
3fe9a6f1 127ok(1, ! defined $dbh->{flags}) ;
128ok(2, ! defined $dbh->{cachesize}) ;
129ok(3, ! defined $dbh->{psize}) ;
130ok(4, ! defined $dbh->{lorder}) ;
131ok(5, ! defined $dbh->{minkeypage}) ;
132ok(6, ! defined $dbh->{maxkeypage}) ;
133ok(7, ! defined $dbh->{compare}) ;
134ok(8, ! defined $dbh->{prefix}) ;
a0d0e21e 135
136$dbh->{flags} = 3000 ;
f6b705ef 137ok(9, $dbh->{flags} == 3000) ;
a0d0e21e 138
139$dbh->{cachesize} = 9000 ;
f6b705ef 140ok(10, $dbh->{cachesize} == 9000);
141
a0d0e21e 142$dbh->{psize} = 400 ;
f6b705ef 143ok(11, $dbh->{psize} == 400) ;
a0d0e21e 144
145$dbh->{lorder} = 65 ;
f6b705ef 146ok(12, $dbh->{lorder} == 65) ;
a0d0e21e 147
148$dbh->{minkeypage} = 123 ;
f6b705ef 149ok(13, $dbh->{minkeypage} == 123) ;
a0d0e21e 150
151$dbh->{maxkeypage} = 1234 ;
f6b705ef 152ok(14, $dbh->{maxkeypage} == 1234 );
a0d0e21e 153
154$dbh->{compare} = 1234 ;
f6b705ef 155ok(15, $dbh->{compare} == 1234) ;
a0d0e21e 156
157$dbh->{prefix} = 1234 ;
f6b705ef 158ok(16, $dbh->{prefix} == 1234 );
a0d0e21e 159
160# Check that an invalid entry is caught both for store & fetch
161eval '$dbh->{fred} = 1234' ;
f6b705ef 162ok(17, $@ =~ /^DB_File::BTREEINFO::STORE - Unknown element 'fred' at/ ) ;
3245f058 163eval 'my $q = $dbh->{fred}' ;
f6b705ef 164ok(18, $@ =~ /^DB_File::BTREEINFO::FETCH - Unknown element 'fred' at/ ) ;
a0d0e21e 165
166# Now check the interface to BTREE
167
3245f058 168my ($X, %h) ;
f6b705ef 169ok(19, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE )) ;
05a54443 170die "Could not tie: $!" unless $X;
a0d0e21e 171
3245f058 172my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
a0d0e21e 173 $blksize,$blocks) = stat($Dfile);
77fd2717 174
175my %noMode = map { $_, 1} qw( amigaos MSWin32 NetWare cygwin ) ;
176
d536870a 177ok(20, ($mode & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640)
77fd2717 178 || $noMode{$^O} );
a0d0e21e 179
3245f058 180my ($key, $value, $i);
a0d0e21e 181while (($key,$value) = each(%h)) {
182 $i++;
183}
f6b705ef 184ok(21, !$i ) ;
a0d0e21e 185
186$h{'goner1'} = 'snork';
187
188$h{'abc'} = 'ABC';
f6b705ef 189ok(22, $h{'abc'} eq 'ABC' );
190ok(23, ! defined $h{'jimmy'} ) ;
191ok(24, ! exists $h{'jimmy'} ) ;
192ok(25, defined $h{'abc'} ) ;
a0d0e21e 193
194$h{'def'} = 'DEF';
195$h{'jkl','mno'} = "JKL\034MNO";
196$h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
197$h{'a'} = 'A';
198
199#$h{'b'} = 'B';
200$X->STORE('b', 'B') ;
201
202$h{'c'} = 'C';
203
204#$h{'d'} = 'D';
205$X->put('d', 'D') ;
206
207$h{'e'} = 'E';
208$h{'f'} = 'F';
209$h{'g'} = 'X';
210$h{'h'} = 'H';
211$h{'i'} = 'I';
212
213$h{'goner2'} = 'snork';
214delete $h{'goner2'};
215
216
217# IMPORTANT - $X must be undefined before the untie otherwise the
218# underlying DB close routine will not get called.
219undef $X ;
220untie(%h);
221
a0d0e21e 222# tie to the same file again
f6b705ef 223ok(26, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE)) ;
a0d0e21e 224
225# Modify an entry from the previous tie
226$h{'g'} = 'G';
227
228$h{'j'} = 'J';
229$h{'k'} = 'K';
230$h{'l'} = 'L';
231$h{'m'} = 'M';
232$h{'n'} = 'N';
233$h{'o'} = 'O';
234$h{'p'} = 'P';
235$h{'q'} = 'Q';
236$h{'r'} = 'R';
237$h{'s'} = 'S';
238$h{'t'} = 'T';
239$h{'u'} = 'U';
240$h{'v'} = 'V';
241$h{'w'} = 'W';
242$h{'x'} = 'X';
243$h{'y'} = 'Y';
244$h{'z'} = 'Z';
245
246$h{'goner3'} = 'snork';
247
248delete $h{'goner1'};
249$X->DELETE('goner3');
250
3245f058 251my @keys = keys(%h);
252my @values = values(%h);
a0d0e21e 253
f6b705ef 254ok(27, $#keys == 29 && $#values == 29) ;
a0d0e21e 255
f6b705ef 256$i = 0 ;
a0d0e21e 257while (($key,$value) = each(%h)) {
2f52a358 258 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
a0d0e21e 259 $key =~ y/a-z/A-Z/;
260 $i++ if $key eq $value;
261 }
262}
263
f6b705ef 264ok(28, $i == 30) ;
a0d0e21e 265
55d68b4a 266@keys = ('blurfl', keys(%h), 'dyick');
f6b705ef 267ok(29, $#keys == 31) ;
a0d0e21e 268
269#Check that the keys can be retrieved in order
55497cff 270my @b = keys %h ;
271my @c = sort lexical @b ;
272ok(30, ArrayCompare(\@b, \@c)) ;
a0d0e21e 273
274$h{'foo'} = '';
f6b705ef 275ok(31, $h{'foo'} eq '' ) ;
a0d0e21e 276
3245f058 277# Berkeley DB from version 2.4.10 to 3.0 does not allow null keys.
278# This feature was reenabled in version 3.1 of Berkeley DB.
279my $result = 0 ;
280if ($null_keys_allowed) {
281 $h{''} = 'bar';
282 $result = ( $h{''} eq 'bar' );
283}
284else
285 { $result = 1 }
286ok(32, $result) ;
a0d0e21e 287
288# check cache overflow and numeric keys and contents
3245f058 289my $ok = 1;
a0d0e21e 290for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
291for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
f6b705ef 292ok(33, $ok);
a0d0e21e 293
294($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
295 $blksize,$blocks) = stat($Dfile);
f6b705ef 296ok(34, $size > 0 );
a0d0e21e 297
298@h{0..200} = 200..400;
3245f058 299my @foo = @h{0..200};
f6b705ef 300ok(35, join(':',200..400) eq join(':',@foo) );
a0d0e21e 301
302# Now check all the non-tie specific stuff
303
304
305# Check R_NOOVERWRITE flag will make put fail when attempting to overwrite
306# an existing record.
307
3245f058 308my $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
f6b705ef 309ok(36, $status == 1 );
a0d0e21e 310
311# check that the value of the key 'x' has not been changed by the
312# previous test
f6b705ef 313ok(37, $h{'x'} eq 'X' );
a0d0e21e 314
315# standard put
316$status = $X->put('key', 'value') ;
f6b705ef 317ok(38, $status == 0 );
a0d0e21e 318
319#check that previous put can be retrieved
f6b705ef 320$value = 0 ;
a0d0e21e 321$status = $X->get('key', $value) ;
f6b705ef 322ok(39, $status == 0 );
323ok(40, $value eq 'value' );
a0d0e21e 324
325# Attempting to delete an existing key should work
326
327$status = $X->del('q') ;
f6b705ef 328ok(41, $status == 0 );
3245f058 329if ($null_keys_allowed) {
330 $status = $X->del('') ;
331} else {
332 $status = 0 ;
333}
334ok(42, $status == 0 );
a0d0e21e 335
336# Make sure that the key deleted, cannot be retrieved
ac1ad7f0 337ok(43, ! defined $h{'q'}) ;
338ok(44, ! defined $h{''}) ;
a0d0e21e 339
340undef $X ;
341untie %h ;
342
f6b705ef 343ok(45, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE ));
a0d0e21e 344
345# Attempting to delete a non-existant key should fail
346
347$status = $X->del('joe') ;
f6b705ef 348ok(46, $status == 1 );
a0d0e21e 349
350# Check the get interface
351
352# First a non-existing key
353$status = $X->get('aaaa', $value) ;
f6b705ef 354ok(47, $status == 1 );
a0d0e21e 355
356# Next an existing key
357$status = $X->get('a', $value) ;
f6b705ef 358ok(48, $status == 0 );
359ok(49, $value eq 'A' );
a0d0e21e 360
361# seq
362# ###
363
364# use seq to find an approximate match
365$key = 'ke' ;
366$value = '' ;
367$status = $X->seq($key, $value, R_CURSOR) ;
f6b705ef 368ok(50, $status == 0 );
369ok(51, $key eq 'key' );
370ok(52, $value eq 'value' );
a0d0e21e 371
372# seq when the key does not match
373$key = 'zzz' ;
374$value = '' ;
375$status = $X->seq($key, $value, R_CURSOR) ;
f6b705ef 376ok(53, $status == 1 );
a0d0e21e 377
378
379# use seq to set the cursor, then delete the record @ the cursor.
380
381$key = 'x' ;
382$value = '' ;
383$status = $X->seq($key, $value, R_CURSOR) ;
f6b705ef 384ok(54, $status == 0 );
385ok(55, $key eq 'x' );
386ok(56, $value eq 'X' );
a0d0e21e 387$status = $X->del(0, R_CURSOR) ;
f6b705ef 388ok(57, $status == 0 );
a0d0e21e 389$status = $X->get('x', $value) ;
f6b705ef 390ok(58, $status == 1 );
a0d0e21e 391
392# ditto, but use put to replace the key/value pair.
393$key = 'y' ;
394$value = '' ;
395$status = $X->seq($key, $value, R_CURSOR) ;
f6b705ef 396ok(59, $status == 0 );
397ok(60, $key eq 'y' );
398ok(61, $value eq 'Y' );
a0d0e21e 399
400$key = "replace key" ;
401$value = "replace value" ;
402$status = $X->put($key, $value, R_CURSOR) ;
f6b705ef 403ok(62, $status == 0 );
404ok(63, $key eq 'replace key' );
405ok(64, $value eq 'replace value' );
a0d0e21e 406$status = $X->get('y', $value) ;
1f70e1ea 407ok(65, 1) ; # hard-wire to always pass. the previous test ($status == 1)
a9fd575d 408 # only worked because of a bug in 1.85/6
a0d0e21e 409
410# use seq to walk forwards through a file
411
412$status = $X->seq($key, $value, R_FIRST) ;
f6b705ef 413ok(66, $status == 0 );
3245f058 414my $previous = $key ;
a0d0e21e 415
416$ok = 1 ;
417while (($status = $X->seq($key, $value, R_NEXT)) == 0)
418{
419 ($ok = 0), last if ($previous cmp $key) == 1 ;
420}
421
f6b705ef 422ok(67, $status == 1 );
423ok(68, $ok == 1 );
a0d0e21e 424
425# use seq to walk backwards through a file
426$status = $X->seq($key, $value, R_LAST) ;
f6b705ef 427ok(69, $status == 0 );
a0d0e21e 428$previous = $key ;
429
430$ok = 1 ;
431while (($status = $X->seq($key, $value, R_PREV)) == 0)
432{
433 ($ok = 0), last if ($previous cmp $key) == -1 ;
434 #print "key = [$key] value = [$value]\n" ;
435}
436
f6b705ef 437ok(70, $status == 1 );
438ok(71, $ok == 1 );
a0d0e21e 439
440
441# check seq FIRST/LAST
442
443# sync
444# ####
445
446$status = $X->sync ;
f6b705ef 447ok(72, $status == 0 );
a0d0e21e 448
449
450# fd
451# ##
452
453$status = $X->fd ;
f6b705ef 454ok(73, $status != 0 );
a0d0e21e 455
456
457undef $X ;
458untie %h ;
459
460unlink $Dfile;
461
462# Now try an in memory file
3245f058 463my $Y;
f6b705ef 464ok(74, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE ));
a0d0e21e 465
466# fd with an in memory file should return failure
467$status = $Y->fd ;
f6b705ef 468ok(75, $status == -1 );
a0d0e21e 469
55d68b4a 470
a0d0e21e 471undef $Y ;
472untie %h ;
473
55d68b4a 474# Duplicate keys
475my $bt = new DB_File::BTREEINFO ;
476$bt->{flags} = R_DUP ;
3245f058 477my ($YY, %hh);
f6b705ef 478ok(76, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ;
55d68b4a 479
480$hh{'Wall'} = 'Larry' ;
481$hh{'Wall'} = 'Stone' ; # Note the duplicate key
482$hh{'Wall'} = 'Brick' ; # Note the duplicate key
f6b705ef 483$hh{'Wall'} = 'Brick' ; # Note the duplicate key and value
55d68b4a 484$hh{'Smith'} = 'John' ;
485$hh{'mouse'} = 'mickey' ;
486
487# first work in scalar context
f6b705ef 488ok(77, scalar $YY->get_dup('Unknown') == 0 );
489ok(78, scalar $YY->get_dup('Smith') == 1 );
490ok(79, scalar $YY->get_dup('Wall') == 4 );
55d68b4a 491
492# now in list context
493my @unknown = $YY->get_dup('Unknown') ;
f6b705ef 494ok(80, "@unknown" eq "" );
55d68b4a 495
496my @smith = $YY->get_dup('Smith') ;
f6b705ef 497ok(81, "@smith" eq "John" );
55d68b4a 498
760ac839 499{
f6b705ef 500my @wall = $YY->get_dup('Wall') ;
501my %wall ;
502@wall{@wall} = @wall ;
503ok(82, (@wall == 4 && $wall{'Larry'} && $wall{'Stone'} && $wall{'Brick'}) );
760ac839 504}
55d68b4a 505
506# hash
507my %unknown = $YY->get_dup('Unknown', 1) ;
f6b705ef 508ok(83, keys %unknown == 0 );
55d68b4a 509
510my %smith = $YY->get_dup('Smith', 1) ;
f6b705ef 511ok(84, keys %smith == 1 && $smith{'John'}) ;
55d68b4a 512
f6b705ef 513my %wall = $YY->get_dup('Wall', 1) ;
514ok(85, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1
515 && $wall{'Brick'} == 2);
55d68b4a 516
517undef $YY ;
518untie %hh ;
519unlink $Dfile;
520
521
8e07c86e 522# test multiple callbacks
3245f058 523my $Dfile1 = "btree1" ;
524my $Dfile2 = "btree2" ;
525my $Dfile3 = "btree3" ;
8e07c86e 526
3245f058 527my $dbh1 = new DB_File::BTREEINFO ;
528$dbh1->{compare} = sub {
529 no warnings 'numeric' ;
530 $_[0] <=> $_[1] } ;
8e07c86e 531
3245f058 532my $dbh2 = new DB_File::BTREEINFO ;
8e07c86e 533$dbh2->{compare} = sub { $_[0] cmp $_[1] } ;
534
3245f058 535my $dbh3 = new DB_File::BTREEINFO ;
8e07c86e 536$dbh3->{compare} = sub { length $_[0] <=> length $_[1] } ;
537
538
3245f058 539my (%g, %k);
05a54443 540tie(%h, 'DB_File',$Dfile1, O_RDWR|O_CREAT, 0640, $dbh1 ) or die $!;
541tie(%g, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) or die $!;
542tie(%k, 'DB_File',$Dfile3, O_RDWR|O_CREAT, 0640, $dbh3 ) or die $!;
8e07c86e 543
3245f058 544my @Keys = qw( 0123 12 -1234 9 987654321 def ) ;
545my (@srt_1, @srt_2, @srt_3);
546{
547 no warnings 'numeric' ;
548 @srt_1 = sort { $a <=> $b } @Keys ;
549}
8e07c86e 550@srt_2 = sort { $a cmp $b } @Keys ;
551@srt_3 = sort { length $a <=> length $b } @Keys ;
552
553foreach (@Keys) {
3245f058 554 $h{$_} = 1 ;
8e07c86e 555 $g{$_} = 1 ;
556 $k{$_} = 1 ;
557}
558
559sub ArrayCompare
560{
561 my($a, $b) = @_ ;
562
563 return 0 if @$a != @$b ;
564
565 foreach (1 .. length @$a)
566 {
567 return 0 unless $$a[$_] eq $$b[$_] ;
568 }
569
570 1 ;
571}
572
f6b705ef 573ok(86, ArrayCompare (\@srt_1, [keys %h]) );
574ok(87, ArrayCompare (\@srt_2, [keys %g]) );
575ok(88, ArrayCompare (\@srt_3, [keys %k]) );
8e07c86e 576
577untie %h ;
578untie %g ;
579untie %k ;
580unlink $Dfile1, $Dfile2, $Dfile3 ;
581
f6b705ef 582# clear
583# #####
584
585ok(89, tie(%h, 'DB_File', $Dfile1, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
586foreach (1 .. 10)
587 { $h{$_} = $_ * 100 }
588
589# check that there are 10 elements in the hash
590$i = 0 ;
591while (($key,$value) = each(%h)) {
592 $i++;
593}
594ok(90, $i == 10);
595
596# now clear the hash
597%h = () ;
598
599# check it is empty
600$i = 0 ;
601while (($key,$value) = each(%h)) {
602 $i++;
603}
604ok(91, $i == 0);
605
606untie %h ;
607unlink $Dfile1 ;
608
05475680 609{
610 # check that attempting to tie an array to a DB_BTREE will fail
611
612 my $filename = "xyz" ;
613 my @x ;
614 eval { tie @x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE ; } ;
615 ok(92, $@ =~ /^DB_File can only tie an associative array to a DB_BTREE database/) ;
616 unlink $filename ;
617}
618
a6ed719b 619{
620 # sub-class test
621
622 package Another ;
623
3245f058 624 use warnings ;
a6ed719b 625 use strict ;
626
627 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
628 print FILE <<'EOM' ;
629
630 package SubDB ;
631
3245f058 632 use warnings ;
a6ed719b 633 use strict ;
07200f1b 634 our (@ISA, @EXPORT);
a6ed719b 635
636 require Exporter ;
637 use DB_File;
638 @ISA=qw(DB_File);
639 @EXPORT = @DB_File::EXPORT ;
640
641 sub STORE {
642 my $self = shift ;
643 my $key = shift ;
644 my $value = shift ;
645 $self->SUPER::STORE($key, $value * 2) ;
646 }
647
648 sub FETCH {
649 my $self = shift ;
650 my $key = shift ;
651 $self->SUPER::FETCH($key) - 1 ;
652 }
653
654 sub put {
655 my $self = shift ;
656 my $key = shift ;
657 my $value = shift ;
658 $self->SUPER::put($key, $value * 3) ;
659 }
660
661 sub get {
662 my $self = shift ;
663 $self->SUPER::get($_[0], $_[1]) ;
664 $_[1] -= 2 ;
665 }
666
667 sub A_new_method
668 {
669 my $self = shift ;
670 my $key = shift ;
671 my $value = $self->FETCH($key) ;
672 return "[[$value]]" ;
673 }
674
675 1 ;
676EOM
677
678 close FILE ;
679
a9fd575d 680 BEGIN { push @INC, '.'; }
a6ed719b 681 eval 'use SubDB ; ';
682 main::ok(93, $@ eq "") ;
683 my %h ;
684 my $X ;
685 eval '
686 $X = tie(%h, "SubDB","dbbtree.tmp", O_RDWR|O_CREAT, 0640, $DB_BTREE );
687 ' ;
688
689 main::ok(94, $@ eq "") ;
690
691 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
692 main::ok(95, $@ eq "") ;
693 main::ok(96, $ret == 5) ;
694
695 my $value = 0;
696 $ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ;
697 main::ok(97, $@ eq "") ;
698 main::ok(98, $ret == 10) ;
699
700 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
701 main::ok(99, $@ eq "" ) ;
702 main::ok(100, $ret == 1) ;
703
704 $ret = eval '$X->A_new_method("joe") ' ;
705 main::ok(101, $@ eq "") ;
706 main::ok(102, $ret eq "[[11]]") ;
707
fac76ed7 708 undef $X;
709 untie(%h);
a6ed719b 710 unlink "SubDB.pm", "dbbtree.tmp" ;
711
712}
713
9fe6733a 714{
715 # DBM Filter tests
3245f058 716 use warnings ;
9fe6733a 717 use strict ;
718 my (%h, $db) ;
719 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
720 unlink $Dfile;
721
722 sub checkOutput
723 {
724 my($fk, $sk, $fv, $sv) = @_ ;
725 return
726 $fetch_key eq $fk && $store_key eq $sk &&
727 $fetch_value eq $fv && $store_value eq $sv &&
728 $_ eq 'original' ;
729 }
730
731 ok(103, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
732
733 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
734 $db->filter_store_key (sub { $store_key = $_ }) ;
735 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
736 $db->filter_store_value (sub { $store_value = $_ }) ;
737
738 $_ = "original" ;
739
740 $h{"fred"} = "joe" ;
741 # fk sk fv sv
742 ok(104, checkOutput( "", "fred", "", "joe")) ;
743
744 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
745 ok(105, $h{"fred"} eq "joe");
746 # fk sk fv sv
747 ok(106, checkOutput( "", "fred", "joe", "")) ;
748
749 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
750 ok(107, $db->FIRSTKEY() eq "fred") ;
751 # fk sk fv sv
752 ok(108, checkOutput( "fred", "", "", "")) ;
753
754 # replace the filters, but remember the previous set
755 my ($old_fk) = $db->filter_fetch_key
756 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
757 my ($old_sk) = $db->filter_store_key
758 (sub { $_ = lc $_ ; $store_key = $_ }) ;
759 my ($old_fv) = $db->filter_fetch_value
760 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
761 my ($old_sv) = $db->filter_store_value
762 (sub { s/o/x/g; $store_value = $_ }) ;
763
764 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
765 $h{"Fred"} = "Joe" ;
766 # fk sk fv sv
767 ok(109, checkOutput( "", "fred", "", "Jxe")) ;
768
769 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
770 ok(110, $h{"Fred"} eq "[Jxe]");
771 # fk sk fv sv
772 ok(111, checkOutput( "", "fred", "[Jxe]", "")) ;
773
774 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
775 ok(112, $db->FIRSTKEY() eq "FRED") ;
776 # fk sk fv sv
777 ok(113, checkOutput( "FRED", "", "", "")) ;
778
779 # put the original filters back
780 $db->filter_fetch_key ($old_fk);
781 $db->filter_store_key ($old_sk);
782 $db->filter_fetch_value ($old_fv);
783 $db->filter_store_value ($old_sv);
784
785 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
786 $h{"fred"} = "joe" ;
787 ok(114, checkOutput( "", "fred", "", "joe")) ;
788
789 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
790 ok(115, $h{"fred"} eq "joe");
791 ok(116, checkOutput( "", "fred", "joe", "")) ;
792
793 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
794 ok(117, $db->FIRSTKEY() eq "fred") ;
795 ok(118, checkOutput( "fred", "", "", "")) ;
796
797 # delete the filters
798 $db->filter_fetch_key (undef);
799 $db->filter_store_key (undef);
800 $db->filter_fetch_value (undef);
801 $db->filter_store_value (undef);
802
803 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
804 $h{"fred"} = "joe" ;
805 ok(119, checkOutput( "", "", "", "")) ;
806
807 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
808 ok(120, $h{"fred"} eq "joe");
809 ok(121, checkOutput( "", "", "", "")) ;
810
811 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
812 ok(122, $db->FIRSTKEY() eq "fred") ;
813 ok(123, checkOutput( "", "", "", "")) ;
814
815 undef $db ;
816 untie %h;
817 unlink $Dfile;
818}
819
820{
821 # DBM Filter with a closure
822
3245f058 823 use warnings ;
9fe6733a 824 use strict ;
825 my (%h, $db) ;
826
827 unlink $Dfile;
828 ok(124, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
829
830 my %result = () ;
831
832 sub Closure
833 {
834 my ($name) = @_ ;
835 my $count = 0 ;
836 my @kept = () ;
837
838 return sub { ++$count ;
839 push @kept, $_ ;
840 $result{$name} = "$name - $count: [@kept]" ;
841 }
842 }
843
844 $db->filter_store_key(Closure("store key")) ;
845 $db->filter_store_value(Closure("store value")) ;
846 $db->filter_fetch_key(Closure("fetch key")) ;
847 $db->filter_fetch_value(Closure("fetch value")) ;
848
849 $_ = "original" ;
850
851 $h{"fred"} = "joe" ;
852 ok(125, $result{"store key"} eq "store key - 1: [fred]");
853 ok(126, $result{"store value"} eq "store value - 1: [joe]");
854 ok(127, ! defined $result{"fetch key"} );
855 ok(128, ! defined $result{"fetch value"} );
856 ok(129, $_ eq "original") ;
857
858 ok(130, $db->FIRSTKEY() eq "fred") ;
859 ok(131, $result{"store key"} eq "store key - 1: [fred]");
860 ok(132, $result{"store value"} eq "store value - 1: [joe]");
861 ok(133, $result{"fetch key"} eq "fetch key - 1: [fred]");
862 ok(134, ! defined $result{"fetch value"} );
863 ok(135, $_ eq "original") ;
864
865 $h{"jim"} = "john" ;
866 ok(136, $result{"store key"} eq "store key - 2: [fred jim]");
867 ok(137, $result{"store value"} eq "store value - 2: [joe john]");
868 ok(138, $result{"fetch key"} eq "fetch key - 1: [fred]");
869 ok(139, ! defined $result{"fetch value"} );
870 ok(140, $_ eq "original") ;
871
872 ok(141, $h{"fred"} eq "joe");
873 ok(142, $result{"store key"} eq "store key - 3: [fred jim fred]");
874 ok(143, $result{"store value"} eq "store value - 2: [joe john]");
875 ok(144, $result{"fetch key"} eq "fetch key - 1: [fred]");
876 ok(145, $result{"fetch value"} eq "fetch value - 1: [joe]");
877 ok(146, $_ eq "original") ;
878
879 undef $db ;
880 untie %h;
881 unlink $Dfile;
882}
883
884{
885 # DBM Filter recursion detection
3245f058 886 use warnings ;
9fe6733a 887 use strict ;
888 my (%h, $db) ;
889 unlink $Dfile;
890
891 ok(147, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
892
893 $db->filter_store_key (sub { $_ = $h{$_} }) ;
894
895 eval '$h{1} = 1234' ;
896 ok(148, $@ =~ /^recursion detected in filter_store_key at/ );
897
898 undef $db ;
899 untie %h;
900 unlink $Dfile;
901}
902
903
2c2d71f5 904{
905 # Examples from the POD
906
907
908 my $file = "xyzt" ;
909 {
910 my $redirect = new Redirect $file ;
911
912 # BTREE example 1
913 ###
914
3245f058 915 use warnings FATAL => qw(all) ;
2c2d71f5 916 use strict ;
917 use DB_File ;
918
919 my %h ;
920
921 sub Compare
922 {
923 my ($key1, $key2) = @_ ;
924 "\L$key1" cmp "\L$key2" ;
925 }
926
927 # specify the Perl sub that will do the comparison
928 $DB_BTREE->{'compare'} = \&Compare ;
929
930 unlink "tree" ;
931 tie %h, "DB_File", "tree", O_RDWR|O_CREAT, 0640, $DB_BTREE
932 or die "Cannot open file 'tree': $!\n" ;
933
934 # Add a key/value pair to the file
935 $h{'Wall'} = 'Larry' ;
936 $h{'Smith'} = 'John' ;
937 $h{'mouse'} = 'mickey' ;
938 $h{'duck'} = 'donald' ;
939
940 # Delete
941 delete $h{"duck"} ;
942
943 # Cycle through the keys printing them in order.
944 # Note it is not necessary to sort the keys as
945 # the btree will have kept them in order automatically.
946 foreach (keys %h)
947 { print "$_\n" }
948
949 untie %h ;
950
951 unlink "tree" ;
952 }
953
954 delete $DB_BTREE->{'compare'} ;
955
956 ok(149, docat_del($file) eq <<'EOM') ;
957mouse
958Smith
959Wall
960EOM
961
962 {
963 my $redirect = new Redirect $file ;
964
965 # BTREE example 2
966 ###
967
3245f058 968 use warnings FATAL => qw(all) ;
2c2d71f5 969 use strict ;
970 use DB_File ;
971
962cee9f 972 my ($filename, %h);
2c2d71f5 973
974 $filename = "tree" ;
975 unlink $filename ;
976
977 # Enable duplicate records
978 $DB_BTREE->{'flags'} = R_DUP ;
979
980 tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
981 or die "Cannot open $filename: $!\n";
982
983 # Add some key/value pairs to the file
984 $h{'Wall'} = 'Larry' ;
985 $h{'Wall'} = 'Brick' ; # Note the duplicate key
986 $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
987 $h{'Smith'} = 'John' ;
988 $h{'mouse'} = 'mickey' ;
989
990 # iterate through the associative array
991 # and print each key/value pair.
992 foreach (keys %h)
993 { print "$_ -> $h{$_}\n" }
994
995 untie %h ;
996
997 unlink $filename ;
998 }
999
039d031f 1000 ok(150, docat_del($file) eq ($db185mode ? <<'EOM' : <<'EOM') ) ;
2c2d71f5 1001Smith -> John
1002Wall -> Brick
1003Wall -> Brick
1004Wall -> Brick
1005mouse -> mickey
1006EOM
1007Smith -> John
1008Wall -> Larry
1009Wall -> Larry
1010Wall -> Larry
1011mouse -> mickey
1012EOM
1013
1014 {
1015 my $redirect = new Redirect $file ;
1016
1017 # BTREE example 3
1018 ###
1019
3245f058 1020 use warnings FATAL => qw(all) ;
2c2d71f5 1021 use strict ;
1022 use DB_File ;
1023
962cee9f 1024 my ($filename, $x, %h, $status, $key, $value);
2c2d71f5 1025
1026 $filename = "tree" ;
1027 unlink $filename ;
1028
1029 # Enable duplicate records
1030 $DB_BTREE->{'flags'} = R_DUP ;
1031
1032 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1033 or die "Cannot open $filename: $!\n";
1034
1035 # Add some key/value pairs to the file
1036 $h{'Wall'} = 'Larry' ;
1037 $h{'Wall'} = 'Brick' ; # Note the duplicate key
1038 $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
1039 $h{'Smith'} = 'John' ;
1040 $h{'mouse'} = 'mickey' ;
1041
1042 # iterate through the btree using seq
1043 # and print each key/value pair.
1044 $key = $value = 0 ;
1045 for ($status = $x->seq($key, $value, R_FIRST) ;
1046 $status == 0 ;
1047 $status = $x->seq($key, $value, R_NEXT) )
1048 { print "$key -> $value\n" }
1049
1050
1051 undef $x ;
1052 untie %h ;
1053 }
1054
039d031f 1055 ok(151, docat_del($file) eq ($db185mode == 1 ? <<'EOM' : <<'EOM') ) ;
2c2d71f5 1056Smith -> John
1057Wall -> Brick
1058Wall -> Brick
1059Wall -> Larry
1060mouse -> mickey
1061EOM
1062Smith -> John
1063Wall -> Larry
1064Wall -> Brick
1065Wall -> Brick
1066mouse -> mickey
1067EOM
1068
1069
1070 {
1071 my $redirect = new Redirect $file ;
1072
1073 # BTREE example 4
1074 ###
1075
3245f058 1076 use warnings FATAL => qw(all) ;
2c2d71f5 1077 use strict ;
1078 use DB_File ;
1079
962cee9f 1080 my ($filename, $x, %h);
2c2d71f5 1081
1082 $filename = "tree" ;
1083
1084 # Enable duplicate records
1085 $DB_BTREE->{'flags'} = R_DUP ;
1086
1087 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1088 or die "Cannot open $filename: $!\n";
1089
1090 my $cnt = $x->get_dup("Wall") ;
1091 print "Wall occurred $cnt times\n" ;
1092
1093 my %hash = $x->get_dup("Wall", 1) ;
1094 print "Larry is there\n" if $hash{'Larry'} ;
1095 print "There are $hash{'Brick'} Brick Walls\n" ;
1096
1097 my @list = sort $x->get_dup("Wall") ;
1098 print "Wall => [@list]\n" ;
1099
1100 @list = $x->get_dup("Smith") ;
1101 print "Smith => [@list]\n" ;
1102
1103 @list = $x->get_dup("Dog") ;
1104 print "Dog => [@list]\n" ;
1105
1106 undef $x ;
1107 untie %h ;
1108 }
1109
1110 ok(152, docat_del($file) eq <<'EOM') ;
1111Wall occurred 3 times
1112Larry is there
1113There are 2 Brick Walls
1114Wall => [Brick Brick Larry]
1115Smith => [John]
1116Dog => []
1117EOM
1118
1119 {
1120 my $redirect = new Redirect $file ;
1121
1122 # BTREE example 5
1123 ###
1124
3245f058 1125 use warnings FATAL => qw(all) ;
2c2d71f5 1126 use strict ;
1127 use DB_File ;
1128
962cee9f 1129 my ($filename, $x, %h, $found);
2c2d71f5 1130
07200f1b 1131 $filename = "tree" ;
2c2d71f5 1132
1133 # Enable duplicate records
1134 $DB_BTREE->{'flags'} = R_DUP ;
1135
1136 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1137 or die "Cannot open $filename: $!\n";
1138
1139 $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
1140 print "Larry Wall is $found there\n" ;
1141
1142 $found = ( $x->find_dup("Wall", "Harry") == 0 ? "" : "not") ;
1143 print "Harry Wall is $found there\n" ;
1144
1145 undef $x ;
1146 untie %h ;
1147 }
1148
1149 ok(153, docat_del($file) eq <<'EOM') ;
1150Larry Wall is there
1151Harry Wall is not there
1152EOM
1153
1154 {
1155 my $redirect = new Redirect $file ;
1156
1157 # BTREE example 6
1158 ###
1159
3245f058 1160 use warnings FATAL => qw(all) ;
2c2d71f5 1161 use strict ;
1162 use DB_File ;
1163
962cee9f 1164 my ($filename, $x, %h, $found);
2c2d71f5 1165
07200f1b 1166 $filename = "tree" ;
2c2d71f5 1167
1168 # Enable duplicate records
1169 $DB_BTREE->{'flags'} = R_DUP ;
1170
1171 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1172 or die "Cannot open $filename: $!\n";
1173
1174 $x->del_dup("Wall", "Larry") ;
1175
1176 $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
1177 print "Larry Wall is $found there\n" ;
1178
1179 undef $x ;
1180 untie %h ;
1181
1182 unlink $filename ;
1183 }
1184
1185 ok(154, docat_del($file) eq <<'EOM') ;
1186Larry Wall is not there
1187EOM
1188
1189 {
1190 my $redirect = new Redirect $file ;
1191
1192 # BTREE example 7
1193 ###
1194
3245f058 1195 use warnings FATAL => qw(all) ;
2c2d71f5 1196 use strict ;
1197 use DB_File ;
1198 use Fcntl ;
1199
962cee9f 1200 my ($filename, $x, %h, $st, $key, $value);
2c2d71f5 1201
1202 sub match
1203 {
1204 my $key = shift ;
1205 my $value = 0;
1206 my $orig_key = $key ;
1207 $x->seq($key, $value, R_CURSOR) ;
1208 print "$orig_key\t-> $key\t-> $value\n" ;
1209 }
1210
1211 $filename = "tree" ;
1212 unlink $filename ;
1213
1214 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1215 or die "Cannot open $filename: $!\n";
1216
1217 # Add some key/value pairs to the file
1218 $h{'mouse'} = 'mickey' ;
1219 $h{'Wall'} = 'Larry' ;
1220 $h{'Walls'} = 'Brick' ;
1221 $h{'Smith'} = 'John' ;
1222
1223
1224 $key = $value = 0 ;
1225 print "IN ORDER\n" ;
1226 for ($st = $x->seq($key, $value, R_FIRST) ;
1227 $st == 0 ;
1228 $st = $x->seq($key, $value, R_NEXT) )
1229
1230 { print "$key -> $value\n" }
1231
1232 print "\nPARTIAL MATCH\n" ;
1233
1234 match "Wa" ;
1235 match "A" ;
1236 match "a" ;
1237
1238 undef $x ;
1239 untie %h ;
1240
1241 unlink $filename ;
1242
1243 }
1244
1245 ok(155, docat_del($file) eq <<'EOM') ;
1246IN ORDER
1247Smith -> John
1248Wall -> Larry
1249Walls -> Brick
1250mouse -> mickey
1251
1252PARTIAL MATCH
1253Wa -> Wall -> Larry
1254A -> Smith -> John
1255a -> mouse -> mickey
1256EOM
1257
1258}
1259
a62982a8 1260#{
1261# # R_SETCURSOR
1262# use strict ;
1263# my (%h, $db) ;
1264# unlink $Dfile;
1265#
1266# ok(156, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1267#
1268# $h{abc} = 33 ;
1269# my $k = "newest" ;
1270# my $v = 44 ;
1271# my $status = $db->put($k, $v, R_SETCURSOR) ;
1272# print "status = [$status]\n" ;
1273# ok(157, $status == 0) ;
1274# $status = $db->del($k, R_CURSOR) ;
1275# print "status = [$status]\n" ;
1276# ok(158, $status == 0) ;
1277# $k = "newest" ;
1278# ok(159, $db->get($k, $v, R_CURSOR)) ;
1279#
1280# ok(160, keys %h == 1) ;
1281#
1282# undef $db ;
1283# untie %h;
1284# unlink $Dfile;
1285#}
1286
cbc5248d 1287{
1288 # Bug ID 20001013.009
1289 #
1290 # test that $hash{KEY} = undef doesn't produce the warning
1291 # Use of uninitialized value in null operation
1292 use warnings ;
1293 use strict ;
1294 use DB_File ;
1295
1296 unlink $Dfile;
1297 my %h ;
1298 my $a = "";
1299 local $SIG{__WARN__} = sub {$a = $_[0]} ;
1300
3245f058 1301 tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE
cbc5248d 1302 or die "Can't open file: $!\n" ;
1303 $h{ABC} = undef;
1304 ok(156, $a eq "") ;
1305 untie %h ;
1306 unlink $Dfile;
1307}
1308
3245f058 1309{
1310 # test that %hash = () doesn't produce the warning
1311 # Argument "" isn't numeric in entersub
1312 use warnings ;
1313 use strict ;
1314 use DB_File ;
1315
1316 unlink $Dfile;
1317 my %h ;
1318 my $a = "";
1319 local $SIG{__WARN__} = sub {$a = $_[0]} ;
1320
1321 tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE
1322 or die "Can't open file: $!\n" ;
1323 %h = (); ;
1324 ok(157, $a eq "") ;
1325 untie %h ;
1326 unlink $Dfile;
1327}
1328
0bf2e707 1329{
1330 # When iterating over a tied hash using "each", the key passed to FETCH
1331 # will be recycled and passed to NEXTKEY. If a Source Filter modifies the
1332 # key in FETCH via a filter_fetch_key method we need to check that the
1333 # modified key doesn't get passed to NEXTKEY.
1334 # Also Test "keys" & "values" while we are at it.
1335
1336 use warnings ;
1337 use strict ;
1338 use DB_File ;
1339
1340 unlink $Dfile;
1341 my $bad_key = 0 ;
1342 my %h = () ;
1343 my $db ;
1344 ok(158, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1345 $db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ;
1346 $db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ;
1347
1348 $h{'Alpha_ABC'} = 2 ;
1349 $h{'Alpha_DEF'} = 5 ;
1350
1351 ok(159, $h{'Alpha_ABC'} == 2);
1352 ok(160, $h{'Alpha_DEF'} == 5);
1353
1354 my ($k, $v) = ("","");
1355 while (($k, $v) = each %h) {}
1356 ok(161, $bad_key == 0);
1357
1358 $bad_key = 0 ;
1359 foreach $k (keys %h) {}
1360 ok(162, $bad_key == 0);
1361
1362 $bad_key = 0 ;
1363 foreach $v (values %h) {}
1364 ok(163, $bad_key == 0);
1365
1366 undef $db ;
1367 untie %h ;
1368 unlink $Dfile;
1369}
1370
a0d0e21e 1371exit ;