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