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
|
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'
BEGIN {print "1..35\n";}
END {print "not ok 1\n" unless $loaded;}
use CDB_File;
$loaded = 1;
print "ok 1\n";
# Test that attempt to tie to nonexist file fails.
tie %h, CDB_File, 'nonesuch.cdb' and print 'not ';
print "ok 2\n";
# Test that attempt to read incorrect file fails.
open OUT, '> bad.cdb'; close OUT;
tie %h, CDB_File, 'bad.cdb' or print 'not ';
print "ok 3\n";
eval { print $h{'one'} };
print 'not ' unless $@ =~ /^Read of CDB_File failed:/;
print "ok 4\n";
untie %h;
unlink 'bad.cdb';
# Test that file can be created.
%a = qw(one Hello two Goodbye);
eval { CDB_File::create %a, 'good.cdb', 'good.tmp' or print 'not ' };
print "$@ not " if $@;
print "ok 5\n";
($d, $i, $mode, $l, $u, $g, $r, $s, $a, $m, $c, $b, $n) = stat 'good.cdb';
# Hmm... really should use S_IRUSR and friends here.
$mode & 0222 and print 'not ';
print "ok 6\n";
# Test that good file works.
tie %h, CDB_File, 'good.cdb' or print 'not ';
print "ok 7\n";
$t = tied %h;
$t->FETCH('one') eq 'Hello' or print 'not ';
print "ok 8\n";
$h{'one'} eq 'Hello' or print 'not ';
print "ok 9\n";
defined $h{'1'} and print 'not ';
print "ok 10\n";
exists $h{'two'} or print 'not ';
print "ok 11\n";
exists $h{'three'} and print 'not ';
print "ok 12\n";
@h = sort keys %h;
@h == 2 and $h[0] eq 'one' and $h[1] eq 'two' or print 'not ';
print "ok 13\n";
eval { $h{'four'} = 'foo' };
print 'not ' unless $@ =~ /Modification of a CDB_File attempted/;
print "ok 14\n";
eval { delete $h{'five'} };
print 'not ' unless $@ =~ /Modification of a CDB_File attempted/;
print "ok 15\n";
unlink 'good.cdb';
# Test empty file.
undef %a;
eval { CDB_File::create %a, 'empty.cdb', 'empty.tmp' or print 'not ' };
print "$@ not " if $@;
print "ok 16\n";
tie %h, CDB_File, 'empty.cdb' or print 'not ';
print "ok 17\n";
keys %h == 0 or print 'not ';
print "ok 18\n";
unlink 'empty.cdb';
# Test failing new.
new CDB_File '..', '.' and print 'not ';
print "ok 19\n";
# Test file with repeated keys.
$tmp = 'repeat.tmp';
$cdbm = new CDB_File 'repeat.cdb', $tmp or print 'not ';
print "ok 20\n";
$cdbm->insert('dog', 'perro');
$cdbm->insert('cat', 'gato');
$cdbm->insert('cat', 'chat');
$cdbm->insert('dog', 'chien');
$cdbm->insert('rabbit', 'conejo');
$tmp = 'ERROR!'; # Test that name was stashed correctly.
$cdbm->finish;
$t = tie %h, CDB_File, 'repeat.cdb' or print 'not ';
print "ok 21\n";
# Test that NEXTKEY can't be used immediately after TIEHASH.
eval { $t->NEXTKEY('dog') };
print 'not ' unless $@ =~ /^Use CDB_File::FIRSTKEY before CDB_File::NEXTKEY/;
print "ok 22\n";
@k = keys %h;
@v = values %h;
$k[0] eq 'dog' and $k[1] eq 'cat' and $k[2] eq 'cat' and $k[3] eq 'dog' and $k[4] eq 'rabbit' and
$v[0] eq 'perro' and $v[1] eq 'gato' and $v[2] eq 'chat' and $v[3] eq 'chien' and $v[4] eq 'conejo' or
print 'not ';
print "ok 23\n";
@k = ();
@v = ();
while (($k, $v) = each %h) {
push @k, $k;
push @v, $v;
}
$k[0] eq 'dog' and $k[1] eq 'cat' and $k[2] eq 'cat' and $k[3] eq 'dog' and $k[4] eq 'rabbit' and
$v[0] eq 'perro' and $v[1] eq 'gato' and $v[2] eq 'chat' and $v[3] eq 'chien' and $v[4] eq 'conejo' or
print 'not ';
print "ok 24\n";
$v = $t->multi_get('cat');
@$v == 2 and $$v[0] eq 'gato' and $$v[1] eq 'chat' or print 'not ';
print "ok 25\n";
$v = $t->multi_get('dog');
@$v == 2 and $$v[0] eq 'perro' and $$v[1] eq 'chien' or print 'not ';
print "ok 26\n";
$v = $t->multi_get('rabbit');
@$v == 1 and $$v[0] eq 'conejo' or print 'not ';
print "ok 27\n";
$v = $t->multi_get('foo');
defined @$v and print 'not ';
print "ok 28\n";
# Test undefined keys.
{
local $SIG{__WARN__} = sub { $warned = 1 if $_[0] =~ /^Use of uninitialized value/ };
local $^W = 1;
$warned = 0;
$x = undef;
not defined $h{$x} and $warned or print 'not ';
print "ok 29\n";
$warned = 0;
not exists $h{$x} and $warned or print 'not ';
print "ok 30\n";
$warned = 0;
$v = $t->multi_get('rabbit') and not $warned or print 'not ';
print "ok 31\n";
}
# Check that object is readonly.
eval { $$t = 'foo' };
$@ =~ /^Modification of a read-only value/ and $h{'cat'} eq 'gato' or print 'not ';
print "ok 32\n";
unlink 'repeat.cdb';
# Regression test - dumps core in 0.6.
%a = ('one', '');
CDB_File::create %a, 'good.cdb', 'good.tmp' or print "not ";
tie %h, CDB_File, 'good.cdb' or print "not ";
print "not " if $h{'zero'} or $h{'one'};
print "ok 33\n";
# And here's one I introduced while fixing 33 :-(.
defined $h{'one'} or print "not ";
print "ok 34\n";
unlink 'good.cdb';
# Test numeric data (broken before 0.8)
$h = new CDB_File 't.cdb', 't.tmp' or print "not ";
$h->insert(1, 1 * 23);
$h->finish or print "not ";
tie %h, CDB_File, 't.cdb' or print "not ";
$h{1} == 23 or print "not ";
untie %h;
print "ok 35\n";
unlink 't.cdb';
|