Commit right before adding the keylist
[dbsrgits/DBM-Deep.git] / t / 04_array.t
CommitLineData
ffed8b01 1##
2# DBM::Deep Test
3##
4use strict;
4b603f25 5use Test::More tests => 109;
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(
2a81bf9e 13 file => $filename,
ffed8b01 14 type => DBM::Deep->TYPE_ARRAY
15);
ffed8b01 16
17TODO: {
18 local $TODO = "How is this test ever supposed to pass?";
19 ok( !$db->clear, "If the file has never been written to, clear() returns false" );
20}
21
22##
23# basic put/get/push
24##
25$db->[0] = "elem1";
26$db->push( "elem2" );
27$db->put(2, "elem3");
28$db->store(3, "elem4");
29$db->unshift("elem0");
30
31is( $db->[0], 'elem0', "Array get for shift works" );
32is( $db->[1], 'elem1', "Array get for array set works" );
33is( $db->[2], 'elem2', "Array get for push() works" );
34is( $db->[3], 'elem3', "Array get for put() works" );
35is( $db->[4], 'elem4', "Array get for store() works" );
36
37is( $db->get(0), 'elem0', "get() for shift() works" );
38is( $db->get(1), 'elem1', "get() for array set works" );
39is( $db->get(2), 'elem2', "get() for push() works" );
40is( $db->get(3), 'elem3', "get() for put() works" );
41is( $db->get(4), 'elem4', "get() for store() works" );
42
43is( $db->fetch(0), 'elem0', "fetch() for shift() works" );
44is( $db->fetch(1), 'elem1', "fetch() for array set works" );
45is( $db->fetch(2), 'elem2', "fetch() for push() works" );
46is( $db->fetch(3), 'elem3', "fetch() for put() works" );
47is( $db->fetch(4), 'elem4', "fetch() for store() works" );
48
49is( $db->length, 5, "... and we have five elements" );
50
7f441181 51is( $db->[-1], $db->[4], "-1st index is 4th index" );
52is( $db->[-2], $db->[3], "-2nd index is 3rd index" );
53is( $db->[-3], $db->[2], "-3rd index is 2nd index" );
54is( $db->[-4], $db->[1], "-4th index is 1st index" );
55is( $db->[-5], $db->[0], "-5th index is 0th index" );
c9cec40e 56
57# This is for Perls older than 5.8.0 because of is()'s prototype
58{ my $v = $db->[-6]; is( $v, undef, "-6th index is undef" ); }
59
ffed8b01 60is( $db->length, 5, "... and we have five elements after abortive -6 index lookup" );
61
cb79ec85 62$db->[-1] = 'elem4.1';
63is( $db->[-1], 'elem4.1' );
64is( $db->[4], 'elem4.1' );
65is( $db->get(4), 'elem4.1' );
66is( $db->fetch(4), 'elem4.1' );
67
baa27ab6 68throws_ok {
69 $db->[-6] = 'whoops!';
70} qr/Modification of non-creatable array value attempted, subscript -6/, "Correct error thrown";
71
ffed8b01 72my $popped = $db->pop;
73is( $db->length, 4, "... and we have four after popping" );
74is( $db->[0], 'elem0', "0th element still there after popping" );
75is( $db->[1], 'elem1', "1st element still there after popping" );
76is( $db->[2], 'elem2', "2nd element still there after popping" );
77is( $db->[3], 'elem3', "3rd element still there after popping" );
cb79ec85 78is( $popped, 'elem4.1', "Popped value is correct" );
ffed8b01 79
80my $shifted = $db->shift;
81is( $db->length, 3, "... and we have three after shifting" );
82is( $db->[0], 'elem1', "0th element still there after shifting" );
83is( $db->[1], 'elem2', "1st element still there after shifting" );
84is( $db->[2], 'elem3', "2nd element still there after shifting" );
85is( $shifted, 'elem0', "Shifted value is correct" );
86
87##
88# delete
89##
90my $deleted = $db->delete(0);
91is( $db->length, 3, "... and we still have three after deleting" );
92is( $db->[0], undef, "0th element now undef" );
93is( $db->[1], 'elem2', "1st element still there after deleting" );
94is( $db->[2], 'elem3', "2nd element still there after deleting" );
81d3d316 95is( $deleted, 'elem1', "Deleted value is correct" );
ffed8b01 96
97is( $db->delete(99), undef, 'delete on an element not in the array returns undef' );
98is( $db->length, 3, "... and we still have three after a delete on an out-of-range index" );
99
100is( delete $db->[99], undef, 'DELETE on an element not in the array returns undef' );
101is( $db->length, 3, "... and we still have three after a DELETE on an out-of-range index" );
102
103is( $db->delete(-99), undef, 'delete on an element (neg) not in the array returns undef' );
104is( $db->length, 3, "... and we still have three after a DELETE on an out-of-range negative index" );
105
106is( delete $db->[-99], undef, 'DELETE on an element (neg) not in the array returns undef' );
107is( $db->length, 3, "... and we still have three after a DELETE on an out-of-range negative index" );
108
109$deleted = $db->delete(-2);
110is( $db->length, 3, "... and we still have three after deleting" );
111is( $db->[0], undef, "0th element still undef" );
9281d66b 112is( $db->[1], undef, "1st element now undef" );
ffed8b01 113is( $db->[2], 'elem3', "2nd element still there after deleting" );
9281d66b 114is( $deleted, 'elem2', "Deleted value is correct" );
ffed8b01 115
116$db->[1] = 'elem2';
117
118##
119# exists
120##
121ok( $db->exists(1), "The 1st value exists" );
122ok( !$db->exists(0), "The 0th value doesn't exists" );
123ok( !$db->exists(22), "The 22nd value doesn't exists" );
baa27ab6 124ok( $db->exists(-1), "The -1st value does exists" );
ffed8b01 125ok( !$db->exists(-22), "The -22nd value doesn't exists" );
126
127##
128# clear
129##
130ok( $db->clear(), "clear() returns true if the file was ever non-empty" );
131is( $db->length(), 0, "After clear(), no more elements" );
132
133is( $db->pop, undef, "pop on an empty array returns undef" );
134is( $db->length(), 0, "After pop() on empty array, length is still 0" );
135
136is( $db->shift, undef, "shift on an empty array returns undef" );
137is( $db->length(), 0, "After shift() on empty array, length is still 0" );
138
8f6d6ed0 139is( $db->unshift( 1, 2, 3 ), 3, "unshift returns the number of elements in the array" );
140is( $db->unshift( 1, 2, 3 ), 6, "unshift returns the number of elements in the array" );
141is( $db->push( 1, 2, 3 ), 9, "push returns the number of elements in the array" );
142
ffed8b01 143is( $db->length(), 9, "After unshift and push on empty array, length is now 9" );
144
145$db->clear;
146
147##
148# multi-push
149##
150$db->push( 'elem first', "elem middle", "elem last" );
151is( $db->length, 3, "3-element push results in three elements" );
152is($db->[0], "elem first", "First element is 'elem first'");
153is($db->[1], "elem middle", "Second element is 'elem middle'");
154is($db->[2], "elem last", "Third element is 'elem last'");
155
156##
157# splice with length 1
158##
8fec41b9 159my @returned = $db->splice( 1, 1, "middle A", "middle B" );
160is( scalar(@returned), 1, "One element was removed" );
161is( $returned[0], 'elem middle', "... and it was correctly removed" );
ffed8b01 162is($db->length(), 4);
163is($db->[0], "elem first");
164is($db->[1], "middle A");
165is($db->[2], "middle B");
166is($db->[3], "elem last");
167
168##
169# splice with length of 0
170##
8fec41b9 171@returned = $db->splice( -1, 0, "middle C" );
172is( scalar(@returned), 0, "No elements were removed" );
ffed8b01 173is($db->length(), 5);
174is($db->[0], "elem first");
175is($db->[1], "middle A");
176is($db->[2], "middle B");
177is($db->[3], "middle C");
178is($db->[4], "elem last");
179
180##
181# splice with length of 3
182##
8fec41b9 183my $returned = $db->splice( 1, 3, "middle ABC" );
184is( $returned, 'middle C', "Just the last element was returned" );
ffed8b01 185is($db->length(), 3);
186is($db->[0], "elem first");
187is($db->[1], "middle ABC");
188is($db->[2], "elem last");
189
53fdf3d7 190@returned = $db->splice( 1 );
191is($db->length(), 1);
192is($db->[0], "elem first");
193is($returned[0], "middle ABC");
194is($returned[1], "elem last");
195
196$db->push( @returned );
197
198@returned = $db->splice( 1, -1 );
199is($db->length(), 2);
200is($db->[0], "elem first");
201is($db->[1], "elem last");
202is($returned[0], "middle ABC");
203
ffed8b01 204$db->[0] = [ 1 .. 3 ];
205$db->[1] = { a => 'foo' };
ffed8b01 206is( $db->[1]->fetch('a'), 'foo', "Reuse of same space with hash successful" );
994ccd8e 207is( $db->[0]->length, 3, "Reuse of same space with array successful" );
4b603f25 208
209# Test autovivification
210
211$db->[9999]{bar} = 1;
212ok( $db->[9999] );
213cmp_ok( $db->[9999]{bar}, '==', 1 );