This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to Module-Build-0.2803
[perl5.git] / lib / AnyDBM_File.t
CommitLineData
bee1dbe2
LW
1#!./perl
2
a0d0e21e
LW
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
be3174d2 6 require Config; import Config;
be6bd645 7 require Test::More; import Test::More;
b3ce290b 8 plan(tests, 12);
a0d0e21e 9}
223eb2ff 10
a0d0e21e 11require AnyDBM_File;
a0d0e21e
LW
12use Fcntl;
13
bee1dbe2 14
592a6412
JH
15$Is_Dosish = ($^O eq 'amigaos' || $^O eq 'MSWin32' ||
16 $^O eq 'NetWare' || $^O eq 'dos' ||
17 $^O eq 'os2' || $^O eq 'mint' ||
18 $^O eq 'cygwin');
8736538c 19
3aefca04 20unlink <Op_dbmx*>;
a0d0e21e 21
bee1dbe2 22umask(0);
223eb2ff
JS
23
24ok( tie(%h,AnyDBM_File,'Op_dbmx', O_RDWR|O_CREAT, 0640), "Tie");
a0d0e21e 25
bf99883d 26$Dfile = "Op_dbmx.pag";
a0d0e21e 27if (! -e $Dfile) {
3aefca04 28 ($Dfile) = <Op_dbmx*>;
a0d0e21e 29}
223eb2ff
JS
30
31SKIP:
32{
33 skip( "different file permission semantics",1)
34 if ($Is_Dosish || $^O eq 'MacOS') ;
544a3566
CS
35 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
36 $blksize,$blocks) = stat($Dfile);
2825d001 37 ok(($mode & 0777) == ($^O eq 'vos' ? 0750 : 0640) , "File permissions");
544a3566 38}
223eb2ff 39
a0d0e21e 40while (($key,$value) = each(%h)) {
bee1dbe2
LW
41 $i++;
42}
223eb2ff
JS
43
44ok(!$i,"Hash created empty");
bee1dbe2
LW
45
46$h{'goner1'} = 'snork';
47
48$h{'abc'} = 'ABC';
49$h{'def'} = 'DEF';
50$h{'jkl','mno'} = "JKL\034MNO";
51$h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
52$h{'a'} = 'A';
53$h{'b'} = 'B';
54$h{'c'} = 'C';
55$h{'d'} = 'D';
56$h{'e'} = 'E';
57$h{'f'} = 'F';
58$h{'g'} = 'G';
59$h{'h'} = 'H';
60$h{'i'} = 'I';
61
62$h{'goner2'} = 'snork';
63delete $h{'goner2'};
64
a0d0e21e 65untie(%h);
223eb2ff 66ok(tie(%h,AnyDBM_File,'Op_dbmx', O_RDWR, 0640),"Re-tie hash");
bee1dbe2
LW
67
68$h{'j'} = 'J';
69$h{'k'} = 'K';
70$h{'l'} = 'L';
71$h{'m'} = 'M';
72$h{'n'} = 'N';
73$h{'o'} = 'O';
74$h{'p'} = 'P';
75$h{'q'} = 'Q';
76$h{'r'} = 'R';
77$h{'s'} = 'S';
78$h{'t'} = 'T';
79$h{'u'} = 'U';
80$h{'v'} = 'V';
81$h{'w'} = 'W';
82$h{'x'} = 'X';
83$h{'y'} = 'Y';
84$h{'z'} = 'Z';
85
86$h{'goner3'} = 'snork';
87
88delete $h{'goner1'};
89delete $h{'goner3'};
90
91@keys = keys(%h);
92@values = values(%h);
93
223eb2ff 94ok( ($#keys == 29 && $#values == 29),'$#keys == $#values');
bee1dbe2 95
c6aa4a32 96while (($key,$value) = each(%h)) {
2f52a358 97 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
bee1dbe2
LW
98 $key =~ y/a-z/A-Z/;
99 $i++ if $key eq $value;
100 }
101}
102
223eb2ff 103ok($i == 30,"keys and values match");
bee1dbe2 104
c6aa4a32 105@keys = ('blurfl', keys(%h), 'dyick');
223eb2ff 106ok($#keys == 31,"Correct number of keys");
bee1dbe2
LW
107
108$h{'foo'} = '';
109$h{''} = 'bar';
110
111# check cache overflow and numeric keys and contents
112$ok = 1;
113for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
114for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
223eb2ff 115ok($ok, "cache overflow and numeric keys and contents");
bee1dbe2
LW
116
117($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
a0d0e21e 118 $blksize,$blocks) = stat($Dfile);
223eb2ff 119ok($size > 0, "check file size");
bee1dbe2
LW
120
121@h{0..200} = 200..400;
122@foo = @h{0..200};
223eb2ff 123ok( join(':',200..400) eq join(':',@foo), "hash slice");
bee1dbe2 124
223eb2ff
JS
125ok($h{'foo'} eq '', "empty value");
126
127my $compact = '';
128
408a2300 129if ($AnyDBM_File::ISA[0] eq 'DB_File' && ($DB_File::db_ver >= 2.004010 && $DB_File::db_ver < 3.001)) {
f94c1b37
PM
130 ($major, $minor, $patch) = ($DB_File::db_ver =~ /^(\d+)\.(\d\d\d)(\d\d\d)/) ;
131 $major =~ s/^0+// ;
132 $minor =~ s/^0+// ;
133 $patch =~ s/^0+// ;
134 $compact = "$major.$minor.$patch" ;
206483f1
GS
135 #
136 # anydbm.t test 12 will fail when AnyDBM_File uses the combination of
137 # DB_File and Berkeley DB 2.4.10 (or greater).
138 # You are using DB_File $DB_File::VERSION and Berkeley DB $compact
139 #
140 # Berkeley DB 2 from version 2.4.10 onwards does not allow null keys.
408a2300 141 # This feature returned with version 3.1
206483f1 142 #
223eb2ff
JS
143}
144
145SKIP:
146{
147 skip( "db v$compact, no null key support", 1) if $compact;
148 ok($h{''} eq 'bar','null key');
f94c1b37 149}
bee1dbe2 150
bbad3607 151untie %h;
223eb2ff 152
bf99883d
HM
153if ($^O eq 'VMS') {
154 unlink 'Op_dbmx.sdbm_dir', $Dfile;
155} else {
156 unlink 'Op_dbmx.dir', $Dfile;
157}