perl 5.003_07: Configure
[p5sagit/p5-mst-13.2.git] / t / lib / db-btree.t
CommitLineData
f6b705ef 1#!./perl -w
a0d0e21e 2
3BEGIN {
4 @INC = '../lib';
5 require Config; import Config;
6 if ($Config{'extensions'} !~ /\bDB_File\b/) {
7 print "1..0\n";
8 exit 0;
9 }
10}
11
12use DB_File;
13use Fcntl;
14
f6b705ef 15print "1..91\n";
16
17sub ok
18{
19 my $no = shift ;
20 my $result = shift ;
21
22 print "not " unless $result ;
23 print "ok $no\n" ;
24}
a0d0e21e 25
55d68b4a 26$Dfile = "dbbtree.tmp";
a0d0e21e 27unlink $Dfile;
28
29umask(0);
30
31# Check the interface to BTREEINFO
32
f6b705ef 33my $dbh = new DB_File::BTREEINFO ;
34$^W = 0 ;
35ok(1, $dbh->{flags} == undef) ;
36ok(2, $dbh->{cachesize} == undef) ;
37ok(3, $dbh->{psize} == undef) ;
38ok(4, $dbh->{lorder} == undef) ;
39ok(5, $dbh->{minkeypage} == undef) ;
40ok(6, $dbh->{maxkeypage} == undef) ;
41ok(7, $dbh->{compare} == undef) ;
42ok(8, $dbh->{prefix} == undef) ;
43$^W = 1 ;
a0d0e21e 44
45$dbh->{flags} = 3000 ;
f6b705ef 46ok(9, $dbh->{flags} == 3000) ;
a0d0e21e 47
48$dbh->{cachesize} = 9000 ;
f6b705ef 49ok(10, $dbh->{cachesize} == 9000);
50
a0d0e21e 51$dbh->{psize} = 400 ;
f6b705ef 52ok(11, $dbh->{psize} == 400) ;
a0d0e21e 53
54$dbh->{lorder} = 65 ;
f6b705ef 55ok(12, $dbh->{lorder} == 65) ;
a0d0e21e 56
57$dbh->{minkeypage} = 123 ;
f6b705ef 58ok(13, $dbh->{minkeypage} == 123) ;
a0d0e21e 59
60$dbh->{maxkeypage} = 1234 ;
f6b705ef 61ok(14, $dbh->{maxkeypage} == 1234 );
a0d0e21e 62
63$dbh->{compare} = 1234 ;
f6b705ef 64ok(15, $dbh->{compare} == 1234) ;
a0d0e21e 65
66$dbh->{prefix} = 1234 ;
f6b705ef 67ok(16, $dbh->{prefix} == 1234 );
a0d0e21e 68
69# Check that an invalid entry is caught both for store & fetch
70eval '$dbh->{fred} = 1234' ;
f6b705ef 71ok(17, $@ =~ /^DB_File::BTREEINFO::STORE - Unknown element 'fred' at/ ) ;
a0d0e21e 72eval '$q = $dbh->{fred}' ;
f6b705ef 73ok(18, $@ =~ /^DB_File::BTREEINFO::FETCH - Unknown element 'fred' at/ ) ;
a0d0e21e 74
75# Now check the interface to BTREE
76
f6b705ef 77ok(19, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE )) ;
a0d0e21e 78
79($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
80 $blksize,$blocks) = stat($Dfile);
f6b705ef 81ok(20, ($mode & 0777) == 0640 );
a0d0e21e 82
83while (($key,$value) = each(%h)) {
84 $i++;
85}
f6b705ef 86ok(21, !$i ) ;
a0d0e21e 87
88$h{'goner1'} = 'snork';
89
90$h{'abc'} = 'ABC';
f6b705ef 91ok(22, $h{'abc'} eq 'ABC' );
92ok(23, ! defined $h{'jimmy'} ) ;
93ok(24, ! exists $h{'jimmy'} ) ;
94ok(25, defined $h{'abc'} ) ;
a0d0e21e 95
96$h{'def'} = 'DEF';
97$h{'jkl','mno'} = "JKL\034MNO";
98$h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
99$h{'a'} = 'A';
100
101#$h{'b'} = 'B';
102$X->STORE('b', 'B') ;
103
104$h{'c'} = 'C';
105
106#$h{'d'} = 'D';
107$X->put('d', 'D') ;
108
109$h{'e'} = 'E';
110$h{'f'} = 'F';
111$h{'g'} = 'X';
112$h{'h'} = 'H';
113$h{'i'} = 'I';
114
115$h{'goner2'} = 'snork';
116delete $h{'goner2'};
117
118
119# IMPORTANT - $X must be undefined before the untie otherwise the
120# underlying DB close routine will not get called.
121undef $X ;
122untie(%h);
123
124
125# tie to the same file again
f6b705ef 126ok(26, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE)) ;
a0d0e21e 127
128# Modify an entry from the previous tie
129$h{'g'} = 'G';
130
131$h{'j'} = 'J';
132$h{'k'} = 'K';
133$h{'l'} = 'L';
134$h{'m'} = 'M';
135$h{'n'} = 'N';
136$h{'o'} = 'O';
137$h{'p'} = 'P';
138$h{'q'} = 'Q';
139$h{'r'} = 'R';
140$h{'s'} = 'S';
141$h{'t'} = 'T';
142$h{'u'} = 'U';
143$h{'v'} = 'V';
144$h{'w'} = 'W';
145$h{'x'} = 'X';
146$h{'y'} = 'Y';
147$h{'z'} = 'Z';
148
149$h{'goner3'} = 'snork';
150
151delete $h{'goner1'};
152$X->DELETE('goner3');
153
154@keys = keys(%h);
155@values = values(%h);
156
f6b705ef 157ok(27, $#keys == 29 && $#values == 29) ;
a0d0e21e 158
f6b705ef 159$i = 0 ;
a0d0e21e 160while (($key,$value) = each(%h)) {
2f52a358 161 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
a0d0e21e 162 $key =~ y/a-z/A-Z/;
163 $i++ if $key eq $value;
164 }
165}
166
f6b705ef 167ok(28, $i == 30) ;
a0d0e21e 168
55d68b4a 169@keys = ('blurfl', keys(%h), 'dyick');
f6b705ef 170ok(29, $#keys == 31) ;
a0d0e21e 171
172#Check that the keys can be retrieved in order
173$ok = 1 ;
174foreach (keys %h)
175{
176 ($ok = 0), last if defined $previous && $previous gt $_ ;
177 $previous = $_ ;
178}
f6b705ef 179ok(30, $ok ) ;
a0d0e21e 180
181$h{'foo'} = '';
f6b705ef 182ok(31, $h{'foo'} eq '' ) ;
a0d0e21e 183
184$h{''} = 'bar';
f6b705ef 185ok(32, $h{''} eq 'bar' );
a0d0e21e 186
187# check cache overflow and numeric keys and contents
188$ok = 1;
189for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
190for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
f6b705ef 191ok(33, $ok);
a0d0e21e 192
193($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
194 $blksize,$blocks) = stat($Dfile);
f6b705ef 195ok(34, $size > 0 );
a0d0e21e 196
197@h{0..200} = 200..400;
198@foo = @h{0..200};
f6b705ef 199ok(35, join(':',200..400) eq join(':',@foo) );
a0d0e21e 200
201# Now check all the non-tie specific stuff
202
203
204# Check R_NOOVERWRITE flag will make put fail when attempting to overwrite
205# an existing record.
206
207$status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
f6b705ef 208ok(36, $status == 1 );
a0d0e21e 209
210# check that the value of the key 'x' has not been changed by the
211# previous test
f6b705ef 212ok(37, $h{'x'} eq 'X' );
a0d0e21e 213
214# standard put
215$status = $X->put('key', 'value') ;
f6b705ef 216ok(38, $status == 0 );
a0d0e21e 217
218#check that previous put can be retrieved
f6b705ef 219$value = 0 ;
a0d0e21e 220$status = $X->get('key', $value) ;
f6b705ef 221ok(39, $status == 0 );
222ok(40, $value eq 'value' );
a0d0e21e 223
224# Attempting to delete an existing key should work
225
226$status = $X->del('q') ;
f6b705ef 227ok(41, $status == 0 );
a0d0e21e 228$status = $X->del('') ;
f6b705ef 229ok(42, $status == 0 );
a0d0e21e 230
231# Make sure that the key deleted, cannot be retrieved
f6b705ef 232$^W = 0 ;
233ok(43, $h{'q'} eq undef) ;
234ok(44, $h{''} eq undef) ;
235$^W = 1 ;
a0d0e21e 236
237undef $X ;
238untie %h ;
239
f6b705ef 240ok(45, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE ));
a0d0e21e 241
242# Attempting to delete a non-existant key should fail
243
244$status = $X->del('joe') ;
f6b705ef 245ok(46, $status == 1 );
a0d0e21e 246
247# Check the get interface
248
249# First a non-existing key
250$status = $X->get('aaaa', $value) ;
f6b705ef 251ok(47, $status == 1 );
a0d0e21e 252
253# Next an existing key
254$status = $X->get('a', $value) ;
f6b705ef 255ok(48, $status == 0 );
256ok(49, $value eq 'A' );
a0d0e21e 257
258# seq
259# ###
260
261# use seq to find an approximate match
262$key = 'ke' ;
263$value = '' ;
264$status = $X->seq($key, $value, R_CURSOR) ;
f6b705ef 265ok(50, $status == 0 );
266ok(51, $key eq 'key' );
267ok(52, $value eq 'value' );
a0d0e21e 268
269# seq when the key does not match
270$key = 'zzz' ;
271$value = '' ;
272$status = $X->seq($key, $value, R_CURSOR) ;
f6b705ef 273ok(53, $status == 1 );
a0d0e21e 274
275
276# use seq to set the cursor, then delete the record @ the cursor.
277
278$key = 'x' ;
279$value = '' ;
280$status = $X->seq($key, $value, R_CURSOR) ;
f6b705ef 281ok(54, $status == 0 );
282ok(55, $key eq 'x' );
283ok(56, $value eq 'X' );
a0d0e21e 284$status = $X->del(0, R_CURSOR) ;
f6b705ef 285ok(57, $status == 0 );
a0d0e21e 286$status = $X->get('x', $value) ;
f6b705ef 287ok(58, $status == 1 );
a0d0e21e 288
289# ditto, but use put to replace the key/value pair.
290$key = 'y' ;
291$value = '' ;
292$status = $X->seq($key, $value, R_CURSOR) ;
f6b705ef 293ok(59, $status == 0 );
294ok(60, $key eq 'y' );
295ok(61, $value eq 'Y' );
a0d0e21e 296
297$key = "replace key" ;
298$value = "replace value" ;
299$status = $X->put($key, $value, R_CURSOR) ;
f6b705ef 300ok(62, $status == 0 );
301ok(63, $key eq 'replace key' );
302ok(64, $value eq 'replace value' );
a0d0e21e 303$status = $X->get('y', $value) ;
f6b705ef 304ok(65, $status == 1 );
a0d0e21e 305
306# use seq to walk forwards through a file
307
308$status = $X->seq($key, $value, R_FIRST) ;
f6b705ef 309ok(66, $status == 0 );
a0d0e21e 310$previous = $key ;
311
312$ok = 1 ;
313while (($status = $X->seq($key, $value, R_NEXT)) == 0)
314{
315 ($ok = 0), last if ($previous cmp $key) == 1 ;
316}
317
f6b705ef 318ok(67, $status == 1 );
319ok(68, $ok == 1 );
a0d0e21e 320
321# use seq to walk backwards through a file
322$status = $X->seq($key, $value, R_LAST) ;
f6b705ef 323ok(69, $status == 0 );
a0d0e21e 324$previous = $key ;
325
326$ok = 1 ;
327while (($status = $X->seq($key, $value, R_PREV)) == 0)
328{
329 ($ok = 0), last if ($previous cmp $key) == -1 ;
330 #print "key = [$key] value = [$value]\n" ;
331}
332
f6b705ef 333ok(70, $status == 1 );
334ok(71, $ok == 1 );
a0d0e21e 335
336
337# check seq FIRST/LAST
338
339# sync
340# ####
341
342$status = $X->sync ;
f6b705ef 343ok(72, $status == 0 );
a0d0e21e 344
345
346# fd
347# ##
348
349$status = $X->fd ;
f6b705ef 350ok(73, $status != 0 );
a0d0e21e 351
352
353undef $X ;
354untie %h ;
355
356unlink $Dfile;
357
358# Now try an in memory file
f6b705ef 359ok(74, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE ));
a0d0e21e 360
361# fd with an in memory file should return failure
362$status = $Y->fd ;
f6b705ef 363ok(75, $status == -1 );
a0d0e21e 364
55d68b4a 365
a0d0e21e 366undef $Y ;
367untie %h ;
368
55d68b4a 369# Duplicate keys
370my $bt = new DB_File::BTREEINFO ;
371$bt->{flags} = R_DUP ;
f6b705ef 372ok(76, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ;
55d68b4a 373
374$hh{'Wall'} = 'Larry' ;
375$hh{'Wall'} = 'Stone' ; # Note the duplicate key
376$hh{'Wall'} = 'Brick' ; # Note the duplicate key
f6b705ef 377$hh{'Wall'} = 'Brick' ; # Note the duplicate key and value
55d68b4a 378$hh{'Smith'} = 'John' ;
379$hh{'mouse'} = 'mickey' ;
380
381# first work in scalar context
f6b705ef 382ok(77, scalar $YY->get_dup('Unknown') == 0 );
383ok(78, scalar $YY->get_dup('Smith') == 1 );
384ok(79, scalar $YY->get_dup('Wall') == 4 );
55d68b4a 385
386# now in list context
387my @unknown = $YY->get_dup('Unknown') ;
f6b705ef 388ok(80, "@unknown" eq "" );
55d68b4a 389
390my @smith = $YY->get_dup('Smith') ;
f6b705ef 391ok(81, "@smith" eq "John" );
55d68b4a 392
760ac839 393{
f6b705ef 394my @wall = $YY->get_dup('Wall') ;
395my %wall ;
396@wall{@wall} = @wall ;
397ok(82, (@wall == 4 && $wall{'Larry'} && $wall{'Stone'} && $wall{'Brick'}) );
760ac839 398}
55d68b4a 399
400# hash
401my %unknown = $YY->get_dup('Unknown', 1) ;
f6b705ef 402ok(83, keys %unknown == 0 );
55d68b4a 403
404my %smith = $YY->get_dup('Smith', 1) ;
f6b705ef 405ok(84, keys %smith == 1 && $smith{'John'}) ;
55d68b4a 406
f6b705ef 407my %wall = $YY->get_dup('Wall', 1) ;
408ok(85, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1
409 && $wall{'Brick'} == 2);
55d68b4a 410
411undef $YY ;
412untie %hh ;
413unlink $Dfile;
414
415
8e07c86e 416# test multiple callbacks
417$Dfile1 = "btree1" ;
418$Dfile2 = "btree2" ;
419$Dfile3 = "btree3" ;
420
421$dbh1 = TIEHASH DB_File::BTREEINFO ;
422$dbh1->{compare} = sub { $_[0] <=> $_[1] } ;
423
424$dbh2 = TIEHASH DB_File::BTREEINFO ;
425$dbh2->{compare} = sub { $_[0] cmp $_[1] } ;
426
427$dbh3 = TIEHASH DB_File::BTREEINFO ;
428$dbh3->{compare} = sub { length $_[0] <=> length $_[1] } ;
429
430
f6b705ef 431tie(%h, 'DB_File',$Dfile1, O_RDWR|O_CREAT, 0640, $dbh1 ) ;
432tie(%g, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) ;
433tie(%k, 'DB_File',$Dfile3, O_RDWR|O_CREAT, 0640, $dbh3 ) ;
8e07c86e 434
435@Keys = qw( 0123 12 -1234 9 987654321 def ) ;
f6b705ef 436$^W = 0 ;
8e07c86e 437@srt_1 = sort { $a <=> $b } @Keys ;
f6b705ef 438$^W = 1 ;
8e07c86e 439@srt_2 = sort { $a cmp $b } @Keys ;
440@srt_3 = sort { length $a <=> length $b } @Keys ;
441
442foreach (@Keys) {
f6b705ef 443 $^W = 0 ; $h{$_} = 1 ; $^W = 1 ;
8e07c86e 444 $g{$_} = 1 ;
445 $k{$_} = 1 ;
446}
447
448sub ArrayCompare
449{
450 my($a, $b) = @_ ;
451
452 return 0 if @$a != @$b ;
453
454 foreach (1 .. length @$a)
455 {
456 return 0 unless $$a[$_] eq $$b[$_] ;
457 }
458
459 1 ;
460}
461
f6b705ef 462ok(86, ArrayCompare (\@srt_1, [keys %h]) );
463ok(87, ArrayCompare (\@srt_2, [keys %g]) );
464ok(88, ArrayCompare (\@srt_3, [keys %k]) );
8e07c86e 465
466untie %h ;
467untie %g ;
468untie %k ;
469unlink $Dfile1, $Dfile2, $Dfile3 ;
470
f6b705ef 471# clear
472# #####
473
474ok(89, tie(%h, 'DB_File', $Dfile1, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
475foreach (1 .. 10)
476 { $h{$_} = $_ * 100 }
477
478# check that there are 10 elements in the hash
479$i = 0 ;
480while (($key,$value) = each(%h)) {
481 $i++;
482}
483ok(90, $i == 10);
484
485# now clear the hash
486%h = () ;
487
488# check it is empty
489$i = 0 ;
490while (($key,$value) = each(%h)) {
491 $i++;
492}
493ok(91, $i == 0);
494
495untie %h ;
496unlink $Dfile1 ;
497
a0d0e21e 498exit ;