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