Once again syncing after too long an absence
[p5sagit/p5-mst-13.2.git] / t / lib / gdbm.t
1 #!./perl
2
3 # $RCSfile: dbm.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:43 $
4
5 BEGIN {
6     @INC = '../lib';
7     require Config; import Config;
8     if ($Config{'extensions'} !~ /\bGDBM_File\b/) {
9         print "1..0 # Skip: GDBM_File was not built\n";
10         exit 0;
11     }
12 }
13
14 use GDBM_File;
15
16 print "1..68\n";
17
18 unlink <Op.dbmx*>;
19
20 umask(0);
21 print (tie(%h,GDBM_File,'Op.dbmx', &GDBM_WRCREAT, 0640) ? "ok 1\n" : "not ok 1\n");
22
23 $Dfile = "Op.dbmx.pag";
24 if (! -e $Dfile) {
25         ($Dfile) = <Op.dbmx*>;
26 }
27 if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' || $^O eq 'dos') {
28     print "ok 2 # Skipped: different file permission semantics\n";
29 }
30 else {
31     ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
32      $blksize,$blocks) = stat($Dfile);
33     print (($mode & 0777) == 0640 ? "ok 2\n" : "not ok 2\n");
34 }
35 while (($key,$value) = each(%h)) {
36     $i++;
37 }
38 print (!$i ? "ok 3\n" : "not ok 3\n");
39
40 $h{'goner1'} = 'snork';
41
42 $h{'abc'} = 'ABC';
43 $h{'def'} = 'DEF';
44 $h{'jkl','mno'} = "JKL\034MNO";
45 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
46 $h{'a'} = 'A';
47 $h{'b'} = 'B';
48 $h{'c'} = 'C';
49 $h{'d'} = 'D';
50 $h{'e'} = 'E';
51 $h{'f'} = 'F';
52 $h{'g'} = 'G';
53 $h{'h'} = 'H';
54 $h{'i'} = 'I';
55
56 $h{'goner2'} = 'snork';
57 delete $h{'goner2'};
58
59 untie(%h);
60 print (tie(%h,GDBM_File,'Op.dbmx', &GDBM_WRCREAT, 0640) ? "ok 4\n" : "not ok 4\n");
61
62 $h{'j'} = 'J';
63 $h{'k'} = 'K';
64 $h{'l'} = 'L';
65 $h{'m'} = 'M';
66 $h{'n'} = 'N';
67 $h{'o'} = 'O';
68 $h{'p'} = 'P';
69 $h{'q'} = 'Q';
70 $h{'r'} = 'R';
71 $h{'s'} = 'S';
72 $h{'t'} = 'T';
73 $h{'u'} = 'U';
74 $h{'v'} = 'V';
75 $h{'w'} = 'W';
76 $h{'x'} = 'X';
77 $h{'y'} = 'Y';
78 $h{'z'} = 'Z';
79
80 $h{'goner3'} = 'snork';
81
82 delete $h{'goner1'};
83 delete $h{'goner3'};
84
85 @keys = keys(%h);
86 @values = values(%h);
87
88 if ($#keys == 29 && $#values == 29) {print "ok 5\n";} else {print "not ok 5\n";}
89
90 while (($key,$value) = each(%h)) {
91     if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
92         $key =~ y/a-z/A-Z/;
93         $i++ if $key eq $value;
94     }
95 }
96
97 if ($i == 30) {print "ok 6\n";} else {print "not ok 6\n";}
98
99 @keys = ('blurfl', keys(%h), 'dyick');
100 if ($#keys == 31) {print "ok 7\n";} else {print "not ok 7\n";}
101
102 $h{'foo'} = '';
103 $h{''} = 'bar';
104
105 # check cache overflow and numeric keys and contents
106 $ok = 1;
107 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
108 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
109 print ($ok ? "ok 8\n" : "not ok 8\n");
110
111 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
112    $blksize,$blocks) = stat($Dfile);
113 print ($size > 0 ? "ok 9\n" : "not ok 9\n");
114
115 @h{0..200} = 200..400;
116 @foo = @h{0..200};
117 print join(':',200..400) eq join(':',@foo) ? "ok 10\n" : "not ok 10\n";
118
119 print ($h{'foo'} eq '' ? "ok 11\n" : "not ok 11\n");
120 print ($h{''} eq 'bar' ? "ok 12\n" : "not ok 12\n");
121
122 untie %h;
123 unlink 'Op.dbmx.dir', $Dfile;
124
125 sub ok
126 {
127     my $no = shift ;
128     my $result = shift ;
129
130     print "not " unless $result ;
131     print "ok $no\n" ;
132 }
133
134 {
135    # sub-class test
136
137    package Another ;
138
139    use strict ;
140
141    open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
142    print FILE <<'EOM' ;
143
144    package SubDB ;
145
146    use strict ;
147    use vars qw(@ISA @EXPORT) ;
148
149    require Exporter ;
150    use GDBM_File;
151    @ISA=qw(GDBM_File);
152    @EXPORT = @GDBM_File::EXPORT ;
153
154    sub STORE { 
155         my $self = shift ;
156         my $key = shift ;
157         my $value = shift ;
158         $self->SUPER::STORE($key, $value * 2) ;
159    }
160
161    sub FETCH { 
162         my $self = shift ;
163         my $key = shift ;
164         $self->SUPER::FETCH($key) - 1 ;
165    }
166
167    sub A_new_method
168    {
169         my $self = shift ;
170         my $key = shift ;
171         my $value = $self->FETCH($key) ;
172         return "[[$value]]" ;
173    }
174
175    1 ;
176 EOM
177
178     close FILE ;
179
180     BEGIN { push @INC, '.'; }
181     unlink <dbhash.tmp*> ;
182
183     eval 'use SubDB ; ';
184     main::ok(13, $@ eq "") ;
185     my %h ;
186     my $X ;
187     eval '
188         $X = tie(%h, "SubDB","dbhash.tmp", &GDBM_WRCREAT, 0640 );
189         ' ;
190
191     main::ok(14, $@ eq "") ;
192
193     my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
194     main::ok(15, $@ eq "") ;
195     main::ok(16, $ret == 5) ;
196
197     $ret = eval ' &GDBM_WRCREAT eq &main::GDBM_WRCREAT ' ;
198     main::ok(17, $@ eq "" ) ;
199     main::ok(18, $ret == 1) ;
200
201     $ret = eval '$X->A_new_method("fred") ' ;
202     main::ok(19, $@ eq "") ;
203     main::ok(20, $ret eq "[[5]]") ;
204
205     undef $X;
206     untie(%h);
207     unlink "SubDB.pm", <dbhash.tmp*> ;
208
209 }
210
211 {
212    # DBM Filter tests
213    use strict ;
214    my (%h, $db) ;
215    my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
216
217    sub checkOutput
218    {
219        my($fk, $sk, $fv, $sv) = @_ ;
220        return
221            $fetch_key eq $fk && $store_key eq $sk && 
222            $fetch_value eq $fv && $store_value eq $sv &&
223            $_ eq 'original' ;
224    }
225    
226    unlink <Op.dbmx*>;
227    ok(21, $db = tie(%h, 'GDBM_File','Op.dbmx', &GDBM_WRCREAT, 0640)) ;
228
229    $db->filter_fetch_key   (sub { $fetch_key = $_ }) ;
230    $db->filter_store_key   (sub { $store_key = $_ }) ;
231    $db->filter_fetch_value (sub { $fetch_value = $_}) ;
232    $db->filter_store_value (sub { $store_value = $_ }) ;
233
234    $_ = "original" ;
235
236    $h{"fred"} = "joe" ;
237    #                   fk   sk     fv   sv
238    ok(22, checkOutput( "", "fred", "", "joe")) ;
239
240    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
241    ok(23, $h{"fred"} eq "joe");
242    #                   fk    sk     fv    sv
243    ok(24, checkOutput( "", "fred", "joe", "")) ;
244
245    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
246    ok(25, $db->FIRSTKEY() eq "fred") ;
247    #                    fk     sk  fv  sv
248    ok(26, checkOutput( "fred", "", "", "")) ;
249
250    # replace the filters, but remember the previous set
251    my ($old_fk) = $db->filter_fetch_key   
252                         (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
253    my ($old_sk) = $db->filter_store_key   
254                         (sub { $_ = lc $_ ; $store_key = $_ }) ;
255    my ($old_fv) = $db->filter_fetch_value 
256                         (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
257    my ($old_sv) = $db->filter_store_value 
258                         (sub { s/o/x/g; $store_value = $_ }) ;
259    
260    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
261    $h{"Fred"} = "Joe" ;
262    #                   fk   sk     fv    sv
263    ok(27, checkOutput( "", "fred", "", "Jxe")) ;
264
265    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
266    ok(28, $h{"Fred"} eq "[Jxe]");
267    #                   fk   sk     fv    sv
268    ok(29, checkOutput( "", "fred", "[Jxe]", "")) ;
269
270    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
271    ok(30, $db->FIRSTKEY() eq "FRED") ;
272    #                   fk   sk     fv    sv
273    ok(31, checkOutput( "FRED", "", "", "")) ;
274
275    # put the original filters back
276    $db->filter_fetch_key   ($old_fk);
277    $db->filter_store_key   ($old_sk);
278    $db->filter_fetch_value ($old_fv);
279    $db->filter_store_value ($old_sv);
280
281    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
282    $h{"fred"} = "joe" ;
283    ok(32, checkOutput( "", "fred", "", "joe")) ;
284
285    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
286    ok(33, $h{"fred"} eq "joe");
287    ok(34, checkOutput( "", "fred", "joe", "")) ;
288
289    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
290    ok(35, $db->FIRSTKEY() eq "fred") ;
291    ok(36, checkOutput( "fred", "", "", "")) ;
292
293    # delete the filters
294    $db->filter_fetch_key   (undef);
295    $db->filter_store_key   (undef);
296    $db->filter_fetch_value (undef);
297    $db->filter_store_value (undef);
298
299    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
300    $h{"fred"} = "joe" ;
301    ok(37, checkOutput( "", "", "", "")) ;
302
303    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
304    ok(38, $h{"fred"} eq "joe");
305    ok(39, checkOutput( "", "", "", "")) ;
306
307    ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
308    ok(40, $db->FIRSTKEY() eq "fred") ;
309    ok(41, checkOutput( "", "", "", "")) ;
310
311    undef $db ;
312    untie %h;
313    unlink <Op.dbmx*>;
314 }
315
316 {    
317     # DBM Filter with a closure
318
319     use strict ;
320     my (%h, $db) ;
321
322     unlink <Op.dbmx*>;
323     ok(42, $db = tie(%h, 'GDBM_File','Op.dbmx', &GDBM_WRCREAT, 0640)) ;
324
325     my %result = () ;
326
327     sub Closure
328     {
329         my ($name) = @_ ;
330         my $count = 0 ;
331         my @kept = () ;
332
333         return sub { ++$count ; 
334                      push @kept, $_ ; 
335                      $result{$name} = "$name - $count: [@kept]" ;
336                    }
337     }
338
339     $db->filter_store_key(Closure("store key")) ;
340     $db->filter_store_value(Closure("store value")) ;
341     $db->filter_fetch_key(Closure("fetch key")) ;
342     $db->filter_fetch_value(Closure("fetch value")) ;
343
344     $_ = "original" ;
345
346     $h{"fred"} = "joe" ;
347     ok(43, $result{"store key"} eq "store key - 1: [fred]");
348     ok(44, $result{"store value"} eq "store value - 1: [joe]");
349     ok(45, !defined $result{"fetch key"} );
350     ok(46, !defined $result{"fetch value"} );
351     ok(47, $_ eq "original") ;
352
353     ok(48, $db->FIRSTKEY() eq "fred") ;
354     ok(49, $result{"store key"} eq "store key - 1: [fred]");
355     ok(50, $result{"store value"} eq "store value - 1: [joe]");
356     ok(51, $result{"fetch key"} eq "fetch key - 1: [fred]");
357     ok(52, ! defined $result{"fetch value"} );
358     ok(53, $_ eq "original") ;
359
360     $h{"jim"}  = "john" ;
361     ok(54, $result{"store key"} eq "store key - 2: [fred jim]");
362     ok(55, $result{"store value"} eq "store value - 2: [joe john]");
363     ok(56, $result{"fetch key"} eq "fetch key - 1: [fred]");
364     ok(57, $result{"fetch value"} eq "");
365     ok(58, $_ eq "original") ;
366
367     ok(59, $h{"fred"} eq "joe");
368     ok(60, $result{"store key"} eq "store key - 3: [fred jim fred]");
369     ok(61, $result{"store value"} eq "store value - 2: [joe john]");
370     ok(62, $result{"fetch key"} eq "fetch key - 1: [fred]");
371     ok(63, $result{"fetch value"} eq "fetch value - 1: [joe]");
372     ok(64, $_ eq "original") ;
373
374     undef $db ;
375     untie %h;
376     unlink <Op.dbmx*>;
377 }
378
379 {
380    # DBM Filter recursion detection
381    use strict ;
382    my (%h, $db) ;
383    unlink <Op.dbmx*>;
384
385    ok(65, $db = tie(%h, 'GDBM_File','Op.dbmx', &GDBM_WRCREAT, 0640)) ;
386
387    $db->filter_store_key (sub { $_ = $h{$_} }) ;
388
389    eval '$h{1} = 1234' ;
390    ok(66, $@ =~ /^recursion detected in filter_store_key at/ );
391    
392    undef $db ;
393    untie %h;
394    unlink <Op.dbmx*>;
395 }
396
397 {
398     # Bug ID 20001013.009
399     #
400     # test that $hash{KEY} = undef doesn't produce the warning
401     #     Use of uninitialized value in null operation 
402     use warnings ;
403     use strict ;
404     use GDBM_File ;
405
406     unlink <Op.dbmx*>;
407     my %h ;
408     my $a = "";
409     local $SIG{__WARN__} = sub {$a = $_[0]} ;
410     
411     ok(67, tie(%h, 'GDBM_File','Op.dbmx', &GDBM_WRCREAT, 0640));
412     $h{ABC} = undef;
413     ok(68, $a eq "") ;
414     untie %h;
415     unlink <Op.dbmx*>;
416 }