This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
78d8593319e42e28b73a7d27ba6bb20f587c41cb
[perl5.git] / t / lib / odbm.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     unshift @INC, '../lib';
8     require Config; import Config;
9     if ($Config{'extensions'} !~ /\bODBM_File\b/) {
10         print "1..0\n";
11         exit 0;
12     }
13 }
14
15 require ODBM_File;
16 #If Fcntl is not available, try 0x202 or 0x102 for O_RDWR|O_CREAT
17 use Fcntl;
18
19 print "1..18\n";
20
21 unlink <Op.dbmx*>;
22
23 umask(0);
24 print (tie(%h,ODBM_File,'Op.dbmx', O_RDWR|O_CREAT, 0640) ? "ok 1\n" : "not ok 1\n");
25
26 $Dfile = "Op.dbmx.pag";
27 if (! -e $Dfile) {
28         ($Dfile) = <Op.dbmx*>;
29 }
30 if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32') {
31     print "ok 2 # Skipped: different file permission semantics\n";
32 }
33 else {
34     ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
35      $blksize,$blocks) = stat($Dfile);
36     print (($mode & 0777) == 0640 ? "ok 2\n" : "not ok 2\n");
37 }
38 while (($key,$value) = each(%h)) {
39     $i++;
40 }
41 print (!$i ? "ok 3\n" : "not ok 3\n");
42
43 $h{'goner1'} = 'snork';
44
45 $h{'abc'} = 'ABC';
46 $h{'def'} = 'DEF';
47 $h{'jkl','mno'} = "JKL\034MNO";
48 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
49 $h{'a'} = 'A';
50 $h{'b'} = 'B';
51 $h{'c'} = 'C';
52 $h{'d'} = 'D';
53 $h{'e'} = 'E';
54 $h{'f'} = 'F';
55 $h{'g'} = 'G';
56 $h{'h'} = 'H';
57 $h{'i'} = 'I';
58
59 $h{'goner2'} = 'snork';
60 delete $h{'goner2'};
61
62 untie(%h);
63 print (tie(%h,ODBM_File,'Op.dbmx', O_RDWR, 0640) ? "ok 4\n" : "not ok 4\n");
64
65 $h{'j'} = 'J';
66 $h{'k'} = 'K';
67 $h{'l'} = 'L';
68 $h{'m'} = 'M';
69 $h{'n'} = 'N';
70 $h{'o'} = 'O';
71 $h{'p'} = 'P';
72 $h{'q'} = 'Q';
73 $h{'r'} = 'R';
74 $h{'s'} = 'S';
75 $h{'t'} = 'T';
76 $h{'u'} = 'U';
77 $h{'v'} = 'V';
78 $h{'w'} = 'W';
79 $h{'x'} = 'X';
80 $h{'y'} = 'Y';
81 $h{'z'} = 'Z';
82
83 $h{'goner3'} = 'snork';
84
85 delete $h{'goner1'};
86 delete $h{'goner3'};
87
88 @keys = keys(%h);
89 @values = values(%h);
90
91 if ($#keys == 29 && $#values == 29) {print "ok 5\n";} else {print "not ok 5\n";}
92
93 while (($key,$value) = each(%h)) {
94     if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
95         $key =~ y/a-z/A-Z/;
96         $i++ if $key eq $value;
97     }
98 }
99
100 if ($i == 30) {print "ok 6\n";} else {print "not ok 6\n";}
101
102 @keys = ('blurfl', keys(%h), 'dyick');
103 if ($#keys == 31) {print "ok 7\n";} else {print "not ok 7\n";}
104
105 $h{'foo'} = '';
106 $h{''} = 'bar';
107
108 # check cache overflow and numeric keys and contents
109 $ok = 1;
110 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
111 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
112 print ($ok ? "ok 8\n" : "not ok 8\n");
113
114 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
115    $blksize,$blocks) = stat($Dfile);
116 print ($size > 0 ? "ok 9\n" : "not ok 9\n");
117
118 @h{0..200} = 200..400;
119 @foo = @h{0..200};
120 print join(':',200..400) eq join(':',@foo) ? "ok 10\n" : "not ok 10\n";
121
122 print ($h{'foo'} eq '' ? "ok 11\n" : "not ok 11\n");
123 print ($h{''} eq 'bar' ? "ok 12\n" : "not ok 12\n");
124
125 untie %h;
126 unlink 'Op.dbmx.dir', $Dfile;
127
128 sub ok
129 {
130     my $no = shift ;
131     my $result = shift ;
132
133     print "not " unless $result ;
134     print "ok $no\n" ;
135 }
136
137 {
138    # sub-class test
139
140    package Another ;
141
142    use strict ;
143
144    open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
145    print FILE <<'EOM' ;
146
147    package SubDB ;
148
149    use strict ;
150    use vars qw(@ISA @EXPORT) ;
151
152    require Exporter ;
153    use ODBM_File;
154    @ISA=qw(ODBM_File);
155    @EXPORT = @ODBM_File::EXPORT ;
156
157    sub STORE { 
158         my $self = shift ;
159         my $key = shift ;
160         my $value = shift ;
161         $self->SUPER::STORE($key, $value * 2) ;
162    }
163
164    sub FETCH { 
165         my $self = shift ;
166         my $key = shift ;
167         $self->SUPER::FETCH($key) - 1 ;
168    }
169
170    sub A_new_method
171    {
172         my $self = shift ;
173         my $key = shift ;
174         my $value = $self->FETCH($key) ;
175         return "[[$value]]" ;
176    }
177
178    1 ;
179 EOM
180
181     close FILE ;
182
183     BEGIN { push @INC, '.'; }
184
185     eval 'use SubDB ; use Fcntl ;';
186     main::ok(13, $@ eq "") ;
187     my %h ;
188     my $X ;
189     eval '
190         $X = tie(%h, "SubDB","dbhash.tmp", O_RDWR|O_CREAT, 0640 );
191         ' ;
192
193     main::ok(14, $@ eq "") ;
194
195     my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
196     main::ok(15, $@ eq "") ;
197     main::ok(16, $ret == 5) ;
198
199     $ret = eval '$X->A_new_method("fred") ' ;
200     main::ok(17, $@ eq "") ;
201     main::ok(18, $ret eq "[[5]]") ;
202
203     undef $X;
204     untie(%h);
205     unlink "SubDB.pm", <dbhash.tmp*> ;
206
207 }