examples.t.T
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:9k
- #!./perl -w
- use strict ;
- BEGIN {
- unless(grep /blib/, @INC) {
- chdir 't' if -d 't';
- @INC = '../lib' if -d '../lib';
- }
- }
- use BerkeleyDB;
- use File::Path qw(rmtree);
- print "1..7n";
- my $FA = 0 ;
- {
- sub try::TIEARRAY { bless [], "try" }
- sub try::FETCHSIZE { $FA = 1 }
- $FA = 0 ;
- my @a ;
- tie @a, 'try' ;
- my $a = @a ;
- }
- {
- package LexFile ;
- sub new
- {
- my $self = shift ;
- unlink @_ ;
- bless [ @_ ], $self ;
- }
- sub DESTROY
- {
- my $self = shift ;
- unlink @{ $self } ;
- }
- }
- sub ok
- {
- my $no = shift ;
- my $result = shift ;
-
- print "not " unless $result ;
- print "ok $non" ;
- }
- {
- package Redirect ;
- use Symbol ;
- sub new
- {
- my $class = shift ;
- my $filename = shift ;
- my $fh = gensym ;
- open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
- my $real_stdout = select($fh) ;
- return bless [$fh, $real_stdout ] ;
- }
- sub DESTROY
- {
- my $self = shift ;
- close $self->[0] ;
- select($self->[1]) ;
- }
- }
- sub docat
- {
- my $file = shift;
- local $/ = undef;
- open(CAT,$file) || die "Cannot open $file:$!";
- my $result = <CAT> || "" ;
- close(CAT);
- return $result;
- }
- sub docat_del
- {
- my $file = shift;
- local $/ = undef;
- open(CAT,$file) || die "Cannot open $file: $!";
- my $result = <CAT> || "" ;
- close(CAT);
- unlink $file ;
- return $result;
- }
- my $Dfile = "dbhash.tmp";
- my $Dfile2 = "dbhash2.tmp";
- my $Dfile3 = "dbhash3.tmp";
- unlink $Dfile;
- umask(0) ;
- my $redirect = "xyzt" ;
- {
- my $x = $BerkeleyDB::Error;
- my $redirect = "xyzt" ;
- {
- my $redirectObj = new Redirect $redirect ;
- ## BEGIN simpleHash
- use strict ;
- use BerkeleyDB ;
- use vars qw( %h $k $v ) ;
-
- my $filename = "fruit" ;
- unlink $filename ;
- tie %h, "BerkeleyDB::Hash",
- -Filename => $filename,
- -Flags => DB_CREATE
- or die "Cannot open file $filename: $! $BerkeleyDB::Errorn" ;
- # Add a few key/value pairs to the file
- $h{"apple"} = "red" ;
- $h{"orange"} = "orange" ;
- $h{"banana"} = "yellow" ;
- $h{"tomato"} = "red" ;
-
- # Check for existence of a key
- print "Banana Existsnn" if $h{"banana"} ;
-
- # Delete a key/value pair.
- delete $h{"apple"} ;
-
- # print the contents of the file
- while (($k, $v) = each %h)
- { print "$k -> $vn" }
-
- untie %h ;
- ## END simpleHash
- unlink $filename ;
- }
- #print "[" . docat($redirect) . "]" ;
- ok(1, docat_del($redirect) eq <<'EOM') ;
- Banana Exists
- orange -> orange
- tomato -> red
- banana -> yellow
- EOM
- }
- {
- my $redirect = "xyzt" ;
- {
- my $redirectObj = new Redirect $redirect ;
- ## BEGIN simpleHash2
- use strict ;
- use BerkeleyDB ;
-
- my $filename = "fruit" ;
- unlink $filename ;
- my $db = new BerkeleyDB::Hash
- -Filename => $filename,
- -Flags => DB_CREATE
- or die "Cannot open file $filename: $! $BerkeleyDB::Errorn" ;
- # Add a few key/value pairs to the file
- $db->db_put("apple", "red") ;
- $db->db_put("orange", "orange") ;
- $db->db_put("banana", "yellow") ;
- $db->db_put("tomato", "red") ;
-
- # Check for existence of a key
- print "Banana Existsnn" if $db->db_get("banana", $v) == 0;
-
- # Delete a key/value pair.
- $db->db_del("apple") ;
-
- # print the contents of the file
- my ($k, $v) = ("", "") ;
- my $cursor = $db->db_cursor() ;
- while ($cursor->c_get($k, $v, DB_NEXT) == 0)
- { print "$k -> $vn" }
-
- undef $cursor ;
- undef $db ;
- ## END simpleHash2
- unlink $filename ;
- }
- #print "[" . docat($redirect) . "]" ;
- ok(2, docat_del($redirect) eq <<'EOM') ;
- Banana Exists
- orange -> orange
- tomato -> red
- banana -> yellow
- EOM
- }
- {
- my $redirect = "xyzt" ;
- {
- my $redirectObj = new Redirect $redirect ;
- ## BEGIN btreeSimple
- use strict ;
- use BerkeleyDB ;
- my $filename = "tree" ;
- unlink $filename ;
- my %h ;
- tie %h, 'BerkeleyDB::Btree',
- -Filename => $filename,
- -Flags => DB_CREATE
- or die "Cannot open $filename: $!n" ;
- # Add a key/value pair to the file
- $h{'Wall'} = 'Larry' ;
- $h{'Smith'} = 'John' ;
- $h{'mouse'} = 'mickey' ;
- $h{'duck'} = 'donald' ;
- # Delete
- delete $h{"duck"} ;
- # Cycle through the keys printing them in order.
- # Note it is not necessary to sort the keys as
- # the btree will have kept them in order automatically.
- foreach (keys %h)
- { print "$_n" }
- untie %h ;
- ## END btreeSimple
- unlink $filename ;
- }
- #print "[" . docat($redirect) . "]n" ;
- ok(3, docat_del($redirect) eq <<'EOM') ;
- Smith
- Wall
- mouse
- EOM
- }
- {
- my $redirect = "xyzt" ;
- {
- my $redirectObj = new Redirect $redirect ;
- ## BEGIN btreeSortOrder
- use strict ;
- use BerkeleyDB ;
- my $filename = "tree" ;
- unlink $filename ;
- my %h ;
- tie %h, 'BerkeleyDB::Btree',
- -Filename => $filename,
- -Flags => DB_CREATE,
- -Compare => sub { lc $_[0] cmp lc $_[1] }
- or die "Cannot open $filename: $!n" ;
- # Add a key/value pair to the file
- $h{'Wall'} = 'Larry' ;
- $h{'Smith'} = 'John' ;
- $h{'mouse'} = 'mickey' ;
- $h{'duck'} = 'donald' ;
- # Delete
- delete $h{"duck"} ;
- # Cycle through the keys printing them in order.
- # Note it is not necessary to sort the keys as
- # the btree will have kept them in order automatically.
- foreach (keys %h)
- { print "$_n" }
- untie %h ;
- ## END btreeSortOrder
- unlink $filename ;
- }
- #print "[" . docat($redirect) . "]n" ;
- ok(4, docat_del($redirect) eq <<'EOM') ;
- mouse
- Smith
- Wall
- EOM
- }
- {
- my $redirect = "xyzt" ;
- {
- my $redirectObj = new Redirect $redirect ;
- ## BEGIN nullFilter
- use strict ;
- use BerkeleyDB ;
- my %hash ;
- my $filename = "filt.db" ;
- unlink $filename ;
- my $db = tie %hash, 'BerkeleyDB::Hash',
- -Filename => $filename,
- -Flags => DB_CREATE
- or die "Cannot open $filename: $!n" ;
- # Install DBM Filters
- $db->filter_fetch_key ( sub { s/ $// } ) ;
- $db->filter_store_key ( sub { $_ .= "