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