Commit | Line | Data |
f6b705ef |
1 | #!./perl -w |
a0d0e21e |
2 | |
3 | BEGIN { |
55497cff |
4 | @INC = '../lib' if -d '../lib' ; |
a0d0e21e |
5 | require Config; import Config; |
6 | if ($Config{'extensions'} !~ /\bDB_File\b/) { |
7 | print "1..0\n"; |
8 | exit 0; |
9 | } |
10 | } |
11 | |
12 | use DB_File; |
13 | use Fcntl; |
55d68b4a |
14 | use strict ; |
15 | use vars qw($dbh $Dfile) ; |
a0d0e21e |
16 | |
55d68b4a |
17 | sub ok |
18 | { |
19 | my $no = shift ; |
20 | my $result = shift ; |
a0d0e21e |
21 | |
55d68b4a |
22 | print "not " unless $result ; |
23 | print "ok $no\n" ; |
24 | } |
25 | |
36477c24 |
26 | print "1..55\n"; |
55d68b4a |
27 | |
28 | my $Dfile = "recno.tmp"; |
29 | unlink $Dfile ; |
a0d0e21e |
30 | |
31 | umask(0); |
32 | |
33 | # Check the interface to RECNOINFO |
34 | |
55d68b4a |
35 | my $dbh = new DB_File::RECNOINFO ; |
55497cff |
36 | ok(1, $dbh->{bval} == 0 ) ; |
37 | ok(2, $dbh->{cachesize} == 0) ; |
38 | ok(3, $dbh->{psize} == 0) ; |
39 | ok(4, $dbh->{flags} == 0) ; |
40 | ok(5, $dbh->{lorder} == 0); |
41 | ok(6, $dbh->{reclen} == 0); |
42 | ok(7, $dbh->{bfname} eq ""); |
a0d0e21e |
43 | |
44 | $dbh->{bval} = 3000 ; |
f6b705ef |
45 | ok(8, $dbh->{bval} == 3000 ); |
a0d0e21e |
46 | |
47 | $dbh->{cachesize} = 9000 ; |
f6b705ef |
48 | ok(9, $dbh->{cachesize} == 9000 ); |
a0d0e21e |
49 | |
50 | $dbh->{psize} = 400 ; |
f6b705ef |
51 | ok(10, $dbh->{psize} == 400 ); |
a0d0e21e |
52 | |
53 | $dbh->{flags} = 65 ; |
f6b705ef |
54 | ok(11, $dbh->{flags} == 65 ); |
a0d0e21e |
55 | |
56 | $dbh->{lorder} = 123 ; |
f6b705ef |
57 | ok(12, $dbh->{lorder} == 123 ); |
a0d0e21e |
58 | |
59 | $dbh->{reclen} = 1234 ; |
f6b705ef |
60 | ok(13, $dbh->{reclen} == 1234 ); |
a0d0e21e |
61 | |
62 | $dbh->{bfname} = 1234 ; |
f6b705ef |
63 | ok(14, $dbh->{bfname} == 1234 ); |
a0d0e21e |
64 | |
65 | |
66 | # Check that an invalid entry is caught both for store & fetch |
67 | eval '$dbh->{fred} = 1234' ; |
f6b705ef |
68 | ok(15, $@ =~ /^DB_File::RECNOINFO::STORE - Unknown element 'fred' at/ ); |
55d68b4a |
69 | eval 'my $q = $dbh->{fred}' ; |
f6b705ef |
70 | ok(16, $@ =~ /^DB_File::RECNOINFO::FETCH - Unknown element 'fred' at/ ); |
a0d0e21e |
71 | |
72 | # Now check the interface to RECNOINFO |
73 | |
55d68b4a |
74 | my $X ; |
75 | my @h ; |
76 | ok(17, $X = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ; |
a0d0e21e |
77 | |
053b5721 |
78 | ok(18, ( (stat($Dfile))[2] & 0777) == ($^O eq 'os2' ? 0666 : 0640)) ; |
a0d0e21e |
79 | |
55d68b4a |
80 | #my $l = @h ; |
81 | my $l = $X->length ; |
f6b705ef |
82 | ok(19, !$l ); |
a0d0e21e |
83 | |
55d68b4a |
84 | my @data = qw( a b c d ever f g h i j k longername m n o p) ; |
a0d0e21e |
85 | |
86 | $h[0] = shift @data ; |
f6b705ef |
87 | ok(20, $h[0] eq 'a' ); |
a0d0e21e |
88 | |
55d68b4a |
89 | my $ i; |
a0d0e21e |
90 | foreach (@data) |
91 | { $h[++$i] = $_ } |
92 | |
93 | unshift (@data, 'a') ; |
94 | |
f6b705ef |
95 | ok(21, defined $h[1] ); |
96 | ok(22, ! defined $h[16] ); |
97 | ok(23, $X->length == @data ); |
a0d0e21e |
98 | |
99 | |
100 | # Overwrite an entry & check fetch it |
101 | $h[3] = 'replaced' ; |
102 | $data[3] = 'replaced' ; |
f6b705ef |
103 | ok(24, $h[3] eq 'replaced' ); |
a0d0e21e |
104 | |
105 | #PUSH |
55d68b4a |
106 | my @push_data = qw(added to the end) ; |
f6b705ef |
107 | #my push (@h, @push_data) ; |
a0d0e21e |
108 | $X->push(@push_data) ; |
109 | push (@data, @push_data) ; |
f6b705ef |
110 | ok(25, $h[++$i] eq 'added' ); |
111 | ok(26, $h[++$i] eq 'to' ); |
112 | ok(27, $h[++$i] eq 'the' ); |
113 | ok(28, $h[++$i] eq 'end' ); |
a0d0e21e |
114 | |
115 | # POP |
f6b705ef |
116 | my $popped = pop (@data) ; |
117 | #my $value = pop(@h) ; |
55d68b4a |
118 | my $value = $X->pop ; |
f6b705ef |
119 | ok(29, $value eq $popped) ; |
a0d0e21e |
120 | |
121 | # SHIFT |
122 | #$value = shift @h |
123 | $value = $X->shift ; |
f6b705ef |
124 | my $shifted = shift @data ; |
125 | ok(30, $value eq $shifted ); |
a0d0e21e |
126 | |
127 | # UNSHIFT |
128 | |
129 | # empty list |
130 | $X->unshift ; |
f6b705ef |
131 | ok(31, $X->length == @data ); |
a0d0e21e |
132 | |
55d68b4a |
133 | my @new_data = qw(add this to the start of the array) ; |
a0d0e21e |
134 | #unshift @h, @new_data ; |
135 | $X->unshift (@new_data) ; |
136 | unshift (@data, @new_data) ; |
f6b705ef |
137 | ok(32, $X->length == @data ); |
138 | ok(33, $h[0] eq "add") ; |
139 | ok(34, $h[1] eq "this") ; |
140 | ok(35, $h[2] eq "to") ; |
141 | ok(36, $h[3] eq "the") ; |
142 | ok(37, $h[4] eq "start") ; |
143 | ok(38, $h[5] eq "of") ; |
144 | ok(39, $h[6] eq "the") ; |
145 | ok(40, $h[7] eq "array") ; |
146 | ok(41, $h[8] eq $data[8]) ; |
a0d0e21e |
147 | |
148 | # SPLICE |
149 | |
150 | # Now both arrays should be identical |
151 | |
55d68b4a |
152 | my $ok = 1 ; |
153 | my $j = 0 ; |
a0d0e21e |
154 | foreach (@data) |
155 | { |
156 | $ok = 0, last if $_ ne $h[$j ++] ; |
157 | } |
f6b705ef |
158 | ok(42, $ok ); |
a0d0e21e |
159 | |
55d68b4a |
160 | # Neagtive subscripts |
161 | |
162 | # get the last element of the array |
f6b705ef |
163 | ok(43, $h[-1] eq $data[-1] ); |
164 | ok(44, $h[-1] eq $h[$X->length -1] ); |
55d68b4a |
165 | |
166 | # get the first element using a negative subscript |
167 | eval '$h[ - ( $X->length)] = "abcd"' ; |
f6b705ef |
168 | ok(45, $@ eq "" ); |
169 | ok(46, $h[0] eq "abcd" ); |
55d68b4a |
170 | |
171 | # now try to read before the start of the array |
172 | eval '$h[ - (1 + $X->length)] = 1234' ; |
f6b705ef |
173 | ok(47, $@ =~ '^Modification of non-creatable array value attempted' ); |
55d68b4a |
174 | |
a0d0e21e |
175 | # IMPORTANT - $X must be undefined before the untie otherwise the |
176 | # underlying DB close routine will not get called. |
177 | undef $X ; |
178 | untie(@h); |
179 | |
180 | unlink $Dfile; |
181 | |
36477c24 |
182 | { |
183 | # Check bval defaults to \n |
184 | |
185 | my @h = () ; |
186 | my $dbh = new DB_File::RECNOINFO ; |
187 | ok(48, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ; |
188 | $h[0] = "abc" ; |
189 | $h[1] = "def" ; |
190 | $h[3] = "ghi" ; |
191 | untie @h ; |
192 | my $x = `cat $Dfile` ; |
193 | ok(49, $x eq "abc\ndef\n\nghi\n") ; |
194 | unlink $Dfile; |
195 | } |
196 | |
197 | { |
198 | # Change bval |
199 | |
200 | my @h = () ; |
201 | my $dbh = new DB_File::RECNOINFO ; |
202 | $dbh->{bval} = "-" ; |
203 | ok(50, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ; |
204 | $h[0] = "abc" ; |
205 | $h[1] = "def" ; |
206 | $h[3] = "ghi" ; |
207 | untie @h ; |
208 | my $x = `cat $Dfile` ; |
209 | ok(51, $x eq "abc-def--ghi-") ; |
210 | unlink $Dfile; |
211 | } |
212 | |
213 | { |
214 | # Check R_FIXEDLEN with default bval (space) |
215 | |
216 | my @h = () ; |
217 | my $dbh = new DB_File::RECNOINFO ; |
218 | $dbh->{flags} = R_FIXEDLEN ; |
219 | $dbh->{reclen} = 5 ; |
220 | ok(52, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ; |
221 | $h[0] = "abc" ; |
222 | $h[1] = "def" ; |
223 | $h[3] = "ghi" ; |
224 | untie @h ; |
225 | my $x = `cat $Dfile` ; |
226 | ok(53, $x eq "abc def ghi ") ; |
227 | unlink $Dfile; |
228 | } |
229 | |
230 | { |
231 | # Check R_FIXEDLEN with user-defined bval |
232 | |
233 | my @h = () ; |
234 | my $dbh = new DB_File::RECNOINFO ; |
235 | $dbh->{flags} = R_FIXEDLEN ; |
236 | $dbh->{bval} = "-" ; |
237 | $dbh->{reclen} = 5 ; |
238 | ok(54, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ; |
239 | $h[0] = "abc" ; |
240 | $h[1] = "def" ; |
241 | $h[3] = "ghi" ; |
242 | untie @h ; |
243 | my $x = `cat $Dfile` ; |
244 | ok(55, $x eq "abc--def-------ghi--") ; |
245 | unlink $Dfile; |
246 | } |
247 | |
a0d0e21e |
248 | exit ; |