AnyDBM_File.t 3.36 KB
Newer Older
1 2 3 4 5 6
#!./perl

BEGIN {
    chdir 't' if -d 't';
    @INC = '../lib';
    require Config; import Config;
7 8
    require Test::More; import Test::More;
    plan(tests, 12);
9
}
10

11 12 13 14
require AnyDBM_File;
use Fcntl;


15 16
$Is_Dosish = ($^O eq 'amigaos' || $^O eq 'MSWin32' ||
	      $^O eq 'NetWare' || $^O eq 'dos' ||
17
	      $^O eq 'os2' ||
18
	      $^O eq 'cygwin');
19

20 21
my $filename = "Any_dbmx$$";
unlink <"$filename*">;
22 23

umask(0);
24

25
ok( tie(%h,AnyDBM_File,"$filename", O_RDWR|O_CREAT, 0640), "Tie");
26

27
$Dfile = "$filename.pag";
28
if (! -e $Dfile) {
29
	($Dfile) = <$filename*>;
30
}
31 32 33 34

SKIP:
{
    skip( "different file permission semantics",1)
35
                      if $Is_Dosish;
36 37
    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
     $blksize,$blocks) = stat($Dfile);
38
    ok(($mode & 0777) == 0640 , "File permissions");
39
}
40

41 42 43
while (($key,$value) = each(%h)) {
    $i++;
}
44 45

ok(!$i,"Hash created empty");
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

$h{'goner1'} = 'snork';

$h{'abc'} = 'ABC';
$h{'def'} = 'DEF';
$h{'jkl','mno'} = "JKL\034MNO";
$h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
$h{'a'} = 'A';
$h{'b'} = 'B';
$h{'c'} = 'C';
$h{'d'} = 'D';
$h{'e'} = 'E';
$h{'f'} = 'F';
$h{'g'} = 'G';
$h{'h'} = 'H';
$h{'i'} = 'I';

$h{'goner2'} = 'snork';
delete $h{'goner2'};

untie(%h);
67
ok(tie(%h,AnyDBM_File,"$filename", O_RDWR, 0640),"Re-tie hash");
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

$h{'j'} = 'J';
$h{'k'} = 'K';
$h{'l'} = 'L';
$h{'m'} = 'M';
$h{'n'} = 'N';
$h{'o'} = 'O';
$h{'p'} = 'P';
$h{'q'} = 'Q';
$h{'r'} = 'R';
$h{'s'} = 'S';
$h{'t'} = 'T';
$h{'u'} = 'U';
$h{'v'} = 'V';
$h{'w'} = 'W';
$h{'x'} = 'X';
$h{'y'} = 'Y';
$h{'z'} = 'Z';

$h{'goner3'} = 'snork';

delete $h{'goner1'};
delete $h{'goner3'};

@keys = keys(%h);
@values = values(%h);

95
ok( ($#keys == 29 && $#values == 29),'$#keys == $#values');
96 97 98 99 100 101 102 103

while (($key,$value) = each(%h)) {
    if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
	$key =~ y/a-z/A-Z/;
	$i++ if $key eq $value;
    }
}

104
ok($i == 30,"keys and values match");
105 106

@keys = ('blurfl', keys(%h), 'dyick');
107
ok($#keys == 31,"Correct number of keys");
108 109 110 111 112 113 114 115

$h{'foo'} = '';
$h{''} = 'bar';

# check cache overflow and numeric keys and contents
$ok = 1;
for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
116
ok($ok, "cache overflow and numeric keys and contents");
117 118 119

($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
   $blksize,$blocks) = stat($Dfile);
120
ok($size > 0, "check file size");
121 122 123

@h{0..200} = 200..400;
@foo = @h{0..200};
124
ok( join(':',200..400) eq join(':',@foo), "hash slice");
125

126 127 128 129 130
ok($h{'foo'} eq '', "empty value");

my $compact = '';

if ($AnyDBM_File::ISA[0] eq 'DB_File' && ($DB_File::db_ver >= 2.004010 && $DB_File::db_ver < 3.001)) {
131 132 133 134 135 136 137 138 139 140 141
     ($major, $minor, $patch) = ($DB_File::db_ver =~ /^(\d+)\.(\d\d\d)(\d\d\d)/) ;
     $major =~ s/^0+// ;
     $minor =~ s/^0+// ;
     $patch =~ s/^0+// ;
     $compact = "$major.$minor.$patch" ;
     #
     # anydbm.t test 12 will fail when AnyDBM_File uses the combination of
     # DB_File and Berkeley DB 2.4.10 (or greater). 
     # You are using DB_File $DB_File::VERSION and Berkeley DB $compact
     #
     # Berkeley DB 2 from version 2.4.10 onwards does not allow null keys.
142
     # This feature returned with version 3.1
143
     #
144 145 146 147 148 149
}

SKIP:
{
  skip( "db v$compact, no null key support", 1) if $compact;
  ok($h{''} eq 'bar','null key');
150 151 152
}

untie %h;
153

154
if ($^O eq 'VMS') {
155
  unlink "$filename.sdbm_dir", $Dfile;
156
} else {
157
  unlink "$filename.dir", $Dfile;
158
}