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