Commit | Line | Data |
ffed8b01 |
1 | ## |
2 | # DBM::Deep Test |
3 | ## |
4 | use strict; |
888453b9 |
5 | use Test::More tests => 128; |
ffed8b01 |
6 | use Test::Exception; |
fde3db1a |
7 | use t::common qw( new_fh ); |
ffed8b01 |
8 | |
9 | use_ok( 'DBM::Deep' ); |
10 | |
fde3db1a |
11 | my ($fh, $filename) = new_fh(); |
ffed8b01 |
12 | my $db = DBM::Deep->new( |
45f047f8 |
13 | file => $filename, |
14 | fh => $fh, |
15 | type => DBM::Deep->TYPE_ARRAY |
ffed8b01 |
16 | ); |
ffed8b01 |
17 | |
ffed8b01 |
18 | ## |
19 | # basic put/get/push |
20 | ## |
21 | $db->[0] = "elem1"; |
badf847c |
22 | $db->push( "elem2" ); |
23 | $db->put(2, "elem3"); |
24 | $db->store(3, "elem4"); |
25 | #warn $db->_engine->_dump_file; |
ffed8b01 |
26 | $db->unshift("elem0"); |
badf847c |
27 | #warn $db->_engine->_dump_file; |
28 | #__END__ |
ffed8b01 |
29 | |
30 | is( $db->[0], 'elem0', "Array get for shift works" ); |
31 | is( $db->[1], 'elem1', "Array get for array set works" ); |
32 | is( $db->[2], 'elem2', "Array get for push() works" ); |
33 | is( $db->[3], 'elem3', "Array get for put() works" ); |
34 | is( $db->[4], 'elem4', "Array get for store() works" ); |
35 | |
36 | is( $db->get(0), 'elem0', "get() for shift() works" ); |
37 | is( $db->get(1), 'elem1', "get() for array set works" ); |
38 | is( $db->get(2), 'elem2', "get() for push() works" ); |
39 | is( $db->get(3), 'elem3', "get() for put() works" ); |
40 | is( $db->get(4), 'elem4', "get() for store() works" ); |
41 | |
42 | is( $db->fetch(0), 'elem0', "fetch() for shift() works" ); |
43 | is( $db->fetch(1), 'elem1', "fetch() for array set works" ); |
44 | is( $db->fetch(2), 'elem2', "fetch() for push() works" ); |
45 | is( $db->fetch(3), 'elem3', "fetch() for put() works" ); |
46 | is( $db->fetch(4), 'elem4', "fetch() for store() works" ); |
47 | |
48 | is( $db->length, 5, "... and we have five elements" ); |
49 | |
7f441181 |
50 | is( $db->[-1], $db->[4], "-1st index is 4th index" ); |
51 | is( $db->[-2], $db->[3], "-2nd index is 3rd index" ); |
52 | is( $db->[-3], $db->[2], "-3rd index is 2nd index" ); |
53 | is( $db->[-4], $db->[1], "-4th index is 1st index" ); |
54 | is( $db->[-5], $db->[0], "-5th index is 0th index" ); |
c9cec40e |
55 | |
56 | # This is for Perls older than 5.8.0 because of is()'s prototype |
57 | { my $v = $db->[-6]; is( $v, undef, "-6th index is undef" ); } |
58 | |
ffed8b01 |
59 | is( $db->length, 5, "... and we have five elements after abortive -6 index lookup" ); |
60 | |
cb79ec85 |
61 | $db->[-1] = 'elem4.1'; |
62 | is( $db->[-1], 'elem4.1' ); |
63 | is( $db->[4], 'elem4.1' ); |
64 | is( $db->get(4), 'elem4.1' ); |
65 | is( $db->fetch(4), 'elem4.1' ); |
66 | |
baa27ab6 |
67 | throws_ok { |
68 | $db->[-6] = 'whoops!'; |
45f047f8 |
69 | } qr/Modification of non-creatable array value attempted, subscript -6/, "Correct error thrown"; |
baa27ab6 |
70 | |
badf847c |
71 | warn "1: \n" . $db->_engine->_dump_file; |
ffed8b01 |
72 | my $popped = $db->pop; |
badf847c |
73 | warn "2: \n" . $db->_engine->_dump_file; |
ffed8b01 |
74 | is( $db->length, 4, "... and we have four after popping" ); |
badf847c |
75 | warn "3: \n" . $db->_engine->_dump_file; |
ffed8b01 |
76 | is( $db->[0], 'elem0', "0th element still there after popping" ); |
badf847c |
77 | warn "4: \n" . $db->_engine->_dump_file; |
ffed8b01 |
78 | is( $db->[1], 'elem1', "1st element still there after popping" ); |
badf847c |
79 | warn "5: \n" . $db->_engine->_dump_file; |
ffed8b01 |
80 | is( $db->[2], 'elem2', "2nd element still there after popping" ); |
badf847c |
81 | warn "6: \n" . $db->_engine->_dump_file; |
ffed8b01 |
82 | is( $db->[3], 'elem3', "3rd element still there after popping" ); |
badf847c |
83 | warn "7: \n" . $db->_engine->_dump_file; |
cb79ec85 |
84 | is( $popped, 'elem4.1', "Popped value is correct" ); |
ffed8b01 |
85 | |
badf847c |
86 | die $db->_engine->_dump_file; |
87 | |
ffed8b01 |
88 | my $shifted = $db->shift; |
89 | is( $db->length, 3, "... and we have three after shifting" ); |
90 | is( $db->[0], 'elem1', "0th element still there after shifting" ); |
91 | is( $db->[1], 'elem2', "1st element still there after shifting" ); |
92 | is( $db->[2], 'elem3', "2nd element still there after shifting" ); |
1cff45d7 |
93 | is( $db->[3], undef, "There is no third element now" ); |
ffed8b01 |
94 | is( $shifted, 'elem0', "Shifted value is correct" ); |
95 | |
96 | ## |
97 | # delete |
98 | ## |
99 | my $deleted = $db->delete(0); |
100 | is( $db->length, 3, "... and we still have three after deleting" ); |
101 | is( $db->[0], undef, "0th element now undef" ); |
102 | is( $db->[1], 'elem2', "1st element still there after deleting" ); |
103 | is( $db->[2], 'elem3', "2nd element still there after deleting" ); |
81d3d316 |
104 | is( $deleted, 'elem1', "Deleted value is correct" ); |
ffed8b01 |
105 | |
106 | is( $db->delete(99), undef, 'delete on an element not in the array returns undef' ); |
107 | is( $db->length, 3, "... and we still have three after a delete on an out-of-range index" ); |
108 | |
109 | is( delete $db->[99], undef, 'DELETE on an element not in the array returns undef' ); |
110 | is( $db->length, 3, "... and we still have three after a DELETE on an out-of-range index" ); |
111 | |
112 | is( $db->delete(-99), undef, 'delete on an element (neg) not in the array returns undef' ); |
113 | is( $db->length, 3, "... and we still have three after a DELETE on an out-of-range negative index" ); |
114 | |
115 | is( delete $db->[-99], undef, 'DELETE on an element (neg) not in the array returns undef' ); |
116 | is( $db->length, 3, "... and we still have three after a DELETE on an out-of-range negative index" ); |
117 | |
118 | $deleted = $db->delete(-2); |
119 | is( $db->length, 3, "... and we still have three after deleting" ); |
120 | is( $db->[0], undef, "0th element still undef" ); |
9281d66b |
121 | is( $db->[1], undef, "1st element now undef" ); |
ffed8b01 |
122 | is( $db->[2], 'elem3', "2nd element still there after deleting" ); |
9281d66b |
123 | is( $deleted, 'elem2', "Deleted value is correct" ); |
ffed8b01 |
124 | |
125 | $db->[1] = 'elem2'; |
126 | |
127 | ## |
128 | # exists |
129 | ## |
130 | ok( $db->exists(1), "The 1st value exists" ); |
2120a181 |
131 | ok( $db->exists(0), "The 0th value doesn't exist" ); |
ffed8b01 |
132 | ok( !$db->exists(22), "The 22nd value doesn't exists" ); |
baa27ab6 |
133 | ok( $db->exists(-1), "The -1st value does exists" ); |
ffed8b01 |
134 | ok( !$db->exists(-22), "The -22nd value doesn't exists" ); |
135 | |
136 | ## |
137 | # clear |
138 | ## |
139 | ok( $db->clear(), "clear() returns true if the file was ever non-empty" ); |
140 | is( $db->length(), 0, "After clear(), no more elements" ); |
141 | |
142 | is( $db->pop, undef, "pop on an empty array returns undef" ); |
143 | is( $db->length(), 0, "After pop() on empty array, length is still 0" ); |
144 | |
145 | is( $db->shift, undef, "shift on an empty array returns undef" ); |
146 | is( $db->length(), 0, "After shift() on empty array, length is still 0" ); |
147 | |
badf847c |
148 | warn "BEFORE: " . $db->_engine->_dump_file; |
149 | __END__ |
8f6d6ed0 |
150 | is( $db->unshift( 1, 2, 3 ), 3, "unshift returns the number of elements in the array" ); |
151 | is( $db->unshift( 1, 2, 3 ), 6, "unshift returns the number of elements in the array" ); |
152 | is( $db->push( 1, 2, 3 ), 9, "push returns the number of elements in the array" ); |
153 | |
ffed8b01 |
154 | is( $db->length(), 9, "After unshift and push on empty array, length is now 9" ); |
155 | |
156 | $db->clear; |
157 | |
158 | ## |
159 | # multi-push |
160 | ## |
161 | $db->push( 'elem first', "elem middle", "elem last" ); |
162 | is( $db->length, 3, "3-element push results in three elements" ); |
163 | is($db->[0], "elem first", "First element is 'elem first'"); |
164 | is($db->[1], "elem middle", "Second element is 'elem middle'"); |
165 | is($db->[2], "elem last", "Third element is 'elem last'"); |
166 | |
167 | ## |
168 | # splice with length 1 |
169 | ## |
8fec41b9 |
170 | my @returned = $db->splice( 1, 1, "middle A", "middle B" ); |
171 | is( scalar(@returned), 1, "One element was removed" ); |
172 | is( $returned[0], 'elem middle', "... and it was correctly removed" ); |
ffed8b01 |
173 | is($db->length(), 4); |
174 | is($db->[0], "elem first"); |
175 | is($db->[1], "middle A"); |
176 | is($db->[2], "middle B"); |
177 | is($db->[3], "elem last"); |
178 | |
179 | ## |
180 | # splice with length of 0 |
181 | ## |
8fec41b9 |
182 | @returned = $db->splice( -1, 0, "middle C" ); |
183 | is( scalar(@returned), 0, "No elements were removed" ); |
ffed8b01 |
184 | is($db->length(), 5); |
185 | is($db->[0], "elem first"); |
186 | is($db->[1], "middle A"); |
187 | is($db->[2], "middle B"); |
188 | is($db->[3], "middle C"); |
189 | is($db->[4], "elem last"); |
190 | |
191 | ## |
192 | # splice with length of 3 |
193 | ## |
8fec41b9 |
194 | my $returned = $db->splice( 1, 3, "middle ABC" ); |
195 | is( $returned, 'middle C', "Just the last element was returned" ); |
ffed8b01 |
196 | is($db->length(), 3); |
197 | is($db->[0], "elem first"); |
198 | is($db->[1], "middle ABC"); |
199 | is($db->[2], "elem last"); |
200 | |
53fdf3d7 |
201 | @returned = $db->splice( 1 ); |
202 | is($db->length(), 1); |
203 | is($db->[0], "elem first"); |
204 | is($returned[0], "middle ABC"); |
205 | is($returned[1], "elem last"); |
206 | |
207 | $db->push( @returned ); |
208 | |
209 | @returned = $db->splice( 1, -1 ); |
210 | is($db->length(), 2); |
211 | is($db->[0], "elem first"); |
212 | is($db->[1], "elem last"); |
213 | is($returned[0], "middle ABC"); |
214 | |
888453b9 |
215 | @returned = $db->splice; |
216 | is( $db->length, 0 ); |
217 | is( $returned[0], "elem first" ); |
218 | is( $returned[1], "elem last" ); |
219 | |
ffed8b01 |
220 | $db->[0] = [ 1 .. 3 ]; |
221 | $db->[1] = { a => 'foo' }; |
994ccd8e |
222 | is( $db->[0]->length, 3, "Reuse of same space with array successful" ); |
ea2f6d67 |
223 | is( $db->[1]->fetch('a'), 'foo', "Reuse of same space with hash successful" ); |
4b603f25 |
224 | |
2120a181 |
225 | # Test autovivification |
4b603f25 |
226 | $db->[9999]{bar} = 1; |
227 | ok( $db->[9999] ); |
228 | cmp_ok( $db->[9999]{bar}, '==', 1 ); |
2120a181 |
229 | |
230 | # Test failures |
231 | throws_ok { |
232 | $db->fetch( 'foo' ); |
233 | } qr/Cannot use 'foo' as an array index/, "FETCH fails on an illegal key"; |
234 | |
235 | throws_ok { |
236 | $db->fetch(); |
237 | } qr/Cannot use an undefined array index/, "FETCH fails on an undefined key"; |
238 | |
239 | throws_ok { |
240 | $db->store( 'foo', 'bar' ); |
241 | } qr/Cannot use 'foo' as an array index/, "STORE fails on an illegal key"; |
242 | |
243 | throws_ok { |
244 | $db->store(); |
245 | } qr/Cannot use an undefined array index/, "STORE fails on an undefined key"; |
246 | |
247 | throws_ok { |
248 | $db->delete( 'foo' ); |
249 | } qr/Cannot use 'foo' as an array index/, "DELETE fails on an illegal key"; |
250 | |
251 | throws_ok { |
252 | $db->delete(); |
253 | } qr/Cannot use an undefined array index/, "DELETE fails on an undefined key"; |
254 | |
255 | throws_ok { |
256 | $db->exists( 'foo' ); |
257 | } qr/Cannot use 'foo' as an array index/, "EXISTS fails on an illegal key"; |
258 | |
259 | throws_ok { |
260 | $db->exists(); |
261 | } qr/Cannot use an undefined array index/, "EXISTS fails on an undefined key"; |
262 | |
807f63a7 |
263 | # Bug reported by Mike Schilli |
1cff45d7 |
264 | # Also, RT #29583 reported by HANENKAMP |
807f63a7 |
265 | { |
266 | my ($fh, $filename) = new_fh(); |
267 | my $db = DBM::Deep->new( |
268 | file => $filename, |
45f047f8 |
269 | fh => $fh, |
807f63a7 |
270 | type => DBM::Deep->TYPE_ARRAY |
271 | ); |
272 | |
1cff45d7 |
273 | push @{$db}, 3, { foo => 1 }; |
807f63a7 |
274 | lives_ok { |
275 | shift @{$db}; |
276 | } "Shift doesn't die moving references around"; |
277 | is( $db->[0]{foo}, 1, "Right hashref there" ); |
278 | |
279 | lives_ok { |
1cff45d7 |
280 | unshift @{$db}, [ 1 .. 3, [ 1 .. 3 ] ]; |
807f63a7 |
281 | unshift @{$db}, 1; |
282 | } "Unshift doesn't die moving references around"; |
1cff45d7 |
283 | is( $db->[1][3][1], 2, "Right arrayref there" ); |
807f63a7 |
284 | is( $db->[2]{foo}, 1, "Right hashref there" ); |
285 | |
286 | # Add test for splice moving references around |
287 | lives_ok { |
288 | splice @{$db}, 0, 0, 1 .. 3; |
289 | } "Splice doesn't die moving references around"; |
1cff45d7 |
290 | is( $db->[4][3][1], 2, "Right arrayref there" ); |
807f63a7 |
291 | is( $db->[5]{foo}, 1, "Right hashref there" ); |
292 | } |