forked from brianhaveri/Underscore.php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArraysTest.php
201 lines (162 loc) · 8.37 KB
/
ArraysTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
include_once(__DIR__ . '/../underscore.php');
class UnderscoreArraysTest extends PHPUnit_Framework_TestCase {
public function testFirst() {
// from js
$this->assertEquals(1, _::first(array(1,2,3)), 'can pull out the first element of an array');
$this->assertEquals(array(), _::first(array(1,2,3), 0), 'can pass an index to first');
$this->assertEquals(array(1, 2), _::first(array(1,2,3), 2), 'can pass an index to first');
$this->assertEquals(1, _(array(1,2,3))->first(), 'can perform OO-style "first()"');
$result = _::map(array(array(1,2,3), array(1,2,3)), function($vals) {
return _::first($vals);
});
$this->assertEquals(array(1,1), $result, 'works well with _.map');
$func = function() { return _::first(func_get_args()); };
$result = $func(4,3,2,1);
$this->assertEquals(4, $result, 'works on an arguments object');
// extra
$this->assertEquals(array(1), _::first(array(1,2,3), 1), 'can pass an index of 1 to first');
$this->assertEquals(array(4,5), _(array(4,5,6,7))->first(2), 'can perform OO-style "first()" with index passed');
}
public function testRest() {
$numbers = array(1,2,3,4);
// from js
$this->assertEquals(array(2,3,4), _::rest($numbers), 'working rest()');
$this->assertEquals(array(1,2,3,4), _::rest($numbers, 0), 'working rest(0)');
$this->assertEquals(array(3,4), _::rest($numbers, 2), 'rest can take an index');
// @todo
/*
var result = (function(){ return _(arguments).tail(); })(1, 2, 3, 4);
equals(result.join(', '), '2, 3, 4', 'aliased as tail and works on arguments object');
result = _.map([[1,2,3],[1,2,3]], _.rest);
equals(_.flatten(result).join(','), '2,3,2,3', 'works well with _.map');
*/
}
public function testLast() {
// from js
$this->assertEquals(3, _::last(array(1,2,3)), 'can pull out the last element of an array');
// @todo
/*
var result = (function(){ return _(arguments).last(); })(1, 2, 3, 4);
equals(result, 4, 'works on an arguments object');
*/
}
public function testCompact() {
$vals = array(0, 1, false, 2, false, 3);
// from js
$this->assertEquals(3, count(_::compact($vals)), 'can trim out all falsy values');
// extra
$this->assertEquals(array(1, 2, 3), _::compact($vals), 'can remove all falsy values');
// @todo
/*
var result = (function(){ return _(arguments).compact().length; })(0, 1, false, 2, false, 3);
equals(result, 3, 'works on an arguments object');
*/
}
public function testFlatten() {
$list = array(1, array(2), array(3, array(array(array(4)))));
// from js
$this->assertEquals(array(1,2,3,4), _::flatten($list), 'can flatten nested arrays');
// @todo
/*
var result = (function(){ return _.flatten(arguments); })(1, [2], [3, [[[4]]]]);
equals(result.join(', '), '1, 2, 3, 4', 'works on an arguments object');
*/
}
public function testWithout() {
$list = array(1, 2, 1, 0, 3, 1, 4);
// from js
$this->assertEquals(array(1=>2,4=>3,6=>4), _::without($list, 0, 1), 'can remove all instances of an object');
$list = array(
(object) array('one'=>1),
(object) array('two'=>2)
);
$this->assertEquals(2, count(_::without($list, (object) array('one'=>1))), 'uses real object identity for comparisons.');
$this->assertEquals(1, count(_::without($list, $list[0])), 'ditto.');
// @todo
/*
var result = (function(){ return _.without(arguments, 0, 1); })(1, 2, 1, 0, 3, 1, 4);
equals(result.join(', '), '2, 3, 4', 'works on an arguments object');
*/
}
public function testUniq() {
// from js
$list = array(1, 2, 1, 3, 1, 9);
$this->assertEquals(array(1, 2, 3, 9), _::uniq($list), 'can find the unique values of an unsorted array');
$list = array(1, 1, 1, 2, 2, 3);
$this->assertEquals(array(1, 2, 3), _::uniq($list), 'can find the unique values of a sorted array faster');
$func = function() { return _::uniq(func_get_args()); };
$result = $func(1,2,1,3,1,4);
$this->assertEquals(array(1,2,3,4), $result, 'works on an arguments object');
// extra
$this->assertEquals(array(4,5,6), _(array(4,5,4,4,5,5,6))->uniq(), 'works with OO call');
}
public function testIntersect() {
// from js
$stooges = array('moe', 'curly', 'larry');
$leaders = array('moe', 'groucho');
$this->assertEquals(array('moe'), _::intersect($stooges, $leaders), 'can take the set intersection of two arrays');
$this->assertEquals(array('moe'), _($stooges)->intersect($leaders), 'can perform an OO-style intersection');
$func = function() use ($leaders) { $args = func_get_args(); return _::intersect($args[0], $leaders); };
$result = $func($stooges);
$this->assertEquals(array('moe'), $result, 'works on an arguments object');
}
public function testZip() {
// from js
$names = array('moe', 'larry', 'curly');
$ages = array(30, 40, 50);
$leaders= array(true);
$stooges= array(array('moe', 30, true), array('larry', 40, null), array('curly', 50, null));
$this->assertEquals($stooges, _::zip($names, $ages, $leaders), 'zipped together arrays of different lengths');
// extra
$this->assertEquals($stooges, _($names)->zip($ages, $leaders), 'can perform OO-style zips of different length arrays');
$numbers = array(1,2,3);
$letters = array('a','b','c');
$expected = array(array(1,'a'), array(2,'b'), array(3,'c'));
$this->assertEquals($expected, _::zip($numbers, $letters), 'can perform normal zips');
$this->assertEquals($expected, _($numbers)->zip($letters), 'can perform OO-style zips');
}
public function testIndexOf() {
// from js
$numbers = array(1,2,3);
$this->assertEquals(1, _::indexOf($numbers, 2), 'can compute indexOf');
$this->assertEquals(-1, _::indexOf(null, 2), 'handles nulls properly');
$numbers = array(10, 20, 30, 40, 50);
$this->assertEquals(-1, _::indexOf($numbers, 35), '35 is not in the list');
$this->assertEquals(3, _::indexOf($numbers, 40), '40 is in the list');
$numbers = array(1, 40, 40, 40, 40, 40, 40, 40, 50, 60, 70);
$this->assertEquals(1, _::indexOf($numbers, 40), '40 is in the list');
// @todo
/*
var result = (function(){ return _.indexOf(arguments, 2); })(1, 2, 3);
equals(result, 1, 'works on an arguments object');
*/
}
public function testLastIndexOf() {
// from js
$numbers = array(1, 0, 1, 0, 0, 1, 0, 0, 0);
$this->assertEquals(5, _::lastIndexOf($numbers, 1), 'can computer lastIndexOf');
$this->assertEquals(8, _::lastIndexOf($numbers, 0), 'lastIndexOf the other element');
$this->assertEquals(-1, _::lastIndexOf(null, 2), 'handles nulls properly');
// @todo
/*
var result = (function(){ return _.lastIndexOf(arguments, 1); })(1, 0, 1, 0, 0, 1, 0, 0, 0);
equals(result, 5, 'works on an arguments object');
*/
}
public function testRange() {
// from js
$this->assertEquals(array(), _::range(0), 'range with 0 as a first argument generates an empty array');
$this->assertEquals(array(0,1,2,3), _::range(4), 'range with a single positive argument generates an array of elements 0,1,2,...,n-1');
$this->assertEquals(array(5,6,7), _::range(5, 8), 'range with two arguments a & b, a<b generates an array of elements a,a+1,a+2,...,b-2,b-1');
$this->assertEquals(array(), _::range(8, 5), 'range with two arguments a & b, b<a generates an empty array');
$this->assertEquals(array(3,6,9), _::range(3, 10, 3), 'range with three arguments a & b & c, c < b-a, a < b generates an array of elements a,a+c,a+2c,...,b - (multiplier of a) < c');
$this->assertEquals(array(3), _::range(3, 10, 15), 'range with three arguments a & b & c, c > b-a, a < b generates an array with a single element, equal to a');
$this->assertEquals(array(12,10,8), _::range(12, 7, -2), 'range with three arguments a & b & c, a > b, c < 0 generates an array of elements a,a-c,a-2c and ends with the number not less than b');
$this->assertEquals(array(0, -1, -2, -3, -4, -5, -6, -7, -8, -9), _::range(0, -10, -1), 'final example in the Python docs');
// extra
$this->assertEquals(array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), _::range(10));
$this->assertEquals(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), _::range(1, 11));
$this->assertEquals(array(0, 5, 10, 15, 20, 25), _::range(0, 30, 5));
}
}