perl 5.003_07: Configure
[p5sagit/p5-mst-13.2.git] / t / lib / db-hash.t
CommitLineData
f6b705ef 1#!./perl
2#!./perl -w
a0d0e21e 3
4BEGIN {
f6b705ef 5 #@INC = '../lib' if -d '../lib' ;
6 @INC = '../lib' ;
a0d0e21e 7 require Config; import Config;
8 if ($Config{'extensions'} !~ /\bDB_File\b/) {
9 print "1..0\n";
10 exit 0;
11 }
12}
13
14use DB_File;
15use Fcntl;
16
f6b705ef 17print "1..48\n";
18
19sub ok
20{
21 my $no = shift ;
22 my $result = shift ;
23
24 print "not " unless $result ;
25 print "ok $no\n" ;
26}
a0d0e21e 27
55d68b4a 28$Dfile = "dbhash.tmp";
a0d0e21e 29unlink $Dfile;
30
31umask(0);
32
33# Check the interface to HASHINFO
34
f6b705ef 35my $dbh = new DB_File::HASHINFO ;
36
37$^W = 0 ;
38ok(1, $dbh->{bsize} == undef) ;
39ok(2, $dbh->{ffactor} == undef) ;
40ok(3, $dbh->{nelem} == undef) ;
41ok(4, $dbh->{cachesize} == undef) ;
42ok(5, $dbh->{hash} == undef) ;
43ok(6, $dbh->{lorder} == undef) ;
44$^W = 1 ;
a0d0e21e 45
46$dbh->{bsize} = 3000 ;
f6b705ef 47ok(7, $dbh->{bsize} == 3000 );
a0d0e21e 48
49$dbh->{ffactor} = 9000 ;
f6b705ef 50ok(8, $dbh->{ffactor} == 9000 );
51
a0d0e21e 52$dbh->{nelem} = 400 ;
f6b705ef 53ok(9, $dbh->{nelem} == 400 );
a0d0e21e 54
55$dbh->{cachesize} = 65 ;
f6b705ef 56ok(10, $dbh->{cachesize} == 65 );
a0d0e21e 57
58$dbh->{hash} = "abc" ;
f6b705ef 59ok(11, $dbh->{hash} eq "abc" );
a0d0e21e 60
61$dbh->{lorder} = 1234 ;
f6b705ef 62ok(12, $dbh->{lorder} == 1234 );
a0d0e21e 63
64# Check that an invalid entry is caught both for store & fetch
65eval '$dbh->{fred} = 1234' ;
f6b705ef 66ok(13, $@ =~ /^DB_File::HASHINFO::STORE - Unknown element 'fred' at/ );
a0d0e21e 67eval '$q = $dbh->{fred}' ;
f6b705ef 68ok(14, $@ =~ /^DB_File::HASHINFO::FETCH - Unknown element 'fred' at/ );
a0d0e21e 69
70# Now check the interface to HASH
71
f6b705ef 72ok(15, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
a0d0e21e 73
74($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
75 $blksize,$blocks) = stat($Dfile);
f6b705ef 76ok(16, ($mode & 0777) == 0640 );
a0d0e21e 77
78while (($key,$value) = each(%h)) {
79 $i++;
80}
f6b705ef 81ok(17, !$i );
a0d0e21e 82
83$h{'goner1'} = 'snork';
84
85$h{'abc'} = 'ABC';
f6b705ef 86ok(18, $h{'abc'} eq 'ABC' );
87ok(19, !defined $h{'jimmy'} );
88ok(20, !exists $h{'jimmy'} );
89ok(21, exists $h{'abc'} );
a0d0e21e 90
91$h{'def'} = 'DEF';
92$h{'jkl','mno'} = "JKL\034MNO";
93$h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
94$h{'a'} = 'A';
95
96#$h{'b'} = 'B';
97$X->STORE('b', 'B') ;
98
99$h{'c'} = 'C';
100
101#$h{'d'} = 'D';
102$X->put('d', 'D') ;
103
104$h{'e'} = 'E';
105$h{'f'} = 'F';
106$h{'g'} = 'X';
107$h{'h'} = 'H';
108$h{'i'} = 'I';
109
110$h{'goner2'} = 'snork';
111delete $h{'goner2'};
112
113
114# IMPORTANT - $X must be undefined before the untie otherwise the
115# underlying DB close routine will not get called.
116undef $X ;
117untie(%h);
118
119
120# tie to the same file again, do not supply a type - should default to HASH
f6b705ef 121ok(22, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640) );
a0d0e21e 122
123# Modify an entry from the previous tie
124$h{'g'} = 'G';
125
126$h{'j'} = 'J';
127$h{'k'} = 'K';
128$h{'l'} = 'L';
129$h{'m'} = 'M';
130$h{'n'} = 'N';
131$h{'o'} = 'O';
132$h{'p'} = 'P';
133$h{'q'} = 'Q';
134$h{'r'} = 'R';
135$h{'s'} = 'S';
136$h{'t'} = 'T';
137$h{'u'} = 'U';
138$h{'v'} = 'V';
139$h{'w'} = 'W';
140$h{'x'} = 'X';
141$h{'y'} = 'Y';
142$h{'z'} = 'Z';
143
144$h{'goner3'} = 'snork';
145
146delete $h{'goner1'};
147$X->DELETE('goner3');
148
149@keys = keys(%h);
150@values = values(%h);
151
f6b705ef 152ok(23, $#keys == 29 && $#values == 29) ;
a0d0e21e 153
f6b705ef 154$i = 0 ;
55d68b4a 155while (($key,$value) = each(%h)) {
2f52a358 156 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
a0d0e21e 157 $key =~ y/a-z/A-Z/;
158 $i++ if $key eq $value;
159 }
160}
161
f6b705ef 162ok(24, $i == 30) ;
a0d0e21e 163
55d68b4a 164@keys = ('blurfl', keys(%h), 'dyick');
f6b705ef 165ok(25, $#keys == 31) ;
a0d0e21e 166
167$h{'foo'} = '';
f6b705ef 168ok(26, $h{'foo'} eq '' );
a0d0e21e 169
170$h{''} = 'bar';
f6b705ef 171ok(27, $h{''} eq 'bar' );
a0d0e21e 172
173# check cache overflow and numeric keys and contents
174$ok = 1;
175for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
176for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
f6b705ef 177ok(28, $ok );
a0d0e21e 178
179($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
180 $blksize,$blocks) = stat($Dfile);
f6b705ef 181ok(29, $size > 0 );
a0d0e21e 182
183@h{0..200} = 200..400;
184@foo = @h{0..200};
f6b705ef 185ok(30, join(':',200..400) eq join(':',@foo) );
a0d0e21e 186
187
188# Now check all the non-tie specific stuff
189
190# Check NOOVERWRITE will make put fail when attempting to overwrite
191# an existing record.
192
193$status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
f6b705ef 194ok(31, $status == 1 );
a0d0e21e 195
196# check that the value of the key 'x' has not been changed by the
197# previous test
f6b705ef 198ok(32, $h{'x'} eq 'X' );
a0d0e21e 199
200# standard put
201$status = $X->put('key', 'value') ;
f6b705ef 202ok(33, $status == 0 );
a0d0e21e 203
204#check that previous put can be retrieved
f6b705ef 205$value = 0 ;
a0d0e21e 206$status = $X->get('key', $value) ;
f6b705ef 207ok(34, $status == 0 );
208ok(35, $value eq 'value' );
a0d0e21e 209
210# Attempting to delete an existing key should work
211
212$status = $X->del('q') ;
f6b705ef 213ok(36, $status == 0 );
a0d0e21e 214
215# Make sure that the key deleted, cannot be retrieved
f6b705ef 216$^W = 0 ;
217ok(37, $h{'q'} eq undef );
218$^W = 1 ;
a0d0e21e 219
220# Attempting to delete a non-existant key should fail
221
222$status = $X->del('joe') ;
f6b705ef 223ok(38, $status == 1 );
a0d0e21e 224
225# Check the get interface
226
227# First a non-existing key
228$status = $X->get('aaaa', $value) ;
f6b705ef 229ok(39, $status == 1 );
a0d0e21e 230
231# Next an existing key
232$status = $X->get('a', $value) ;
f6b705ef 233ok(40, $status == 0 );
234ok(41, $value eq 'A' );
a0d0e21e 235
236# seq
237# ###
238
239# ditto, but use put to replace the key/value pair.
240
241# use seq to walk backwards through a file - check that this reversed is
242
243# check seq FIRST/LAST
244
245# sync
246# ####
247
248$status = $X->sync ;
f6b705ef 249ok(42, $status == 0 );
a0d0e21e 250
251
252# fd
253# ##
254
255$status = $X->fd ;
f6b705ef 256ok(43, $status != 0 );
a0d0e21e 257
258undef $X ;
259untie %h ;
260
261unlink $Dfile;
262
f6b705ef 263# clear
264# #####
265
266ok(44, tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
267foreach (1 .. 10)
268 { $h{$_} = $_ * 100 }
269
270# check that there are 10 elements in the hash
271$i = 0 ;
272while (($key,$value) = each(%h)) {
273 $i++;
274}
275ok(45, $i == 10);
276
277# now clear the hash
278%h = () ;
279
280# check it is empty
281$i = 0 ;
282while (($key,$value) = each(%h)) {
283 $i++;
284}
285ok(46, $i == 0);
286
287untie %h ;
288unlink $Dfile ;
289
290
a0d0e21e 291# Now try an in memory file
f6b705ef 292ok(47, $X = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
a0d0e21e 293
294# fd with an in memory file should return fail
295$status = $X->fd ;
f6b705ef 296ok(48, $status == -1 );
a0d0e21e 297
298untie %h ;
299undef $X ;
300
301exit ;