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