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