1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
#!./perl -w
# ID: %I%, %G%
use strict ;
use lib 't' ;
use BerkeleyDB;
use util ;
use Test::More;
plan tests => 50;
my $Dfile = "dbhash.tmp";
unlink $Dfile;
umask(0) ;
# Check for invalid parameters
{
# Check for invalid parameters
my $db ;
eval ' $db = new BerkeleyDB::Unknown -Stupid => 3 ; ' ;
ok $@ =~ /unknown key value\(s\) Stupid/ ;
eval ' $db = new BerkeleyDB::Unknown -Bad => 2, -Mode => 0345, -Stupid => 3; ' ;
ok $@ =~ /unknown key value\(s\) (Bad,? |Stupid,? ){2}/ ;
eval ' $db = new BerkeleyDB::Unknown -Env => 2 ' ;
ok $@ =~ /^Env not of type BerkeleyDB::Env/ ;
eval ' $db = new BerkeleyDB::Unknown -Txn => "fred" ' ;
ok $@ =~ /^Txn not of type BerkeleyDB::Txn/ ;
my $obj = bless [], "main" ;
eval ' $db = new BerkeleyDB::Unknown -Env => $obj ' ;
ok $@ =~ /^Env not of type BerkeleyDB::Env/ ;
}
# check the interface to a rubbish database
{
# first an empty file
my $lex = new LexFile $Dfile ;
ok writeFile($Dfile, "") ;
ok ! (new BerkeleyDB::Unknown -Filename => $Dfile);
# now a non-database file
writeFile($Dfile, "\x2af6") ;
ok ! (new BerkeleyDB::Unknown -Filename => $Dfile);
}
# check the interface to a Hash database
{
my $lex = new LexFile $Dfile ;
# create a hash database
ok my $db = new BerkeleyDB::Hash -Filename => $Dfile,
-Flags => DB_CREATE ;
# Add a few k/v pairs
my $value ;
my $status ;
ok $db->db_put("some key", "some value") == 0
or diag "Cannot db_put: [$!][$BerkeleyDB::Error]\n" ;
ok $db->db_put("key", "value") == 0 ;
# close the database
undef $db ;
# now open it with Unknown
ok $db = new BerkeleyDB::Unknown -Filename => $Dfile;
ok $db->type() == DB_HASH ;
ok $db->db_get("some key", $value) == 0 ;
ok $value eq "some value" ;
ok $db->db_get("key", $value) == 0 ;
ok $value eq "value" ;
my @array ;
eval { $db->Tie(\@array)} ;
ok $@ =~ /^Tie needs a reference to a hash/ ;
my %hash ;
$db->Tie(\%hash) ;
ok $hash{"some key"} eq "some value" ;
}
# check the interface to a Btree database
{
my $lex = new LexFile $Dfile ;
# create a hash database
ok my $db = new BerkeleyDB::Btree -Filename => $Dfile,
-Flags => DB_CREATE ;
# Add a few k/v pairs
my $value ;
my $status ;
ok $db->db_put("some key", "some value") == 0 ;
ok $db->db_put("key", "value") == 0 ;
# close the database
undef $db ;
# now open it with Unknown
# create a hash database
ok $db = new BerkeleyDB::Unknown -Filename => $Dfile;
ok $db->type() == DB_BTREE ;
ok $db->db_get("some key", $value) == 0 ;
ok $value eq "some value" ;
ok $db->db_get("key", $value) == 0 ;
ok $value eq "value" ;
my @array ;
eval { $db->Tie(\@array)} ;
ok $@ =~ /^Tie needs a reference to a hash/ ;
my %hash ;
$db->Tie(\%hash) ;
ok $hash{"some key"} eq "some value" ;
}
# check the interface to a Recno database
if(1)
{
my $lex = new LexFile $Dfile ;
# create a recno database
ok my $db = new BerkeleyDB::Recno -Filename => $Dfile,
-Flags => DB_CREATE ;
# Add a few k/v pairs
my $value ;
my $status ;
ok $db->db_put(0, "some value") == 0 ;
ok $db->db_put(1, "value") == 0 ;
# close the database
undef $db ;
# now open it with Unknown
# create a hash database
ok $db = new BerkeleyDB::Unknown -Filename => $Dfile;
ok $db->type() == DB_RECNO ;
ok $db->db_get(0, $value) == 0 ;
ok $value eq "some value" ;
ok $db->db_get(1, $value) == 0 ;
ok $value eq "value" ;
my %hash ;
eval { $db->Tie(\%hash)} ;
ok $@ =~ /^Tie needs a reference to an array/ ;
my @array ;
$db->Tie(\@array) ;
ok $array[1] eq "value" ;
}
# check the interface to a Heap database
SKIP:
{
skip "Heap support not available", 9
unless BerkeleyDB::has_heap() ;
my $lex = new LexFile $Dfile ;
# create a hash database
ok my $db = new BerkeleyDB::Heap -Filename => $Dfile,
-Flags => DB_CREATE ;
# Add a few k/v pairs
my $key1 = "" ;
my $key2 ;
my $value ;
my $status ;
ok $db->db_put($key1, "some value", DB_APPEND) == 0 ;
ok $db->db_put($key2, "value", DB_APPEND) == 0 ;
# close the database
undef $db ;
# now open it with Unknown
# create a hash database
ok $db = new BerkeleyDB::Unknown -Filename => $Dfile;
ok $db->type() == DB_HEAP ;
ok $db->db_get($key1, $value) == 0
or diag "Cannot db_get: [$!][$BerkeleyDB::Error]\n" ;
ok $value eq "some value" ;
ok $db->db_get($key2, $value) == 0 ;
ok $value eq "value" ;
}
# check i/f to text
|