|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jasny\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use function Jasny\iterable_sort; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @covers \Jasny\iterable_sort |
|
10
|
|
|
*/ |
|
11
|
|
|
class IterableSortTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
use ProvideIterablesTrait; |
|
14
|
|
|
use LazyExecutionIteratorTrait; |
|
15
|
|
|
|
|
16
|
|
|
protected $sorted = [ |
|
17
|
|
|
"Alpha", |
|
18
|
|
|
"Bravo", |
|
19
|
|
|
"Charlie", |
|
20
|
|
|
"Delta", |
|
21
|
|
|
"Echo", |
|
22
|
|
|
"Foxtrot", |
|
23
|
|
|
"Golf", |
|
24
|
|
|
"Hotel", |
|
25
|
|
|
"India", |
|
26
|
|
|
"Juliet", |
|
27
|
|
|
"Kilo", |
|
28
|
|
|
"Lima", |
|
29
|
|
|
"Mike", |
|
30
|
|
|
"November", |
|
31
|
|
|
"Oscar", |
|
32
|
|
|
"Papa", |
|
33
|
|
|
"Quebec", |
|
34
|
|
|
"Romeo", |
|
35
|
|
|
"Sierra", |
|
36
|
|
|
"Tango", |
|
37
|
|
|
"Uniform", |
|
38
|
|
|
"Victor", |
|
39
|
|
|
"Whiskey", |
|
40
|
|
|
"X-ray", |
|
41
|
|
|
"Yankee", |
|
42
|
|
|
"Zulu" |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
public function provider() |
|
46
|
|
|
{ |
|
47
|
|
|
$values = $this->sorted; |
|
48
|
|
|
shuffle($values); |
|
49
|
|
|
|
|
50
|
|
|
return $this->provideIterables($values); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @dataProvider provider |
|
55
|
|
|
*/ |
|
56
|
|
|
public function test($values) |
|
57
|
|
|
{ |
|
58
|
|
|
$iterator = iterable_sort($values); |
|
59
|
|
|
$result = iterator_to_array($iterator); |
|
60
|
|
|
|
|
61
|
|
|
$this->assertSame($this->sorted, $result); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testSortFlags() |
|
65
|
|
|
{ |
|
66
|
|
|
$values = [ |
|
67
|
|
|
'img1.png', |
|
68
|
|
|
'img10.png', |
|
69
|
|
|
'img12.png', |
|
70
|
|
|
'img2.png' |
|
71
|
|
|
]; |
|
72
|
|
|
|
|
73
|
|
|
$iterator = iterable_sort($values, \SORT_NATURAL); |
|
74
|
|
|
|
|
75
|
|
|
$result = iterator_to_array($iterator); |
|
76
|
|
|
|
|
77
|
|
|
$expected = [ |
|
78
|
|
|
'img1.png', |
|
79
|
|
|
'img2.png', |
|
80
|
|
|
'img10.png', |
|
81
|
|
|
'img12.png' |
|
82
|
|
|
]; |
|
83
|
|
|
$this->assertSame($expected, $result); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function testPreserveKeys() |
|
87
|
|
|
{ |
|
88
|
|
|
$values = [ |
|
89
|
|
|
'one' => 'India', |
|
90
|
|
|
'two' => 'Zulu', |
|
91
|
|
|
'three' => 'Papa', |
|
92
|
|
|
'four' => 'Bravo' |
|
93
|
|
|
]; |
|
94
|
|
|
|
|
95
|
|
|
$iterator = iterable_sort($values, \SORT_REGULAR, true); |
|
96
|
|
|
|
|
97
|
|
|
$result = iterator_to_array($iterator); |
|
98
|
|
|
|
|
99
|
|
|
$expected = [ |
|
100
|
|
|
'four' => 'Bravo', |
|
101
|
|
|
'one' => 'India', |
|
102
|
|
|
'three' => 'Papa', |
|
103
|
|
|
'two' => 'Zulu' |
|
104
|
|
|
]; |
|
105
|
|
|
|
|
106
|
|
|
$this->assertSame($expected, $result); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function testGenerator() |
|
110
|
|
|
{ |
|
111
|
|
|
$keys = [(object)['a' => 'a'], ['b' => 'b'], null, 'd', 'd']; |
|
112
|
|
|
$values = [['n' => 'India'], ['n' => 'Zulu'], ['n' => 'Papa'], ['n' => 'Bravo'], ['n' => 'Foxtrot']]; |
|
113
|
|
|
|
|
114
|
|
|
$loop = function($keys, $values) { |
|
115
|
|
|
foreach ($keys as $i => $key) { |
|
116
|
|
|
yield $key => $values[$i]; |
|
117
|
|
|
} |
|
118
|
|
|
}; |
|
119
|
|
|
|
|
120
|
|
|
$generator = $loop($keys, $values); |
|
121
|
|
|
$iterator = iterable_sort($generator, function ($a, $b) { |
|
122
|
|
|
return $a['n'] <=> $b['n']; |
|
123
|
|
|
}, true); |
|
124
|
|
|
|
|
125
|
|
|
$resultKeys = []; |
|
126
|
|
|
$resultValues = []; |
|
127
|
|
|
|
|
128
|
|
|
foreach ($iterator as $key => $value) { |
|
129
|
|
|
$resultKeys[] = $key; |
|
130
|
|
|
$resultValues[] = $value; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$this->assertSame([$values[3], $values[4], $values[0], $values[2], $values[1]], $resultValues); |
|
134
|
|
|
$this->assertSame([$keys[3], $keys[4], $keys[0], $keys[2], $keys[1]], $resultKeys); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function testCallback() |
|
138
|
|
|
{ |
|
139
|
|
|
$compare = function($a, $b) { |
|
140
|
|
|
return (strlen($a) <=> strlen($b)) ?: $a <=> $b; |
|
141
|
|
|
}; |
|
142
|
|
|
|
|
143
|
|
|
$iterator = iterable_sort($this->sorted, $compare); |
|
144
|
|
|
$result = iterator_to_array($iterator); |
|
145
|
|
|
|
|
146
|
|
|
$expected = $this->sorted; |
|
147
|
|
|
usort($expected, $compare); |
|
148
|
|
|
|
|
149
|
|
|
$this->assertSame($expected, $result); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function testCallbackPreserveKeys() |
|
153
|
|
|
{ |
|
154
|
|
|
$values = [ |
|
155
|
|
|
'one' => 'India', |
|
156
|
|
|
'two' => 'Zulu', |
|
157
|
|
|
'three' => 'Papa', |
|
158
|
|
|
'four' => 'Bravo' |
|
159
|
|
|
]; |
|
160
|
|
|
|
|
161
|
|
|
$compare = function($a, $b) { |
|
162
|
|
|
return (strlen($a) <=> strlen($b)) ?: $a <=> $b; |
|
163
|
|
|
}; |
|
164
|
|
|
|
|
165
|
|
|
$iterator = iterable_sort($values, $compare, true); |
|
166
|
|
|
$result = iterator_to_array($iterator); |
|
167
|
|
|
|
|
168
|
|
|
$expected = [ |
|
169
|
|
|
'three' => 'Papa', |
|
170
|
|
|
'two' => 'Zulu', |
|
171
|
|
|
'four' => 'Bravo', |
|
172
|
|
|
'one' => 'India' |
|
173
|
|
|
]; |
|
174
|
|
|
|
|
175
|
|
|
$this->assertSame($expected, $result); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public function testEmpty() |
|
179
|
|
|
{ |
|
180
|
|
|
$iterator = iterable_sort(new \EmptyIterator()); |
|
181
|
|
|
$result = iterator_to_array($iterator); |
|
182
|
|
|
|
|
183
|
|
|
$this->assertSame([], $result); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Test that nothing happens when not iterating |
|
188
|
|
|
*/ |
|
189
|
|
|
public function testLazyExecution() |
|
190
|
|
|
{ |
|
191
|
|
|
$iterator = $this->createLazyExecutionIterator(); |
|
192
|
|
|
|
|
193
|
|
|
iterable_sort($iterator); |
|
194
|
|
|
|
|
195
|
|
|
$this->assertTrue(true, "No warning"); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|