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