Commit | Line | Data |
f6b705ef |
1 | #!./perl -w |
a0d0e21e |
2 | |
3 | BEGIN { |
55497cff |
4 | @INC = '../lib' if -d '../lib' ; |
a0d0e21e |
5 | require Config; import Config; |
6 | if ($Config{'extensions'} !~ /\bDB_File\b/) { |
7 | print "1..0\n"; |
8 | exit 0; |
9 | } |
10 | } |
11 | |
12 | use DB_File; |
13 | use Fcntl; |
14 | |
a6ed719b |
15 | print "1..102\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 | |
55d68b4a |
41 | $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); |
a6ed719b |
94 | ok(20, ($mode & 0777) == (($^O eq 'os2' || $^O eq 'MSWin32') ? 0666 : 0640) || $^O eq 'amigaos'); |
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 | |
193 | $h{''} = 'bar'; |
f6b705ef |
194 | ok(32, $h{''} eq 'bar' ); |
a0d0e21e |
195 | |
196 | # check cache overflow and numeric keys and contents |
197 | $ok = 1; |
198 | for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; } |
199 | for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; } |
f6b705ef |
200 | ok(33, $ok); |
a0d0e21e |
201 | |
202 | ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, |
203 | $blksize,$blocks) = stat($Dfile); |
f6b705ef |
204 | ok(34, $size > 0 ); |
a0d0e21e |
205 | |
206 | @h{0..200} = 200..400; |
207 | @foo = @h{0..200}; |
f6b705ef |
208 | ok(35, join(':',200..400) eq join(':',@foo) ); |
a0d0e21e |
209 | |
210 | # Now check all the non-tie specific stuff |
211 | |
212 | |
213 | # Check R_NOOVERWRITE flag will make put fail when attempting to overwrite |
214 | # an existing record. |
215 | |
216 | $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ; |
f6b705ef |
217 | ok(36, $status == 1 ); |
a0d0e21e |
218 | |
219 | # check that the value of the key 'x' has not been changed by the |
220 | # previous test |
f6b705ef |
221 | ok(37, $h{'x'} eq 'X' ); |
a0d0e21e |
222 | |
223 | # standard put |
224 | $status = $X->put('key', 'value') ; |
f6b705ef |
225 | ok(38, $status == 0 ); |
a0d0e21e |
226 | |
227 | #check that previous put can be retrieved |
f6b705ef |
228 | $value = 0 ; |
a0d0e21e |
229 | $status = $X->get('key', $value) ; |
f6b705ef |
230 | ok(39, $status == 0 ); |
231 | ok(40, $value eq 'value' ); |
a0d0e21e |
232 | |
233 | # Attempting to delete an existing key should work |
234 | |
235 | $status = $X->del('q') ; |
f6b705ef |
236 | ok(41, $status == 0 ); |
a0d0e21e |
237 | $status = $X->del('') ; |
f6b705ef |
238 | ok(42, $status == 0 ); |
a0d0e21e |
239 | |
240 | # Make sure that the key deleted, cannot be retrieved |
ac1ad7f0 |
241 | ok(43, ! defined $h{'q'}) ; |
242 | ok(44, ! defined $h{''}) ; |
a0d0e21e |
243 | |
244 | undef $X ; |
245 | untie %h ; |
246 | |
f6b705ef |
247 | ok(45, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE )); |
a0d0e21e |
248 | |
249 | # Attempting to delete a non-existant key should fail |
250 | |
251 | $status = $X->del('joe') ; |
f6b705ef |
252 | ok(46, $status == 1 ); |
a0d0e21e |
253 | |
254 | # Check the get interface |
255 | |
256 | # First a non-existing key |
257 | $status = $X->get('aaaa', $value) ; |
f6b705ef |
258 | ok(47, $status == 1 ); |
a0d0e21e |
259 | |
260 | # Next an existing key |
261 | $status = $X->get('a', $value) ; |
f6b705ef |
262 | ok(48, $status == 0 ); |
263 | ok(49, $value eq 'A' ); |
a0d0e21e |
264 | |
265 | # seq |
266 | # ### |
267 | |
268 | # use seq to find an approximate match |
269 | $key = 'ke' ; |
270 | $value = '' ; |
271 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
272 | ok(50, $status == 0 ); |
273 | ok(51, $key eq 'key' ); |
274 | ok(52, $value eq 'value' ); |
a0d0e21e |
275 | |
276 | # seq when the key does not match |
277 | $key = 'zzz' ; |
278 | $value = '' ; |
279 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
280 | ok(53, $status == 1 ); |
a0d0e21e |
281 | |
282 | |
283 | # use seq to set the cursor, then delete the record @ the cursor. |
284 | |
285 | $key = 'x' ; |
286 | $value = '' ; |
287 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
288 | ok(54, $status == 0 ); |
289 | ok(55, $key eq 'x' ); |
290 | ok(56, $value eq 'X' ); |
a0d0e21e |
291 | $status = $X->del(0, R_CURSOR) ; |
f6b705ef |
292 | ok(57, $status == 0 ); |
a0d0e21e |
293 | $status = $X->get('x', $value) ; |
f6b705ef |
294 | ok(58, $status == 1 ); |
a0d0e21e |
295 | |
296 | # ditto, but use put to replace the key/value pair. |
297 | $key = 'y' ; |
298 | $value = '' ; |
299 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
300 | ok(59, $status == 0 ); |
301 | ok(60, $key eq 'y' ); |
302 | ok(61, $value eq 'Y' ); |
a0d0e21e |
303 | |
304 | $key = "replace key" ; |
305 | $value = "replace value" ; |
306 | $status = $X->put($key, $value, R_CURSOR) ; |
f6b705ef |
307 | ok(62, $status == 0 ); |
308 | ok(63, $key eq 'replace key' ); |
309 | ok(64, $value eq 'replace value' ); |
a0d0e21e |
310 | $status = $X->get('y', $value) ; |
f6b705ef |
311 | ok(65, $status == 1 ); |
a0d0e21e |
312 | |
313 | # use seq to walk forwards through a file |
314 | |
315 | $status = $X->seq($key, $value, R_FIRST) ; |
f6b705ef |
316 | ok(66, $status == 0 ); |
a0d0e21e |
317 | $previous = $key ; |
318 | |
319 | $ok = 1 ; |
320 | while (($status = $X->seq($key, $value, R_NEXT)) == 0) |
321 | { |
322 | ($ok = 0), last if ($previous cmp $key) == 1 ; |
323 | } |
324 | |
f6b705ef |
325 | ok(67, $status == 1 ); |
326 | ok(68, $ok == 1 ); |
a0d0e21e |
327 | |
328 | # use seq to walk backwards through a file |
329 | $status = $X->seq($key, $value, R_LAST) ; |
f6b705ef |
330 | ok(69, $status == 0 ); |
a0d0e21e |
331 | $previous = $key ; |
332 | |
333 | $ok = 1 ; |
334 | while (($status = $X->seq($key, $value, R_PREV)) == 0) |
335 | { |
336 | ($ok = 0), last if ($previous cmp $key) == -1 ; |
337 | #print "key = [$key] value = [$value]\n" ; |
338 | } |
339 | |
f6b705ef |
340 | ok(70, $status == 1 ); |
341 | ok(71, $ok == 1 ); |
a0d0e21e |
342 | |
343 | |
344 | # check seq FIRST/LAST |
345 | |
346 | # sync |
347 | # #### |
348 | |
349 | $status = $X->sync ; |
f6b705ef |
350 | ok(72, $status == 0 ); |
a0d0e21e |
351 | |
352 | |
353 | # fd |
354 | # ## |
355 | |
356 | $status = $X->fd ; |
f6b705ef |
357 | ok(73, $status != 0 ); |
a0d0e21e |
358 | |
359 | |
360 | undef $X ; |
361 | untie %h ; |
362 | |
363 | unlink $Dfile; |
364 | |
365 | # Now try an in memory file |
f6b705ef |
366 | ok(74, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE )); |
a0d0e21e |
367 | |
368 | # fd with an in memory file should return failure |
369 | $status = $Y->fd ; |
f6b705ef |
370 | ok(75, $status == -1 ); |
a0d0e21e |
371 | |
55d68b4a |
372 | |
a0d0e21e |
373 | undef $Y ; |
374 | untie %h ; |
375 | |
55d68b4a |
376 | # Duplicate keys |
377 | my $bt = new DB_File::BTREEINFO ; |
378 | $bt->{flags} = R_DUP ; |
f6b705ef |
379 | ok(76, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ; |
55d68b4a |
380 | |
381 | $hh{'Wall'} = 'Larry' ; |
382 | $hh{'Wall'} = 'Stone' ; # Note the duplicate key |
383 | $hh{'Wall'} = 'Brick' ; # Note the duplicate key |
f6b705ef |
384 | $hh{'Wall'} = 'Brick' ; # Note the duplicate key and value |
55d68b4a |
385 | $hh{'Smith'} = 'John' ; |
386 | $hh{'mouse'} = 'mickey' ; |
387 | |
388 | # first work in scalar context |
f6b705ef |
389 | ok(77, scalar $YY->get_dup('Unknown') == 0 ); |
390 | ok(78, scalar $YY->get_dup('Smith') == 1 ); |
391 | ok(79, scalar $YY->get_dup('Wall') == 4 ); |
55d68b4a |
392 | |
393 | # now in list context |
394 | my @unknown = $YY->get_dup('Unknown') ; |
f6b705ef |
395 | ok(80, "@unknown" eq "" ); |
55d68b4a |
396 | |
397 | my @smith = $YY->get_dup('Smith') ; |
f6b705ef |
398 | ok(81, "@smith" eq "John" ); |
55d68b4a |
399 | |
760ac839 |
400 | { |
f6b705ef |
401 | my @wall = $YY->get_dup('Wall') ; |
402 | my %wall ; |
403 | @wall{@wall} = @wall ; |
404 | ok(82, (@wall == 4 && $wall{'Larry'} && $wall{'Stone'} && $wall{'Brick'}) ); |
760ac839 |
405 | } |
55d68b4a |
406 | |
407 | # hash |
408 | my %unknown = $YY->get_dup('Unknown', 1) ; |
f6b705ef |
409 | ok(83, keys %unknown == 0 ); |
55d68b4a |
410 | |
411 | my %smith = $YY->get_dup('Smith', 1) ; |
f6b705ef |
412 | ok(84, keys %smith == 1 && $smith{'John'}) ; |
55d68b4a |
413 | |
f6b705ef |
414 | my %wall = $YY->get_dup('Wall', 1) ; |
415 | ok(85, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1 |
416 | && $wall{'Brick'} == 2); |
55d68b4a |
417 | |
418 | undef $YY ; |
419 | untie %hh ; |
420 | unlink $Dfile; |
421 | |
422 | |
8e07c86e |
423 | # test multiple callbacks |
424 | $Dfile1 = "btree1" ; |
425 | $Dfile2 = "btree2" ; |
426 | $Dfile3 = "btree3" ; |
427 | |
36477c24 |
428 | $dbh1 = new DB_File::BTREEINFO ; |
ac1ad7f0 |
429 | { local $^W = 0 ; |
430 | $dbh1->{compare} = sub { $_[0] <=> $_[1] } ; } |
8e07c86e |
431 | |
36477c24 |
432 | $dbh2 = new DB_File::BTREEINFO ; |
8e07c86e |
433 | $dbh2->{compare} = sub { $_[0] cmp $_[1] } ; |
434 | |
36477c24 |
435 | $dbh3 = new DB_File::BTREEINFO ; |
8e07c86e |
436 | $dbh3->{compare} = sub { length $_[0] <=> length $_[1] } ; |
437 | |
438 | |
f6b705ef |
439 | tie(%h, 'DB_File',$Dfile1, O_RDWR|O_CREAT, 0640, $dbh1 ) ; |
440 | tie(%g, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) ; |
441 | tie(%k, 'DB_File',$Dfile3, O_RDWR|O_CREAT, 0640, $dbh3 ) ; |
8e07c86e |
442 | |
443 | @Keys = qw( 0123 12 -1234 9 987654321 def ) ; |
ac1ad7f0 |
444 | { local $^W = 0 ; |
445 | @srt_1 = sort { $a <=> $b } @Keys ; } |
8e07c86e |
446 | @srt_2 = sort { $a cmp $b } @Keys ; |
447 | @srt_3 = sort { length $a <=> length $b } @Keys ; |
448 | |
449 | foreach (@Keys) { |
ac1ad7f0 |
450 | { local $^W = 0 ; |
451 | $h{$_} = 1 ; } |
8e07c86e |
452 | $g{$_} = 1 ; |
453 | $k{$_} = 1 ; |
454 | } |
455 | |
456 | sub ArrayCompare |
457 | { |
458 | my($a, $b) = @_ ; |
459 | |
460 | return 0 if @$a != @$b ; |
461 | |
462 | foreach (1 .. length @$a) |
463 | { |
464 | return 0 unless $$a[$_] eq $$b[$_] ; |
465 | } |
466 | |
467 | 1 ; |
468 | } |
469 | |
f6b705ef |
470 | ok(86, ArrayCompare (\@srt_1, [keys %h]) ); |
471 | ok(87, ArrayCompare (\@srt_2, [keys %g]) ); |
472 | ok(88, ArrayCompare (\@srt_3, [keys %k]) ); |
8e07c86e |
473 | |
474 | untie %h ; |
475 | untie %g ; |
476 | untie %k ; |
477 | unlink $Dfile1, $Dfile2, $Dfile3 ; |
478 | |
f6b705ef |
479 | # clear |
480 | # ##### |
481 | |
482 | ok(89, tie(%h, 'DB_File', $Dfile1, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
483 | foreach (1 .. 10) |
484 | { $h{$_} = $_ * 100 } |
485 | |
486 | # check that there are 10 elements in the hash |
487 | $i = 0 ; |
488 | while (($key,$value) = each(%h)) { |
489 | $i++; |
490 | } |
491 | ok(90, $i == 10); |
492 | |
493 | # now clear the hash |
494 | %h = () ; |
495 | |
496 | # check it is empty |
497 | $i = 0 ; |
498 | while (($key,$value) = each(%h)) { |
499 | $i++; |
500 | } |
501 | ok(91, $i == 0); |
502 | |
503 | untie %h ; |
504 | unlink $Dfile1 ; |
505 | |
05475680 |
506 | { |
507 | # check that attempting to tie an array to a DB_BTREE will fail |
508 | |
509 | my $filename = "xyz" ; |
510 | my @x ; |
511 | eval { tie @x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE ; } ; |
512 | ok(92, $@ =~ /^DB_File can only tie an associative array to a DB_BTREE database/) ; |
513 | unlink $filename ; |
514 | } |
515 | |
a6ed719b |
516 | |
517 | { |
518 | # sub-class test |
519 | |
520 | package Another ; |
521 | |
522 | use strict ; |
523 | |
524 | open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ; |
525 | print FILE <<'EOM' ; |
526 | |
527 | package SubDB ; |
528 | |
529 | use strict ; |
530 | use vars qw( @ISA @EXPORT) ; |
531 | |
532 | require Exporter ; |
533 | use DB_File; |
534 | @ISA=qw(DB_File); |
535 | @EXPORT = @DB_File::EXPORT ; |
536 | |
537 | sub STORE { |
538 | my $self = shift ; |
539 | my $key = shift ; |
540 | my $value = shift ; |
541 | $self->SUPER::STORE($key, $value * 2) ; |
542 | } |
543 | |
544 | sub FETCH { |
545 | my $self = shift ; |
546 | my $key = shift ; |
547 | $self->SUPER::FETCH($key) - 1 ; |
548 | } |
549 | |
550 | sub put { |
551 | my $self = shift ; |
552 | my $key = shift ; |
553 | my $value = shift ; |
554 | $self->SUPER::put($key, $value * 3) ; |
555 | } |
556 | |
557 | sub get { |
558 | my $self = shift ; |
559 | $self->SUPER::get($_[0], $_[1]) ; |
560 | $_[1] -= 2 ; |
561 | } |
562 | |
563 | sub A_new_method |
564 | { |
565 | my $self = shift ; |
566 | my $key = shift ; |
567 | my $value = $self->FETCH($key) ; |
568 | return "[[$value]]" ; |
569 | } |
570 | |
571 | 1 ; |
572 | EOM |
573 | |
574 | close FILE ; |
575 | |
576 | BEGIN { push @INC, '.'; } |
577 | eval 'use SubDB ; '; |
578 | main::ok(93, $@ eq "") ; |
579 | my %h ; |
580 | my $X ; |
581 | eval ' |
582 | $X = tie(%h, "SubDB","dbbtree.tmp", O_RDWR|O_CREAT, 0640, $DB_BTREE ); |
583 | ' ; |
584 | |
585 | main::ok(94, $@ eq "") ; |
586 | |
587 | my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ; |
588 | main::ok(95, $@ eq "") ; |
589 | main::ok(96, $ret == 5) ; |
590 | |
591 | my $value = 0; |
592 | $ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ; |
593 | main::ok(97, $@ eq "") ; |
594 | main::ok(98, $ret == 10) ; |
595 | |
596 | $ret = eval ' R_NEXT eq main::R_NEXT ' ; |
597 | main::ok(99, $@ eq "" ) ; |
598 | main::ok(100, $ret == 1) ; |
599 | |
600 | $ret = eval '$X->A_new_method("joe") ' ; |
601 | main::ok(101, $@ eq "") ; |
602 | main::ok(102, $ret eq "[[11]]") ; |
603 | |
fac76ed7 |
604 | undef $X; |
605 | untie(%h); |
a6ed719b |
606 | unlink "SubDB.pm", "dbbtree.tmp" ; |
607 | |
608 | } |
609 | |
a0d0e21e |
610 | exit ; |