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