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