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