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