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