This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
0391b7b4900c221f0eded606a04ef428ccb5fca1
[perl5.git] / t / lib / anydbm.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 }
9 require AnyDBM_File;
10 #If Fcntl is not available, try 0x202 or 0x102 for O_RDWR|O_CREAT
11 use Fcntl;
12
13 print "1..12\n";
14
15 unlink <Op_dbmx*>;
16
17 umask(0);
18 print (tie(%h,AnyDBM_File,'Op_dbmx', O_RDWR|O_CREAT, 0640)
19        ? "ok 1\n" : "not ok 1\n");
20
21 $Dfile = "Op_dbmx.pag";
22 if (! -e $Dfile) {
23         ($Dfile) = <Op_dbmx*>;
24 }
25 if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' || $^O eq 'dos') {
26     print "ok 2 # Skipped: different file permission semantics\n";
27 }
28 else {
29     ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
30      $blksize,$blocks) = stat($Dfile);
31     print (($mode & 0777) == 0640 ? "ok 2\n" : "not ok 2\n");
32 }
33 while (($key,$value) = each(%h)) {
34     $i++;
35 }
36 print (!$i ? "ok 3\n" : "not ok 3 # i=$i\n\n");
37
38 $h{'goner1'} = 'snork';
39
40 $h{'abc'} = 'ABC';
41 $h{'def'} = 'DEF';
42 $h{'jkl','mno'} = "JKL\034MNO";
43 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
44 $h{'a'} = 'A';
45 $h{'b'} = 'B';
46 $h{'c'} = 'C';
47 $h{'d'} = 'D';
48 $h{'e'} = 'E';
49 $h{'f'} = 'F';
50 $h{'g'} = 'G';
51 $h{'h'} = 'H';
52 $h{'i'} = 'I';
53
54 $h{'goner2'} = 'snork';
55 delete $h{'goner2'};
56
57 untie(%h);
58 print (tie(%h,AnyDBM_File,'Op_dbmx', O_RDWR, 0640) ? "ok 4\n" : "not ok 4\n");
59
60 $h{'j'} = 'J';
61 $h{'k'} = 'K';
62 $h{'l'} = 'L';
63 $h{'m'} = 'M';
64 $h{'n'} = 'N';
65 $h{'o'} = 'O';
66 $h{'p'} = 'P';
67 $h{'q'} = 'Q';
68 $h{'r'} = 'R';
69 $h{'s'} = 'S';
70 $h{'t'} = 'T';
71 $h{'u'} = 'U';
72 $h{'v'} = 'V';
73 $h{'w'} = 'W';
74 $h{'x'} = 'X';
75 $h{'y'} = 'Y';
76 $h{'z'} = 'Z';
77
78 $h{'goner3'} = 'snork';
79
80 delete $h{'goner1'};
81 delete $h{'goner3'};
82
83 @keys = keys(%h);
84 @values = values(%h);
85
86 if ($#keys == 29 && $#values == 29) {print "ok 5\n";} else {print "not ok 5\n";}
87
88 while (($key,$value) = each(%h)) {
89     if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
90         $key =~ y/a-z/A-Z/;
91         $i++ if $key eq $value;
92     }
93 }
94
95 if ($i == 30) {print "ok 6\n";} else {print "not ok 6\n";}
96
97 @keys = ('blurfl', keys(%h), 'dyick');
98 if ($#keys == 31) {print "ok 7\n";} else {print "not ok 7\n";}
99
100 $h{'foo'} = '';
101 $h{''} = 'bar';
102
103 # check cache overflow and numeric keys and contents
104 $ok = 1;
105 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
106 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
107 print ($ok ? "ok 8\n" : "not ok 8\n");
108
109 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
110    $blksize,$blocks) = stat($Dfile);
111 print ($size > 0 ? "ok 9\n" : "not ok 9\n");
112
113 @h{0..200} = 200..400;
114 @foo = @h{0..200};
115 print join(':',200..400) eq join(':',@foo) ? "ok 10\n" : "not ok 10\n";
116
117 print ($h{'foo'} eq '' ? "ok 11\n" : "not ok 11\n");
118 print ($h{''} eq 'bar' ? "ok 12\n" : "not ok 12\n");
119
120 untie %h;
121 if ($^O eq 'VMS') {
122   unlink 'Op_dbmx.sdbm_dir', $Dfile;
123 } else {
124   unlink 'Op_dbmx.dir', $Dfile;  
125 }