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