Commit | Line | Data |
f6b705ef |
1 | #!./perl -w |
a0d0e21e |
2 | |
3 | BEGIN { |
88587957 |
4 | chdir 't' if -d 't'; |
20822f61 |
5 | @INC = '../lib'; |
a0d0e21e |
6 | require Config; import Config; |
7 | if ($Config{'extensions'} !~ /\bDB_File\b/) { |
45c0de28 |
8 | print "1..0 # Skip: DB_File was not built\n"; |
a0d0e21e |
9 | exit 0; |
10 | } |
11 | } |
12 | |
3245f058 |
13 | use warnings; |
14 | use strict; |
a0d0e21e |
15 | use DB_File; |
16 | use Fcntl; |
17 | |
3245f058 |
18 | print "1..157\n"; |
f6b705ef |
19 | |
20 | sub ok |
21 | { |
22 | my $no = shift ; |
23 | my $result = shift ; |
24 | |
25 | print "not " unless $result ; |
26 | print "ok $no\n" ; |
27 | } |
a0d0e21e |
28 | |
55497cff |
29 | sub lexical |
30 | { |
31 | my(@a) = unpack ("C*", $a) ; |
32 | my(@b) = unpack ("C*", $b) ; |
33 | |
34 | my $len = (@a > @b ? @b : @a) ; |
35 | my $i = 0 ; |
36 | |
37 | foreach $i ( 0 .. $len -1) { |
38 | return $a[$i] - $b[$i] if $a[$i] != $b[$i] ; |
39 | } |
40 | |
41 | return @a - @b ; |
42 | } |
43 | |
2c2d71f5 |
44 | { |
45 | package Redirect ; |
46 | use Symbol ; |
47 | |
48 | sub new |
49 | { |
50 | my $class = shift ; |
51 | my $filename = shift ; |
52 | my $fh = gensym ; |
53 | open ($fh, ">$filename") || die "Cannot open $filename: $!" ; |
54 | my $real_stdout = select($fh) ; |
55 | return bless [$fh, $real_stdout ] ; |
56 | |
57 | } |
58 | sub DESTROY |
59 | { |
60 | my $self = shift ; |
61 | close $self->[0] ; |
62 | select($self->[1]) ; |
63 | } |
64 | } |
65 | |
66 | sub docat |
67 | { |
68 | my $file = shift; |
69 | #local $/ = undef unless wantarray ; |
70 | open(CAT,$file) || die "Cannot open $file: $!"; |
71 | my @result = <CAT>; |
72 | close(CAT); |
73 | wantarray ? @result : join("", @result) ; |
74 | } |
75 | |
76 | sub docat_del |
77 | { |
78 | my $file = shift; |
79 | #local $/ = undef unless wantarray ; |
80 | open(CAT,$file) || die "Cannot open $file: $!"; |
81 | my @result = <CAT>; |
82 | close(CAT); |
83 | unlink $file ; |
84 | wantarray ? @result : join("", @result) ; |
85 | } |
86 | |
87 | |
3245f058 |
88 | my $db185mode = ($DB_File::db_version == 1 && ! $DB_File::db_185_compat) ; |
89 | my $null_keys_allowed = ($DB_File::db_ver < 2.004010 |
90 | || $DB_File::db_ver >= 3.1 ); |
039d031f |
91 | |
9fe6733a |
92 | my $Dfile = "dbbtree.tmp"; |
a0d0e21e |
93 | unlink $Dfile; |
94 | |
95 | umask(0); |
96 | |
97 | # Check the interface to BTREEINFO |
98 | |
f6b705ef |
99 | my $dbh = new DB_File::BTREEINFO ; |
3fe9a6f1 |
100 | ok(1, ! defined $dbh->{flags}) ; |
101 | ok(2, ! defined $dbh->{cachesize}) ; |
102 | ok(3, ! defined $dbh->{psize}) ; |
103 | ok(4, ! defined $dbh->{lorder}) ; |
104 | ok(5, ! defined $dbh->{minkeypage}) ; |
105 | ok(6, ! defined $dbh->{maxkeypage}) ; |
106 | ok(7, ! defined $dbh->{compare}) ; |
107 | ok(8, ! defined $dbh->{prefix}) ; |
a0d0e21e |
108 | |
109 | $dbh->{flags} = 3000 ; |
f6b705ef |
110 | ok(9, $dbh->{flags} == 3000) ; |
a0d0e21e |
111 | |
112 | $dbh->{cachesize} = 9000 ; |
f6b705ef |
113 | ok(10, $dbh->{cachesize} == 9000); |
114 | |
a0d0e21e |
115 | $dbh->{psize} = 400 ; |
f6b705ef |
116 | ok(11, $dbh->{psize} == 400) ; |
a0d0e21e |
117 | |
118 | $dbh->{lorder} = 65 ; |
f6b705ef |
119 | ok(12, $dbh->{lorder} == 65) ; |
a0d0e21e |
120 | |
121 | $dbh->{minkeypage} = 123 ; |
f6b705ef |
122 | ok(13, $dbh->{minkeypage} == 123) ; |
a0d0e21e |
123 | |
124 | $dbh->{maxkeypage} = 1234 ; |
f6b705ef |
125 | ok(14, $dbh->{maxkeypage} == 1234 ); |
a0d0e21e |
126 | |
127 | $dbh->{compare} = 1234 ; |
f6b705ef |
128 | ok(15, $dbh->{compare} == 1234) ; |
a0d0e21e |
129 | |
130 | $dbh->{prefix} = 1234 ; |
f6b705ef |
131 | ok(16, $dbh->{prefix} == 1234 ); |
a0d0e21e |
132 | |
133 | # Check that an invalid entry is caught both for store & fetch |
134 | eval '$dbh->{fred} = 1234' ; |
f6b705ef |
135 | ok(17, $@ =~ /^DB_File::BTREEINFO::STORE - Unknown element 'fred' at/ ) ; |
3245f058 |
136 | eval 'my $q = $dbh->{fred}' ; |
f6b705ef |
137 | ok(18, $@ =~ /^DB_File::BTREEINFO::FETCH - Unknown element 'fred' at/ ) ; |
a0d0e21e |
138 | |
139 | # Now check the interface to BTREE |
140 | |
3245f058 |
141 | my ($X, %h) ; |
f6b705ef |
142 | ok(19, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE )) ; |
a0d0e21e |
143 | |
3245f058 |
144 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, |
a0d0e21e |
145 | $blksize,$blocks) = stat($Dfile); |
d536870a |
146 | ok(20, ($mode & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640) |
147 | || $^O eq 'amigaos' || $^O eq 'MSWin32' || $^O eq 'NetWare'); |
a0d0e21e |
148 | |
3245f058 |
149 | my ($key, $value, $i); |
a0d0e21e |
150 | while (($key,$value) = each(%h)) { |
151 | $i++; |
152 | } |
f6b705ef |
153 | ok(21, !$i ) ; |
a0d0e21e |
154 | |
155 | $h{'goner1'} = 'snork'; |
156 | |
157 | $h{'abc'} = 'ABC'; |
f6b705ef |
158 | ok(22, $h{'abc'} eq 'ABC' ); |
159 | ok(23, ! defined $h{'jimmy'} ) ; |
160 | ok(24, ! exists $h{'jimmy'} ) ; |
161 | ok(25, defined $h{'abc'} ) ; |
a0d0e21e |
162 | |
163 | $h{'def'} = 'DEF'; |
164 | $h{'jkl','mno'} = "JKL\034MNO"; |
165 | $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5); |
166 | $h{'a'} = 'A'; |
167 | |
168 | #$h{'b'} = 'B'; |
169 | $X->STORE('b', 'B') ; |
170 | |
171 | $h{'c'} = 'C'; |
172 | |
173 | #$h{'d'} = 'D'; |
174 | $X->put('d', 'D') ; |
175 | |
176 | $h{'e'} = 'E'; |
177 | $h{'f'} = 'F'; |
178 | $h{'g'} = 'X'; |
179 | $h{'h'} = 'H'; |
180 | $h{'i'} = 'I'; |
181 | |
182 | $h{'goner2'} = 'snork'; |
183 | delete $h{'goner2'}; |
184 | |
185 | |
186 | # IMPORTANT - $X must be undefined before the untie otherwise the |
187 | # underlying DB close routine will not get called. |
188 | undef $X ; |
189 | untie(%h); |
190 | |
a0d0e21e |
191 | # tie to the same file again |
f6b705ef |
192 | ok(26, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE)) ; |
a0d0e21e |
193 | |
194 | # Modify an entry from the previous tie |
195 | $h{'g'} = 'G'; |
196 | |
197 | $h{'j'} = 'J'; |
198 | $h{'k'} = 'K'; |
199 | $h{'l'} = 'L'; |
200 | $h{'m'} = 'M'; |
201 | $h{'n'} = 'N'; |
202 | $h{'o'} = 'O'; |
203 | $h{'p'} = 'P'; |
204 | $h{'q'} = 'Q'; |
205 | $h{'r'} = 'R'; |
206 | $h{'s'} = 'S'; |
207 | $h{'t'} = 'T'; |
208 | $h{'u'} = 'U'; |
209 | $h{'v'} = 'V'; |
210 | $h{'w'} = 'W'; |
211 | $h{'x'} = 'X'; |
212 | $h{'y'} = 'Y'; |
213 | $h{'z'} = 'Z'; |
214 | |
215 | $h{'goner3'} = 'snork'; |
216 | |
217 | delete $h{'goner1'}; |
218 | $X->DELETE('goner3'); |
219 | |
3245f058 |
220 | my @keys = keys(%h); |
221 | my @values = values(%h); |
a0d0e21e |
222 | |
f6b705ef |
223 | ok(27, $#keys == 29 && $#values == 29) ; |
a0d0e21e |
224 | |
f6b705ef |
225 | $i = 0 ; |
a0d0e21e |
226 | while (($key,$value) = each(%h)) { |
2f52a358 |
227 | if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) { |
a0d0e21e |
228 | $key =~ y/a-z/A-Z/; |
229 | $i++ if $key eq $value; |
230 | } |
231 | } |
232 | |
f6b705ef |
233 | ok(28, $i == 30) ; |
a0d0e21e |
234 | |
55d68b4a |
235 | @keys = ('blurfl', keys(%h), 'dyick'); |
f6b705ef |
236 | ok(29, $#keys == 31) ; |
a0d0e21e |
237 | |
238 | #Check that the keys can be retrieved in order |
55497cff |
239 | my @b = keys %h ; |
240 | my @c = sort lexical @b ; |
241 | ok(30, ArrayCompare(\@b, \@c)) ; |
a0d0e21e |
242 | |
243 | $h{'foo'} = ''; |
f6b705ef |
244 | ok(31, $h{'foo'} eq '' ) ; |
a0d0e21e |
245 | |
3245f058 |
246 | # Berkeley DB from version 2.4.10 to 3.0 does not allow null keys. |
247 | # This feature was reenabled in version 3.1 of Berkeley DB. |
248 | my $result = 0 ; |
249 | if ($null_keys_allowed) { |
250 | $h{''} = 'bar'; |
251 | $result = ( $h{''} eq 'bar' ); |
252 | } |
253 | else |
254 | { $result = 1 } |
255 | ok(32, $result) ; |
a0d0e21e |
256 | |
257 | # check cache overflow and numeric keys and contents |
3245f058 |
258 | my $ok = 1; |
a0d0e21e |
259 | for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; } |
260 | for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; } |
f6b705ef |
261 | ok(33, $ok); |
a0d0e21e |
262 | |
263 | ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, |
264 | $blksize,$blocks) = stat($Dfile); |
f6b705ef |
265 | ok(34, $size > 0 ); |
a0d0e21e |
266 | |
267 | @h{0..200} = 200..400; |
3245f058 |
268 | my @foo = @h{0..200}; |
f6b705ef |
269 | ok(35, join(':',200..400) eq join(':',@foo) ); |
a0d0e21e |
270 | |
271 | # Now check all the non-tie specific stuff |
272 | |
273 | |
274 | # Check R_NOOVERWRITE flag will make put fail when attempting to overwrite |
275 | # an existing record. |
276 | |
3245f058 |
277 | my $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ; |
f6b705ef |
278 | ok(36, $status == 1 ); |
a0d0e21e |
279 | |
280 | # check that the value of the key 'x' has not been changed by the |
281 | # previous test |
f6b705ef |
282 | ok(37, $h{'x'} eq 'X' ); |
a0d0e21e |
283 | |
284 | # standard put |
285 | $status = $X->put('key', 'value') ; |
f6b705ef |
286 | ok(38, $status == 0 ); |
a0d0e21e |
287 | |
288 | #check that previous put can be retrieved |
f6b705ef |
289 | $value = 0 ; |
a0d0e21e |
290 | $status = $X->get('key', $value) ; |
f6b705ef |
291 | ok(39, $status == 0 ); |
292 | ok(40, $value eq 'value' ); |
a0d0e21e |
293 | |
294 | # Attempting to delete an existing key should work |
295 | |
296 | $status = $X->del('q') ; |
f6b705ef |
297 | ok(41, $status == 0 ); |
3245f058 |
298 | if ($null_keys_allowed) { |
299 | $status = $X->del('') ; |
300 | } else { |
301 | $status = 0 ; |
302 | } |
303 | ok(42, $status == 0 ); |
a0d0e21e |
304 | |
305 | # Make sure that the key deleted, cannot be retrieved |
ac1ad7f0 |
306 | ok(43, ! defined $h{'q'}) ; |
307 | ok(44, ! defined $h{''}) ; |
a0d0e21e |
308 | |
309 | undef $X ; |
310 | untie %h ; |
311 | |
f6b705ef |
312 | ok(45, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE )); |
a0d0e21e |
313 | |
314 | # Attempting to delete a non-existant key should fail |
315 | |
316 | $status = $X->del('joe') ; |
f6b705ef |
317 | ok(46, $status == 1 ); |
a0d0e21e |
318 | |
319 | # Check the get interface |
320 | |
321 | # First a non-existing key |
322 | $status = $X->get('aaaa', $value) ; |
f6b705ef |
323 | ok(47, $status == 1 ); |
a0d0e21e |
324 | |
325 | # Next an existing key |
326 | $status = $X->get('a', $value) ; |
f6b705ef |
327 | ok(48, $status == 0 ); |
328 | ok(49, $value eq 'A' ); |
a0d0e21e |
329 | |
330 | # seq |
331 | # ### |
332 | |
333 | # use seq to find an approximate match |
334 | $key = 'ke' ; |
335 | $value = '' ; |
336 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
337 | ok(50, $status == 0 ); |
338 | ok(51, $key eq 'key' ); |
339 | ok(52, $value eq 'value' ); |
a0d0e21e |
340 | |
341 | # seq when the key does not match |
342 | $key = 'zzz' ; |
343 | $value = '' ; |
344 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
345 | ok(53, $status == 1 ); |
a0d0e21e |
346 | |
347 | |
348 | # use seq to set the cursor, then delete the record @ the cursor. |
349 | |
350 | $key = 'x' ; |
351 | $value = '' ; |
352 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
353 | ok(54, $status == 0 ); |
354 | ok(55, $key eq 'x' ); |
355 | ok(56, $value eq 'X' ); |
a0d0e21e |
356 | $status = $X->del(0, R_CURSOR) ; |
f6b705ef |
357 | ok(57, $status == 0 ); |
a0d0e21e |
358 | $status = $X->get('x', $value) ; |
f6b705ef |
359 | ok(58, $status == 1 ); |
a0d0e21e |
360 | |
361 | # ditto, but use put to replace the key/value pair. |
362 | $key = 'y' ; |
363 | $value = '' ; |
364 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
365 | ok(59, $status == 0 ); |
366 | ok(60, $key eq 'y' ); |
367 | ok(61, $value eq 'Y' ); |
a0d0e21e |
368 | |
369 | $key = "replace key" ; |
370 | $value = "replace value" ; |
371 | $status = $X->put($key, $value, R_CURSOR) ; |
f6b705ef |
372 | ok(62, $status == 0 ); |
373 | ok(63, $key eq 'replace key' ); |
374 | ok(64, $value eq 'replace value' ); |
a0d0e21e |
375 | $status = $X->get('y', $value) ; |
1f70e1ea |
376 | ok(65, 1) ; # hard-wire to always pass. the previous test ($status == 1) |
a9fd575d |
377 | # only worked because of a bug in 1.85/6 |
a0d0e21e |
378 | |
379 | # use seq to walk forwards through a file |
380 | |
381 | $status = $X->seq($key, $value, R_FIRST) ; |
f6b705ef |
382 | ok(66, $status == 0 ); |
3245f058 |
383 | my $previous = $key ; |
a0d0e21e |
384 | |
385 | $ok = 1 ; |
386 | while (($status = $X->seq($key, $value, R_NEXT)) == 0) |
387 | { |
388 | ($ok = 0), last if ($previous cmp $key) == 1 ; |
389 | } |
390 | |
f6b705ef |
391 | ok(67, $status == 1 ); |
392 | ok(68, $ok == 1 ); |
a0d0e21e |
393 | |
394 | # use seq to walk backwards through a file |
395 | $status = $X->seq($key, $value, R_LAST) ; |
f6b705ef |
396 | ok(69, $status == 0 ); |
a0d0e21e |
397 | $previous = $key ; |
398 | |
399 | $ok = 1 ; |
400 | while (($status = $X->seq($key, $value, R_PREV)) == 0) |
401 | { |
402 | ($ok = 0), last if ($previous cmp $key) == -1 ; |
403 | #print "key = [$key] value = [$value]\n" ; |
404 | } |
405 | |
f6b705ef |
406 | ok(70, $status == 1 ); |
407 | ok(71, $ok == 1 ); |
a0d0e21e |
408 | |
409 | |
410 | # check seq FIRST/LAST |
411 | |
412 | # sync |
413 | # #### |
414 | |
415 | $status = $X->sync ; |
f6b705ef |
416 | ok(72, $status == 0 ); |
a0d0e21e |
417 | |
418 | |
419 | # fd |
420 | # ## |
421 | |
422 | $status = $X->fd ; |
f6b705ef |
423 | ok(73, $status != 0 ); |
a0d0e21e |
424 | |
425 | |
426 | undef $X ; |
427 | untie %h ; |
428 | |
429 | unlink $Dfile; |
430 | |
431 | # Now try an in memory file |
3245f058 |
432 | my $Y; |
f6b705ef |
433 | ok(74, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE )); |
a0d0e21e |
434 | |
435 | # fd with an in memory file should return failure |
436 | $status = $Y->fd ; |
f6b705ef |
437 | ok(75, $status == -1 ); |
a0d0e21e |
438 | |
55d68b4a |
439 | |
a0d0e21e |
440 | undef $Y ; |
441 | untie %h ; |
442 | |
55d68b4a |
443 | # Duplicate keys |
444 | my $bt = new DB_File::BTREEINFO ; |
445 | $bt->{flags} = R_DUP ; |
3245f058 |
446 | my ($YY, %hh); |
f6b705ef |
447 | ok(76, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ; |
55d68b4a |
448 | |
449 | $hh{'Wall'} = 'Larry' ; |
450 | $hh{'Wall'} = 'Stone' ; # Note the duplicate key |
451 | $hh{'Wall'} = 'Brick' ; # Note the duplicate key |
f6b705ef |
452 | $hh{'Wall'} = 'Brick' ; # Note the duplicate key and value |
55d68b4a |
453 | $hh{'Smith'} = 'John' ; |
454 | $hh{'mouse'} = 'mickey' ; |
455 | |
456 | # first work in scalar context |
f6b705ef |
457 | ok(77, scalar $YY->get_dup('Unknown') == 0 ); |
458 | ok(78, scalar $YY->get_dup('Smith') == 1 ); |
459 | ok(79, scalar $YY->get_dup('Wall') == 4 ); |
55d68b4a |
460 | |
461 | # now in list context |
462 | my @unknown = $YY->get_dup('Unknown') ; |
f6b705ef |
463 | ok(80, "@unknown" eq "" ); |
55d68b4a |
464 | |
465 | my @smith = $YY->get_dup('Smith') ; |
f6b705ef |
466 | ok(81, "@smith" eq "John" ); |
55d68b4a |
467 | |
760ac839 |
468 | { |
f6b705ef |
469 | my @wall = $YY->get_dup('Wall') ; |
470 | my %wall ; |
471 | @wall{@wall} = @wall ; |
472 | ok(82, (@wall == 4 && $wall{'Larry'} && $wall{'Stone'} && $wall{'Brick'}) ); |
760ac839 |
473 | } |
55d68b4a |
474 | |
475 | # hash |
476 | my %unknown = $YY->get_dup('Unknown', 1) ; |
f6b705ef |
477 | ok(83, keys %unknown == 0 ); |
55d68b4a |
478 | |
479 | my %smith = $YY->get_dup('Smith', 1) ; |
f6b705ef |
480 | ok(84, keys %smith == 1 && $smith{'John'}) ; |
55d68b4a |
481 | |
f6b705ef |
482 | my %wall = $YY->get_dup('Wall', 1) ; |
483 | ok(85, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1 |
484 | && $wall{'Brick'} == 2); |
55d68b4a |
485 | |
486 | undef $YY ; |
487 | untie %hh ; |
488 | unlink $Dfile; |
489 | |
490 | |
8e07c86e |
491 | # test multiple callbacks |
3245f058 |
492 | my $Dfile1 = "btree1" ; |
493 | my $Dfile2 = "btree2" ; |
494 | my $Dfile3 = "btree3" ; |
8e07c86e |
495 | |
3245f058 |
496 | my $dbh1 = new DB_File::BTREEINFO ; |
497 | $dbh1->{compare} = sub { |
498 | no warnings 'numeric' ; |
499 | $_[0] <=> $_[1] } ; |
8e07c86e |
500 | |
3245f058 |
501 | my $dbh2 = new DB_File::BTREEINFO ; |
8e07c86e |
502 | $dbh2->{compare} = sub { $_[0] cmp $_[1] } ; |
503 | |
3245f058 |
504 | my $dbh3 = new DB_File::BTREEINFO ; |
8e07c86e |
505 | $dbh3->{compare} = sub { length $_[0] <=> length $_[1] } ; |
506 | |
507 | |
3245f058 |
508 | my (%g, %k); |
509 | tie(%h, 'DB_File',$Dfile1, O_RDWR|O_CREAT, 0640, $dbh1 ) ; |
f6b705ef |
510 | tie(%g, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) ; |
511 | tie(%k, 'DB_File',$Dfile3, O_RDWR|O_CREAT, 0640, $dbh3 ) ; |
8e07c86e |
512 | |
3245f058 |
513 | my @Keys = qw( 0123 12 -1234 9 987654321 def ) ; |
514 | my (@srt_1, @srt_2, @srt_3); |
515 | { |
516 | no warnings 'numeric' ; |
517 | @srt_1 = sort { $a <=> $b } @Keys ; |
518 | } |
8e07c86e |
519 | @srt_2 = sort { $a cmp $b } @Keys ; |
520 | @srt_3 = sort { length $a <=> length $b } @Keys ; |
521 | |
522 | foreach (@Keys) { |
3245f058 |
523 | $h{$_} = 1 ; |
8e07c86e |
524 | $g{$_} = 1 ; |
525 | $k{$_} = 1 ; |
526 | } |
527 | |
528 | sub ArrayCompare |
529 | { |
530 | my($a, $b) = @_ ; |
531 | |
532 | return 0 if @$a != @$b ; |
533 | |
534 | foreach (1 .. length @$a) |
535 | { |
536 | return 0 unless $$a[$_] eq $$b[$_] ; |
537 | } |
538 | |
539 | 1 ; |
540 | } |
541 | |
f6b705ef |
542 | ok(86, ArrayCompare (\@srt_1, [keys %h]) ); |
543 | ok(87, ArrayCompare (\@srt_2, [keys %g]) ); |
544 | ok(88, ArrayCompare (\@srt_3, [keys %k]) ); |
8e07c86e |
545 | |
546 | untie %h ; |
547 | untie %g ; |
548 | untie %k ; |
549 | unlink $Dfile1, $Dfile2, $Dfile3 ; |
550 | |
f6b705ef |
551 | # clear |
552 | # ##### |
553 | |
554 | ok(89, tie(%h, 'DB_File', $Dfile1, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
555 | foreach (1 .. 10) |
556 | { $h{$_} = $_ * 100 } |
557 | |
558 | # check that there are 10 elements in the hash |
559 | $i = 0 ; |
560 | while (($key,$value) = each(%h)) { |
561 | $i++; |
562 | } |
563 | ok(90, $i == 10); |
564 | |
565 | # now clear the hash |
566 | %h = () ; |
567 | |
568 | # check it is empty |
569 | $i = 0 ; |
570 | while (($key,$value) = each(%h)) { |
571 | $i++; |
572 | } |
573 | ok(91, $i == 0); |
574 | |
575 | untie %h ; |
576 | unlink $Dfile1 ; |
577 | |
05475680 |
578 | { |
579 | # check that attempting to tie an array to a DB_BTREE will fail |
580 | |
581 | my $filename = "xyz" ; |
582 | my @x ; |
583 | eval { tie @x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE ; } ; |
584 | ok(92, $@ =~ /^DB_File can only tie an associative array to a DB_BTREE database/) ; |
585 | unlink $filename ; |
586 | } |
587 | |
a6ed719b |
588 | { |
589 | # sub-class test |
590 | |
591 | package Another ; |
592 | |
3245f058 |
593 | use warnings ; |
a6ed719b |
594 | use strict ; |
595 | |
596 | open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ; |
597 | print FILE <<'EOM' ; |
598 | |
599 | package SubDB ; |
600 | |
3245f058 |
601 | use warnings ; |
a6ed719b |
602 | use strict ; |
603 | use vars qw( @ISA @EXPORT) ; |
604 | |
605 | require Exporter ; |
606 | use DB_File; |
607 | @ISA=qw(DB_File); |
608 | @EXPORT = @DB_File::EXPORT ; |
609 | |
610 | sub STORE { |
611 | my $self = shift ; |
612 | my $key = shift ; |
613 | my $value = shift ; |
614 | $self->SUPER::STORE($key, $value * 2) ; |
615 | } |
616 | |
617 | sub FETCH { |
618 | my $self = shift ; |
619 | my $key = shift ; |
620 | $self->SUPER::FETCH($key) - 1 ; |
621 | } |
622 | |
623 | sub put { |
624 | my $self = shift ; |
625 | my $key = shift ; |
626 | my $value = shift ; |
627 | $self->SUPER::put($key, $value * 3) ; |
628 | } |
629 | |
630 | sub get { |
631 | my $self = shift ; |
632 | $self->SUPER::get($_[0], $_[1]) ; |
633 | $_[1] -= 2 ; |
634 | } |
635 | |
636 | sub A_new_method |
637 | { |
638 | my $self = shift ; |
639 | my $key = shift ; |
640 | my $value = $self->FETCH($key) ; |
641 | return "[[$value]]" ; |
642 | } |
643 | |
644 | 1 ; |
645 | EOM |
646 | |
647 | close FILE ; |
648 | |
a9fd575d |
649 | BEGIN { push @INC, '.'; } |
a6ed719b |
650 | eval 'use SubDB ; '; |
651 | main::ok(93, $@ eq "") ; |
652 | my %h ; |
653 | my $X ; |
654 | eval ' |
655 | $X = tie(%h, "SubDB","dbbtree.tmp", O_RDWR|O_CREAT, 0640, $DB_BTREE ); |
656 | ' ; |
657 | |
658 | main::ok(94, $@ eq "") ; |
659 | |
660 | my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ; |
661 | main::ok(95, $@ eq "") ; |
662 | main::ok(96, $ret == 5) ; |
663 | |
664 | my $value = 0; |
665 | $ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ; |
666 | main::ok(97, $@ eq "") ; |
667 | main::ok(98, $ret == 10) ; |
668 | |
669 | $ret = eval ' R_NEXT eq main::R_NEXT ' ; |
670 | main::ok(99, $@ eq "" ) ; |
671 | main::ok(100, $ret == 1) ; |
672 | |
673 | $ret = eval '$X->A_new_method("joe") ' ; |
674 | main::ok(101, $@ eq "") ; |
675 | main::ok(102, $ret eq "[[11]]") ; |
676 | |
fac76ed7 |
677 | undef $X; |
678 | untie(%h); |
a6ed719b |
679 | unlink "SubDB.pm", "dbbtree.tmp" ; |
680 | |
681 | } |
682 | |
9fe6733a |
683 | { |
684 | # DBM Filter tests |
3245f058 |
685 | use warnings ; |
9fe6733a |
686 | use strict ; |
687 | my (%h, $db) ; |
688 | my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
689 | unlink $Dfile; |
690 | |
691 | sub checkOutput |
692 | { |
693 | my($fk, $sk, $fv, $sv) = @_ ; |
694 | return |
695 | $fetch_key eq $fk && $store_key eq $sk && |
696 | $fetch_value eq $fv && $store_value eq $sv && |
697 | $_ eq 'original' ; |
698 | } |
699 | |
700 | ok(103, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
701 | |
702 | $db->filter_fetch_key (sub { $fetch_key = $_ }) ; |
703 | $db->filter_store_key (sub { $store_key = $_ }) ; |
704 | $db->filter_fetch_value (sub { $fetch_value = $_}) ; |
705 | $db->filter_store_value (sub { $store_value = $_ }) ; |
706 | |
707 | $_ = "original" ; |
708 | |
709 | $h{"fred"} = "joe" ; |
710 | # fk sk fv sv |
711 | ok(104, checkOutput( "", "fred", "", "joe")) ; |
712 | |
713 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
714 | ok(105, $h{"fred"} eq "joe"); |
715 | # fk sk fv sv |
716 | ok(106, checkOutput( "", "fred", "joe", "")) ; |
717 | |
718 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
719 | ok(107, $db->FIRSTKEY() eq "fred") ; |
720 | # fk sk fv sv |
721 | ok(108, checkOutput( "fred", "", "", "")) ; |
722 | |
723 | # replace the filters, but remember the previous set |
724 | my ($old_fk) = $db->filter_fetch_key |
725 | (sub { $_ = uc $_ ; $fetch_key = $_ }) ; |
726 | my ($old_sk) = $db->filter_store_key |
727 | (sub { $_ = lc $_ ; $store_key = $_ }) ; |
728 | my ($old_fv) = $db->filter_fetch_value |
729 | (sub { $_ = "[$_]"; $fetch_value = $_ }) ; |
730 | my ($old_sv) = $db->filter_store_value |
731 | (sub { s/o/x/g; $store_value = $_ }) ; |
732 | |
733 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
734 | $h{"Fred"} = "Joe" ; |
735 | # fk sk fv sv |
736 | ok(109, checkOutput( "", "fred", "", "Jxe")) ; |
737 | |
738 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
739 | ok(110, $h{"Fred"} eq "[Jxe]"); |
740 | # fk sk fv sv |
741 | ok(111, checkOutput( "", "fred", "[Jxe]", "")) ; |
742 | |
743 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
744 | ok(112, $db->FIRSTKEY() eq "FRED") ; |
745 | # fk sk fv sv |
746 | ok(113, checkOutput( "FRED", "", "", "")) ; |
747 | |
748 | # put the original filters back |
749 | $db->filter_fetch_key ($old_fk); |
750 | $db->filter_store_key ($old_sk); |
751 | $db->filter_fetch_value ($old_fv); |
752 | $db->filter_store_value ($old_sv); |
753 | |
754 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
755 | $h{"fred"} = "joe" ; |
756 | ok(114, checkOutput( "", "fred", "", "joe")) ; |
757 | |
758 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
759 | ok(115, $h{"fred"} eq "joe"); |
760 | ok(116, checkOutput( "", "fred", "joe", "")) ; |
761 | |
762 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
763 | ok(117, $db->FIRSTKEY() eq "fred") ; |
764 | ok(118, checkOutput( "fred", "", "", "")) ; |
765 | |
766 | # delete the filters |
767 | $db->filter_fetch_key (undef); |
768 | $db->filter_store_key (undef); |
769 | $db->filter_fetch_value (undef); |
770 | $db->filter_store_value (undef); |
771 | |
772 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
773 | $h{"fred"} = "joe" ; |
774 | ok(119, checkOutput( "", "", "", "")) ; |
775 | |
776 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
777 | ok(120, $h{"fred"} eq "joe"); |
778 | ok(121, checkOutput( "", "", "", "")) ; |
779 | |
780 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
781 | ok(122, $db->FIRSTKEY() eq "fred") ; |
782 | ok(123, checkOutput( "", "", "", "")) ; |
783 | |
784 | undef $db ; |
785 | untie %h; |
786 | unlink $Dfile; |
787 | } |
788 | |
789 | { |
790 | # DBM Filter with a closure |
791 | |
3245f058 |
792 | use warnings ; |
9fe6733a |
793 | use strict ; |
794 | my (%h, $db) ; |
795 | |
796 | unlink $Dfile; |
797 | ok(124, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
798 | |
799 | my %result = () ; |
800 | |
801 | sub Closure |
802 | { |
803 | my ($name) = @_ ; |
804 | my $count = 0 ; |
805 | my @kept = () ; |
806 | |
807 | return sub { ++$count ; |
808 | push @kept, $_ ; |
809 | $result{$name} = "$name - $count: [@kept]" ; |
810 | } |
811 | } |
812 | |
813 | $db->filter_store_key(Closure("store key")) ; |
814 | $db->filter_store_value(Closure("store value")) ; |
815 | $db->filter_fetch_key(Closure("fetch key")) ; |
816 | $db->filter_fetch_value(Closure("fetch value")) ; |
817 | |
818 | $_ = "original" ; |
819 | |
820 | $h{"fred"} = "joe" ; |
821 | ok(125, $result{"store key"} eq "store key - 1: [fred]"); |
822 | ok(126, $result{"store value"} eq "store value - 1: [joe]"); |
823 | ok(127, ! defined $result{"fetch key"} ); |
824 | ok(128, ! defined $result{"fetch value"} ); |
825 | ok(129, $_ eq "original") ; |
826 | |
827 | ok(130, $db->FIRSTKEY() eq "fred") ; |
828 | ok(131, $result{"store key"} eq "store key - 1: [fred]"); |
829 | ok(132, $result{"store value"} eq "store value - 1: [joe]"); |
830 | ok(133, $result{"fetch key"} eq "fetch key - 1: [fred]"); |
831 | ok(134, ! defined $result{"fetch value"} ); |
832 | ok(135, $_ eq "original") ; |
833 | |
834 | $h{"jim"} = "john" ; |
835 | ok(136, $result{"store key"} eq "store key - 2: [fred jim]"); |
836 | ok(137, $result{"store value"} eq "store value - 2: [joe john]"); |
837 | ok(138, $result{"fetch key"} eq "fetch key - 1: [fred]"); |
838 | ok(139, ! defined $result{"fetch value"} ); |
839 | ok(140, $_ eq "original") ; |
840 | |
841 | ok(141, $h{"fred"} eq "joe"); |
842 | ok(142, $result{"store key"} eq "store key - 3: [fred jim fred]"); |
843 | ok(143, $result{"store value"} eq "store value - 2: [joe john]"); |
844 | ok(144, $result{"fetch key"} eq "fetch key - 1: [fred]"); |
845 | ok(145, $result{"fetch value"} eq "fetch value - 1: [joe]"); |
846 | ok(146, $_ eq "original") ; |
847 | |
848 | undef $db ; |
849 | untie %h; |
850 | unlink $Dfile; |
851 | } |
852 | |
853 | { |
854 | # DBM Filter recursion detection |
3245f058 |
855 | use warnings ; |
9fe6733a |
856 | use strict ; |
857 | my (%h, $db) ; |
858 | unlink $Dfile; |
859 | |
860 | ok(147, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
861 | |
862 | $db->filter_store_key (sub { $_ = $h{$_} }) ; |
863 | |
864 | eval '$h{1} = 1234' ; |
865 | ok(148, $@ =~ /^recursion detected in filter_store_key at/ ); |
866 | |
867 | undef $db ; |
868 | untie %h; |
869 | unlink $Dfile; |
870 | } |
871 | |
872 | |
2c2d71f5 |
873 | { |
874 | # Examples from the POD |
875 | |
876 | |
877 | my $file = "xyzt" ; |
878 | { |
879 | my $redirect = new Redirect $file ; |
880 | |
881 | # BTREE example 1 |
882 | ### |
883 | |
3245f058 |
884 | use warnings FATAL => qw(all) ; |
2c2d71f5 |
885 | use strict ; |
886 | use DB_File ; |
887 | |
888 | my %h ; |
889 | |
890 | sub Compare |
891 | { |
892 | my ($key1, $key2) = @_ ; |
893 | "\L$key1" cmp "\L$key2" ; |
894 | } |
895 | |
896 | # specify the Perl sub that will do the comparison |
897 | $DB_BTREE->{'compare'} = \&Compare ; |
898 | |
899 | unlink "tree" ; |
900 | tie %h, "DB_File", "tree", O_RDWR|O_CREAT, 0640, $DB_BTREE |
901 | or die "Cannot open file 'tree': $!\n" ; |
902 | |
903 | # Add a key/value pair to the file |
904 | $h{'Wall'} = 'Larry' ; |
905 | $h{'Smith'} = 'John' ; |
906 | $h{'mouse'} = 'mickey' ; |
907 | $h{'duck'} = 'donald' ; |
908 | |
909 | # Delete |
910 | delete $h{"duck"} ; |
911 | |
912 | # Cycle through the keys printing them in order. |
913 | # Note it is not necessary to sort the keys as |
914 | # the btree will have kept them in order automatically. |
915 | foreach (keys %h) |
916 | { print "$_\n" } |
917 | |
918 | untie %h ; |
919 | |
920 | unlink "tree" ; |
921 | } |
922 | |
923 | delete $DB_BTREE->{'compare'} ; |
924 | |
925 | ok(149, docat_del($file) eq <<'EOM') ; |
926 | mouse |
927 | Smith |
928 | Wall |
929 | EOM |
930 | |
931 | { |
932 | my $redirect = new Redirect $file ; |
933 | |
934 | # BTREE example 2 |
935 | ### |
936 | |
3245f058 |
937 | use warnings FATAL => qw(all) ; |
2c2d71f5 |
938 | use strict ; |
939 | use DB_File ; |
940 | |
941 | use vars qw($filename %h ) ; |
942 | |
943 | $filename = "tree" ; |
944 | unlink $filename ; |
945 | |
946 | # Enable duplicate records |
947 | $DB_BTREE->{'flags'} = R_DUP ; |
948 | |
949 | tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
950 | or die "Cannot open $filename: $!\n"; |
951 | |
952 | # Add some key/value pairs to the file |
953 | $h{'Wall'} = 'Larry' ; |
954 | $h{'Wall'} = 'Brick' ; # Note the duplicate key |
955 | $h{'Wall'} = 'Brick' ; # Note the duplicate key and value |
956 | $h{'Smith'} = 'John' ; |
957 | $h{'mouse'} = 'mickey' ; |
958 | |
959 | # iterate through the associative array |
960 | # and print each key/value pair. |
961 | foreach (keys %h) |
962 | { print "$_ -> $h{$_}\n" } |
963 | |
964 | untie %h ; |
965 | |
966 | unlink $filename ; |
967 | } |
968 | |
039d031f |
969 | ok(150, docat_del($file) eq ($db185mode ? <<'EOM' : <<'EOM') ) ; |
2c2d71f5 |
970 | Smith -> John |
971 | Wall -> Brick |
972 | Wall -> Brick |
973 | Wall -> Brick |
974 | mouse -> mickey |
975 | EOM |
976 | Smith -> John |
977 | Wall -> Larry |
978 | Wall -> Larry |
979 | Wall -> Larry |
980 | mouse -> mickey |
981 | EOM |
982 | |
983 | { |
984 | my $redirect = new Redirect $file ; |
985 | |
986 | # BTREE example 3 |
987 | ### |
988 | |
3245f058 |
989 | use warnings FATAL => qw(all) ; |
2c2d71f5 |
990 | use strict ; |
991 | use DB_File ; |
992 | |
993 | use vars qw($filename $x %h $status $key $value) ; |
994 | |
995 | $filename = "tree" ; |
996 | unlink $filename ; |
997 | |
998 | # Enable duplicate records |
999 | $DB_BTREE->{'flags'} = R_DUP ; |
1000 | |
1001 | $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
1002 | or die "Cannot open $filename: $!\n"; |
1003 | |
1004 | # Add some key/value pairs to the file |
1005 | $h{'Wall'} = 'Larry' ; |
1006 | $h{'Wall'} = 'Brick' ; # Note the duplicate key |
1007 | $h{'Wall'} = 'Brick' ; # Note the duplicate key and value |
1008 | $h{'Smith'} = 'John' ; |
1009 | $h{'mouse'} = 'mickey' ; |
1010 | |
1011 | # iterate through the btree using seq |
1012 | # and print each key/value pair. |
1013 | $key = $value = 0 ; |
1014 | for ($status = $x->seq($key, $value, R_FIRST) ; |
1015 | $status == 0 ; |
1016 | $status = $x->seq($key, $value, R_NEXT) ) |
1017 | { print "$key -> $value\n" } |
1018 | |
1019 | |
1020 | undef $x ; |
1021 | untie %h ; |
1022 | } |
1023 | |
039d031f |
1024 | ok(151, docat_del($file) eq ($db185mode == 1 ? <<'EOM' : <<'EOM') ) ; |
2c2d71f5 |
1025 | Smith -> John |
1026 | Wall -> Brick |
1027 | Wall -> Brick |
1028 | Wall -> Larry |
1029 | mouse -> mickey |
1030 | EOM |
1031 | Smith -> John |
1032 | Wall -> Larry |
1033 | Wall -> Brick |
1034 | Wall -> Brick |
1035 | mouse -> mickey |
1036 | EOM |
1037 | |
1038 | |
1039 | { |
1040 | my $redirect = new Redirect $file ; |
1041 | |
1042 | # BTREE example 4 |
1043 | ### |
1044 | |
3245f058 |
1045 | use warnings FATAL => qw(all) ; |
2c2d71f5 |
1046 | use strict ; |
1047 | use DB_File ; |
1048 | |
1049 | use vars qw($filename $x %h ) ; |
1050 | |
1051 | $filename = "tree" ; |
1052 | |
1053 | # Enable duplicate records |
1054 | $DB_BTREE->{'flags'} = R_DUP ; |
1055 | |
1056 | $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
1057 | or die "Cannot open $filename: $!\n"; |
1058 | |
1059 | my $cnt = $x->get_dup("Wall") ; |
1060 | print "Wall occurred $cnt times\n" ; |
1061 | |
1062 | my %hash = $x->get_dup("Wall", 1) ; |
1063 | print "Larry is there\n" if $hash{'Larry'} ; |
1064 | print "There are $hash{'Brick'} Brick Walls\n" ; |
1065 | |
1066 | my @list = sort $x->get_dup("Wall") ; |
1067 | print "Wall => [@list]\n" ; |
1068 | |
1069 | @list = $x->get_dup("Smith") ; |
1070 | print "Smith => [@list]\n" ; |
1071 | |
1072 | @list = $x->get_dup("Dog") ; |
1073 | print "Dog => [@list]\n" ; |
1074 | |
1075 | undef $x ; |
1076 | untie %h ; |
1077 | } |
1078 | |
1079 | ok(152, docat_del($file) eq <<'EOM') ; |
1080 | Wall occurred 3 times |
1081 | Larry is there |
1082 | There are 2 Brick Walls |
1083 | Wall => [Brick Brick Larry] |
1084 | Smith => [John] |
1085 | Dog => [] |
1086 | EOM |
1087 | |
1088 | { |
1089 | my $redirect = new Redirect $file ; |
1090 | |
1091 | # BTREE example 5 |
1092 | ### |
1093 | |
3245f058 |
1094 | use warnings FATAL => qw(all) ; |
2c2d71f5 |
1095 | use strict ; |
1096 | use DB_File ; |
1097 | |
1098 | use vars qw($filename $x %h $found) ; |
1099 | |
1100 | my $filename = "tree" ; |
1101 | |
1102 | # Enable duplicate records |
1103 | $DB_BTREE->{'flags'} = R_DUP ; |
1104 | |
1105 | $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
1106 | or die "Cannot open $filename: $!\n"; |
1107 | |
1108 | $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ; |
1109 | print "Larry Wall is $found there\n" ; |
1110 | |
1111 | $found = ( $x->find_dup("Wall", "Harry") == 0 ? "" : "not") ; |
1112 | print "Harry Wall is $found there\n" ; |
1113 | |
1114 | undef $x ; |
1115 | untie %h ; |
1116 | } |
1117 | |
1118 | ok(153, docat_del($file) eq <<'EOM') ; |
1119 | Larry Wall is there |
1120 | Harry Wall is not there |
1121 | EOM |
1122 | |
1123 | { |
1124 | my $redirect = new Redirect $file ; |
1125 | |
1126 | # BTREE example 6 |
1127 | ### |
1128 | |
3245f058 |
1129 | use warnings FATAL => qw(all) ; |
2c2d71f5 |
1130 | use strict ; |
1131 | use DB_File ; |
1132 | |
1133 | use vars qw($filename $x %h $found) ; |
1134 | |
1135 | my $filename = "tree" ; |
1136 | |
1137 | # Enable duplicate records |
1138 | $DB_BTREE->{'flags'} = R_DUP ; |
1139 | |
1140 | $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
1141 | or die "Cannot open $filename: $!\n"; |
1142 | |
1143 | $x->del_dup("Wall", "Larry") ; |
1144 | |
1145 | $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ; |
1146 | print "Larry Wall is $found there\n" ; |
1147 | |
1148 | undef $x ; |
1149 | untie %h ; |
1150 | |
1151 | unlink $filename ; |
1152 | } |
1153 | |
1154 | ok(154, docat_del($file) eq <<'EOM') ; |
1155 | Larry Wall is not there |
1156 | EOM |
1157 | |
1158 | { |
1159 | my $redirect = new Redirect $file ; |
1160 | |
1161 | # BTREE example 7 |
1162 | ### |
1163 | |
3245f058 |
1164 | use warnings FATAL => qw(all) ; |
2c2d71f5 |
1165 | use strict ; |
1166 | use DB_File ; |
1167 | use Fcntl ; |
1168 | |
1169 | use vars qw($filename $x %h $st $key $value) ; |
1170 | |
1171 | sub match |
1172 | { |
1173 | my $key = shift ; |
1174 | my $value = 0; |
1175 | my $orig_key = $key ; |
1176 | $x->seq($key, $value, R_CURSOR) ; |
1177 | print "$orig_key\t-> $key\t-> $value\n" ; |
1178 | } |
1179 | |
1180 | $filename = "tree" ; |
1181 | unlink $filename ; |
1182 | |
1183 | $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
1184 | or die "Cannot open $filename: $!\n"; |
1185 | |
1186 | # Add some key/value pairs to the file |
1187 | $h{'mouse'} = 'mickey' ; |
1188 | $h{'Wall'} = 'Larry' ; |
1189 | $h{'Walls'} = 'Brick' ; |
1190 | $h{'Smith'} = 'John' ; |
1191 | |
1192 | |
1193 | $key = $value = 0 ; |
1194 | print "IN ORDER\n" ; |
1195 | for ($st = $x->seq($key, $value, R_FIRST) ; |
1196 | $st == 0 ; |
1197 | $st = $x->seq($key, $value, R_NEXT) ) |
1198 | |
1199 | { print "$key -> $value\n" } |
1200 | |
1201 | print "\nPARTIAL MATCH\n" ; |
1202 | |
1203 | match "Wa" ; |
1204 | match "A" ; |
1205 | match "a" ; |
1206 | |
1207 | undef $x ; |
1208 | untie %h ; |
1209 | |
1210 | unlink $filename ; |
1211 | |
1212 | } |
1213 | |
1214 | ok(155, docat_del($file) eq <<'EOM') ; |
1215 | IN ORDER |
1216 | Smith -> John |
1217 | Wall -> Larry |
1218 | Walls -> Brick |
1219 | mouse -> mickey |
1220 | |
1221 | PARTIAL MATCH |
1222 | Wa -> Wall -> Larry |
1223 | A -> Smith -> John |
1224 | a -> mouse -> mickey |
1225 | EOM |
1226 | |
1227 | } |
1228 | |
a62982a8 |
1229 | #{ |
1230 | # # R_SETCURSOR |
1231 | # use strict ; |
1232 | # my (%h, $db) ; |
1233 | # unlink $Dfile; |
1234 | # |
1235 | # ok(156, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
1236 | # |
1237 | # $h{abc} = 33 ; |
1238 | # my $k = "newest" ; |
1239 | # my $v = 44 ; |
1240 | # my $status = $db->put($k, $v, R_SETCURSOR) ; |
1241 | # print "status = [$status]\n" ; |
1242 | # ok(157, $status == 0) ; |
1243 | # $status = $db->del($k, R_CURSOR) ; |
1244 | # print "status = [$status]\n" ; |
1245 | # ok(158, $status == 0) ; |
1246 | # $k = "newest" ; |
1247 | # ok(159, $db->get($k, $v, R_CURSOR)) ; |
1248 | # |
1249 | # ok(160, keys %h == 1) ; |
1250 | # |
1251 | # undef $db ; |
1252 | # untie %h; |
1253 | # unlink $Dfile; |
1254 | #} |
1255 | |
cbc5248d |
1256 | { |
1257 | # Bug ID 20001013.009 |
1258 | # |
1259 | # test that $hash{KEY} = undef doesn't produce the warning |
1260 | # Use of uninitialized value in null operation |
1261 | use warnings ; |
1262 | use strict ; |
1263 | use DB_File ; |
1264 | |
1265 | unlink $Dfile; |
1266 | my %h ; |
1267 | my $a = ""; |
1268 | local $SIG{__WARN__} = sub {$a = $_[0]} ; |
1269 | |
3245f058 |
1270 | tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE |
cbc5248d |
1271 | or die "Can't open file: $!\n" ; |
1272 | $h{ABC} = undef; |
1273 | ok(156, $a eq "") ; |
1274 | untie %h ; |
1275 | unlink $Dfile; |
1276 | } |
1277 | |
3245f058 |
1278 | { |
1279 | # test that %hash = () doesn't produce the warning |
1280 | # Argument "" isn't numeric in entersub |
1281 | use warnings ; |
1282 | use strict ; |
1283 | use DB_File ; |
1284 | |
1285 | unlink $Dfile; |
1286 | my %h ; |
1287 | my $a = ""; |
1288 | local $SIG{__WARN__} = sub {$a = $_[0]} ; |
1289 | |
1290 | tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE |
1291 | or die "Can't open file: $!\n" ; |
1292 | %h = (); ; |
1293 | ok(157, $a eq "") ; |
1294 | untie %h ; |
1295 | unlink $Dfile; |
1296 | } |
1297 | |
a0d0e21e |
1298 | exit ; |