Converted all tests to use File::Temp instead of t/test.db
[dbsrgits/DBM-Deep.git] / t / 04_array.t
1 ##
2 # DBM::Deep Test
3 ##
4 use strict;
5 use Test::More tests => 107;
6 use Test::Exception;
7 use File::Temp qw( tempfile tempdir );
8
9 use_ok( 'DBM::Deep' );
10
11 my $dir = tempdir( CLEANUP => 1 );
12 my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
13 my $db = DBM::Deep->new(
14         file => $filename,
15         type => DBM::Deep->TYPE_ARRAY
16 );
17
18 TODO: {
19     local $TODO = "How is this test ever supposed to pass?";
20     ok( !$db->clear, "If the file has never been written to, clear() returns false" );
21 }
22
23 ##
24 # basic put/get/push
25 ##
26 $db->[0] = "elem1";
27 $db->push( "elem2" );
28 $db->put(2, "elem3");
29 $db->store(3, "elem4");
30 $db->unshift("elem0");
31
32 is( $db->[0], 'elem0', "Array get for shift works" );
33 is( $db->[1], 'elem1', "Array get for array set works" );
34 is( $db->[2], 'elem2', "Array get for push() works" );
35 is( $db->[3], 'elem3', "Array get for put() works" );
36 is( $db->[4], 'elem4', "Array get for store() works" );
37
38 is( $db->get(0), 'elem0', "get() for shift() works" );
39 is( $db->get(1), 'elem1', "get() for array set works" );
40 is( $db->get(2), 'elem2', "get() for push() works" );
41 is( $db->get(3), 'elem3', "get() for put() works" );
42 is( $db->get(4), 'elem4', "get() for store() works" );
43
44 is( $db->fetch(0), 'elem0', "fetch() for shift() works" );
45 is( $db->fetch(1), 'elem1', "fetch() for array set works" );
46 is( $db->fetch(2), 'elem2', "fetch() for push() works" );
47 is( $db->fetch(3), 'elem3', "fetch() for put() works" );
48 is( $db->fetch(4), 'elem4', "fetch() for store() works" );
49
50 is( $db->length, 5, "... and we have five elements" );
51
52 is( $db->[-1], $db->[4], "-1st index is 4th index" );
53 is( $db->[-2], $db->[3], "-2nd index is 3rd index" );
54 is( $db->[-3], $db->[2], "-3rd index is 2nd index" );
55 is( $db->[-4], $db->[1], "-4th index is 1st index" );
56 is( $db->[-5], $db->[0], "-5th index is 0th index" );
57 is( $db->[-6], undef, "-6th index is undef" );
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( $shifted, 'elem0', "Shifted value is correct" );
84
85 ##
86 # delete
87 ##
88 my $deleted = $db->delete(0);
89 is( $db->length, 3, "... and we still have three after deleting" );
90 is( $db->[0], undef, "0th element now undef" );
91 is( $db->[1], 'elem2', "1st element still there after deleting" );
92 is( $db->[2], 'elem3', "2nd element still there after deleting" );
93 is( $deleted, 'elem1', "Deleted value is correct" );
94
95 is( $db->delete(99), undef, 'delete on an element not in the array returns undef' );
96 is( $db->length, 3, "... and we still have three after a delete on an out-of-range index" );
97
98 is( delete $db->[99], undef, 'DELETE on an element not in the array returns undef' );
99 is( $db->length, 3, "... and we still have three after a DELETE on an out-of-range index" );
100
101 is( $db->delete(-99), undef, 'delete on an element (neg) not in the array returns undef' );
102 is( $db->length, 3, "... and we still have three after a DELETE on an out-of-range negative index" );
103
104 is( delete $db->[-99], undef, 'DELETE on an element (neg) not in the array returns undef' );
105 is( $db->length, 3, "... and we still have three after a DELETE on an out-of-range negative index" );
106
107 $deleted = $db->delete(-2);
108 is( $db->length, 3, "... and we still have three after deleting" );
109 is( $db->[0], undef, "0th element still undef" );
110 is( $db->[1], undef, "1st element now undef" );
111 is( $db->[2], 'elem3', "2nd element still there after deleting" );
112 is( $deleted, 'elem2', "Deleted value is correct" );
113
114 $db->[1] = 'elem2';
115
116 ##
117 # exists
118 ##
119 ok( $db->exists(1), "The 1st value exists" );
120 ok( !$db->exists(0), "The 0th value doesn't exists" );
121 ok( !$db->exists(22), "The 22nd value doesn't exists" );
122 ok( $db->exists(-1), "The -1st value does exists" );
123 ok( !$db->exists(-22), "The -22nd value doesn't exists" );
124
125 ##
126 # clear
127 ##
128 ok( $db->clear(), "clear() returns true if the file was ever non-empty" );
129 is( $db->length(), 0, "After clear(), no more elements" );
130
131 is( $db->pop, undef, "pop on an empty array returns undef" );
132 is( $db->length(), 0, "After pop() on empty array, length is still 0" );
133
134 is( $db->shift, undef, "shift on an empty array returns undef" );
135 is( $db->length(), 0, "After shift() on empty array, length is still 0" );
136
137 is( $db->unshift( 1, 2, 3 ), 3, "unshift returns the number of elements in the array" );
138 is( $db->unshift( 1, 2, 3 ), 6, "unshift returns the number of elements in the array" );
139 is( $db->push( 1, 2, 3 ), 9, "push returns the number of elements in the array" );
140
141 is( $db->length(), 9, "After unshift and push on empty array, length is now 9" );
142
143 $db->clear;
144
145 ##
146 # multi-push
147 ##
148 $db->push( 'elem first', "elem middle", "elem last" );
149 is( $db->length, 3, "3-element push results in three elements" );
150 is($db->[0], "elem first", "First element is 'elem first'");
151 is($db->[1], "elem middle", "Second element is 'elem middle'");
152 is($db->[2], "elem last", "Third element is 'elem last'");
153
154 ##
155 # splice with length 1
156 ##
157 my @returned = $db->splice( 1, 1, "middle A", "middle B" );
158 is( scalar(@returned), 1, "One element was removed" );
159 is( $returned[0], 'elem middle', "... and it was correctly removed" );
160 is($db->length(), 4);
161 is($db->[0], "elem first");
162 is($db->[1], "middle A");
163 is($db->[2], "middle B");
164 is($db->[3], "elem last");
165
166 ##
167 # splice with length of 0
168 ##
169 @returned = $db->splice( -1, 0, "middle C" );
170 is( scalar(@returned), 0, "No elements were removed" );
171 is($db->length(), 5);
172 is($db->[0], "elem first");
173 is($db->[1], "middle A");
174 is($db->[2], "middle B");
175 is($db->[3], "middle C");
176 is($db->[4], "elem last");
177
178 ##
179 # splice with length of 3
180 ##
181 my $returned = $db->splice( 1, 3, "middle ABC" );
182 is( $returned, 'middle C', "Just the last element was returned" );
183 is($db->length(), 3);
184 is($db->[0], "elem first");
185 is($db->[1], "middle ABC");
186 is($db->[2], "elem last");
187
188 @returned = $db->splice( 1 );
189 is($db->length(), 1);
190 is($db->[0], "elem first");
191 is($returned[0], "middle ABC");
192 is($returned[1], "elem last");
193
194 $db->push( @returned );
195
196 @returned = $db->splice( 1, -1 );
197 is($db->length(), 2);
198 is($db->[0], "elem first");
199 is($db->[1], "elem last");
200 is($returned[0], "middle ABC");
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" );