Integrate with Sarathy. perl.h and util.c required manual resolving.
[p5sagit/p5-mst-13.2.git] / t / lib / db-hash.t
1 #!./perl -w
2
3 BEGIN {
4     unshift @INC, '../lib' if -d '../lib' ;
5     require Config; import Config;
6     if ($Config{'extensions'} !~ /\bDB_File\b/) {
7         print "1..0 # Skip: DB_File was not built\n";
8         exit 0;
9     }
10 }
11
12 use DB_File; 
13 use Fcntl;
14
15 print "1..109\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 {
27     package Redirect ;
28     use Symbol ;
29
30     sub new
31     {
32         my $class = shift ;
33         my $filename = shift ;
34         my $fh = gensym ;
35         open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
36         my $real_stdout = select($fh) ;
37         return bless [$fh, $real_stdout ] ;
38
39     }
40     sub DESTROY
41     {
42         my $self = shift ;
43         close $self->[0] ;
44         select($self->[1]) ;
45     }
46 }
47
48 sub docat_del
49
50     my $file = shift;
51     local $/ = undef;
52     open(CAT,$file) || die "Cannot open $file: $!";
53     my $result = <CAT>;
54     close(CAT);
55     unlink $file ;
56     return $result;
57 }   
58
59 my $Dfile = "dbhash.tmp";
60 unlink $Dfile;
61
62 umask(0);
63
64 # Check the interface to HASHINFO
65
66 my $dbh = new DB_File::HASHINFO ;
67
68 ok(1, ! defined $dbh->{bsize}) ;
69 ok(2, ! defined $dbh->{ffactor}) ;
70 ok(3, ! defined $dbh->{nelem}) ;
71 ok(4, ! defined $dbh->{cachesize}) ;
72 ok(5, ! defined $dbh->{hash}) ;
73 ok(6, ! defined $dbh->{lorder}) ;
74
75 $dbh->{bsize} = 3000 ;
76 ok(7, $dbh->{bsize} == 3000 );
77
78 $dbh->{ffactor} = 9000 ;
79 ok(8, $dbh->{ffactor} == 9000 );
80
81 $dbh->{nelem} = 400 ;
82 ok(9, $dbh->{nelem} == 400 );
83
84 $dbh->{cachesize} = 65 ;
85 ok(10, $dbh->{cachesize} == 65 );
86
87 $dbh->{hash} = "abc" ;
88 ok(11, $dbh->{hash} eq "abc" );
89
90 $dbh->{lorder} = 1234 ;
91 ok(12, $dbh->{lorder} == 1234 );
92
93 # Check that an invalid entry is caught both for store & fetch
94 eval '$dbh->{fred} = 1234' ;
95 ok(13, $@ =~ /^DB_File::HASHINFO::STORE - Unknown element 'fred' at/ );
96 eval 'my $q = $dbh->{fred}' ;
97 ok(14, $@ =~ /^DB_File::HASHINFO::FETCH - Unknown element 'fred' at/ );
98
99
100 # Now check the interface to HASH
101
102 ok(15, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
103
104 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
105    $blksize,$blocks) = stat($Dfile);
106 ok(16, ($mode & 0777) == ($^O eq 'os2' ? 0666 : 0640) || $^O eq 'amigaos' || $^O eq 'MSWin32');
107
108 while (($key,$value) = each(%h)) {
109     $i++;
110 }
111 ok(17, !$i );
112
113 $h{'goner1'} = 'snork';
114
115 $h{'abc'} = 'ABC';
116 ok(18, $h{'abc'} eq 'ABC' );
117 ok(19, !defined $h{'jimmy'} );
118 ok(20, !exists $h{'jimmy'} );
119 ok(21, exists $h{'abc'} );
120
121 $h{'def'} = 'DEF';
122 $h{'jkl','mno'} = "JKL\034MNO";
123 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
124 $h{'a'} = 'A';
125
126 #$h{'b'} = 'B';
127 $X->STORE('b', 'B') ;
128
129 $h{'c'} = 'C';
130
131 #$h{'d'} = 'D';
132 $X->put('d', 'D') ;
133
134 $h{'e'} = 'E';
135 $h{'f'} = 'F';
136 $h{'g'} = 'X';
137 $h{'h'} = 'H';
138 $h{'i'} = 'I';
139
140 $h{'goner2'} = 'snork';
141 delete $h{'goner2'};
142
143
144 # IMPORTANT - $X must be undefined before the untie otherwise the
145 #             underlying DB close routine will not get called.
146 undef $X ;
147 untie(%h);
148
149
150 # tie to the same file again, do not supply a type - should default to HASH
151 ok(22, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640) );
152
153 # Modify an entry from the previous tie
154 $h{'g'} = 'G';
155
156 $h{'j'} = 'J';
157 $h{'k'} = 'K';
158 $h{'l'} = 'L';
159 $h{'m'} = 'M';
160 $h{'n'} = 'N';
161 $h{'o'} = 'O';
162 $h{'p'} = 'P';
163 $h{'q'} = 'Q';
164 $h{'r'} = 'R';
165 $h{'s'} = 'S';
166 $h{'t'} = 'T';
167 $h{'u'} = 'U';
168 $h{'v'} = 'V';
169 $h{'w'} = 'W';
170 $h{'x'} = 'X';
171 $h{'y'} = 'Y';
172 $h{'z'} = 'Z';
173
174 $h{'goner3'} = 'snork';
175
176 delete $h{'goner1'};
177 $X->DELETE('goner3');
178
179 @keys = keys(%h);
180 @values = values(%h);
181
182 ok(23, $#keys == 29 && $#values == 29) ;
183
184 $i = 0 ;
185 while (($key,$value) = each(%h)) {
186     if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
187         $key =~ y/a-z/A-Z/;
188         $i++ if $key eq $value;
189     }
190 }
191
192 ok(24, $i == 30) ;
193
194 @keys = ('blurfl', keys(%h), 'dyick');
195 ok(25, $#keys == 31) ;
196
197 $h{'foo'} = '';
198 ok(26, $h{'foo'} eq '' );
199
200 #$h{''} = 'bar';
201 #ok(27, $h{''} eq 'bar' );
202 ok(27,1) ;
203
204 # check cache overflow and numeric keys and contents
205 $ok = 1;
206 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
207 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
208 ok(28, $ok );
209
210 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
211    $blksize,$blocks) = stat($Dfile);
212 ok(29, $size > 0 );
213
214 @h{0..200} = 200..400;
215 @foo = @h{0..200};
216 ok(30, join(':',200..400) eq join(':',@foo) );
217
218
219 # Now check all the non-tie specific stuff
220
221 # Check NOOVERWRITE will make put fail when attempting to overwrite
222 # an existing record.
223  
224 $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
225 ok(31, $status == 1 );
226  
227 # check that the value of the key 'x' has not been changed by the 
228 # previous test
229 ok(32, $h{'x'} eq 'X' );
230
231 # standard put
232 $status = $X->put('key', 'value') ;
233 ok(33, $status == 0 );
234
235 #check that previous put can be retrieved
236 $value = 0 ;
237 $status = $X->get('key', $value) ;
238 ok(34, $status == 0 );
239 ok(35, $value eq 'value' );
240
241 # Attempting to delete an existing key should work
242
243 $status = $X->del('q') ;
244 ok(36, $status == 0 );
245
246 # Make sure that the key deleted, cannot be retrieved
247 $^W = 0 ;
248 ok(37, $h{'q'} eq undef );
249 $^W = 1 ;
250
251 # Attempting to delete a non-existant key should fail
252
253 $status = $X->del('joe') ;
254 ok(38, $status == 1 );
255
256 # Check the get interface
257
258 # First a non-existing key
259 $status = $X->get('aaaa', $value) ;
260 ok(39, $status == 1 );
261
262 # Next an existing key
263 $status = $X->get('a', $value) ;
264 ok(40, $status == 0 );
265 ok(41, $value eq 'A' );
266
267 # seq
268 # ###
269
270 # ditto, but use put to replace the key/value pair.
271
272 # use seq to walk backwards through a file - check that this reversed is
273
274 # check seq FIRST/LAST
275
276 # sync
277 # ####
278
279 $status = $X->sync ;
280 ok(42, $status == 0 );
281
282
283 # fd
284 # ##
285
286 $status = $X->fd ;
287 ok(43, $status != 0 );
288
289 undef $X ;
290 untie %h ;
291
292 unlink $Dfile;
293
294 # clear
295 # #####
296
297 ok(44, tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
298 foreach (1 .. 10)
299   { $h{$_} = $_ * 100 }
300
301 # check that there are 10 elements in the hash
302 $i = 0 ;
303 while (($key,$value) = each(%h)) {
304     $i++;
305 }
306 ok(45, $i == 10);
307
308 # now clear the hash
309 %h = () ;
310
311 # check it is empty
312 $i = 0 ;
313 while (($key,$value) = each(%h)) {
314     $i++;
315 }
316 ok(46, $i == 0);
317
318 untie %h ;
319 unlink $Dfile ;
320
321
322 # Now try an in memory file
323 ok(47, $X = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
324
325 # fd with an in memory file should return fail
326 $status = $X->fd ;
327 ok(48, $status == -1 );
328
329 undef $X ;
330 untie %h ;
331
332 {
333     # check ability to override the default hashing
334     my %x ;
335     my $filename = "xyz" ;
336     my $hi = new DB_File::HASHINFO ;
337     $::count = 0 ;
338     $hi->{hash} = sub { ++$::count ; length $_[0] } ;
339     ok(49, tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $hi ) ;
340     $h{"abc"} = 123 ;
341     ok(50, $h{"abc"} == 123) ;
342     untie %x ;
343     unlink $filename ;
344     ok(51, $::count >0) ;
345 }
346
347 {
348     # check that attempting to tie an array to a DB_HASH will fail
349
350     my $filename = "xyz" ;
351     my @x ;
352     eval { tie @x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_HASH ; } ;
353     ok(52, $@ =~ /^DB_File can only tie an associative array to a DB_HASH database/) ;
354     unlink $filename ;
355 }
356
357 {
358    # sub-class test
359
360    package Another ;
361
362    use strict ;
363
364    open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
365    print FILE <<'EOM' ;
366
367    package SubDB ;
368
369    use strict ;
370    use vars qw( @ISA @EXPORT) ;
371
372    require Exporter ;
373    use DB_File;
374    @ISA=qw(DB_File);
375    @EXPORT = @DB_File::EXPORT ;
376
377    sub STORE { 
378         my $self = shift ;
379         my $key = shift ;
380         my $value = shift ;
381         $self->SUPER::STORE($key, $value * 2) ;
382    }
383
384    sub FETCH { 
385         my $self = shift ;
386         my $key = shift ;
387         $self->SUPER::FETCH($key) - 1 ;
388    }
389
390    sub put { 
391         my $self = shift ;
392         my $key = shift ;
393         my $value = shift ;
394         $self->SUPER::put($key, $value * 3) ;
395    }
396
397    sub get { 
398         my $self = shift ;
399         $self->SUPER::get($_[0], $_[1]) ;
400         $_[1] -= 2 ;
401    }
402
403    sub A_new_method
404    {
405         my $self = shift ;
406         my $key = shift ;
407         my $value = $self->FETCH($key) ;
408         return "[[$value]]" ;
409    }
410
411    1 ;
412 EOM
413
414     close FILE ;
415
416     BEGIN { push @INC, '.'; }             
417     eval 'use SubDB ; ';
418     main::ok(53, $@ eq "") ;
419     my %h ;
420     my $X ;
421     eval '
422         $X = tie(%h, "SubDB","dbhash.tmp", O_RDWR|O_CREAT, 0640, $DB_HASH );
423         ' ;
424
425     main::ok(54, $@ eq "") ;
426
427     my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
428     main::ok(55, $@ eq "") ;
429     main::ok(56, $ret == 5) ;
430
431     my $value = 0;
432     $ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ;
433     main::ok(57, $@ eq "") ;
434     main::ok(58, $ret == 10) ;
435
436     $ret = eval ' R_NEXT eq main::R_NEXT ' ;
437     main::ok(59, $@ eq "" ) ;
438     main::ok(60, $ret == 1) ;
439
440     $ret = eval '$X->A_new_method("joe") ' ;
441     main::ok(61, $@ eq "") ;
442     main::ok(62, $ret eq "[[11]]") ;
443
444     undef $X;
445     untie(%h);
446     unlink "SubDB.pm", "dbhash.tmp" ;
447
448 }
449
450 {
451    # DBM Filter tests
452    use strict ;
453    my (%h, $db) ;
454    my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
455    unlink $Dfile;
456
457    sub checkOutput
458    {
459        my($fk, $sk, $fv, $sv) = @_ ;
460        return
461            $fetch_key eq $fk && $store_key eq $sk && 
462            $fetch_value eq $fv && $store_value eq $sv &&
463            $_ eq 'original' ;
464    }
465    
466    ok(63, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
467
468    $db->filter_fetch_key   (sub { $fetch_key = $_ }) ;
469    $db->filter_store_key   (sub { $store_key = $_ }) ;
470    $db->filter_fetch_value (sub { $fetch_value = $_}) ;
471    $db->filter_store_value (sub { $store_value = $_ }) ;
472
473    $_ = "original" ;
474
475    $h{"fred"} = "joe" ;
476    #                   fk   sk     fv   sv
477    ok(64, checkOutput( "", "fred", "", "joe")) ;
478
479    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
480    ok(65, $h{"fred"} eq "joe");
481    #                   fk    sk     fv    sv
482    ok(66, checkOutput( "", "fred", "joe", "")) ;
483
484    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
485    ok(67, $db->FIRSTKEY() eq "fred") ;
486    #                    fk     sk  fv  sv
487    ok(68, checkOutput( "fred", "", "", "")) ;
488
489    # replace the filters, but remember the previous set
490    my ($old_fk) = $db->filter_fetch_key   
491                         (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
492    my ($old_sk) = $db->filter_store_key   
493                         (sub { $_ = lc $_ ; $store_key = $_ }) ;
494    my ($old_fv) = $db->filter_fetch_value 
495                         (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
496    my ($old_sv) = $db->filter_store_value 
497                         (sub { s/o/x/g; $store_value = $_ }) ;
498    
499    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
500    $h{"Fred"} = "Joe" ;
501    #                   fk   sk     fv    sv
502    ok(69, checkOutput( "", "fred", "", "Jxe")) ;
503
504    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
505    ok(70, $h{"Fred"} eq "[Jxe]");
506    #                   fk   sk     fv    sv
507    ok(71, checkOutput( "", "fred", "[Jxe]", "")) ;
508
509    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
510    ok(72, $db->FIRSTKEY() eq "FRED") ;
511    #                   fk   sk     fv    sv
512    ok(73, checkOutput( "FRED", "", "", "")) ;
513
514    # put the original filters back
515    $db->filter_fetch_key   ($old_fk);
516    $db->filter_store_key   ($old_sk);
517    $db->filter_fetch_value ($old_fv);
518    $db->filter_store_value ($old_sv);
519
520    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
521    $h{"fred"} = "joe" ;
522    ok(74, checkOutput( "", "fred", "", "joe")) ;
523
524    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
525    ok(75, $h{"fred"} eq "joe");
526    ok(76, checkOutput( "", "fred", "joe", "")) ;
527
528    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
529    ok(77, $db->FIRSTKEY() eq "fred") ;
530    ok(78, checkOutput( "fred", "", "", "")) ;
531
532    # delete the filters
533    $db->filter_fetch_key   (undef);
534    $db->filter_store_key   (undef);
535    $db->filter_fetch_value (undef);
536    $db->filter_store_value (undef);
537
538    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
539    $h{"fred"} = "joe" ;
540    ok(79, checkOutput( "", "", "", "")) ;
541
542    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
543    ok(80, $h{"fred"} eq "joe");
544    ok(81, checkOutput( "", "", "", "")) ;
545
546    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
547    ok(82, $db->FIRSTKEY() eq "fred") ;
548    ok(83, checkOutput( "", "", "", "")) ;
549
550    undef $db ;
551    untie %h;
552    unlink $Dfile;
553 }
554
555 {    
556     # DBM Filter with a closure
557
558     use strict ;
559     my (%h, $db) ;
560
561     unlink $Dfile;
562     ok(84, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
563
564     my %result = () ;
565
566     sub Closure
567     {
568         my ($name) = @_ ;
569         my $count = 0 ;
570         my @kept = () ;
571
572         return sub { ++$count ; 
573                      push @kept, $_ ; 
574                      $result{$name} = "$name - $count: [@kept]" ;
575                    }
576     }
577
578     $db->filter_store_key(Closure("store key")) ;
579     $db->filter_store_value(Closure("store value")) ;
580     $db->filter_fetch_key(Closure("fetch key")) ;
581     $db->filter_fetch_value(Closure("fetch value")) ;
582
583     $_ = "original" ;
584
585     $h{"fred"} = "joe" ;
586     ok(85, $result{"store key"} eq "store key - 1: [fred]");
587     ok(86, $result{"store value"} eq "store value - 1: [joe]");
588     ok(87, ! defined $result{"fetch key"} );
589     ok(88, ! defined $result{"fetch value"} );
590     ok(89, $_ eq "original") ;
591
592     ok(90, $db->FIRSTKEY() eq "fred") ;
593     ok(91, $result{"store key"} eq "store key - 1: [fred]");
594     ok(92, $result{"store value"} eq "store value - 1: [joe]");
595     ok(93, $result{"fetch key"} eq "fetch key - 1: [fred]");
596     ok(94, ! defined $result{"fetch value"} );
597     ok(95, $_ eq "original") ;
598
599     $h{"jim"}  = "john" ;
600     ok(96, $result{"store key"} eq "store key - 2: [fred jim]");
601     ok(97, $result{"store value"} eq "store value - 2: [joe john]");
602     ok(98, $result{"fetch key"} eq "fetch key - 1: [fred]");
603     ok(99, ! defined $result{"fetch value"} );
604     ok(100, $_ eq "original") ;
605
606     ok(101, $h{"fred"} eq "joe");
607     ok(102, $result{"store key"} eq "store key - 3: [fred jim fred]");
608     ok(103, $result{"store value"} eq "store value - 2: [joe john]");
609     ok(104, $result{"fetch key"} eq "fetch key - 1: [fred]");
610     ok(105, $result{"fetch value"} eq "fetch value - 1: [joe]");
611     ok(106, $_ eq "original") ;
612
613     undef $db ;
614     untie %h;
615     unlink $Dfile;
616 }               
617
618 {
619    # DBM Filter recursion detection
620    use strict ;
621    my (%h, $db) ;
622    unlink $Dfile;
623
624    ok(107, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
625
626    $db->filter_store_key (sub { $_ = $h{$_} }) ;
627
628    eval '$h{1} = 1234' ;
629    ok(108, $@ =~ /^recursion detected in filter_store_key at/ );
630    
631    undef $db ;
632    untie %h;
633    unlink $Dfile;
634 }
635
636
637 {
638    # Examples from the POD
639
640   my $file = "xyzt" ;
641   {
642     my $redirect = new Redirect $file ;
643
644     use strict ;
645     use DB_File ;
646     use vars qw( %h $k $v ) ;
647
648     unlink "fruit" ;
649     tie %h, "DB_File", "fruit", O_RDWR|O_CREAT, 0640, $DB_HASH 
650         or die "Cannot open file 'fruit': $!\n";
651
652     # Add a few key/value pairs to the file
653     $h{"apple"} = "red" ;
654     $h{"orange"} = "orange" ;
655     $h{"banana"} = "yellow" ;
656     $h{"tomato"} = "red" ;
657
658     # Check for existence of a key
659     print "Banana Exists\n\n" if $h{"banana"} ;
660
661     # Delete a key/value pair.
662     delete $h{"apple"} ;
663
664     # print the contents of the file
665     while (($k, $v) = each %h)
666       { print "$k -> $v\n" }
667
668     untie %h ;
669
670     unlink "fruit" ;
671   }  
672
673   ok(109, docat_del($file) eq <<'EOM') ;
674 Banana Exists
675
676 orange -> orange
677 tomato -> red
678 banana -> yellow
679 EOM
680    
681 }
682
683 exit ;