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