Commit | Line | Data |
bee1dbe2 |
1 | #!./perl |
2 | |
a0d0e21e |
3 | # $RCSfile: dbm.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:43 $ |
4 | |
5 | BEGIN { |
20822f61 |
6 | @INC = '../lib'; |
a0d0e21e |
7 | require Config; import Config; |
8 | if ($Config{'extensions'} !~ /\bGDBM_File\b/) { |
45c0de28 |
9 | print "1..0 # Skip: GDBM_File was not built\n"; |
a0d0e21e |
10 | exit 0; |
11 | } |
12 | } |
13 | |
698828ad |
14 | use strict; |
15 | use warnings; |
16 | |
17 | |
a0d0e21e |
18 | use GDBM_File; |
19 | |
cbc5248d |
20 | print "1..68\n"; |
bee1dbe2 |
21 | |
a0d0e21e |
22 | unlink <Op.dbmx*>; |
23 | |
bee1dbe2 |
24 | umask(0); |
698828ad |
25 | my %h ; |
26 | print (tie(%h,'GDBM_File','Op.dbmx', &GDBM_WRCREAT, 0640) ? "ok 1\n" : "not ok 1\n"); |
a0d0e21e |
27 | |
698828ad |
28 | my $Dfile = "Op.dbmx.pag"; |
a0d0e21e |
29 | if (! -e $Dfile) { |
30 | ($Dfile) = <Op.dbmx*>; |
31 | } |
39e571d4 |
32 | if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' || $^O eq 'dos') { |
fac76ed7 |
33 | print "ok 2 # Skipped: different file permission semantics\n"; |
544a3566 |
34 | } |
35 | else { |
698828ad |
36 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, |
544a3566 |
37 | $blksize,$blocks) = stat($Dfile); |
38 | print (($mode & 0777) == 0640 ? "ok 2\n" : "not ok 2\n"); |
39 | } |
698828ad |
40 | my $i = 0; |
41 | while (my ($key,$value) = each(%h)) { |
bee1dbe2 |
42 | $i++; |
43 | } |
44 | print (!$i ? "ok 3\n" : "not ok 3\n"); |
45 | |
46 | $h{'goner1'} = 'snork'; |
47 | |
48 | $h{'abc'} = 'ABC'; |
49 | $h{'def'} = 'DEF'; |
50 | $h{'jkl','mno'} = "JKL\034MNO"; |
51 | $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5); |
52 | $h{'a'} = 'A'; |
53 | $h{'b'} = 'B'; |
54 | $h{'c'} = 'C'; |
55 | $h{'d'} = 'D'; |
56 | $h{'e'} = 'E'; |
57 | $h{'f'} = 'F'; |
58 | $h{'g'} = 'G'; |
59 | $h{'h'} = 'H'; |
60 | $h{'i'} = 'I'; |
61 | |
62 | $h{'goner2'} = 'snork'; |
63 | delete $h{'goner2'}; |
64 | |
a0d0e21e |
65 | untie(%h); |
698828ad |
66 | print (tie(%h,'GDBM_File','Op.dbmx', &GDBM_WRCREAT, 0640) ? "ok 4\n" : "not ok 4\n"); |
bee1dbe2 |
67 | |
68 | $h{'j'} = 'J'; |
69 | $h{'k'} = 'K'; |
70 | $h{'l'} = 'L'; |
71 | $h{'m'} = 'M'; |
72 | $h{'n'} = 'N'; |
73 | $h{'o'} = 'O'; |
74 | $h{'p'} = 'P'; |
75 | $h{'q'} = 'Q'; |
76 | $h{'r'} = 'R'; |
77 | $h{'s'} = 'S'; |
78 | $h{'t'} = 'T'; |
79 | $h{'u'} = 'U'; |
80 | $h{'v'} = 'V'; |
81 | $h{'w'} = 'W'; |
82 | $h{'x'} = 'X'; |
83 | $h{'y'} = 'Y'; |
84 | $h{'z'} = 'Z'; |
85 | |
86 | $h{'goner3'} = 'snork'; |
87 | |
88 | delete $h{'goner1'}; |
89 | delete $h{'goner3'}; |
90 | |
698828ad |
91 | my @keys = keys(%h); |
92 | my @values = values(%h); |
bee1dbe2 |
93 | |
94 | if ($#keys == 29 && $#values == 29) {print "ok 5\n";} else {print "not ok 5\n";} |
95 | |
698828ad |
96 | while (my ($key,$value) = each(%h)) { |
2f52a358 |
97 | if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) { |
bee1dbe2 |
98 | $key =~ y/a-z/A-Z/; |
99 | $i++ if $key eq $value; |
100 | } |
101 | } |
102 | |
103 | if ($i == 30) {print "ok 6\n";} else {print "not ok 6\n";} |
104 | |
c6aa4a32 |
105 | @keys = ('blurfl', keys(%h), 'dyick'); |
bee1dbe2 |
106 | if ($#keys == 31) {print "ok 7\n";} else {print "not ok 7\n";} |
107 | |
108 | $h{'foo'} = ''; |
109 | $h{''} = 'bar'; |
110 | |
111 | # check cache overflow and numeric keys and contents |
698828ad |
112 | my $ok = 1; |
bee1dbe2 |
113 | for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; } |
114 | for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; } |
115 | print ($ok ? "ok 8\n" : "not ok 8\n"); |
116 | |
698828ad |
117 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, |
a0d0e21e |
118 | $blksize,$blocks) = stat($Dfile); |
bee1dbe2 |
119 | print ($size > 0 ? "ok 9\n" : "not ok 9\n"); |
120 | |
121 | @h{0..200} = 200..400; |
698828ad |
122 | my @foo = @h{0..200}; |
bee1dbe2 |
123 | print join(':',200..400) eq join(':',@foo) ? "ok 10\n" : "not ok 10\n"; |
124 | |
125 | print ($h{'foo'} eq '' ? "ok 11\n" : "not ok 11\n"); |
126 | print ($h{''} eq 'bar' ? "ok 12\n" : "not ok 12\n"); |
127 | |
bbad3607 |
128 | untie %h; |
a0d0e21e |
129 | unlink 'Op.dbmx.dir', $Dfile; |
4e2a63a7 |
130 | |
131 | sub ok |
132 | { |
133 | my $no = shift ; |
134 | my $result = shift ; |
135 | |
136 | print "not " unless $result ; |
137 | print "ok $no\n" ; |
138 | } |
139 | |
140 | { |
141 | # sub-class test |
142 | |
143 | package Another ; |
144 | |
145 | use strict ; |
698828ad |
146 | use warnings ; |
4e2a63a7 |
147 | |
148 | open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ; |
149 | print FILE <<'EOM' ; |
150 | |
151 | package SubDB ; |
152 | |
153 | use strict ; |
154 | use vars qw(@ISA @EXPORT) ; |
155 | |
156 | require Exporter ; |
157 | use GDBM_File; |
158 | @ISA=qw(GDBM_File); |
159 | @EXPORT = @GDBM_File::EXPORT ; |
160 | |
161 | sub STORE { |
162 | my $self = shift ; |
163 | my $key = shift ; |
164 | my $value = shift ; |
165 | $self->SUPER::STORE($key, $value * 2) ; |
166 | } |
167 | |
168 | sub FETCH { |
169 | my $self = shift ; |
170 | my $key = shift ; |
171 | $self->SUPER::FETCH($key) - 1 ; |
172 | } |
173 | |
174 | sub A_new_method |
175 | { |
176 | my $self = shift ; |
177 | my $key = shift ; |
178 | my $value = $self->FETCH($key) ; |
179 | return "[[$value]]" ; |
180 | } |
181 | |
182 | 1 ; |
183 | EOM |
184 | |
185 | close FILE ; |
186 | |
187 | BEGIN { push @INC, '.'; } |
cbc5248d |
188 | unlink <dbhash.tmp*> ; |
4e2a63a7 |
189 | |
190 | eval 'use SubDB ; '; |
191 | main::ok(13, $@ eq "") ; |
192 | my %h ; |
193 | my $X ; |
194 | eval ' |
195 | $X = tie(%h, "SubDB","dbhash.tmp", &GDBM_WRCREAT, 0640 ); |
196 | ' ; |
197 | |
198 | main::ok(14, $@ eq "") ; |
199 | |
200 | my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ; |
201 | main::ok(15, $@ eq "") ; |
202 | main::ok(16, $ret == 5) ; |
203 | |
204 | $ret = eval ' &GDBM_WRCREAT eq &main::GDBM_WRCREAT ' ; |
205 | main::ok(17, $@ eq "" ) ; |
206 | main::ok(18, $ret == 1) ; |
207 | |
208 | $ret = eval '$X->A_new_method("fred") ' ; |
209 | main::ok(19, $@ eq "") ; |
210 | main::ok(20, $ret eq "[[5]]") ; |
211 | |
fac76ed7 |
212 | undef $X; |
213 | untie(%h); |
84902520 |
214 | unlink "SubDB.pm", <dbhash.tmp*> ; |
4e2a63a7 |
215 | |
216 | } |
9fe6733a |
217 | |
218 | { |
219 | # DBM Filter tests |
220 | use strict ; |
698828ad |
221 | use warnings ; |
9fe6733a |
222 | my (%h, $db) ; |
223 | my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
224 | |
225 | sub checkOutput |
226 | { |
227 | my($fk, $sk, $fv, $sv) = @_ ; |
228 | return |
229 | $fetch_key eq $fk && $store_key eq $sk && |
230 | $fetch_value eq $fv && $store_value eq $sv && |
231 | $_ eq 'original' ; |
232 | } |
233 | |
234 | unlink <Op.dbmx*>; |
235 | ok(21, $db = tie(%h, 'GDBM_File','Op.dbmx', &GDBM_WRCREAT, 0640)) ; |
236 | |
237 | $db->filter_fetch_key (sub { $fetch_key = $_ }) ; |
238 | $db->filter_store_key (sub { $store_key = $_ }) ; |
239 | $db->filter_fetch_value (sub { $fetch_value = $_}) ; |
240 | $db->filter_store_value (sub { $store_value = $_ }) ; |
241 | |
242 | $_ = "original" ; |
243 | |
244 | $h{"fred"} = "joe" ; |
245 | # fk sk fv sv |
246 | ok(22, checkOutput( "", "fred", "", "joe")) ; |
247 | |
248 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
249 | ok(23, $h{"fred"} eq "joe"); |
250 | # fk sk fv sv |
251 | ok(24, checkOutput( "", "fred", "joe", "")) ; |
252 | |
253 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
254 | ok(25, $db->FIRSTKEY() eq "fred") ; |
255 | # fk sk fv sv |
256 | ok(26, checkOutput( "fred", "", "", "")) ; |
257 | |
258 | # replace the filters, but remember the previous set |
259 | my ($old_fk) = $db->filter_fetch_key |
260 | (sub { $_ = uc $_ ; $fetch_key = $_ }) ; |
261 | my ($old_sk) = $db->filter_store_key |
262 | (sub { $_ = lc $_ ; $store_key = $_ }) ; |
263 | my ($old_fv) = $db->filter_fetch_value |
264 | (sub { $_ = "[$_]"; $fetch_value = $_ }) ; |
265 | my ($old_sv) = $db->filter_store_value |
266 | (sub { s/o/x/g; $store_value = $_ }) ; |
267 | |
268 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
269 | $h{"Fred"} = "Joe" ; |
270 | # fk sk fv sv |
271 | ok(27, checkOutput( "", "fred", "", "Jxe")) ; |
272 | |
273 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
274 | ok(28, $h{"Fred"} eq "[Jxe]"); |
275 | # fk sk fv sv |
276 | ok(29, checkOutput( "", "fred", "[Jxe]", "")) ; |
277 | |
278 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
279 | ok(30, $db->FIRSTKEY() eq "FRED") ; |
280 | # fk sk fv sv |
281 | ok(31, checkOutput( "FRED", "", "", "")) ; |
282 | |
283 | # put the original filters back |
284 | $db->filter_fetch_key ($old_fk); |
285 | $db->filter_store_key ($old_sk); |
286 | $db->filter_fetch_value ($old_fv); |
287 | $db->filter_store_value ($old_sv); |
288 | |
289 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
290 | $h{"fred"} = "joe" ; |
291 | ok(32, checkOutput( "", "fred", "", "joe")) ; |
292 | |
293 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
294 | ok(33, $h{"fred"} eq "joe"); |
295 | ok(34, checkOutput( "", "fred", "joe", "")) ; |
296 | |
297 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
298 | ok(35, $db->FIRSTKEY() eq "fred") ; |
299 | ok(36, checkOutput( "fred", "", "", "")) ; |
300 | |
301 | # delete the filters |
302 | $db->filter_fetch_key (undef); |
303 | $db->filter_store_key (undef); |
304 | $db->filter_fetch_value (undef); |
305 | $db->filter_store_value (undef); |
306 | |
307 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
308 | $h{"fred"} = "joe" ; |
309 | ok(37, checkOutput( "", "", "", "")) ; |
310 | |
311 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
312 | ok(38, $h{"fred"} eq "joe"); |
313 | ok(39, checkOutput( "", "", "", "")) ; |
314 | |
315 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
316 | ok(40, $db->FIRSTKEY() eq "fred") ; |
317 | ok(41, checkOutput( "", "", "", "")) ; |
318 | |
319 | undef $db ; |
320 | untie %h; |
321 | unlink <Op.dbmx*>; |
322 | } |
323 | |
324 | { |
325 | # DBM Filter with a closure |
326 | |
327 | use strict ; |
698828ad |
328 | use warnings ; |
9fe6733a |
329 | my (%h, $db) ; |
330 | |
331 | unlink <Op.dbmx*>; |
332 | ok(42, $db = tie(%h, 'GDBM_File','Op.dbmx', &GDBM_WRCREAT, 0640)) ; |
333 | |
334 | my %result = () ; |
335 | |
336 | sub Closure |
337 | { |
338 | my ($name) = @_ ; |
339 | my $count = 0 ; |
340 | my @kept = () ; |
341 | |
342 | return sub { ++$count ; |
343 | push @kept, $_ ; |
344 | $result{$name} = "$name - $count: [@kept]" ; |
345 | } |
346 | } |
347 | |
348 | $db->filter_store_key(Closure("store key")) ; |
349 | $db->filter_store_value(Closure("store value")) ; |
350 | $db->filter_fetch_key(Closure("fetch key")) ; |
351 | $db->filter_fetch_value(Closure("fetch value")) ; |
352 | |
353 | $_ = "original" ; |
354 | |
355 | $h{"fred"} = "joe" ; |
356 | ok(43, $result{"store key"} eq "store key - 1: [fred]"); |
357 | ok(44, $result{"store value"} eq "store value - 1: [joe]"); |
358 | ok(45, !defined $result{"fetch key"} ); |
359 | ok(46, !defined $result{"fetch value"} ); |
360 | ok(47, $_ eq "original") ; |
361 | |
362 | ok(48, $db->FIRSTKEY() eq "fred") ; |
363 | ok(49, $result{"store key"} eq "store key - 1: [fred]"); |
364 | ok(50, $result{"store value"} eq "store value - 1: [joe]"); |
365 | ok(51, $result{"fetch key"} eq "fetch key - 1: [fred]"); |
366 | ok(52, ! defined $result{"fetch value"} ); |
367 | ok(53, $_ eq "original") ; |
368 | |
369 | $h{"jim"} = "john" ; |
370 | ok(54, $result{"store key"} eq "store key - 2: [fred jim]"); |
371 | ok(55, $result{"store value"} eq "store value - 2: [joe john]"); |
372 | ok(56, $result{"fetch key"} eq "fetch key - 1: [fred]"); |
698828ad |
373 | ok(57, ! defined $result{"fetch value"} ); |
9fe6733a |
374 | ok(58, $_ eq "original") ; |
375 | |
376 | ok(59, $h{"fred"} eq "joe"); |
377 | ok(60, $result{"store key"} eq "store key - 3: [fred jim fred]"); |
378 | ok(61, $result{"store value"} eq "store value - 2: [joe john]"); |
379 | ok(62, $result{"fetch key"} eq "fetch key - 1: [fred]"); |
380 | ok(63, $result{"fetch value"} eq "fetch value - 1: [joe]"); |
381 | ok(64, $_ eq "original") ; |
382 | |
383 | undef $db ; |
384 | untie %h; |
385 | unlink <Op.dbmx*>; |
386 | } |
387 | |
388 | { |
389 | # DBM Filter recursion detection |
390 | use strict ; |
698828ad |
391 | use warnings ; |
9fe6733a |
392 | my (%h, $db) ; |
393 | unlink <Op.dbmx*>; |
394 | |
395 | ok(65, $db = tie(%h, 'GDBM_File','Op.dbmx', &GDBM_WRCREAT, 0640)) ; |
396 | |
397 | $db->filter_store_key (sub { $_ = $h{$_} }) ; |
398 | |
399 | eval '$h{1} = 1234' ; |
400 | ok(66, $@ =~ /^recursion detected in filter_store_key at/ ); |
401 | |
402 | undef $db ; |
403 | untie %h; |
404 | unlink <Op.dbmx*>; |
405 | } |
cbc5248d |
406 | |
407 | { |
408 | # Bug ID 20001013.009 |
409 | # |
410 | # test that $hash{KEY} = undef doesn't produce the warning |
411 | # Use of uninitialized value in null operation |
412 | use warnings ; |
413 | use strict ; |
414 | use GDBM_File ; |
415 | |
416 | unlink <Op.dbmx*>; |
417 | my %h ; |
418 | my $a = ""; |
419 | local $SIG{__WARN__} = sub {$a = $_[0]} ; |
420 | |
421 | ok(67, tie(%h, 'GDBM_File','Op.dbmx', &GDBM_WRCREAT, 0640)); |
422 | $h{ABC} = undef; |
423 | ok(68, $a eq "") ; |
424 | untie %h; |
425 | unlink <Op.dbmx*>; |
426 | } |