Commit | Line | Data |
bee1dbe2 |
1 | #!./perl |
2 | |
a0d0e21e |
3 | # $RCSfile: dbm.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:43 $ |
4 | |
5 | BEGIN { |
a0d0e21e |
6 | require Config; import Config; |
ae5c2706 |
7 | if ($Config{'extensions'} !~ /\bODBM_File\b/ or $Config{'d_cplusplus'}) { |
45c0de28 |
8 | print "1..0 # Skip: ODBM_File was not built\n"; |
a0d0e21e |
9 | exit 0; |
10 | } |
11 | } |
12 | |
698828ad |
13 | use strict; |
14 | use warnings; |
15 | |
16 | sub ok |
17 | { |
18 | my $no = shift ; |
19 | my $result = shift ; |
20 | |
21 | print "not " unless $result ; |
22 | print "ok $no\n" ; |
23 | } |
24 | |
a0d0e21e |
25 | require ODBM_File; |
26 | #If Fcntl is not available, try 0x202 or 0x102 for O_RDWR|O_CREAT |
27 | use Fcntl; |
28 | |
6a31061a |
29 | print "1..78\n"; |
bee1dbe2 |
30 | |
a0d0e21e |
31 | unlink <Op.dbmx*>; |
32 | |
bee1dbe2 |
33 | umask(0); |
698828ad |
34 | my %h; |
35 | ok(1, tie(%h,'ODBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)); |
a0d0e21e |
36 | |
698828ad |
37 | my $Dfile = "Op.dbmx.pag"; |
a0d0e21e |
38 | if (! -e $Dfile) { |
39 | ($Dfile) = <Op.dbmx*>; |
40 | } |
2986a63f |
41 | if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' || $^O eq 'NetWare') { |
fac76ed7 |
42 | print "ok 2 # Skipped: different file permission semantics\n"; |
544a3566 |
43 | } |
44 | else { |
698828ad |
45 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, |
544a3566 |
46 | $blksize,$blocks) = stat($Dfile); |
47 | print (($mode & 0777) == 0640 ? "ok 2\n" : "not ok 2\n"); |
48 | } |
698828ad |
49 | my $i = 0; |
50 | while (my ($key,$value) = each(%h)) { |
bee1dbe2 |
51 | $i++; |
52 | } |
53 | print (!$i ? "ok 3\n" : "not ok 3\n"); |
54 | |
55 | $h{'goner1'} = 'snork'; |
56 | |
57 | $h{'abc'} = 'ABC'; |
58 | $h{'def'} = 'DEF'; |
59 | $h{'jkl','mno'} = "JKL\034MNO"; |
60 | $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5); |
61 | $h{'a'} = 'A'; |
62 | $h{'b'} = 'B'; |
63 | $h{'c'} = 'C'; |
64 | $h{'d'} = 'D'; |
65 | $h{'e'} = 'E'; |
66 | $h{'f'} = 'F'; |
67 | $h{'g'} = 'G'; |
68 | $h{'h'} = 'H'; |
69 | $h{'i'} = 'I'; |
70 | |
71 | $h{'goner2'} = 'snork'; |
72 | delete $h{'goner2'}; |
73 | |
a0d0e21e |
74 | untie(%h); |
698828ad |
75 | print (tie(%h,'ODBM_File','Op.dbmx', O_RDWR, 0640) ? "ok 4\n" : "not ok 4\n"); |
bee1dbe2 |
76 | |
77 | $h{'j'} = 'J'; |
78 | $h{'k'} = 'K'; |
79 | $h{'l'} = 'L'; |
80 | $h{'m'} = 'M'; |
81 | $h{'n'} = 'N'; |
82 | $h{'o'} = 'O'; |
83 | $h{'p'} = 'P'; |
84 | $h{'q'} = 'Q'; |
85 | $h{'r'} = 'R'; |
86 | $h{'s'} = 'S'; |
87 | $h{'t'} = 'T'; |
88 | $h{'u'} = 'U'; |
89 | $h{'v'} = 'V'; |
90 | $h{'w'} = 'W'; |
91 | $h{'x'} = 'X'; |
92 | $h{'y'} = 'Y'; |
93 | $h{'z'} = 'Z'; |
94 | |
95 | $h{'goner3'} = 'snork'; |
96 | |
97 | delete $h{'goner1'}; |
98 | delete $h{'goner3'}; |
99 | |
698828ad |
100 | my @keys = keys(%h); |
101 | my @values = values(%h); |
bee1dbe2 |
102 | |
103 | if ($#keys == 29 && $#values == 29) {print "ok 5\n";} else {print "not ok 5\n";} |
104 | |
698828ad |
105 | while (my ($key,$value) = each(%h)) { |
2f52a358 |
106 | if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) { |
bee1dbe2 |
107 | $key =~ y/a-z/A-Z/; |
108 | $i++ if $key eq $value; |
109 | } |
110 | } |
111 | |
112 | if ($i == 30) {print "ok 6\n";} else {print "not ok 6\n";} |
113 | |
c6aa4a32 |
114 | @keys = ('blurfl', keys(%h), 'dyick'); |
bee1dbe2 |
115 | if ($#keys == 31) {print "ok 7\n";} else {print "not ok 7\n";} |
116 | |
117 | $h{'foo'} = ''; |
118 | $h{''} = 'bar'; |
119 | |
120 | # check cache overflow and numeric keys and contents |
698828ad |
121 | my $ok = 1; |
bee1dbe2 |
122 | for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; } |
123 | for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; } |
124 | print ($ok ? "ok 8\n" : "not ok 8\n"); |
125 | |
698828ad |
126 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, |
a0d0e21e |
127 | $blksize,$blocks) = stat($Dfile); |
bee1dbe2 |
128 | print ($size > 0 ? "ok 9\n" : "not ok 9\n"); |
129 | |
130 | @h{0..200} = 200..400; |
698828ad |
131 | my @foo = @h{0..200}; |
bee1dbe2 |
132 | print join(':',200..400) eq join(':',@foo) ? "ok 10\n" : "not ok 10\n"; |
133 | |
134 | print ($h{'foo'} eq '' ? "ok 11\n" : "not ok 11\n"); |
135 | print ($h{''} eq 'bar' ? "ok 12\n" : "not ok 12\n"); |
136 | |
bbad3607 |
137 | untie %h; |
a0d0e21e |
138 | unlink 'Op.dbmx.dir', $Dfile; |
4e2a63a7 |
139 | |
4e2a63a7 |
140 | { |
141 | # sub-class test |
142 | |
143 | package Another ; |
144 | |
145 | use strict ; |
698828ad |
146 | use warnings ; |
4e2a63a7 |
147 | |
148 | open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ; |
149 | print FILE <<'EOM' ; |
150 | |
151 | package SubDB ; |
152 | |
153 | use strict ; |
698828ad |
154 | use warnings ; |
4e2a63a7 |
155 | use vars qw(@ISA @EXPORT) ; |
156 | |
157 | require Exporter ; |
158 | use ODBM_File; |
159 | @ISA=qw(ODBM_File); |
160 | @EXPORT = @ODBM_File::EXPORT ; |
161 | |
162 | sub STORE { |
163 | my $self = shift ; |
164 | my $key = shift ; |
165 | my $value = shift ; |
166 | $self->SUPER::STORE($key, $value * 2) ; |
167 | } |
168 | |
169 | sub FETCH { |
170 | my $self = shift ; |
171 | my $key = shift ; |
172 | $self->SUPER::FETCH($key) - 1 ; |
173 | } |
174 | |
175 | sub A_new_method |
176 | { |
177 | my $self = shift ; |
178 | my $key = shift ; |
179 | my $value = $self->FETCH($key) ; |
180 | return "[[$value]]" ; |
181 | } |
182 | |
183 | 1 ; |
184 | EOM |
185 | |
186 | close FILE ; |
187 | |
188 | BEGIN { push @INC, '.'; } |
189 | |
190 | eval 'use SubDB ; use Fcntl ;'; |
191 | main::ok(13, $@ eq "") ; |
192 | my %h ; |
193 | my $X ; |
194 | eval ' |
195 | $X = tie(%h, "SubDB","dbhash.tmp", O_RDWR|O_CREAT, 0640 ); |
196 | ' ; |
197 | |
198 | main::ok(14, $@ eq "") ; |
199 | |
200 | my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ; |
201 | main::ok(15, $@ eq "") ; |
202 | main::ok(16, $ret == 5) ; |
203 | |
204 | $ret = eval '$X->A_new_method("fred") ' ; |
205 | main::ok(17, $@ eq "") ; |
206 | main::ok(18, $ret eq "[[5]]") ; |
207 | |
fac76ed7 |
208 | undef $X; |
209 | untie(%h); |
84902520 |
210 | unlink "SubDB.pm", <dbhash.tmp*> ; |
4e2a63a7 |
211 | |
212 | } |
9fe6733a |
213 | |
214 | { |
215 | # DBM Filter tests |
216 | use strict ; |
698828ad |
217 | use warnings ; |
9fe6733a |
218 | my (%h, $db) ; |
219 | my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
220 | |
221 | sub checkOutput |
222 | { |
223 | my($fk, $sk, $fv, $sv) = @_ ; |
1b882d32 |
224 | print "# ", join('|', $fetch_key, $fk, $store_key, $sk, |
225 | $fetch_value, $fv, $store_value, $sv, $_), "\n"; |
9fe6733a |
226 | return |
227 | $fetch_key eq $fk && $store_key eq $sk && |
228 | $fetch_value eq $fv && $store_value eq $sv && |
229 | $_ eq 'original' ; |
230 | } |
231 | |
232 | unlink <Op.dbmx*>; |
233 | ok(19, $db = tie(%h, 'ODBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ; |
234 | |
235 | $db->filter_fetch_key (sub { $fetch_key = $_ }) ; |
236 | $db->filter_store_key (sub { $store_key = $_ }) ; |
237 | $db->filter_fetch_value (sub { $fetch_value = $_}) ; |
238 | $db->filter_store_value (sub { $store_value = $_ }) ; |
239 | |
240 | $_ = "original" ; |
241 | |
242 | $h{"fred"} = "joe" ; |
243 | # fk sk fv sv |
244 | ok(20, checkOutput( "", "fred", "", "joe")) ; |
245 | |
246 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
247 | ok(21, $h{"fred"} eq "joe"); |
248 | # fk sk fv sv |
249 | ok(22, checkOutput( "", "fred", "joe", "")) ; |
250 | |
251 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
252 | ok(23, $db->FIRSTKEY() eq "fred") ; |
253 | # fk sk fv sv |
254 | ok(24, checkOutput( "fred", "", "", "")) ; |
255 | |
256 | # replace the filters, but remember the previous set |
257 | my ($old_fk) = $db->filter_fetch_key |
258 | (sub { $_ = uc $_ ; $fetch_key = $_ }) ; |
259 | my ($old_sk) = $db->filter_store_key |
260 | (sub { $_ = lc $_ ; $store_key = $_ }) ; |
261 | my ($old_fv) = $db->filter_fetch_value |
262 | (sub { $_ = "[$_]"; $fetch_value = $_ }) ; |
263 | my ($old_sv) = $db->filter_store_value |
264 | (sub { s/o/x/g; $store_value = $_ }) ; |
265 | |
266 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
267 | $h{"Fred"} = "Joe" ; |
268 | # fk sk fv sv |
269 | ok(25, checkOutput( "", "fred", "", "Jxe")) ; |
270 | |
271 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
272 | ok(26, $h{"Fred"} eq "[Jxe]"); |
273 | # fk sk fv sv |
274 | ok(27, checkOutput( "", "fred", "[Jxe]", "")) ; |
275 | |
276 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
277 | ok(28, $db->FIRSTKEY() eq "FRED") ; |
278 | # fk sk fv sv |
279 | ok(29, checkOutput( "FRED", "", "", "")) ; |
280 | |
281 | # put the original filters back |
282 | $db->filter_fetch_key ($old_fk); |
283 | $db->filter_store_key ($old_sk); |
284 | $db->filter_fetch_value ($old_fv); |
285 | $db->filter_store_value ($old_sv); |
286 | |
287 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
288 | $h{"fred"} = "joe" ; |
289 | ok(30, checkOutput( "", "fred", "", "joe")) ; |
290 | |
291 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
292 | ok(31, $h{"fred"} eq "joe"); |
293 | ok(32, checkOutput( "", "fred", "joe", "")) ; |
294 | |
295 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
296 | ok(33, $db->FIRSTKEY() eq "fred") ; |
297 | ok(34, checkOutput( "fred", "", "", "")) ; |
298 | |
299 | # delete the filters |
300 | $db->filter_fetch_key (undef); |
301 | $db->filter_store_key (undef); |
302 | $db->filter_fetch_value (undef); |
303 | $db->filter_store_value (undef); |
304 | |
305 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
306 | $h{"fred"} = "joe" ; |
307 | ok(35, checkOutput( "", "", "", "")) ; |
308 | |
309 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
310 | ok(36, $h{"fred"} eq "joe"); |
311 | ok(37, checkOutput( "", "", "", "")) ; |
312 | |
313 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
314 | ok(38, $db->FIRSTKEY() eq "fred") ; |
315 | ok(39, checkOutput( "", "", "", "")) ; |
316 | |
317 | undef $db ; |
318 | untie %h; |
319 | unlink <Op.dbmx*>; |
320 | } |
321 | |
322 | { |
323 | # DBM Filter with a closure |
324 | |
325 | use strict ; |
698828ad |
326 | use warnings ; |
9fe6733a |
327 | my (%h, $db) ; |
328 | |
329 | unlink <Op.dbmx*>; |
330 | ok(40, $db = tie(%h, 'ODBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ; |
331 | |
332 | my %result = () ; |
333 | |
334 | sub Closure |
335 | { |
336 | my ($name) = @_ ; |
337 | my $count = 0 ; |
338 | my @kept = () ; |
339 | |
340 | return sub { ++$count ; |
341 | push @kept, $_ ; |
342 | $result{$name} = "$name - $count: [@kept]" ; |
343 | } |
344 | } |
345 | |
346 | $db->filter_store_key(Closure("store key")) ; |
347 | $db->filter_store_value(Closure("store value")) ; |
348 | $db->filter_fetch_key(Closure("fetch key")) ; |
349 | $db->filter_fetch_value(Closure("fetch value")) ; |
350 | |
351 | $_ = "original" ; |
352 | |
353 | $h{"fred"} = "joe" ; |
354 | ok(41, $result{"store key"} eq "store key - 1: [fred]"); |
355 | ok(42, $result{"store value"} eq "store value - 1: [joe]"); |
356 | ok(43, !defined $result{"fetch key"} ); |
357 | ok(44, !defined $result{"fetch value"} ); |
358 | ok(45, $_ eq "original") ; |
359 | |
360 | ok(46, $db->FIRSTKEY() eq "fred") ; |
361 | ok(47, $result{"store key"} eq "store key - 1: [fred]"); |
362 | ok(48, $result{"store value"} eq "store value - 1: [joe]"); |
363 | ok(49, $result{"fetch key"} eq "fetch key - 1: [fred]"); |
364 | ok(50, ! defined $result{"fetch value"} ); |
365 | ok(51, $_ eq "original") ; |
366 | |
367 | $h{"jim"} = "john" ; |
368 | ok(52, $result{"store key"} eq "store key - 2: [fred jim]"); |
369 | ok(53, $result{"store value"} eq "store value - 2: [joe john]"); |
370 | ok(54, $result{"fetch key"} eq "fetch key - 1: [fred]"); |
698828ad |
371 | ok(55, ! defined $result{"fetch value"} ); |
9fe6733a |
372 | ok(56, $_ eq "original") ; |
373 | |
374 | ok(57, $h{"fred"} eq "joe"); |
375 | ok(58, $result{"store key"} eq "store key - 3: [fred jim fred]"); |
376 | ok(59, $result{"store value"} eq "store value - 2: [joe john]"); |
377 | ok(60, $result{"fetch key"} eq "fetch key - 1: [fred]"); |
378 | ok(61, $result{"fetch value"} eq "fetch value - 1: [joe]"); |
379 | ok(62, $_ eq "original") ; |
380 | |
381 | undef $db ; |
382 | untie %h; |
383 | unlink <Op.dbmx*>; |
384 | } |
385 | |
386 | { |
387 | # DBM Filter recursion detection |
388 | use strict ; |
698828ad |
389 | use warnings ; |
9fe6733a |
390 | my (%h, $db) ; |
391 | unlink <Op.dbmx*>; |
392 | |
393 | ok(63, $db = tie(%h, 'ODBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ; |
394 | |
395 | $db->filter_store_key (sub { $_ = $h{$_} }) ; |
396 | |
397 | eval '$h{1} = 1234' ; |
398 | ok(64, $@ =~ /^recursion detected in filter_store_key at/ ); |
399 | |
400 | undef $db ; |
401 | untie %h; |
402 | unlink <Op.dbmx*>; |
403 | } |
9394203c |
404 | |
cbc5248d |
405 | { |
406 | # Bug ID 20001013.009 |
407 | # |
408 | # test that $hash{KEY} = undef doesn't produce the warning |
409 | # Use of uninitialized value in null operation |
410 | use warnings ; |
411 | use strict ; |
412 | use ODBM_File ; |
413 | |
414 | unlink <Op.dbmx*>; |
415 | my %h ; |
416 | my $a = ""; |
417 | local $SIG{__WARN__} = sub {$a = $_[0]} ; |
418 | |
419 | ok(65, tie(%h, 'ODBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ; |
420 | $h{ABC} = undef; |
421 | ok(66, $a eq "") ; |
422 | untie %h; |
423 | unlink <Op.dbmx*>; |
424 | } |
425 | |
0bf2e707 |
426 | { |
427 | # When iterating over a tied hash using "each", the key passed to FETCH |
428 | # will be recycled and passed to NEXTKEY. If a Source Filter modifies the |
429 | # key in FETCH via a filter_fetch_key method we need to check that the |
430 | # modified key doesn't get passed to NEXTKEY. |
431 | # Also Test "keys" & "values" while we are at it. |
432 | |
433 | use warnings ; |
434 | use strict ; |
435 | use ODBM_File ; |
436 | |
437 | unlink <Op.dbmx*>; |
438 | my $bad_key = 0 ; |
439 | my %h = () ; |
440 | ok(67, my $db = tie(%h, 'ODBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ; |
441 | $db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ; |
442 | $db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ; |
443 | |
444 | $h{'Alpha_ABC'} = 2 ; |
445 | $h{'Alpha_DEF'} = 5 ; |
446 | |
447 | ok(68, $h{'Alpha_ABC'} == 2); |
448 | ok(69, $h{'Alpha_DEF'} == 5); |
449 | |
450 | my ($k, $v) = ("",""); |
451 | while (($k, $v) = each %h) {} |
452 | ok(70, $bad_key == 0); |
453 | |
454 | $bad_key = 0 ; |
455 | foreach $k (keys %h) {} |
456 | ok(71, $bad_key == 0); |
457 | |
458 | $bad_key = 0 ; |
459 | foreach $v (values %h) {} |
460 | ok(72, $bad_key == 0); |
461 | |
462 | undef $db ; |
463 | untie %h ; |
464 | unlink <Op.dbmx*>; |
465 | } |
466 | |
6a31061a |
467 | |
468 | { |
469 | # Check that DBM Filter can cope with read-only $_ |
470 | |
471 | use warnings ; |
472 | use strict ; |
473 | my %h ; |
474 | unlink <Op.dbmx*>; |
475 | |
476 | ok(73, my $db = tie(%h, 'ODBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ; |
477 | |
478 | $db->filter_fetch_key (sub { }) ; |
479 | $db->filter_store_key (sub { }) ; |
480 | $db->filter_fetch_value (sub { }) ; |
481 | $db->filter_store_value (sub { }) ; |
482 | |
483 | $_ = "original" ; |
484 | |
485 | $h{"fred"} = "joe" ; |
486 | ok(74, $h{"fred"} eq "joe"); |
487 | |
488 | eval { grep { $h{$_} } (1, 2, 3) }; |
489 | ok (75, ! $@); |
490 | |
491 | |
492 | # delete the filters |
493 | $db->filter_fetch_key (undef); |
494 | $db->filter_store_key (undef); |
495 | $db->filter_fetch_value (undef); |
496 | $db->filter_store_value (undef); |
497 | |
498 | $h{"fred"} = "joe" ; |
499 | |
500 | ok(76, $h{"fred"} eq "joe"); |
501 | |
502 | ok(77, $db->FIRSTKEY() eq "fred") ; |
503 | |
504 | eval { grep { $h{$_} } (1, 2, 3) }; |
505 | ok (78, ! $@); |
506 | |
507 | undef $db ; |
508 | untie %h; |
509 | unlink <Op.dbmx*>; |
510 | } |
0bf2e707 |
511 | exit ; |
9394203c |
512 | if ($^O eq 'hpux') { |
513 | print <<EOM; |
514 | # |
515 | # If you experience failures with the odbm test in HP-UX, |
516 | # this is a well-known bug that's unfortunately very hard to fix. |
517 | # The suggested course of action is to avoid using the ODBM_File, |
518 | # but to use instead the NDBM_File extension. |
519 | # |
520 | EOM |
521 | } |