db-hash.t
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:16k
- #!./perl
- use warnings ;
- use strict ;
- BEGIN {
- unless(grep /blib/, @INC) {
- chdir 't' if -d 't';
- @INC = '../lib' if -d '../lib';
- }
- }
-
- use Config;
-
- BEGIN {
- if(-d "lib" && -f "TEST") {
- if ($Config{'extensions'} !~ /bDB_Fileb/ ) {
- print "1..111n";
- exit 0;
- }
- }
- }
- use DB_File;
- use Fcntl;
- print "1..111n";
- 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_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 $null_keys_allowed = ($DB_File::db_ver < 2.004010
- || $DB_File::db_ver >= 3.1 );
- unlink $Dfile;
- umask(0);
- # Check the interface to HASHINFO
- my $dbh = new DB_File::HASHINFO ;
- ok(1, ! defined $dbh->{bsize}) ;
- ok(2, ! defined $dbh->{ffactor}) ;
- ok(3, ! defined $dbh->{nelem}) ;
- ok(4, ! defined $dbh->{cachesize}) ;
- ok(5, ! defined $dbh->{hash}) ;
- ok(6, ! defined $dbh->{lorder}) ;
- $dbh->{bsize} = 3000 ;
- ok(7, $dbh->{bsize} == 3000 );
- $dbh->{ffactor} = 9000 ;
- ok(8, $dbh->{ffactor} == 9000 );
- $dbh->{nelem} = 400 ;
- ok(9, $dbh->{nelem} == 400 );
- $dbh->{cachesize} = 65 ;
- ok(10, $dbh->{cachesize} == 65 );
- $dbh->{hash} = "abc" ;
- ok(11, $dbh->{hash} eq "abc" );
- $dbh->{lorder} = 1234 ;
- ok(12, $dbh->{lorder} == 1234 );
- # Check that an invalid entry is caught both for store & fetch
- eval '$dbh->{fred} = 1234' ;
- ok(13, $@ =~ /^DB_File::HASHINFO::STORE - Unknown element 'fred' at/ );
- eval 'my $q = $dbh->{fred}' ;
- ok(14, $@ =~ /^DB_File::HASHINFO::FETCH - Unknown element 'fred' at/ );
- # Now check the interface to HASH
- my ($X, %h);
- ok(15, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
- my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
- $blksize,$blocks) = stat($Dfile);
- ok(16, ($mode & 0777) == ($^O eq 'os2' ? 0666 : 0640) || $^O eq 'amigaos' || $^O eq 'MSWin32');
- my ($key, $value, $i);
- while (($key,$value) = each(%h)) {
- $i++;
- }
- ok(17, !$i );
- $h{'goner1'} = 'snork';
- $h{'abc'} = 'ABC';
- ok(18, $h{'abc'} eq 'ABC' );
- ok(19, !defined $h{'jimmy'} );
- ok(20, !exists $h{'jimmy'} );
- ok(21, exists $h{'abc'} );
- $h{'def'} = 'DEF';
- $h{'jkl','mno'} = "JKL 34MNO";
- $h{'a',2,3,4,5} = join("