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