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