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