Commit | Line | Data |
f6b705ef |
1 | #!./perl -w |
a0d0e21e |
2 | |
3 | BEGIN { |
20822f61 |
4 | @INC = '../lib'; |
a0d0e21e |
5 | require Config; import Config; |
6 | if ($Config{'extensions'} !~ /\bDB_File\b/) { |
45c0de28 |
7 | print "1..0 # Skip: DB_File was not built\n"; |
a0d0e21e |
8 | exit 0; |
9 | } |
10 | } |
11 | |
12 | use DB_File; |
13 | use Fcntl; |
55d68b4a |
14 | use strict ; |
3245f058 |
15 | use warnings; |
045291aa |
16 | use vars qw($dbh $Dfile $bad_ones $FA) ; |
17 | |
18 | # full tied array support started in Perl 5.004_57 |
a9fd575d |
19 | # Double check to see if it is available. |
20 | |
21 | { |
22 | sub try::TIEARRAY { bless [], "try" } |
23 | sub try::FETCHSIZE { $FA = 1 } |
24 | $FA = 0 ; |
25 | my @a ; |
26 | tie @a, 'try' ; |
27 | my $a = @a ; |
28 | } |
29 | |
a0d0e21e |
30 | |
55d68b4a |
31 | sub ok |
32 | { |
33 | my $no = shift ; |
34 | my $result = shift ; |
a0d0e21e |
35 | |
55d68b4a |
36 | print "not " unless $result ; |
37 | print "ok $no\n" ; |
6250ba0a |
38 | |
39 | return $result ; |
40 | } |
41 | |
9b761c68 |
42 | { |
43 | package Redirect ; |
44 | use Symbol ; |
45 | |
46 | sub new |
47 | { |
48 | my $class = shift ; |
49 | my $filename = shift ; |
50 | my $fh = gensym ; |
51 | open ($fh, ">$filename") || die "Cannot open $filename: $!" ; |
52 | my $real_stdout = select($fh) ; |
53 | return bless [$fh, $real_stdout ] ; |
54 | |
55 | } |
56 | sub DESTROY |
57 | { |
58 | my $self = shift ; |
59 | close $self->[0] ; |
60 | select($self->[1]) ; |
61 | } |
62 | } |
63 | |
64 | sub docat |
65 | { |
66 | my $file = shift; |
67 | local $/ = undef; |
68 | open(CAT,$file) || die "Cannot open $file:$!"; |
69 | my $result = <CAT>; |
70 | close(CAT); |
71 | return $result; |
72 | } |
73 | |
74 | sub docat_del |
75 | { |
76 | my $file = shift; |
77 | local $/ = undef; |
78 | open(CAT,$file) || die "Cannot open $file: $!"; |
79 | my $result = <CAT>; |
80 | close(CAT); |
81 | unlink $file ; |
82 | return $result; |
83 | } |
84 | |
6250ba0a |
85 | sub bad_one |
86 | { |
25268f15 |
87 | print STDERR <<EOM unless $bad_ones++ ; |
88 | # |
20896112 |
89 | # Some older versions of Berkeley DB version 1 will fail tests 51, |
90 | # 53 and 55. |
6250ba0a |
91 | # |
92 | # You can safely ignore the errors if you're never going to use the |
93 | # broken functionality (recno databases with a modified bval). |
94 | # Otherwise you'll have to upgrade your DB library. |
95 | # |
20896112 |
96 | # If you want to use Berkeley DB version 1, then 1.85 and 1.86 are the |
97 | # last versions that were released. Berkeley DB version 2 is continually |
98 | # being updated -- Check out http://www.sleepycat.com/ for more details. |
6250ba0a |
99 | # |
100 | EOM |
55d68b4a |
101 | } |
102 | |
3245f058 |
103 | print "1..128\n"; |
55d68b4a |
104 | |
105 | my $Dfile = "recno.tmp"; |
106 | unlink $Dfile ; |
a0d0e21e |
107 | |
108 | umask(0); |
109 | |
110 | # Check the interface to RECNOINFO |
111 | |
55d68b4a |
112 | my $dbh = new DB_File::RECNOINFO ; |
3fe9a6f1 |
113 | ok(1, ! defined $dbh->{bval}) ; |
114 | ok(2, ! defined $dbh->{cachesize}) ; |
115 | ok(3, ! defined $dbh->{psize}) ; |
116 | ok(4, ! defined $dbh->{flags}) ; |
117 | ok(5, ! defined $dbh->{lorder}) ; |
118 | ok(6, ! defined $dbh->{reclen}) ; |
119 | ok(7, ! defined $dbh->{bfname}) ; |
a0d0e21e |
120 | |
121 | $dbh->{bval} = 3000 ; |
f6b705ef |
122 | ok(8, $dbh->{bval} == 3000 ); |
a0d0e21e |
123 | |
124 | $dbh->{cachesize} = 9000 ; |
f6b705ef |
125 | ok(9, $dbh->{cachesize} == 9000 ); |
a0d0e21e |
126 | |
127 | $dbh->{psize} = 400 ; |
f6b705ef |
128 | ok(10, $dbh->{psize} == 400 ); |
a0d0e21e |
129 | |
130 | $dbh->{flags} = 65 ; |
f6b705ef |
131 | ok(11, $dbh->{flags} == 65 ); |
a0d0e21e |
132 | |
133 | $dbh->{lorder} = 123 ; |
f6b705ef |
134 | ok(12, $dbh->{lorder} == 123 ); |
a0d0e21e |
135 | |
136 | $dbh->{reclen} = 1234 ; |
f6b705ef |
137 | ok(13, $dbh->{reclen} == 1234 ); |
a0d0e21e |
138 | |
139 | $dbh->{bfname} = 1234 ; |
f6b705ef |
140 | ok(14, $dbh->{bfname} == 1234 ); |
a0d0e21e |
141 | |
142 | |
143 | # Check that an invalid entry is caught both for store & fetch |
144 | eval '$dbh->{fred} = 1234' ; |
f6b705ef |
145 | ok(15, $@ =~ /^DB_File::RECNOINFO::STORE - Unknown element 'fred' at/ ); |
55d68b4a |
146 | eval 'my $q = $dbh->{fred}' ; |
f6b705ef |
147 | ok(16, $@ =~ /^DB_File::RECNOINFO::FETCH - Unknown element 'fred' at/ ); |
a0d0e21e |
148 | |
149 | # Now check the interface to RECNOINFO |
150 | |
55d68b4a |
151 | my $X ; |
152 | my @h ; |
153 | ok(17, $X = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ; |
a0d0e21e |
154 | |
a9fd575d |
155 | ok(18, ((stat($Dfile))[2] & 0777) == ($^O eq 'os2' ? 0666 : 0640) |
156 | || $^O eq 'MSWin32' || $^O eq 'amigaos') ; |
a0d0e21e |
157 | |
55d68b4a |
158 | #my $l = @h ; |
159 | my $l = $X->length ; |
045291aa |
160 | ok(19, ($FA ? @h == 0 : !$l) ); |
a0d0e21e |
161 | |
55d68b4a |
162 | my @data = qw( a b c d ever f g h i j k longername m n o p) ; |
a0d0e21e |
163 | |
164 | $h[0] = shift @data ; |
f6b705ef |
165 | ok(20, $h[0] eq 'a' ); |
a0d0e21e |
166 | |
55d68b4a |
167 | my $ i; |
a0d0e21e |
168 | foreach (@data) |
169 | { $h[++$i] = $_ } |
170 | |
171 | unshift (@data, 'a') ; |
172 | |
f6b705ef |
173 | ok(21, defined $h[1] ); |
174 | ok(22, ! defined $h[16] ); |
045291aa |
175 | ok(23, $FA ? @h == @data : $X->length == @data ); |
a0d0e21e |
176 | |
177 | |
178 | # Overwrite an entry & check fetch it |
179 | $h[3] = 'replaced' ; |
180 | $data[3] = 'replaced' ; |
f6b705ef |
181 | ok(24, $h[3] eq 'replaced' ); |
a0d0e21e |
182 | |
183 | #PUSH |
55d68b4a |
184 | my @push_data = qw(added to the end) ; |
045291aa |
185 | ($FA ? push(@h, @push_data) : $X->push(@push_data)) ; |
a0d0e21e |
186 | push (@data, @push_data) ; |
f6b705ef |
187 | ok(25, $h[++$i] eq 'added' ); |
188 | ok(26, $h[++$i] eq 'to' ); |
189 | ok(27, $h[++$i] eq 'the' ); |
190 | ok(28, $h[++$i] eq 'end' ); |
a0d0e21e |
191 | |
192 | # POP |
f6b705ef |
193 | my $popped = pop (@data) ; |
045291aa |
194 | my $value = ($FA ? pop @h : $X->pop) ; |
f6b705ef |
195 | ok(29, $value eq $popped) ; |
a0d0e21e |
196 | |
197 | # SHIFT |
045291aa |
198 | $value = ($FA ? shift @h : $X->shift) ; |
f6b705ef |
199 | my $shifted = shift @data ; |
200 | ok(30, $value eq $shifted ); |
a0d0e21e |
201 | |
202 | # UNSHIFT |
203 | |
204 | # empty list |
045291aa |
205 | ($FA ? unshift @h : $X->unshift) ; |
206 | ok(31, ($FA ? @h == @data : $X->length == @data )); |
a0d0e21e |
207 | |
55d68b4a |
208 | my @new_data = qw(add this to the start of the array) ; |
045291aa |
209 | $FA ? unshift (@h, @new_data) : $X->unshift (@new_data) ; |
a0d0e21e |
210 | unshift (@data, @new_data) ; |
045291aa |
211 | ok(32, $FA ? @h == @data : $X->length == @data ); |
f6b705ef |
212 | ok(33, $h[0] eq "add") ; |
213 | ok(34, $h[1] eq "this") ; |
214 | ok(35, $h[2] eq "to") ; |
215 | ok(36, $h[3] eq "the") ; |
216 | ok(37, $h[4] eq "start") ; |
217 | ok(38, $h[5] eq "of") ; |
218 | ok(39, $h[6] eq "the") ; |
219 | ok(40, $h[7] eq "array") ; |
220 | ok(41, $h[8] eq $data[8]) ; |
a0d0e21e |
221 | |
222 | # SPLICE |
223 | |
224 | # Now both arrays should be identical |
225 | |
55d68b4a |
226 | my $ok = 1 ; |
227 | my $j = 0 ; |
a0d0e21e |
228 | foreach (@data) |
229 | { |
230 | $ok = 0, last if $_ ne $h[$j ++] ; |
231 | } |
f6b705ef |
232 | ok(42, $ok ); |
a0d0e21e |
233 | |
55d68b4a |
234 | # Neagtive subscripts |
235 | |
236 | # get the last element of the array |
f6b705ef |
237 | ok(43, $h[-1] eq $data[-1] ); |
045291aa |
238 | ok(44, $h[-1] eq $h[ ($FA ? @h : $X->length) -1] ); |
55d68b4a |
239 | |
240 | # get the first element using a negative subscript |
045291aa |
241 | eval '$h[ - ( $FA ? @h : $X->length)] = "abcd"' ; |
f6b705ef |
242 | ok(45, $@ eq "" ); |
243 | ok(46, $h[0] eq "abcd" ); |
55d68b4a |
244 | |
245 | # now try to read before the start of the array |
045291aa |
246 | eval '$h[ - (1 + ($FA ? @h : $X->length))] = 1234' ; |
f6b705ef |
247 | ok(47, $@ =~ '^Modification of non-creatable array value attempted' ); |
55d68b4a |
248 | |
a0d0e21e |
249 | # IMPORTANT - $X must be undefined before the untie otherwise the |
250 | # underlying DB close routine will not get called. |
251 | undef $X ; |
252 | untie(@h); |
253 | |
254 | unlink $Dfile; |
255 | |
a6ed719b |
256 | |
36477c24 |
257 | { |
258 | # Check bval defaults to \n |
259 | |
260 | my @h = () ; |
261 | my $dbh = new DB_File::RECNOINFO ; |
262 | ok(48, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ; |
263 | $h[0] = "abc" ; |
264 | $h[1] = "def" ; |
265 | $h[3] = "ghi" ; |
266 | untie @h ; |
a6ed719b |
267 | my $x = docat($Dfile) ; |
36477c24 |
268 | unlink $Dfile; |
6250ba0a |
269 | ok(49, $x eq "abc\ndef\n\nghi\n") ; |
36477c24 |
270 | } |
271 | |
272 | { |
273 | # Change bval |
274 | |
275 | my @h = () ; |
276 | my $dbh = new DB_File::RECNOINFO ; |
277 | $dbh->{bval} = "-" ; |
278 | ok(50, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ; |
279 | $h[0] = "abc" ; |
280 | $h[1] = "def" ; |
281 | $h[3] = "ghi" ; |
282 | untie @h ; |
a6ed719b |
283 | my $x = docat($Dfile) ; |
36477c24 |
284 | unlink $Dfile; |
6250ba0a |
285 | my $ok = ($x eq "abc-def--ghi-") ; |
286 | bad_one() unless $ok ; |
287 | ok(51, $ok) ; |
36477c24 |
288 | } |
289 | |
290 | { |
291 | # Check R_FIXEDLEN with default bval (space) |
292 | |
293 | my @h = () ; |
294 | my $dbh = new DB_File::RECNOINFO ; |
295 | $dbh->{flags} = R_FIXEDLEN ; |
296 | $dbh->{reclen} = 5 ; |
297 | ok(52, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ; |
298 | $h[0] = "abc" ; |
299 | $h[1] = "def" ; |
300 | $h[3] = "ghi" ; |
301 | untie @h ; |
a6ed719b |
302 | my $x = docat($Dfile) ; |
36477c24 |
303 | unlink $Dfile; |
6250ba0a |
304 | my $ok = ($x eq "abc def ghi ") ; |
305 | bad_one() unless $ok ; |
306 | ok(53, $ok) ; |
36477c24 |
307 | } |
308 | |
309 | { |
310 | # Check R_FIXEDLEN with user-defined bval |
311 | |
312 | my @h = () ; |
313 | my $dbh = new DB_File::RECNOINFO ; |
314 | $dbh->{flags} = R_FIXEDLEN ; |
315 | $dbh->{bval} = "-" ; |
316 | $dbh->{reclen} = 5 ; |
317 | ok(54, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ; |
318 | $h[0] = "abc" ; |
319 | $h[1] = "def" ; |
320 | $h[3] = "ghi" ; |
321 | untie @h ; |
a6ed719b |
322 | my $x = docat($Dfile) ; |
36477c24 |
323 | unlink $Dfile; |
6250ba0a |
324 | my $ok = ($x eq "abc--def-------ghi--") ; |
325 | bad_one() unless $ok ; |
326 | ok(55, $ok) ; |
36477c24 |
327 | } |
328 | |
05475680 |
329 | { |
330 | # check that attempting to tie an associative array to a DB_RECNO will fail |
331 | |
332 | my $filename = "xyz" ; |
333 | my %x ; |
334 | eval { tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO ; } ; |
335 | ok(56, $@ =~ /^DB_File can only tie an array to a DB_RECNO database/) ; |
336 | unlink $filename ; |
337 | } |
338 | |
a6ed719b |
339 | { |
340 | # sub-class test |
341 | |
342 | package Another ; |
343 | |
3245f058 |
344 | use warnings ; |
a6ed719b |
345 | use strict ; |
346 | |
347 | open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ; |
348 | print FILE <<'EOM' ; |
349 | |
350 | package SubDB ; |
351 | |
3245f058 |
352 | use warnings ; |
a6ed719b |
353 | use strict ; |
354 | use vars qw( @ISA @EXPORT) ; |
355 | |
356 | require Exporter ; |
357 | use DB_File; |
358 | @ISA=qw(DB_File); |
359 | @EXPORT = @DB_File::EXPORT ; |
360 | |
361 | sub STORE { |
362 | my $self = shift ; |
363 | my $key = shift ; |
364 | my $value = shift ; |
365 | $self->SUPER::STORE($key, $value * 2) ; |
366 | } |
367 | |
368 | sub FETCH { |
369 | my $self = shift ; |
370 | my $key = shift ; |
371 | $self->SUPER::FETCH($key) - 1 ; |
372 | } |
373 | |
374 | sub put { |
375 | my $self = shift ; |
376 | my $key = shift ; |
377 | my $value = shift ; |
378 | $self->SUPER::put($key, $value * 3) ; |
379 | } |
380 | |
381 | sub get { |
382 | my $self = shift ; |
383 | $self->SUPER::get($_[0], $_[1]) ; |
384 | $_[1] -= 2 ; |
385 | } |
386 | |
387 | sub A_new_method |
388 | { |
389 | my $self = shift ; |
390 | my $key = shift ; |
391 | my $value = $self->FETCH($key) ; |
392 | return "[[$value]]" ; |
393 | } |
394 | |
395 | 1 ; |
396 | EOM |
397 | |
398 | close FILE ; |
399 | |
045291aa |
400 | BEGIN { push @INC, '.'; } |
a6ed719b |
401 | eval 'use SubDB ; '; |
402 | main::ok(57, $@ eq "") ; |
403 | my @h ; |
404 | my $X ; |
405 | eval ' |
406 | $X = tie(@h, "SubDB","recno.tmp", O_RDWR|O_CREAT, 0640, $DB_RECNO ); |
407 | ' ; |
408 | |
409 | main::ok(58, $@ eq "") ; |
410 | |
411 | my $ret = eval '$h[3] = 3 ; return $h[3] ' ; |
412 | main::ok(59, $@ eq "") ; |
413 | main::ok(60, $ret == 5) ; |
414 | |
415 | my $value = 0; |
416 | $ret = eval '$X->put(1, 4) ; $X->get(1, $value) ; return $value' ; |
417 | main::ok(61, $@ eq "") ; |
418 | main::ok(62, $ret == 10) ; |
419 | |
420 | $ret = eval ' R_NEXT eq main::R_NEXT ' ; |
421 | main::ok(63, $@ eq "" ) ; |
422 | main::ok(64, $ret == 1) ; |
423 | |
424 | $ret = eval '$X->A_new_method(1) ' ; |
425 | main::ok(65, $@ eq "") ; |
426 | main::ok(66, $ret eq "[[11]]") ; |
427 | |
fac76ed7 |
428 | undef $X; |
429 | untie(@h); |
a6ed719b |
430 | unlink "SubDB.pm", "recno.tmp" ; |
431 | |
432 | } |
433 | |
045291aa |
434 | { |
435 | |
436 | # test $# |
437 | my $self ; |
438 | unlink $Dfile; |
439 | ok(67, $self = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ; |
440 | $h[0] = "abc" ; |
441 | $h[1] = "def" ; |
442 | $h[2] = "ghi" ; |
443 | $h[3] = "jkl" ; |
444 | ok(68, $FA ? $#h == 3 : $self->length() == 4) ; |
445 | undef $self ; |
446 | untie @h ; |
447 | my $x = docat($Dfile) ; |
448 | ok(69, $x eq "abc\ndef\nghi\njkl\n") ; |
449 | |
450 | # $# sets array to same length |
451 | ok(70, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ; |
452 | if ($FA) |
453 | { $#h = 3 } |
454 | else |
455 | { $self->STORESIZE(4) } |
456 | ok(71, $FA ? $#h == 3 : $self->length() == 4) ; |
457 | undef $self ; |
458 | untie @h ; |
459 | $x = docat($Dfile) ; |
460 | ok(72, $x eq "abc\ndef\nghi\njkl\n") ; |
461 | |
462 | # $# sets array to bigger |
463 | ok(73, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ; |
464 | if ($FA) |
465 | { $#h = 6 } |
466 | else |
467 | { $self->STORESIZE(7) } |
468 | ok(74, $FA ? $#h == 6 : $self->length() == 7) ; |
469 | undef $self ; |
470 | untie @h ; |
471 | $x = docat($Dfile) ; |
472 | ok(75, $x eq "abc\ndef\nghi\njkl\n\n\n\n") ; |
473 | |
474 | # $# sets array smaller |
475 | ok(76, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ; |
476 | if ($FA) |
477 | { $#h = 2 } |
478 | else |
479 | { $self->STORESIZE(3) } |
480 | ok(77, $FA ? $#h == 2 : $self->length() == 3) ; |
481 | undef $self ; |
482 | untie @h ; |
483 | $x = docat($Dfile) ; |
484 | ok(78, $x eq "abc\ndef\nghi\n") ; |
485 | |
486 | unlink $Dfile; |
487 | |
488 | |
489 | } |
490 | |
9fe6733a |
491 | { |
492 | # DBM Filter tests |
3245f058 |
493 | use warnings ; |
9fe6733a |
494 | use strict ; |
495 | my (@h, $db) ; |
496 | my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
497 | unlink $Dfile; |
498 | |
499 | sub checkOutput |
500 | { |
501 | my($fk, $sk, $fv, $sv) = @_ ; |
502 | return |
503 | $fetch_key eq $fk && $store_key eq $sk && |
504 | $fetch_value eq $fv && $store_value eq $sv && |
505 | $_ eq 'original' ; |
506 | } |
507 | |
508 | ok(79, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ); |
509 | |
510 | $db->filter_fetch_key (sub { $fetch_key = $_ }) ; |
511 | $db->filter_store_key (sub { $store_key = $_ }) ; |
512 | $db->filter_fetch_value (sub { $fetch_value = $_}) ; |
513 | $db->filter_store_value (sub { $store_value = $_ }) ; |
514 | |
515 | $_ = "original" ; |
516 | |
517 | $h[0] = "joe" ; |
518 | # fk sk fv sv |
519 | ok(80, checkOutput( "", 0, "", "joe")) ; |
520 | |
521 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
522 | ok(81, $h[0] eq "joe"); |
523 | # fk sk fv sv |
524 | ok(82, checkOutput( "", 0, "joe", "")) ; |
525 | |
526 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
527 | ok(83, $db->FIRSTKEY() == 0) ; |
528 | # fk sk fv sv |
529 | ok(84, checkOutput( 0, "", "", "")) ; |
530 | |
531 | # replace the filters, but remember the previous set |
532 | my ($old_fk) = $db->filter_fetch_key |
533 | (sub { ++ $_ ; $fetch_key = $_ }) ; |
534 | my ($old_sk) = $db->filter_store_key |
535 | (sub { $_ *= 2 ; $store_key = $_ }) ; |
536 | my ($old_fv) = $db->filter_fetch_value |
537 | (sub { $_ = "[$_]"; $fetch_value = $_ }) ; |
538 | my ($old_sv) = $db->filter_store_value |
539 | (sub { s/o/x/g; $store_value = $_ }) ; |
540 | |
541 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
542 | $h[1] = "Joe" ; |
543 | # fk sk fv sv |
544 | ok(85, checkOutput( "", 2, "", "Jxe")) ; |
545 | |
546 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
547 | ok(86, $h[1] eq "[Jxe]"); |
548 | # fk sk fv sv |
549 | ok(87, checkOutput( "", 2, "[Jxe]", "")) ; |
550 | |
551 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
552 | ok(88, $db->FIRSTKEY() == 1) ; |
553 | # fk sk fv sv |
554 | ok(89, checkOutput( 1, "", "", "")) ; |
555 | |
556 | # put the original filters back |
557 | $db->filter_fetch_key ($old_fk); |
558 | $db->filter_store_key ($old_sk); |
559 | $db->filter_fetch_value ($old_fv); |
560 | $db->filter_store_value ($old_sv); |
561 | |
562 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
563 | $h[0] = "joe" ; |
564 | ok(90, checkOutput( "", 0, "", "joe")) ; |
565 | |
566 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
567 | ok(91, $h[0] eq "joe"); |
568 | ok(92, checkOutput( "", 0, "joe", "")) ; |
569 | |
570 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
571 | ok(93, $db->FIRSTKEY() == 0) ; |
572 | ok(94, checkOutput( 0, "", "", "")) ; |
573 | |
574 | # delete the filters |
575 | $db->filter_fetch_key (undef); |
576 | $db->filter_store_key (undef); |
577 | $db->filter_fetch_value (undef); |
578 | $db->filter_store_value (undef); |
579 | |
580 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
581 | $h[0] = "joe" ; |
582 | ok(95, checkOutput( "", "", "", "")) ; |
583 | |
584 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
585 | ok(96, $h[0] eq "joe"); |
586 | ok(97, checkOutput( "", "", "", "")) ; |
587 | |
588 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
589 | ok(98, $db->FIRSTKEY() == 0) ; |
590 | ok(99, checkOutput( "", "", "", "")) ; |
591 | |
592 | undef $db ; |
593 | untie @h; |
594 | unlink $Dfile; |
595 | } |
596 | |
597 | { |
598 | # DBM Filter with a closure |
599 | |
3245f058 |
600 | use warnings ; |
9fe6733a |
601 | use strict ; |
602 | my (@h, $db) ; |
603 | |
604 | unlink $Dfile; |
605 | ok(100, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ); |
606 | |
607 | my %result = () ; |
608 | |
609 | sub Closure |
610 | { |
611 | my ($name) = @_ ; |
612 | my $count = 0 ; |
613 | my @kept = () ; |
614 | |
615 | return sub { ++$count ; |
616 | push @kept, $_ ; |
617 | $result{$name} = "$name - $count: [@kept]" ; |
618 | } |
619 | } |
620 | |
621 | $db->filter_store_key(Closure("store key")) ; |
622 | $db->filter_store_value(Closure("store value")) ; |
623 | $db->filter_fetch_key(Closure("fetch key")) ; |
624 | $db->filter_fetch_value(Closure("fetch value")) ; |
625 | |
626 | $_ = "original" ; |
627 | |
628 | $h[0] = "joe" ; |
629 | ok(101, $result{"store key"} eq "store key - 1: [0]"); |
630 | ok(102, $result{"store value"} eq "store value - 1: [joe]"); |
631 | ok(103, ! defined $result{"fetch key"} ); |
632 | ok(104, ! defined $result{"fetch value"} ); |
633 | ok(105, $_ eq "original") ; |
634 | |
635 | ok(106, $db->FIRSTKEY() == 0 ) ; |
636 | ok(107, $result{"store key"} eq "store key - 1: [0]"); |
637 | ok(108, $result{"store value"} eq "store value - 1: [joe]"); |
638 | ok(109, $result{"fetch key"} eq "fetch key - 1: [0]"); |
639 | ok(110, ! defined $result{"fetch value"} ); |
640 | ok(111, $_ eq "original") ; |
641 | |
642 | $h[7] = "john" ; |
643 | ok(112, $result{"store key"} eq "store key - 2: [0 7]"); |
644 | ok(113, $result{"store value"} eq "store value - 2: [joe john]"); |
645 | ok(114, $result{"fetch key"} eq "fetch key - 1: [0]"); |
646 | ok(115, ! defined $result{"fetch value"} ); |
647 | ok(116, $_ eq "original") ; |
648 | |
649 | ok(117, $h[0] eq "joe"); |
650 | ok(118, $result{"store key"} eq "store key - 3: [0 7 0]"); |
651 | ok(119, $result{"store value"} eq "store value - 2: [joe john]"); |
652 | ok(120, $result{"fetch key"} eq "fetch key - 1: [0]"); |
653 | ok(121, $result{"fetch value"} eq "fetch value - 1: [joe]"); |
654 | ok(122, $_ eq "original") ; |
655 | |
656 | undef $db ; |
657 | untie @h; |
658 | unlink $Dfile; |
659 | } |
660 | |
661 | { |
662 | # DBM Filter recursion detection |
3245f058 |
663 | use warnings ; |
9fe6733a |
664 | use strict ; |
665 | my (@h, $db) ; |
666 | unlink $Dfile; |
667 | |
668 | ok(123, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ); |
669 | |
670 | $db->filter_store_key (sub { $_ = $h[0] }) ; |
671 | |
672 | eval '$h[1] = 1234' ; |
673 | ok(124, $@ =~ /^recursion detected in filter_store_key at/ ); |
674 | |
675 | undef $db ; |
676 | untie @h; |
677 | unlink $Dfile; |
678 | } |
679 | |
9b761c68 |
680 | |
681 | { |
682 | # Examples from the POD |
683 | |
684 | my $file = "xyzt" ; |
685 | { |
686 | my $redirect = new Redirect $file ; |
687 | |
3245f058 |
688 | use warnings FATAL => qw(all); |
9b761c68 |
689 | use strict ; |
690 | use DB_File ; |
691 | |
692 | my $filename = "text" ; |
693 | unlink $filename ; |
694 | |
695 | my @h ; |
696 | my $x = tie @h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO |
697 | or die "Cannot open file 'text': $!\n" ; |
698 | |
699 | # Add a few key/value pairs to the file |
700 | $h[0] = "orange" ; |
701 | $h[1] = "blue" ; |
702 | $h[2] = "yellow" ; |
703 | |
704 | $FA ? push @h, "green", "black" |
705 | : $x->push("green", "black") ; |
706 | |
707 | my $elements = $FA ? scalar @h : $x->length ; |
708 | print "The array contains $elements entries\n" ; |
709 | |
710 | my $last = $FA ? pop @h : $x->pop ; |
711 | print "popped $last\n" ; |
712 | |
713 | $FA ? unshift @h, "white" |
714 | : $x->unshift("white") ; |
715 | my $first = $FA ? shift @h : $x->shift ; |
716 | print "shifted $first\n" ; |
717 | |
718 | # Check for existence of a key |
719 | print "Element 1 Exists with value $h[1]\n" if $h[1] ; |
720 | |
721 | # use a negative index |
722 | print "The last element is $h[-1]\n" ; |
723 | print "The 2nd last element is $h[-2]\n" ; |
724 | |
725 | undef $x ; |
726 | untie @h ; |
727 | |
728 | unlink $filename ; |
729 | } |
730 | |
731 | ok(125, docat_del($file) eq <<'EOM') ; |
732 | The array contains 5 entries |
733 | popped black |
734 | shifted white |
735 | Element 1 Exists with value blue |
736 | The last element is green |
737 | The 2nd last element is yellow |
738 | EOM |
739 | |
740 | my $save_output = "xyzt" ; |
741 | { |
742 | my $redirect = new Redirect $save_output ; |
743 | |
3245f058 |
744 | use warnings FATAL => qw(all); |
9b761c68 |
745 | use strict ; |
746 | use vars qw(@h $H $file $i) ; |
747 | use DB_File ; |
748 | use Fcntl ; |
749 | |
750 | $file = "text" ; |
751 | |
752 | unlink $file ; |
753 | |
754 | $H = tie @h, "DB_File", $file, O_RDWR|O_CREAT, 0640, $DB_RECNO |
755 | or die "Cannot open file $file: $!\n" ; |
756 | |
757 | # first create a text file to play with |
758 | $h[0] = "zero" ; |
759 | $h[1] = "one" ; |
760 | $h[2] = "two" ; |
761 | $h[3] = "three" ; |
762 | $h[4] = "four" ; |
763 | |
764 | |
765 | # Print the records in order. |
766 | # |
767 | # The length method is needed here because evaluating a tied |
768 | # array in a scalar context does not return the number of |
769 | # elements in the array. |
770 | |
771 | print "\nORIGINAL\n" ; |
772 | foreach $i (0 .. $H->length - 1) { |
773 | print "$i: $h[$i]\n" ; |
774 | } |
775 | |
776 | # use the push & pop methods |
777 | $a = $H->pop ; |
778 | $H->push("last") ; |
779 | print "\nThe last record was [$a]\n" ; |
780 | |
781 | # and the shift & unshift methods |
782 | $a = $H->shift ; |
783 | $H->unshift("first") ; |
784 | print "The first record was [$a]\n" ; |
785 | |
786 | # Use the API to add a new record after record 2. |
787 | $i = 2 ; |
788 | $H->put($i, "Newbie", R_IAFTER) ; |
789 | |
790 | # and a new record before record 1. |
791 | $i = 1 ; |
792 | $H->put($i, "New One", R_IBEFORE) ; |
793 | |
794 | # delete record 3 |
795 | $H->del(3) ; |
796 | |
797 | # now print the records in reverse order |
798 | print "\nREVERSE\n" ; |
799 | for ($i = $H->length - 1 ; $i >= 0 ; -- $i) |
800 | { print "$i: $h[$i]\n" } |
801 | |
802 | # same again, but use the API functions instead |
803 | print "\nREVERSE again\n" ; |
804 | my ($s, $k, $v) = (0, 0, 0) ; |
805 | for ($s = $H->seq($k, $v, R_LAST) ; |
806 | $s == 0 ; |
807 | $s = $H->seq($k, $v, R_PREV)) |
808 | { print "$k: $v\n" } |
809 | |
810 | undef $H ; |
811 | untie @h ; |
812 | |
813 | unlink $file ; |
814 | } |
815 | |
816 | ok(126, docat_del($save_output) eq <<'EOM') ; |
817 | |
818 | ORIGINAL |
819 | 0: zero |
820 | 1: one |
821 | 2: two |
822 | 3: three |
823 | 4: four |
824 | |
825 | The last record was [four] |
826 | The first record was [zero] |
827 | |
828 | REVERSE |
829 | 5: last |
830 | 4: three |
831 | 3: Newbie |
832 | 2: one |
833 | 1: New One |
834 | 0: first |
835 | |
836 | REVERSE again |
837 | 5: last |
838 | 4: three |
839 | 3: Newbie |
840 | 2: one |
841 | 1: New One |
842 | 0: first |
843 | EOM |
844 | |
845 | } |
846 | |
cbc5248d |
847 | { |
848 | # Bug ID 20001013.009 |
849 | # |
850 | # test that $hash{KEY} = undef doesn't produce the warning |
851 | # Use of uninitialized value in null operation |
852 | use warnings ; |
853 | use strict ; |
854 | use DB_File ; |
855 | |
856 | unlink $Dfile; |
857 | my @h ; |
858 | my $a = ""; |
859 | local $SIG{__WARN__} = sub {$a = $_[0]} ; |
860 | |
3245f058 |
861 | tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO |
cbc5248d |
862 | or die "Can't open file: $!\n" ; |
863 | $h[0] = undef; |
864 | ok(127, $a eq "") ; |
3245f058 |
865 | untie @h ; |
866 | unlink $Dfile; |
867 | } |
868 | |
869 | { |
870 | # test that %hash = () doesn't produce the warning |
871 | # Argument "" isn't numeric in entersub |
872 | use warnings ; |
873 | use strict ; |
874 | use DB_File ; |
875 | my $a = ""; |
876 | local $SIG{__WARN__} = sub {$a = $_[0]} ; |
877 | |
878 | unlink $Dfile; |
879 | my @h ; |
880 | |
881 | tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO |
882 | or die "Can't open file: $!\n" ; |
883 | @h = (); ; |
884 | ok(128, $a eq "") ; |
885 | untie @h ; |
cbc5248d |
886 | unlink $Dfile; |
887 | } |
888 | |
a0d0e21e |
889 | exit ; |