Commit | Line | Data |
f6b705ef |
1 | #!./perl -w |
a0d0e21e |
2 | |
3 | BEGIN { |
93430cb4 |
4 | unshift @INC, '../lib' if -d '../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; |
14 | |
9fe6733a |
15 | print "1..148\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 | |
9fe6733a |
41 | my $Dfile = "dbbtree.tmp"; |
a0d0e21e |
42 | unlink $Dfile; |
43 | |
44 | umask(0); |
45 | |
46 | # Check the interface to BTREEINFO |
47 | |
f6b705ef |
48 | my $dbh = new DB_File::BTREEINFO ; |
3fe9a6f1 |
49 | ok(1, ! defined $dbh->{flags}) ; |
50 | ok(2, ! defined $dbh->{cachesize}) ; |
51 | ok(3, ! defined $dbh->{psize}) ; |
52 | ok(4, ! defined $dbh->{lorder}) ; |
53 | ok(5, ! defined $dbh->{minkeypage}) ; |
54 | ok(6, ! defined $dbh->{maxkeypage}) ; |
55 | ok(7, ! defined $dbh->{compare}) ; |
56 | ok(8, ! defined $dbh->{prefix}) ; |
a0d0e21e |
57 | |
58 | $dbh->{flags} = 3000 ; |
f6b705ef |
59 | ok(9, $dbh->{flags} == 3000) ; |
a0d0e21e |
60 | |
61 | $dbh->{cachesize} = 9000 ; |
f6b705ef |
62 | ok(10, $dbh->{cachesize} == 9000); |
63 | |
a0d0e21e |
64 | $dbh->{psize} = 400 ; |
f6b705ef |
65 | ok(11, $dbh->{psize} == 400) ; |
a0d0e21e |
66 | |
67 | $dbh->{lorder} = 65 ; |
f6b705ef |
68 | ok(12, $dbh->{lorder} == 65) ; |
a0d0e21e |
69 | |
70 | $dbh->{minkeypage} = 123 ; |
f6b705ef |
71 | ok(13, $dbh->{minkeypage} == 123) ; |
a0d0e21e |
72 | |
73 | $dbh->{maxkeypage} = 1234 ; |
f6b705ef |
74 | ok(14, $dbh->{maxkeypage} == 1234 ); |
a0d0e21e |
75 | |
76 | $dbh->{compare} = 1234 ; |
f6b705ef |
77 | ok(15, $dbh->{compare} == 1234) ; |
a0d0e21e |
78 | |
79 | $dbh->{prefix} = 1234 ; |
f6b705ef |
80 | ok(16, $dbh->{prefix} == 1234 ); |
a0d0e21e |
81 | |
82 | # Check that an invalid entry is caught both for store & fetch |
83 | eval '$dbh->{fred} = 1234' ; |
f6b705ef |
84 | ok(17, $@ =~ /^DB_File::BTREEINFO::STORE - Unknown element 'fred' at/ ) ; |
a0d0e21e |
85 | eval '$q = $dbh->{fred}' ; |
f6b705ef |
86 | ok(18, $@ =~ /^DB_File::BTREEINFO::FETCH - Unknown element 'fred' at/ ) ; |
a0d0e21e |
87 | |
88 | # Now check the interface to BTREE |
89 | |
f6b705ef |
90 | ok(19, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE )) ; |
a0d0e21e |
91 | |
92 | ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, |
93 | $blksize,$blocks) = stat($Dfile); |
a9fd575d |
94 | ok(20, ($mode & 0777) == ($^O eq 'os2' ? 0666 : 0640) || $^O eq 'amigaos' || $^O eq 'MSWin32'); |
a0d0e21e |
95 | |
96 | while (($key,$value) = each(%h)) { |
97 | $i++; |
98 | } |
f6b705ef |
99 | ok(21, !$i ) ; |
a0d0e21e |
100 | |
101 | $h{'goner1'} = 'snork'; |
102 | |
103 | $h{'abc'} = 'ABC'; |
f6b705ef |
104 | ok(22, $h{'abc'} eq 'ABC' ); |
105 | ok(23, ! defined $h{'jimmy'} ) ; |
106 | ok(24, ! exists $h{'jimmy'} ) ; |
107 | ok(25, defined $h{'abc'} ) ; |
a0d0e21e |
108 | |
109 | $h{'def'} = 'DEF'; |
110 | $h{'jkl','mno'} = "JKL\034MNO"; |
111 | $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5); |
112 | $h{'a'} = 'A'; |
113 | |
114 | #$h{'b'} = 'B'; |
115 | $X->STORE('b', 'B') ; |
116 | |
117 | $h{'c'} = 'C'; |
118 | |
119 | #$h{'d'} = 'D'; |
120 | $X->put('d', 'D') ; |
121 | |
122 | $h{'e'} = 'E'; |
123 | $h{'f'} = 'F'; |
124 | $h{'g'} = 'X'; |
125 | $h{'h'} = 'H'; |
126 | $h{'i'} = 'I'; |
127 | |
128 | $h{'goner2'} = 'snork'; |
129 | delete $h{'goner2'}; |
130 | |
131 | |
132 | # IMPORTANT - $X must be undefined before the untie otherwise the |
133 | # underlying DB close routine will not get called. |
134 | undef $X ; |
135 | untie(%h); |
136 | |
137 | |
138 | # tie to the same file again |
f6b705ef |
139 | ok(26, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE)) ; |
a0d0e21e |
140 | |
141 | # Modify an entry from the previous tie |
142 | $h{'g'} = 'G'; |
143 | |
144 | $h{'j'} = 'J'; |
145 | $h{'k'} = 'K'; |
146 | $h{'l'} = 'L'; |
147 | $h{'m'} = 'M'; |
148 | $h{'n'} = 'N'; |
149 | $h{'o'} = 'O'; |
150 | $h{'p'} = 'P'; |
151 | $h{'q'} = 'Q'; |
152 | $h{'r'} = 'R'; |
153 | $h{'s'} = 'S'; |
154 | $h{'t'} = 'T'; |
155 | $h{'u'} = 'U'; |
156 | $h{'v'} = 'V'; |
157 | $h{'w'} = 'W'; |
158 | $h{'x'} = 'X'; |
159 | $h{'y'} = 'Y'; |
160 | $h{'z'} = 'Z'; |
161 | |
162 | $h{'goner3'} = 'snork'; |
163 | |
164 | delete $h{'goner1'}; |
165 | $X->DELETE('goner3'); |
166 | |
167 | @keys = keys(%h); |
168 | @values = values(%h); |
169 | |
f6b705ef |
170 | ok(27, $#keys == 29 && $#values == 29) ; |
a0d0e21e |
171 | |
f6b705ef |
172 | $i = 0 ; |
a0d0e21e |
173 | while (($key,$value) = each(%h)) { |
2f52a358 |
174 | if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) { |
a0d0e21e |
175 | $key =~ y/a-z/A-Z/; |
176 | $i++ if $key eq $value; |
177 | } |
178 | } |
179 | |
f6b705ef |
180 | ok(28, $i == 30) ; |
a0d0e21e |
181 | |
55d68b4a |
182 | @keys = ('blurfl', keys(%h), 'dyick'); |
f6b705ef |
183 | ok(29, $#keys == 31) ; |
a0d0e21e |
184 | |
185 | #Check that the keys can be retrieved in order |
55497cff |
186 | my @b = keys %h ; |
187 | my @c = sort lexical @b ; |
188 | ok(30, ArrayCompare(\@b, \@c)) ; |
a0d0e21e |
189 | |
190 | $h{'foo'} = ''; |
f6b705ef |
191 | ok(31, $h{'foo'} eq '' ) ; |
a0d0e21e |
192 | |
a9fd575d |
193 | #$h{''} = 'bar'; |
194 | #ok(32, $h{''} eq 'bar' ); |
195 | ok(32,1) ; |
a0d0e21e |
196 | |
197 | # check cache overflow and numeric keys and contents |
198 | $ok = 1; |
199 | for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; } |
200 | for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; } |
f6b705ef |
201 | ok(33, $ok); |
a0d0e21e |
202 | |
203 | ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, |
204 | $blksize,$blocks) = stat($Dfile); |
f6b705ef |
205 | ok(34, $size > 0 ); |
a0d0e21e |
206 | |
207 | @h{0..200} = 200..400; |
208 | @foo = @h{0..200}; |
f6b705ef |
209 | ok(35, join(':',200..400) eq join(':',@foo) ); |
a0d0e21e |
210 | |
211 | # Now check all the non-tie specific stuff |
212 | |
213 | |
214 | # Check R_NOOVERWRITE flag will make put fail when attempting to overwrite |
215 | # an existing record. |
216 | |
217 | $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ; |
f6b705ef |
218 | ok(36, $status == 1 ); |
a0d0e21e |
219 | |
220 | # check that the value of the key 'x' has not been changed by the |
221 | # previous test |
f6b705ef |
222 | ok(37, $h{'x'} eq 'X' ); |
a0d0e21e |
223 | |
224 | # standard put |
225 | $status = $X->put('key', 'value') ; |
f6b705ef |
226 | ok(38, $status == 0 ); |
a0d0e21e |
227 | |
228 | #check that previous put can be retrieved |
f6b705ef |
229 | $value = 0 ; |
a0d0e21e |
230 | $status = $X->get('key', $value) ; |
f6b705ef |
231 | ok(39, $status == 0 ); |
232 | ok(40, $value eq 'value' ); |
a0d0e21e |
233 | |
234 | # Attempting to delete an existing key should work |
235 | |
236 | $status = $X->del('q') ; |
f6b705ef |
237 | ok(41, $status == 0 ); |
a9fd575d |
238 | #$status = $X->del('') ; |
239 | #ok(42, $status == 0 ); |
240 | ok(42,1) ; |
a0d0e21e |
241 | |
242 | # Make sure that the key deleted, cannot be retrieved |
ac1ad7f0 |
243 | ok(43, ! defined $h{'q'}) ; |
244 | ok(44, ! defined $h{''}) ; |
a0d0e21e |
245 | |
246 | undef $X ; |
247 | untie %h ; |
248 | |
f6b705ef |
249 | ok(45, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE )); |
a0d0e21e |
250 | |
251 | # Attempting to delete a non-existant key should fail |
252 | |
253 | $status = $X->del('joe') ; |
f6b705ef |
254 | ok(46, $status == 1 ); |
a0d0e21e |
255 | |
256 | # Check the get interface |
257 | |
258 | # First a non-existing key |
259 | $status = $X->get('aaaa', $value) ; |
f6b705ef |
260 | ok(47, $status == 1 ); |
a0d0e21e |
261 | |
262 | # Next an existing key |
263 | $status = $X->get('a', $value) ; |
f6b705ef |
264 | ok(48, $status == 0 ); |
265 | ok(49, $value eq 'A' ); |
a0d0e21e |
266 | |
267 | # seq |
268 | # ### |
269 | |
270 | # use seq to find an approximate match |
271 | $key = 'ke' ; |
272 | $value = '' ; |
273 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
274 | ok(50, $status == 0 ); |
275 | ok(51, $key eq 'key' ); |
276 | ok(52, $value eq 'value' ); |
a0d0e21e |
277 | |
278 | # seq when the key does not match |
279 | $key = 'zzz' ; |
280 | $value = '' ; |
281 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
282 | ok(53, $status == 1 ); |
a0d0e21e |
283 | |
284 | |
285 | # use seq to set the cursor, then delete the record @ the cursor. |
286 | |
287 | $key = 'x' ; |
288 | $value = '' ; |
289 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
290 | ok(54, $status == 0 ); |
291 | ok(55, $key eq 'x' ); |
292 | ok(56, $value eq 'X' ); |
a0d0e21e |
293 | $status = $X->del(0, R_CURSOR) ; |
f6b705ef |
294 | ok(57, $status == 0 ); |
a0d0e21e |
295 | $status = $X->get('x', $value) ; |
f6b705ef |
296 | ok(58, $status == 1 ); |
a0d0e21e |
297 | |
298 | # ditto, but use put to replace the key/value pair. |
299 | $key = 'y' ; |
300 | $value = '' ; |
301 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
302 | ok(59, $status == 0 ); |
303 | ok(60, $key eq 'y' ); |
304 | ok(61, $value eq 'Y' ); |
a0d0e21e |
305 | |
306 | $key = "replace key" ; |
307 | $value = "replace value" ; |
308 | $status = $X->put($key, $value, R_CURSOR) ; |
f6b705ef |
309 | ok(62, $status == 0 ); |
310 | ok(63, $key eq 'replace key' ); |
311 | ok(64, $value eq 'replace value' ); |
a0d0e21e |
312 | $status = $X->get('y', $value) ; |
1f70e1ea |
313 | ok(65, 1) ; # hard-wire to always pass. the previous test ($status == 1) |
a9fd575d |
314 | # only worked because of a bug in 1.85/6 |
a0d0e21e |
315 | |
316 | # use seq to walk forwards through a file |
317 | |
318 | $status = $X->seq($key, $value, R_FIRST) ; |
f6b705ef |
319 | ok(66, $status == 0 ); |
a0d0e21e |
320 | $previous = $key ; |
321 | |
322 | $ok = 1 ; |
323 | while (($status = $X->seq($key, $value, R_NEXT)) == 0) |
324 | { |
325 | ($ok = 0), last if ($previous cmp $key) == 1 ; |
326 | } |
327 | |
f6b705ef |
328 | ok(67, $status == 1 ); |
329 | ok(68, $ok == 1 ); |
a0d0e21e |
330 | |
331 | # use seq to walk backwards through a file |
332 | $status = $X->seq($key, $value, R_LAST) ; |
f6b705ef |
333 | ok(69, $status == 0 ); |
a0d0e21e |
334 | $previous = $key ; |
335 | |
336 | $ok = 1 ; |
337 | while (($status = $X->seq($key, $value, R_PREV)) == 0) |
338 | { |
339 | ($ok = 0), last if ($previous cmp $key) == -1 ; |
340 | #print "key = [$key] value = [$value]\n" ; |
341 | } |
342 | |
f6b705ef |
343 | ok(70, $status == 1 ); |
344 | ok(71, $ok == 1 ); |
a0d0e21e |
345 | |
346 | |
347 | # check seq FIRST/LAST |
348 | |
349 | # sync |
350 | # #### |
351 | |
352 | $status = $X->sync ; |
f6b705ef |
353 | ok(72, $status == 0 ); |
a0d0e21e |
354 | |
355 | |
356 | # fd |
357 | # ## |
358 | |
359 | $status = $X->fd ; |
f6b705ef |
360 | ok(73, $status != 0 ); |
a0d0e21e |
361 | |
362 | |
363 | undef $X ; |
364 | untie %h ; |
365 | |
366 | unlink $Dfile; |
367 | |
368 | # Now try an in memory file |
f6b705ef |
369 | ok(74, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE )); |
a0d0e21e |
370 | |
371 | # fd with an in memory file should return failure |
372 | $status = $Y->fd ; |
f6b705ef |
373 | ok(75, $status == -1 ); |
a0d0e21e |
374 | |
55d68b4a |
375 | |
a0d0e21e |
376 | undef $Y ; |
377 | untie %h ; |
378 | |
55d68b4a |
379 | # Duplicate keys |
380 | my $bt = new DB_File::BTREEINFO ; |
381 | $bt->{flags} = R_DUP ; |
f6b705ef |
382 | ok(76, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ; |
55d68b4a |
383 | |
384 | $hh{'Wall'} = 'Larry' ; |
385 | $hh{'Wall'} = 'Stone' ; # Note the duplicate key |
386 | $hh{'Wall'} = 'Brick' ; # Note the duplicate key |
f6b705ef |
387 | $hh{'Wall'} = 'Brick' ; # Note the duplicate key and value |
55d68b4a |
388 | $hh{'Smith'} = 'John' ; |
389 | $hh{'mouse'} = 'mickey' ; |
390 | |
391 | # first work in scalar context |
f6b705ef |
392 | ok(77, scalar $YY->get_dup('Unknown') == 0 ); |
393 | ok(78, scalar $YY->get_dup('Smith') == 1 ); |
394 | ok(79, scalar $YY->get_dup('Wall') == 4 ); |
55d68b4a |
395 | |
396 | # now in list context |
397 | my @unknown = $YY->get_dup('Unknown') ; |
f6b705ef |
398 | ok(80, "@unknown" eq "" ); |
55d68b4a |
399 | |
400 | my @smith = $YY->get_dup('Smith') ; |
f6b705ef |
401 | ok(81, "@smith" eq "John" ); |
55d68b4a |
402 | |
760ac839 |
403 | { |
f6b705ef |
404 | my @wall = $YY->get_dup('Wall') ; |
405 | my %wall ; |
406 | @wall{@wall} = @wall ; |
407 | ok(82, (@wall == 4 && $wall{'Larry'} && $wall{'Stone'} && $wall{'Brick'}) ); |
760ac839 |
408 | } |
55d68b4a |
409 | |
410 | # hash |
411 | my %unknown = $YY->get_dup('Unknown', 1) ; |
f6b705ef |
412 | ok(83, keys %unknown == 0 ); |
55d68b4a |
413 | |
414 | my %smith = $YY->get_dup('Smith', 1) ; |
f6b705ef |
415 | ok(84, keys %smith == 1 && $smith{'John'}) ; |
55d68b4a |
416 | |
f6b705ef |
417 | my %wall = $YY->get_dup('Wall', 1) ; |
418 | ok(85, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1 |
419 | && $wall{'Brick'} == 2); |
55d68b4a |
420 | |
421 | undef $YY ; |
422 | untie %hh ; |
423 | unlink $Dfile; |
424 | |
425 | |
8e07c86e |
426 | # test multiple callbacks |
427 | $Dfile1 = "btree1" ; |
428 | $Dfile2 = "btree2" ; |
429 | $Dfile3 = "btree3" ; |
430 | |
36477c24 |
431 | $dbh1 = new DB_File::BTREEINFO ; |
ac1ad7f0 |
432 | { local $^W = 0 ; |
433 | $dbh1->{compare} = sub { $_[0] <=> $_[1] } ; } |
8e07c86e |
434 | |
36477c24 |
435 | $dbh2 = new DB_File::BTREEINFO ; |
8e07c86e |
436 | $dbh2->{compare} = sub { $_[0] cmp $_[1] } ; |
437 | |
36477c24 |
438 | $dbh3 = new DB_File::BTREEINFO ; |
8e07c86e |
439 | $dbh3->{compare} = sub { length $_[0] <=> length $_[1] } ; |
440 | |
441 | |
f6b705ef |
442 | tie(%h, 'DB_File',$Dfile1, O_RDWR|O_CREAT, 0640, $dbh1 ) ; |
443 | tie(%g, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) ; |
444 | tie(%k, 'DB_File',$Dfile3, O_RDWR|O_CREAT, 0640, $dbh3 ) ; |
8e07c86e |
445 | |
446 | @Keys = qw( 0123 12 -1234 9 987654321 def ) ; |
ac1ad7f0 |
447 | { local $^W = 0 ; |
448 | @srt_1 = sort { $a <=> $b } @Keys ; } |
8e07c86e |
449 | @srt_2 = sort { $a cmp $b } @Keys ; |
450 | @srt_3 = sort { length $a <=> length $b } @Keys ; |
451 | |
452 | foreach (@Keys) { |
ac1ad7f0 |
453 | { local $^W = 0 ; |
454 | $h{$_} = 1 ; } |
8e07c86e |
455 | $g{$_} = 1 ; |
456 | $k{$_} = 1 ; |
457 | } |
458 | |
459 | sub ArrayCompare |
460 | { |
461 | my($a, $b) = @_ ; |
462 | |
463 | return 0 if @$a != @$b ; |
464 | |
465 | foreach (1 .. length @$a) |
466 | { |
467 | return 0 unless $$a[$_] eq $$b[$_] ; |
468 | } |
469 | |
470 | 1 ; |
471 | } |
472 | |
f6b705ef |
473 | ok(86, ArrayCompare (\@srt_1, [keys %h]) ); |
474 | ok(87, ArrayCompare (\@srt_2, [keys %g]) ); |
475 | ok(88, ArrayCompare (\@srt_3, [keys %k]) ); |
8e07c86e |
476 | |
477 | untie %h ; |
478 | untie %g ; |
479 | untie %k ; |
480 | unlink $Dfile1, $Dfile2, $Dfile3 ; |
481 | |
f6b705ef |
482 | # clear |
483 | # ##### |
484 | |
485 | ok(89, tie(%h, 'DB_File', $Dfile1, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
486 | foreach (1 .. 10) |
487 | { $h{$_} = $_ * 100 } |
488 | |
489 | # check that there are 10 elements in the hash |
490 | $i = 0 ; |
491 | while (($key,$value) = each(%h)) { |
492 | $i++; |
493 | } |
494 | ok(90, $i == 10); |
495 | |
496 | # now clear the hash |
497 | %h = () ; |
498 | |
499 | # check it is empty |
500 | $i = 0 ; |
501 | while (($key,$value) = each(%h)) { |
502 | $i++; |
503 | } |
504 | ok(91, $i == 0); |
505 | |
506 | untie %h ; |
507 | unlink $Dfile1 ; |
508 | |
05475680 |
509 | { |
510 | # check that attempting to tie an array to a DB_BTREE will fail |
511 | |
512 | my $filename = "xyz" ; |
513 | my @x ; |
514 | eval { tie @x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE ; } ; |
515 | ok(92, $@ =~ /^DB_File can only tie an associative array to a DB_BTREE database/) ; |
516 | unlink $filename ; |
517 | } |
518 | |
a6ed719b |
519 | { |
520 | # sub-class test |
521 | |
522 | package Another ; |
523 | |
524 | use strict ; |
525 | |
526 | open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ; |
527 | print FILE <<'EOM' ; |
528 | |
529 | package SubDB ; |
530 | |
531 | use strict ; |
532 | use vars qw( @ISA @EXPORT) ; |
533 | |
534 | require Exporter ; |
535 | use DB_File; |
536 | @ISA=qw(DB_File); |
537 | @EXPORT = @DB_File::EXPORT ; |
538 | |
539 | sub STORE { |
540 | my $self = shift ; |
541 | my $key = shift ; |
542 | my $value = shift ; |
543 | $self->SUPER::STORE($key, $value * 2) ; |
544 | } |
545 | |
546 | sub FETCH { |
547 | my $self = shift ; |
548 | my $key = shift ; |
549 | $self->SUPER::FETCH($key) - 1 ; |
550 | } |
551 | |
552 | sub put { |
553 | my $self = shift ; |
554 | my $key = shift ; |
555 | my $value = shift ; |
556 | $self->SUPER::put($key, $value * 3) ; |
557 | } |
558 | |
559 | sub get { |
560 | my $self = shift ; |
561 | $self->SUPER::get($_[0], $_[1]) ; |
562 | $_[1] -= 2 ; |
563 | } |
564 | |
565 | sub A_new_method |
566 | { |
567 | my $self = shift ; |
568 | my $key = shift ; |
569 | my $value = $self->FETCH($key) ; |
570 | return "[[$value]]" ; |
571 | } |
572 | |
573 | 1 ; |
574 | EOM |
575 | |
576 | close FILE ; |
577 | |
a9fd575d |
578 | BEGIN { push @INC, '.'; } |
a6ed719b |
579 | eval 'use SubDB ; '; |
580 | main::ok(93, $@ eq "") ; |
581 | my %h ; |
582 | my $X ; |
583 | eval ' |
584 | $X = tie(%h, "SubDB","dbbtree.tmp", O_RDWR|O_CREAT, 0640, $DB_BTREE ); |
585 | ' ; |
586 | |
587 | main::ok(94, $@ eq "") ; |
588 | |
589 | my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ; |
590 | main::ok(95, $@ eq "") ; |
591 | main::ok(96, $ret == 5) ; |
592 | |
593 | my $value = 0; |
594 | $ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ; |
595 | main::ok(97, $@ eq "") ; |
596 | main::ok(98, $ret == 10) ; |
597 | |
598 | $ret = eval ' R_NEXT eq main::R_NEXT ' ; |
599 | main::ok(99, $@ eq "" ) ; |
600 | main::ok(100, $ret == 1) ; |
601 | |
602 | $ret = eval '$X->A_new_method("joe") ' ; |
603 | main::ok(101, $@ eq "") ; |
604 | main::ok(102, $ret eq "[[11]]") ; |
605 | |
fac76ed7 |
606 | undef $X; |
607 | untie(%h); |
a6ed719b |
608 | unlink "SubDB.pm", "dbbtree.tmp" ; |
609 | |
610 | } |
611 | |
9fe6733a |
612 | { |
613 | # DBM Filter tests |
614 | use strict ; |
615 | my (%h, $db) ; |
616 | my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
617 | unlink $Dfile; |
618 | |
619 | sub checkOutput |
620 | { |
621 | my($fk, $sk, $fv, $sv) = @_ ; |
622 | return |
623 | $fetch_key eq $fk && $store_key eq $sk && |
624 | $fetch_value eq $fv && $store_value eq $sv && |
625 | $_ eq 'original' ; |
626 | } |
627 | |
628 | ok(103, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
629 | |
630 | $db->filter_fetch_key (sub { $fetch_key = $_ }) ; |
631 | $db->filter_store_key (sub { $store_key = $_ }) ; |
632 | $db->filter_fetch_value (sub { $fetch_value = $_}) ; |
633 | $db->filter_store_value (sub { $store_value = $_ }) ; |
634 | |
635 | $_ = "original" ; |
636 | |
637 | $h{"fred"} = "joe" ; |
638 | # fk sk fv sv |
639 | ok(104, checkOutput( "", "fred", "", "joe")) ; |
640 | |
641 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
642 | ok(105, $h{"fred"} eq "joe"); |
643 | # fk sk fv sv |
644 | ok(106, checkOutput( "", "fred", "joe", "")) ; |
645 | |
646 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
647 | ok(107, $db->FIRSTKEY() eq "fred") ; |
648 | # fk sk fv sv |
649 | ok(108, checkOutput( "fred", "", "", "")) ; |
650 | |
651 | # replace the filters, but remember the previous set |
652 | my ($old_fk) = $db->filter_fetch_key |
653 | (sub { $_ = uc $_ ; $fetch_key = $_ }) ; |
654 | my ($old_sk) = $db->filter_store_key |
655 | (sub { $_ = lc $_ ; $store_key = $_ }) ; |
656 | my ($old_fv) = $db->filter_fetch_value |
657 | (sub { $_ = "[$_]"; $fetch_value = $_ }) ; |
658 | my ($old_sv) = $db->filter_store_value |
659 | (sub { s/o/x/g; $store_value = $_ }) ; |
660 | |
661 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
662 | $h{"Fred"} = "Joe" ; |
663 | # fk sk fv sv |
664 | ok(109, checkOutput( "", "fred", "", "Jxe")) ; |
665 | |
666 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
667 | ok(110, $h{"Fred"} eq "[Jxe]"); |
668 | # fk sk fv sv |
669 | ok(111, checkOutput( "", "fred", "[Jxe]", "")) ; |
670 | |
671 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
672 | ok(112, $db->FIRSTKEY() eq "FRED") ; |
673 | # fk sk fv sv |
674 | ok(113, checkOutput( "FRED", "", "", "")) ; |
675 | |
676 | # put the original filters back |
677 | $db->filter_fetch_key ($old_fk); |
678 | $db->filter_store_key ($old_sk); |
679 | $db->filter_fetch_value ($old_fv); |
680 | $db->filter_store_value ($old_sv); |
681 | |
682 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
683 | $h{"fred"} = "joe" ; |
684 | ok(114, checkOutput( "", "fred", "", "joe")) ; |
685 | |
686 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
687 | ok(115, $h{"fred"} eq "joe"); |
688 | ok(116, checkOutput( "", "fred", "joe", "")) ; |
689 | |
690 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
691 | ok(117, $db->FIRSTKEY() eq "fred") ; |
692 | ok(118, checkOutput( "fred", "", "", "")) ; |
693 | |
694 | # delete the filters |
695 | $db->filter_fetch_key (undef); |
696 | $db->filter_store_key (undef); |
697 | $db->filter_fetch_value (undef); |
698 | $db->filter_store_value (undef); |
699 | |
700 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
701 | $h{"fred"} = "joe" ; |
702 | ok(119, checkOutput( "", "", "", "")) ; |
703 | |
704 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
705 | ok(120, $h{"fred"} eq "joe"); |
706 | ok(121, checkOutput( "", "", "", "")) ; |
707 | |
708 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
709 | ok(122, $db->FIRSTKEY() eq "fred") ; |
710 | ok(123, checkOutput( "", "", "", "")) ; |
711 | |
712 | undef $db ; |
713 | untie %h; |
714 | unlink $Dfile; |
715 | } |
716 | |
717 | { |
718 | # DBM Filter with a closure |
719 | |
720 | use strict ; |
721 | my (%h, $db) ; |
722 | |
723 | unlink $Dfile; |
724 | ok(124, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
725 | |
726 | my %result = () ; |
727 | |
728 | sub Closure |
729 | { |
730 | my ($name) = @_ ; |
731 | my $count = 0 ; |
732 | my @kept = () ; |
733 | |
734 | return sub { ++$count ; |
735 | push @kept, $_ ; |
736 | $result{$name} = "$name - $count: [@kept]" ; |
737 | } |
738 | } |
739 | |
740 | $db->filter_store_key(Closure("store key")) ; |
741 | $db->filter_store_value(Closure("store value")) ; |
742 | $db->filter_fetch_key(Closure("fetch key")) ; |
743 | $db->filter_fetch_value(Closure("fetch value")) ; |
744 | |
745 | $_ = "original" ; |
746 | |
747 | $h{"fred"} = "joe" ; |
748 | ok(125, $result{"store key"} eq "store key - 1: [fred]"); |
749 | ok(126, $result{"store value"} eq "store value - 1: [joe]"); |
750 | ok(127, ! defined $result{"fetch key"} ); |
751 | ok(128, ! defined $result{"fetch value"} ); |
752 | ok(129, $_ eq "original") ; |
753 | |
754 | ok(130, $db->FIRSTKEY() eq "fred") ; |
755 | ok(131, $result{"store key"} eq "store key - 1: [fred]"); |
756 | ok(132, $result{"store value"} eq "store value - 1: [joe]"); |
757 | ok(133, $result{"fetch key"} eq "fetch key - 1: [fred]"); |
758 | ok(134, ! defined $result{"fetch value"} ); |
759 | ok(135, $_ eq "original") ; |
760 | |
761 | $h{"jim"} = "john" ; |
762 | ok(136, $result{"store key"} eq "store key - 2: [fred jim]"); |
763 | ok(137, $result{"store value"} eq "store value - 2: [joe john]"); |
764 | ok(138, $result{"fetch key"} eq "fetch key - 1: [fred]"); |
765 | ok(139, ! defined $result{"fetch value"} ); |
766 | ok(140, $_ eq "original") ; |
767 | |
768 | ok(141, $h{"fred"} eq "joe"); |
769 | ok(142, $result{"store key"} eq "store key - 3: [fred jim fred]"); |
770 | ok(143, $result{"store value"} eq "store value - 2: [joe john]"); |
771 | ok(144, $result{"fetch key"} eq "fetch key - 1: [fred]"); |
772 | ok(145, $result{"fetch value"} eq "fetch value - 1: [joe]"); |
773 | ok(146, $_ eq "original") ; |
774 | |
775 | undef $db ; |
776 | untie %h; |
777 | unlink $Dfile; |
778 | } |
779 | |
780 | { |
781 | # DBM Filter recursion detection |
782 | use strict ; |
783 | my (%h, $db) ; |
784 | unlink $Dfile; |
785 | |
786 | ok(147, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
787 | |
788 | $db->filter_store_key (sub { $_ = $h{$_} }) ; |
789 | |
790 | eval '$h{1} = 1234' ; |
791 | ok(148, $@ =~ /^recursion detected in filter_store_key at/ ); |
792 | |
793 | undef $db ; |
794 | untie %h; |
795 | unlink $Dfile; |
796 | } |
797 | |
798 | |
a0d0e21e |
799 | exit ; |