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