1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
#!./perl
# $RCSfile: dbm.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:43 $
BEGIN {
require Config; import Config;
if ($Config{'extensions'} !~ /\bODBM_File\b/ or $Config{'d_cplusplus'}) {
print "1..0 # Skip: ODBM_File was not built\n";
exit 0;
}
}
use strict;
use warnings;
use Test::More tests => 81;
require ODBM_File;
#If Fcntl is not available, try 0x202 or 0x102 for O_RDWR|O_CREAT
use Fcntl;
unlink <Op_dbmx.*>;
umask(0);
my %h;
isa_ok(tie(%h, 'ODBM_File', 'Op_dbmx', O_RDWR|O_CREAT, 0640), 'ODBM_File');
my $Dfile = "Op_dbmx.pag";
if (! -e $Dfile) {
($Dfile) = <Op_dbmx*>;
}
SKIP: {
skip "different file permission semantics on $^O", 1
if $^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'dos' || $^O eq 'cygwin';
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
$blksize,$blocks) = stat($Dfile);
is($mode & 0777, 0640);
}
my $i = 0;
while (my ($key,$value) = each(%h)) {
$i++;
}
is($i, 0);
$h{'goner1'} = 'snork';
$h{'abc'} = 'ABC';
$h{'def'} = 'DEF';
$h{'jkl','mno'} = "JKL\034MNO";
$h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
$h{'a'} = 'A';
$h{'b'} = 'B';
$h{'c'} = 'C';
$h{'d'} = 'D';
$h{'e'} = 'E';
$h{'f'} = 'F';
$h{'g'} = 'G';
$h{'h'} = 'H';
$h{'i'} = 'I';
$h{'goner2'} = 'snork';
delete $h{'goner2'};
untie(%h);
isa_ok(tie(%h, 'ODBM_File', 'Op_dbmx', O_RDWR, 0640), 'ODBM_File');
$h{'j'} = 'J';
$h{'k'} = 'K';
$h{'l'} = 'L';
$h{'m'} = 'M';
$h{'n'} = 'N';
$h{'o'} = 'O';
$h{'p'} = 'P';
$h{'q'} = 'Q';
$h{'r'} = 'R';
$h{'s'} = 'S';
$h{'t'} = 'T';
$h{'u'} = 'U';
$h{'v'} = 'V';
$h{'w'} = 'W';
$h{'x'} = 'X';
$h{'y'} = 'Y';
$h{'z'} = 'Z';
$h{'goner3'} = 'snork';
delete $h{'goner1'};
delete $h{'goner3'};
my @keys = keys(%h);
my @values = values(%h);
is($#keys, 29);
is($#values, 29);
while (my ($key,$value) = each(%h)) {
if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
$key =~ y/a-z/A-Z/;
$i++ if $key eq $value;
}
}
is($i, 30);
@keys = ('blurfl', keys(%h), 'dyick');
is($#keys, 31);
$h{'foo'} = '';
$h{''} = 'bar';
my $ok = 1;
for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
is($ok, 1, 'check cache overflow and numeric keys and contents');
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
$blksize,$blocks) = stat($Dfile);
cmp_ok($size, '>', 0);
@h{0..200} = 200..400;
my @foo = @h{0..200};
is(join(':',200..400), join(':',@foo));
is($h{'foo'}, '');
is($h{''}, 'bar');
untie %h;
unlink <Op_dbmx*>, $Dfile;
{
# sub-class test
package Another ;
open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
print FILE <<'EOM' ;
package SubDB ;
use strict ;
use warnings ;
use vars qw(@ISA @EXPORT) ;
require Exporter ;
use ODBM_File;
@ISA=qw(ODBM_File);
@EXPORT = @ODBM_File::EXPORT ;
sub STORE {
my $self = shift ;
my $key = shift ;
my $value = shift ;
$self->SUPER::STORE($key, $value * 2) ;
}
sub FETCH {
my $self = shift ;
my $key = shift ;
$self->SUPER::FETCH($key) - 1 ;
}
sub A_new_method
{
my $self = shift ;
my $key = shift ;
my $value = $self->FETCH($key) ;
return "[[$value]]" ;
}
1 ;
EOM
close FILE or die "Could not close: $!";
BEGIN { push @INC, '.'; }
unlink <dbhash_tmp*> ;
eval 'use SubDB ; use Fcntl ;';
main::is($@, "");
my %h ;
my $X ;
eval '
$X = tie(%h, "SubDB","dbhash_tmp", O_RDWR|O_CREAT, 0640 );
' ;
main::is($@, "");
my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
main::is($@, "");
main::is($ret, 5);
$ret = eval '$X->A_new_method("fred") ' ;
main::is($@, "");
main::is($ret, "[[5]]");
undef $X;
untie(%h);
unlink "SubDB.pm", <dbhash_tmp.*> ;
}
untie %h;
unlink <Op_dbmx*>, $Dfile;
{
# DBM Filter tests
my (%h, $db) ;
my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
sub checkOutput
{
my($fk, $sk, $fv, $sv) = @_ ;
return
$fetch_key eq $fk && $store_key eq $sk &&
$fetch_value eq $fv && $store_value eq $sv &&
$_ eq 'original' ;
}
unlink <Op_dbmx*>;
$db = tie %h, 'ODBM_File', 'Op_dbmx', O_RDWR|O_CREAT, 0640;
isa_ok($db, 'ODBM_File');
$db->filter_fetch_key (sub { $fetch_key = $_ }) ;
$db->filter_store_key (sub { $store_key = $_ }) ;
$db->filter_fetch_value (sub { $fetch_value = $_}) ;
$db->filter_store_value (sub { $store_value = $_ }) ;
$_ = "original" ;
$h{"fred"} = "joe" ;
# fk sk fv sv
ok(checkOutput("", "fred", "", "joe"));
($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
is($h{"fred"}, "joe");
# fk sk fv sv
ok(checkOutput("", "fred", "joe", ""));
($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
is($db->FIRSTKEY(), "fred");
# fk sk fv sv
ok(checkOutput("fred", "", "", ""));
# replace the filters, but remember the previous set
my ($old_fk) = $db->filter_fetch_key
(sub { $_ = uc $_ ; $fetch_key = $_ }) ;
my ($old_sk) = $db->filter_store_key
(sub { $_ = lc $_ ; $store_key = $_ }) ;
my ($old_fv) = $db->filter_fetch_value
(sub { $_ = "[$_]"; $fetch_value = $_ }) ;
my ($old_sv) = $db->filter_store_value
(sub { s/o/x/g; $store_value = $_ }) ;
($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
$h{"Fred"} = "Joe" ;
# fk sk fv sv
ok(checkOutput("", "fred", "", "Jxe"));
($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
is($h{"Fred"}, "[Jxe]");
# fk sk fv sv
ok(checkOutput("", "fred", "[Jxe]", ""));
($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
is($db->FIRSTKEY(), "FRED");
# fk sk fv sv
ok(checkOutput("FRED", "", "", ""));
# put the original filters back
$db->filter_fetch_key ($old_fk);
$db->filter_store_key ($old_sk);
$db->filter_fetch_value ($old_fv);
$db->filter_store_value ($old_sv);
($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
$h{"fred"} = "joe" ;
ok(checkOutput("", "fred", "", "joe"));
($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
is($h{"fred"}, "joe");
ok(checkOutput("", "fred", "joe", ""));
($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
is($db->FIRSTKEY(), "fred");
ok(checkOutput("fred", "", "", ""));
# delete the filters
$db->filter_fetch_key (undef);
$db->filter_store_key (undef);
$db->filter_fetch_value (undef);
$db->filter_store_value (undef);
($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
$h{"fred"} = "joe" ;
ok(checkOutput("", "", "", ""));
($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
is($h{"fred"}, "joe");
ok(checkOutput("", "", "", ""));
($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
is($db->FIRSTKEY(), "fred");
ok(checkOutput("", "", "", ""));
undef $db ;
untie %h;
unlink <Op_dbmx*>;
}
{
# DBM Filter with a closure
my (%h, $db) ;
unlink <Op_dbmx*>;
$db = tie %h, 'ODBM_File', 'Op_dbmx', O_RDWR|O_CREAT, 0640;
isa_ok($db, 'ODBM_File');
my %result = () ;
sub Closure
{
my ($name) = @_ ;
my $count = 0 ;
my @kept = () ;
return sub { ++$count ;
push @kept, $_ ;
$result{$name} = "$name - $count: [@kept]" ;
}
}
$db->filter_store_key(Closure("store key")) ;
$db->filter_store_value(Closure("store value")) ;
$db->filter_fetch_key(Closure("fetch key")) ;
$db->filter_fetch_value(Closure("fetch value")) ;
$_ = "original" ;
$h{"fred"} = "joe" ;
is($result{"store key"}, "store key - 1: [fred]");
is($result{"store value"}, "store value - 1: [joe]");
is($result{"fetch key"}, undef);
is($result{"fetch value"}, undef);
is($_, "original");
is($db->FIRSTKEY(), "fred");
is($result{"store key"}, "store key - 1: [fred]");
is($result{"store value"}, "store value - 1: [joe]");
is($result{"fetch key"}, "fetch key - 1: [fred]");
is($result{"fetch value"}, undef);
is($_, "original");
$h{"jim"} = "john" ;
is($result{"store key"}, "store key - 2: [fred jim]");
is($result{"store value"}, "store value - 2: [joe john]");
is($result{"fetch key"}, "fetch key - 1: [fred]");
is($result{"fetch value"}, undef);
is($_, "original");
is($h{"fred"}, "joe");
is($result{"store key"}, "store key - 3: [fred jim fred]");
is($result{"store value"}, "store value - 2: [joe john]");
is($result{"fetch key"}, "fetch key - 1: [fred]");
is($result{"fetch value"}, "fetch value - 1: [joe]");
is($_, "original");
undef $db ;
untie %h;
unlink <Op_dbmx*>;
}
{
# DBM Filter recursion detection
my (%h, $db) ;
unlink <Op_dbmx*>;
$db = tie %h, 'ODBM_File', 'Op_dbmx', O_RDWR|O_CREAT, 0640;
isa_ok($db, 'ODBM_File');
$db->filter_store_key (sub { $_ = $h{$_} }) ;
eval '$h{1} = 1234' ;
like($@, qr/^recursion detected in filter_store_key at/);
undef $db ;
untie %h;
unlink <Op_dbmx*>;
}
{
# Bug ID 20001013.009
#
# test that $hash{KEY} = undef doesn't produce the warning
# Use of uninitialized value in null operation
unlink <Op_dbmx*>;
my %h ;
my $a = "";
local $SIG{__WARN__} = sub {$a = $_[0]} ;
isa_ok(tie(%h, 'ODBM_File', 'Op_dbmx', O_RDWR|O_CREAT, 0640), 'ODBM_File');
$h{ABC} = undef;
is($a, "");
untie %h;
unlink <Op_dbmx*>;
}
{
# When iterating over a tied hash using "each", the key passed to FETCH
# will be recycled and passed to NEXTKEY. If a Source Filter modifies the
# key in FETCH via a filter_fetch_key method we need to check that the
# modified key doesn't get passed to NEXTKEY.
# Also Test "keys" & "values" while we are at it.
unlink <Op_dbmx*>;
my $bad_key = 0 ;
my %h = () ;
my $db = tie %h, 'ODBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640;
isa_ok($db, 'ODBM_File');
$db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ;
$db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ;
$h{'Alpha_ABC'} = 2 ;
$h{'Alpha_DEF'} = 5 ;
is($h{'Alpha_ABC'}, 2);
is($h{'Alpha_DEF'}, 5);
my ($k, $v) = ("","");
while (($k, $v) = each %h) {}
is($bad_key, 0);
$bad_key = 0 ;
foreach $k (keys %h) {}
is($bad_key, 0);
$bad_key = 0 ;
foreach $v (values %h) {}
is($bad_key, 0);
undef $db ;
untie %h ;
unlink <Op_dbmx*>;
}
{
# Check that DBM Filter can cope with read-only $_
my %h ;
unlink <Op1_dbmx*>;
my $db = tie %h, 'ODBM_File','Op1_dbmx', O_RDWR|O_CREAT, 0640;
isa_ok($db, 'ODBM_File');
$db->filter_fetch_key (sub { }) ;
$db->filter_store_key (sub { }) ;
$db->filter_fetch_value (sub { }) ;
$db->filter_store_value (sub { }) ;
$_ = "original" ;
$h{"fred"} = "joe" ;
is($h{"fred"}, "joe");
is_deeply([eval { map { $h{$_} } (1, 2, 3) }], [undef, undef, undef]);
is($@, '');
# delete the filters
$db->filter_fetch_key (undef);
$db->filter_store_key (undef);
$db->filter_fetch_value (undef);
$db->filter_store_value (undef);
$h{"fred"} = "joe" ;
is($h{"fred"}, "joe");
is($db->FIRSTKEY(), "fred");
is_deeply([eval { map { $h{$_} } (1, 2, 3) }], [undef, undef, undef]);
is($@, '');
undef $db ;
untie %h;
unlink <Op1_dbmx*>;
}
if ($^O eq 'hpux') {
print <<EOM;
#
# If you experience failures with the odbm test in HP-UX,
# this is a well-known bug that's unfortunately very hard to fix.
# The suggested course of action is to avoid using the ODBM_File,
# but to use instead the NDBM_File extension.
#
EOM
}
|