Debian Codegraph @

lang/perl/BerkeleyDB/t/hash.t

raw
1 #!./perl -w
2
3 # ID: %I%, %G%
4
5 use strict ;
6
7 use lib 't' ;
8 use BerkeleyDB;
9 use util ;
10 use Test::More;
11
12 plan tests => 216;
13
14 my $Dfile = "dbhash.tmp";
15 my $Dfile2 = "dbhash2.tmp";
16 my $Dfile3 = "dbhash3.tmp";
17 unlink $Dfile;
18
19 umask(0) ;
20
21
22 # Check for invalid parameters
23 {
24 # Check for invalid parameters
25 my $db ;
26 eval ' $db = new BerkeleyDB::Hash -Stupid => 3 ; ' ;
27 ok $@ =~ /unknown key value\(s\) Stupid/ ;
28
29 eval ' $db = new BerkeleyDB::Hash -Bad => 2, -Mode => 0345, -Stupid => 3; ' ;
30 ok $@ =~ /unknown key value\(s\) (Bad,? |Stupid,? ){2}/ ;
31
32 eval ' $db = new BerkeleyDB::Hash -Env => 2 ' ;
33 ok $@ =~ /^Env not of type BerkeleyDB::Env/ ;
34
35 eval ' $db = new BerkeleyDB::Hash -Txn => "fred" ' ;
36 ok $@ =~ /^Txn not of type BerkeleyDB::Txn/ ;
37
38 my $obj = bless [], "main" ;
39 eval ' $db = new BerkeleyDB::Hash -Env => $obj ' ;
40 ok $@ =~ /^Env not of type BerkeleyDB::Env/ ;
41 }
42
43 # Now check the interface to HASH
44
45 {
46 my $lex = new LexFile $Dfile ;
47
48 ok my $db = new BerkeleyDB::Hash -Filename => $Dfile,
49 -Flags => DB_CREATE ;
50
51 # Add a k/v pair
52 my $value ;
53 my $status ;
54 is $db->Env, undef;
55 ok $db->db_put("some key", "some value") == 0 ;
56 ok $db->status() == 0 ;
57 ok $db->db_get("some key", $value) == 0 ;
58 ok $value eq "some value" ;
59 ok $db->db_put("key", "value") == 0 ;
60 ok $db->db_get("key", $value) == 0 ;
61 ok $value eq "value" ;
62 ok $db->db_del("some key") == 0 ;
63 ok (($status = $db->db_get("some key", $value)) == DB_NOTFOUND) ;
64 ok $status =~ $DB_errors{'DB_NOTFOUND'} ;
65 ok $db->status() == DB_NOTFOUND ;
66 ok $db->status() =~ $DB_errors{'DB_NOTFOUND'};
67
68 ok $db->db_sync() == 0 ;
69
70 # Check NOOVERWRITE will make put fail when attempting to overwrite
71 # an existing record.
72
73 ok $db->db_put( 'key', 'x', DB_NOOVERWRITE) == DB_KEYEXIST ;
74 ok $db->status() =~ $DB_errors{'DB_KEYEXIST'};
75 ok $db->status() == DB_KEYEXIST ;
76
77 # check that the value of the key has not been changed by the
78 # previous test
79 ok $db->db_get("key", $value) == 0 ;
80 ok $value eq "value" ;
81
82 # test DB_GET_BOTH
83 my ($k, $v) = ("key", "value") ;
84 ok $db->db_get($k, $v, DB_GET_BOTH) == 0 ;
85
86 ($k, $v) = ("key", "fred") ;
87 ok $db->db_get($k, $v, DB_GET_BOTH) == DB_NOTFOUND ;
88
89 ($k, $v) = ("another", "value") ;
90 ok $db->db_get($k, $v, DB_GET_BOTH) == DB_NOTFOUND ;
91
92
93 }
94
95 {
96 # Check simple env works with a hash.
97 my $lex = new LexFile $Dfile ;
98
99 my $home = "./fred" ;
100 ok my $lexD = new LexDir($home);
101
102 ok my $env = new BerkeleyDB::Env -Flags => DB_CREATE| DB_INIT_MPOOL,@StdErrFile,
103 -Home => $home ;
104 ok my $db = new BerkeleyDB::Hash -Filename => $Dfile,
105 -Env => $env,
106 -Flags => DB_CREATE ;
107
108 isa_ok $db->Env, 'BerkeleyDB::Env';
109
110 # Add a k/v pair
111 my $value ;
112 ok $db->db_put("some key", "some value") == 0 ;
113 ok $db->db_get("some key", $value) == 0 ;
114 ok $value eq "some value" ;
115 undef $db ;
116 undef $env ;
117 }
118
119
120 {
121 # override default hash
122 my $lex = new LexFile $Dfile ;
123 my $value ;
124 $::count = 0 ;
125 ok my $db = new BerkeleyDB::Hash -Filename => $Dfile,
126 -Hash => sub { ++$::count ; length $_[0] },
127 -Flags => DB_CREATE ;
128
129 ok $db->db_put("some key", "some value") == 0 ;
130 ok $db->db_get("some key", $value) == 0 ;
131 ok $value eq "some value" ;
132 ok $::count > 0 ;
133
134 }
135
136 {
137 # cursors
138
139 my $lex = new LexFile $Dfile ;
140 my %hash ;
141 my ($k, $v) ;
142 ok my $db = new BerkeleyDB::Hash -Filename => $Dfile,
143 -Flags => DB_CREATE ;
144
145 # create some data
146 my %data = (
147 "red" => 2,
148 "green" => "house",
149 "blue" => "sea",
150 ) ;
151
152 my $ret = 0 ;
153 while (($k, $v) = each %data) {
154 $ret += $db->db_put($k, $v) ;
155 }
156 ok $ret == 0 ;
157
158 # create the cursor
159 ok my $cursor = $db->db_cursor() ;
160
161 $k = $v = "" ;
162 my %copy = %data ;
163 my $extras = 0 ;
164 # sequence forwards
165 while ($cursor->c_get($k, $v, DB_NEXT) == 0) {
166 if ( $copy{$k} eq $v )
167 { delete $copy{$k} }
168 else
169 { ++ $extras }
170 }
171 ok $cursor->status() == DB_NOTFOUND ;
172 ok $cursor->status() =~ $DB_errors{'DB_NOTFOUND'} ;
173 ok keys %copy == 0 ;
174 ok $extras == 0 ;
175
176 # sequence backwards
177 %copy = %data ;
178 $extras = 0 ;
179 my $status ;
180 for ( $status = $cursor->c_get($k, $v, DB_LAST) ;
181 $status == 0 ;
182 $status = $cursor->c_get($k, $v, DB_PREV)) {
183 if ( $copy{$k} eq $v )
184 { delete $copy{$k} }
185 else
186 { ++ $extras }
187 }
188 ok $status == DB_NOTFOUND ;
189 ok $status =~ $DB_errors{'DB_NOTFOUND'} ;
190 ok $cursor->status() == $status ;
191 ok $cursor->status() eq $status ;
192 ok keys %copy == 0 ;
193 ok $extras == 0 ;
194
195 ($k, $v) = ("green", "house") ;
196 ok $cursor->c_get($k, $v, DB_GET_BOTH) == 0 ;
197
198 ($k, $v) = ("green", "door") ;
199 ok $cursor->c_get($k, $v, DB_GET_BOTH) == DB_NOTFOUND ;
200
201 ($k, $v) = ("black", "house") ;
202 ok $cursor->c_get($k, $v, DB_GET_BOTH) == DB_NOTFOUND ;
203
204 }
205
206 {
207 # Tied Hash interface
208
209 my $lex = new LexFile $Dfile ;
210 my %hash ;
211 ok tie %hash, 'BerkeleyDB::Hash', -Filename => $Dfile,
212 -Flags => DB_CREATE ;
213
214 # check "each" with an empty database
215 my $count = 0 ;
216 while (my ($k, $v) = each %hash) {
217 ++ $count ;
218 }
219 ok ((tied %hash)->status() == DB_NOTFOUND) ;
220 ok $count == 0 ;
221
222 # Add a k/v pair
223 my $value ;
224 $hash{"some key"} = "some value";
225 ok ((tied %hash)->status() == 0) ;
226 ok $hash{"some key"} eq "some value";
227 ok defined $hash{"some key"} ;
228 ok ((tied %hash)->status() == 0) ;
229 ok exists $hash{"some key"} ;
230 ok !defined $hash{"jimmy"} ;
231 ok ((tied %hash)->status() == DB_NOTFOUND) ;
232 ok !exists $hash{"jimmy"} ;
233 ok ((tied %hash)->status() == DB_NOTFOUND) ;
234
235 delete $hash{"some key"} ;
236 ok ((tied %hash)->status() == 0) ;
237 ok ! defined $hash{"some key"} ;
238 ok ((tied %hash)->status() == DB_NOTFOUND) ;
239 ok ! exists $hash{"some key"} ;
240 ok ((tied %hash)->status() == DB_NOTFOUND) ;
241
242 $hash{1} = 2 ;
243 $hash{10} = 20 ;
244 $hash{1000} = 2000 ;
245
246 my ($keys, $values) = (0,0);
247 $count = 0 ;
248 while (my ($k, $v) = each %hash) {
249 $keys += $k ;
250 $values += $v ;
251 ++ $count ;
252 }
253 ok $count == 3 ;
254 ok $keys == 1011 ;
255 ok $values == 2022 ;
256
257 # now clear the hash
258 %hash = () ;
259 ok keys %hash == 0 ;
260
261 untie %hash ;
262 }
263
264 {
265 # in-memory file
266
267 my $lex = new LexFile $Dfile ;
268 my %hash ;
269 my $fd ;
270 my $value ;
271 ok my $db = tie %hash, 'BerkeleyDB::Hash'
272 or die $BerkeleyDB::Error;
273
274 ok $db->db_put("some key", "some value") == 0 ;
275 ok $db->db_get("some key", $value) == 0 ;
276 ok $value eq "some value" ;
277
278 undef $db ;
279 untie %hash ;
280 }
281
282 {
283 # partial
284 # check works via API
285
286 my $lex = new LexFile $Dfile ;
287 my %hash ;
288 my $value ;
289 ok my $db = tie %hash, 'BerkeleyDB::Hash', -Filename => $Dfile,
290 -Flags => DB_CREATE ;
291
292 # create some data
293 my %data = (
294 "red" => "boat",
295 "green" => "house",
296 "blue" => "sea",
297 ) ;
298
299 my $ret = 0 ;
300 while (my ($k, $v) = each %data) {
301 $ret += $db->db_put($k, $v) ;
302 }
303 ok $ret == 0 ;
304
305
306 # do a partial get
307 my($pon, $off, $len) = $db->partial_set(0,2) ;
308 ok $pon == 0 && $off == 0 && $len == 0 ;
309 ok (( $db->db_get("red", $value) == 0) && $value eq "bo") ;
310 ok (( $db->db_get("green", $value) == 0) && $value eq "ho") ;
311 ok (( $db->db_get("blue", $value) == 0) && $value eq "se") ;
312
313 # do a partial get, off end of data
314 ($pon, $off, $len) = $db->partial_set(3,2) ;
315 ok $pon ;
316 ok $off == 0 ;
317 ok $len == 2 ;
318 ok $db->db_get("red", $value) == 0 && $value eq "t" ;
319 ok $db->db_get("green", $value) == 0 && $value eq "se" ;
320 ok $db->db_get("blue", $value) == 0 && $value eq "" ;
321
322 # switch of partial mode
323 ($pon, $off, $len) = $db->partial_clear() ;
324 ok $pon ;
325 ok $off == 3 ;
326 ok $len == 2 ;
327 ok $db->db_get("red", $value) == 0 && $value eq "boat" ;
328 ok $db->db_get("green", $value) == 0 && $value eq "house" ;
329 ok $db->db_get("blue", $value) == 0 && $value eq "sea" ;
330
331 # now partial put
332 ($pon, $off, $len) = $db->partial_set(0,2) ;
333 ok ! $pon ;
334 ok $off == 0 ;
335 ok $len == 0 ;
336 ok $db->db_put("red", "") == 0 ;
337 ok $db->db_put("green", "AB") == 0 ;
338 ok $db->db_put("blue", "XYZ") == 0 ;
339 ok $db->db_put("new", "KLM") == 0 ;
340
341 $db->partial_clear() ;
342 ok $db->db_get("red", $value) == 0 && $value eq "at" ;
343 ok $db->db_get("green", $value) == 0 && $value eq "ABuse" ;
344 ok $db->db_get("blue", $value) == 0 && $value eq "XYZa" ;
345 ok $db->db_get("new", $value) == 0 && $value eq "KLM" ;
346
347 # now partial put
348 $db->partial_set(3,2) ;
349 ok $db->db_put("red", "PPP") == 0 ;
350 ok $db->db_put("green", "Q") == 0 ;
351 ok $db->db_put("blue", "XYZ") == 0 ;
352 ok $db->db_put("new", "--") == 0 ;
353
354 ($pon, $off, $len) = $db->partial_clear() ;
355 ok $pon ;
356 ok $off == 3 ;
357 ok $len == 2 ;
358 ok $db->db_get("red", $value) == 0 && $value eq "at\0PPP" ;
359 ok $db->db_get("green", $value) == 0 && $value eq "ABuQ" ;
360 ok $db->db_get("blue", $value) == 0 && $value eq "XYZXYZ" ;
361 ok $db->db_get("new", $value) == 0 && $value eq "KLM--" ;
362 }
363
364 {
365 # partial
366 # check works via tied hash
367
368 my $lex = new LexFile $Dfile ;
369 my %hash ;
370 my $value ;
371 ok my $db = tie %hash, 'BerkeleyDB::Hash', -Filename => $Dfile,
372 -Flags => DB_CREATE ;
373
374 # create some data
375 my %data = (
376 "red" => "boat",
377 "green" => "house",
378 "blue" => "sea",
379 ) ;
380
381 while (my ($k, $v) = each %data) {
382 $hash{$k} = $v ;
383 }
384
385
386 # do a partial get
387 $db->partial_set(0,2) ;
388 ok $hash{"red"} eq "bo" ;
389 ok $hash{"green"} eq "ho" ;
390 ok $hash{"blue"} eq "se" ;
391
392 # do a partial get, off end of data
393 $db->partial_set(3,2) ;
394 ok $hash{"red"} eq "t" ;
395 ok $hash{"green"} eq "se" ;
396 ok $hash{"blue"} eq "" ;
397
398 # switch of partial mode
399 $db->partial_clear() ;
400 ok $hash{"red"} eq "boat" ;
401 ok $hash{"green"} eq "house" ;
402 ok $hash{"blue"} eq "sea" ;
403
404 # now partial put
405 $db->partial_set(0,2) ;
406 ok $hash{"red"} = "" ;
407 ok $hash{"green"} = "AB" ;
408 ok $hash{"blue"} = "XYZ" ;
409 ok $hash{"new"} = "KLM" ;
410
411 $db->partial_clear() ;
412 ok $hash{"red"} eq "at" ;
413 ok $hash{"green"} eq "ABuse" ;
414 ok $hash{"blue"} eq "XYZa" ;
415 ok $hash{"new"} eq "KLM" ;
416
417 # now partial put
418 $db->partial_set(3,2) ;
419 ok $hash{"red"} = "PPP" ;
420 ok $hash{"green"} = "Q" ;
421 ok $hash{"blue"} = "XYZ" ;
422 ok $hash{"new"} = "TU" ;
423
424 $db->partial_clear() ;
425 ok $hash{"red"} eq "at\0PPP" ;
426 ok $hash{"green"} eq "ABuQ" ;
427 ok $hash{"blue"} eq "XYZXYZ" ;
428 ok $hash{"new"} eq "KLMTU" ;
429 }
430
431 {
432 # transaction
433
434 my $lex = new LexFile $Dfile ;
435 my %hash ;
436 my $value ;
437
438 my $home = "./fred" ;
439 ok my $lexD = new LexDir($home);
440 ok my $env = new BerkeleyDB::Env -Home => $home,@StdErrFile,
441 -Flags => DB_CREATE|DB_INIT_TXN|
442 DB_INIT_MPOOL|DB_INIT_LOCK ;
443 ok my $txn = $env->txn_begin() ;
444 ok my $db1 = tie %hash, 'BerkeleyDB::Hash', -Filename => $Dfile,
445 -Flags => DB_CREATE ,
446 -Env => $env,
447 -Txn => $txn ;
448
449 isa_ok((tied %hash)->Env, 'BerkeleyDB::Env');
450 (tied %hash)->Env->errPrefix("abc");
451 is((tied %hash)->Env->errPrefix("abc"), 'abc');
452
453 ok $txn->txn_commit() == 0 ;
454 ok $txn = $env->txn_begin() ;
455 $db1->Txn($txn);
456 # create some data
457 my %data = (
458 "red" => "boat",
459 "green" => "house",
460 "blue" => "sea",
461 ) ;
462
463 my $ret = 0 ;
464 while (my ($k, $v) = each %data) {
465 $ret += $db1->db_put($k, $v) ;
466 }
467 ok $ret == 0 ;
468
469 # should be able to see all the records
470
471 ok my $cursor = $db1->db_cursor() ;
472 my ($k, $v) = ("", "") ;
473 my $count = 0 ;
474 # sequence forwards
475 while ($cursor->c_get($k, $v, DB_NEXT) == 0) {
476 ++ $count ;
477 }
478 ok $count == 3 ;
479 undef $cursor ;
480
481 # now abort the transaction
482 ok $txn->txn_abort() == 0 ;
483
484 # there shouldn't be any records in the database
485 $count = 0 ;
486 # sequence forwards
487 ok $cursor = $db1->db_cursor() ;
488 while ($cursor->c_get($k, $v, DB_NEXT) == 0) {
489 ++ $count ;
490 }
491 ok $count == 0 ;
492
493 undef $txn ;
494 undef $cursor ;
495 undef $db1 ;
496 undef $env ;
497 untie %hash ;
498 }
499
500
501 {
502 # DB_DUP
503
504 my $lex = new LexFile $Dfile ;
505 my %hash ;
506 ok my $db = tie %hash, 'BerkeleyDB::Hash', -Filename => $Dfile,
507 -Property => DB_DUP,
508 -Flags => DB_CREATE ;
509
510 $hash{'Wall'} = 'Larry' ;
511 $hash{'Wall'} = 'Stone' ;
512 $hash{'Smith'} = 'John' ;
513 $hash{'Wall'} = 'Brick' ;
514 $hash{'Wall'} = 'Brick' ;
515 $hash{'mouse'} = 'mickey' ;
516
517 ok keys %hash == 6 ;
518
519 # create a cursor
520 ok my $cursor = $db->db_cursor() ;
521
522 my $key = "Wall" ;
523 my $value ;
524 ok $cursor->c_get($key, $value, DB_SET) == 0 ;
525 ok $key eq "Wall" && $value eq "Larry" ;
526 ok $cursor->c_get($key, $value, DB_NEXT) == 0 ;
527 ok $key eq "Wall" && $value eq "Stone" ;
528 ok $cursor->c_get($key, $value, DB_NEXT) == 0 ;
529 ok $key eq "Wall" && $value eq "Brick" ;
530 ok $cursor->c_get($key, $value, DB_NEXT) == 0 ;
531 ok $key eq "Wall" && $value eq "Brick" ;
532
533 #my $ref = $db->db_stat() ;
534 #ok $ref->{bt_flags} | DB_DUP ;
535
536 # test DB_DUP_NEXT
537 my ($k, $v) = ("Wall", "") ;
538 ok $cursor->c_get($k, $v, DB_SET) == 0 ;
539 ok $k eq "Wall" && $v eq "Larry" ;
540 ok $cursor->c_get($k, $v, DB_NEXT_DUP) == 0 ;
541 ok $k eq "Wall" && $v eq "Stone" ;
542 ok $cursor->c_get($k, $v, DB_NEXT_DUP) == 0 ;
543 ok $k eq "Wall" && $v eq "Brick" ;
544 ok $cursor->c_get($k, $v, DB_NEXT_DUP) == 0 ;
545 ok $k eq "Wall" && $v eq "Brick" ;
546 ok $cursor->c_get($k, $v, DB_NEXT_DUP) == DB_NOTFOUND ;
547
548
549 undef $db ;
550 undef $cursor ;
551 untie %hash ;
552
553 }
554
555 {
556 # DB_DUP & DupCompare
557 my $lex = new LexFile $Dfile, $Dfile2;
558 my ($key, $value) ;
559 my (%h, %g) ;
560 my @Keys = qw( 0123 9 12 -1234 9 987654321 9 def ) ;
561 my @Values = qw( 1 11 3 dd x abc 2 0 ) ;
562
563 ok tie %h, "BerkeleyDB::Hash", -Filename => $Dfile,
564 -DupCompare => sub { $_[0] cmp $_[1] },
565 -Property => DB_DUP|DB_DUPSORT,
566 -Flags => DB_CREATE ;
567
568 ok tie %g, 'BerkeleyDB::Hash', -Filename => $Dfile2,
569 -DupCompare => sub { $_[0] <=> $_[1] },
570 -Property => DB_DUP|DB_DUPSORT,
571 -Flags => DB_CREATE ;
572
573 foreach (@Keys) {
574 local $^W = 0 ;
575 my $value = shift @Values ;
576 $h{$_} = $value ;
577 $g{$_} = $value ;
578 }
579
580 ok my $cursor = (tied %h)->db_cursor() ;
581 $key = 9 ; $value = "";
582 ok $cursor->c_get($key, $value, DB_SET) == 0 ;
583 ok $key == 9 && $value eq 11 ;
584 ok $cursor->c_get($key, $value, DB_NEXT) == 0 ;
585 ok $key == 9 && $value == 2 ;
586 ok $cursor->c_get($key, $value, DB_NEXT) == 0 ;
587 ok $key == 9 && $value eq "x" ;
588
589 $cursor = (tied %g)->db_cursor() ;
590 $key = 9 ;
591 ok $cursor->c_get($key, $value, DB_SET) == 0 ;
592 ok $key == 9 && $value eq "x" ;
593 ok $cursor->c_get($key, $value, DB_NEXT) == 0 ;
594 ok $key == 9 && $value == 2 ;
595 ok $cursor->c_get($key, $value, DB_NEXT) == 0 ;
596 ok $key == 9 && $value == 11 ;
597
598
599 }
600
601 {
602 # get_dup etc
603 my $lex = new LexFile $Dfile;
604 my %hh ;
605
606 ok my $YY = tie %hh, "BerkeleyDB::Hash", -Filename => $Dfile,
607 -DupCompare => sub { $_[0] cmp $_[1] },
608 -Property => DB_DUP,
609 -Flags => DB_CREATE ;
610
611 $hh{'Wall'} = 'Larry' ;
612 $hh{'Wall'} = 'Stone' ; # Note the duplicate key
613 $hh{'Wall'} = 'Brick' ; # Note the duplicate key
614 $hh{'Smith'} = 'John' ;
615 $hh{'mouse'} = 'mickey' ;
616
617 # first work in scalar context
618 ok scalar $YY->get_dup('Unknown') == 0 ;
619 ok scalar $YY->get_dup('Smith') == 1 ;
620 ok scalar $YY->get_dup('Wall') == 3 ;
621
622 # now in list context
623 my @unknown = $YY->get_dup('Unknown') ;
624 ok "@unknown" eq "" ;
625
626 my @smith = $YY->get_dup('Smith') ;
627 ok "@smith" eq "John" ;
628
629 {
630 my @wall = $YY->get_dup('Wall') ;
631 my %wall ;
632 @wall{@wall} = @wall ;
633 ok (@wall == 3 && $wall{'Larry'}
634 && $wall{'Stone'} && $wall{'Brick'});
635 }
636
637 # hash
638 my %unknown = $YY->get_dup('Unknown', 1) ;
639 ok keys %unknown == 0 ;
640
641 my %smith = $YY->get_dup('Smith', 1) ;
642 ok keys %smith == 1 && $smith{'John'} ;
643
644 my %wall = $YY->get_dup('Wall', 1) ;
645 ok keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1
646 && $wall{'Brick'} == 1 ;
647
648 undef $YY ;
649 untie %hh ;
650
651 }
652
653 {
654 # sub-class test
655
656 package Another ;
657
658 use strict ;
659
660 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
661 print FILE <<'EOM' ;
662
663 package SubDB ;
664
665 use strict ;
666 use vars qw( @ISA @EXPORT) ;
667
668 require Exporter ;
669 use BerkeleyDB;
670 @ISA=qw(BerkeleyDB BerkeleyDB::Hash);
671 @EXPORT = @BerkeleyDB::EXPORT ;
672
673 sub db_put {
674 my $self = shift ;
675 my $key = shift ;
676 my $value = shift ;
677 $self->SUPER::db_put($key, $value * 3) ;
678 }
679
680 sub db_get {
681 my $self = shift ;
682 $self->SUPER::db_get($_[0], $_[1]) ;
683 $_[1] -= 2 ;
684 }
685
686 sub A_new_method
687 {
688 my $self = shift ;
689 my $key = shift ;
690 my $value = $self->FETCH($key) ;
691 return "[[$value]]" ;
692 }
693
694 1 ;
695 EOM
696
697 close FILE ;
698
699 use Test::More;
700 BEGIN { push @INC, '.'; }
701 eval 'use SubDB ; ';
702 ok $@ eq "" ;
703 my %h ;
704 my $X ;
705 eval '
706 $X = tie(%h, "SubDB", -Filename => "dbhash.tmp",
707 -Flags => DB_CREATE,
708 -Mode => 0640 );
709 ' ;
710
711 ok $@ eq "" ;
712
713 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
714 ok $@ eq "" ;
715 ok $ret == 7 ;
716
717 my $value = 0;
718 $ret = eval '$X->db_put("joe", 4) ; $X->db_get("joe", $value) ; return $value' ;
719 ok $@ eq "" ;
720 ok $ret == 10 ;
721
722 $ret = eval ' DB_NEXT eq main::DB_NEXT ' ;
723 ok $@ eq "" ;
724 ok $ret == 1 ;
725
726 $ret = eval '$X->A_new_method("joe") ' ;
727 ok $@ eq "" ;
728 ok $ret eq "[[10]]" ;
729
730 unlink "SubDB.pm", "dbhash.tmp" ;
731
732 }