|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace itertools; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayIterator; |
|
6
|
|
|
use PHPUnit_Framework_TestCase; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class BatchMapIteratorTest extends PHPUnit_Framework_TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
/** @test */ |
|
12
|
|
|
public function testBatchMapIterator() |
|
13
|
|
|
{ |
|
14
|
|
|
$input = array(1, 1, 1, 1, 1, 1, 1); |
|
15
|
|
|
$mappedInput = new BatchMapIterator($input, function($elements) { |
|
16
|
|
|
foreach($elements as & $element) { |
|
17
|
|
|
$element += count($elements); |
|
18
|
|
|
} |
|
19
|
|
|
return $elements; |
|
20
|
|
|
}, 3); |
|
21
|
|
|
$this->assertEquals(array(4, 4, 4, 4, 4, 4, 2), iterator_to_array($mappedInput)); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** @test */ |
|
25
|
|
|
public function testBatchMapIteratorMerge() |
|
26
|
|
|
{ |
|
27
|
|
|
$input = new ArrayIterator(array(1, 1, 1, 1)); |
|
28
|
|
|
$mappedInput = new BatchMapIterator(new BatchMapIterator($input, function($batch) { |
|
29
|
|
|
foreach($batch as & $a) { $a += 1; } |
|
30
|
|
|
return $batch; |
|
31
|
|
|
}, 3), function($batch) { |
|
32
|
|
|
foreach($batch as & $a) { $a *= 2; } |
|
33
|
|
|
return $batch; |
|
34
|
|
|
}, 3); |
|
35
|
|
|
$this->assertEquals(array(4, 4, 4, 4), iterator_to_array($mappedInput)); |
|
36
|
|
|
$this->assertSame($input, $mappedInput->getInnerIterator()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** @test */ |
|
40
|
|
|
public function testSplitKeyValues() |
|
41
|
|
|
{ |
|
42
|
|
|
$input = array(3 => 7, 4 => 8, 7 => 2); |
|
43
|
|
|
$mappedInput = new BatchMapIterator($input, function($pairs) { |
|
44
|
|
|
foreach($pairs as & $pair) { |
|
45
|
|
|
$pair = array($pair[1], $pair[0]); |
|
46
|
|
|
} |
|
47
|
|
|
return $pairs; |
|
48
|
|
|
}, 3, BatchMapIterator::SPLIT_KEY_VALUES); |
|
49
|
|
|
$this->assertEquals(array(7 => 3, 8 => 4, 2 => 7), iterator_to_array($mappedInput)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** @test */ |
|
53
|
|
|
public function testAddKeyList() |
|
54
|
|
|
{ |
|
55
|
|
|
$input = array(3 => 7, 4 => 8, 7 => 2); |
|
56
|
|
|
$mappedInput = new BatchMapIterator($input, function($keys, $values) { |
|
57
|
|
|
return array($values, $keys); |
|
58
|
|
|
}, 3, BatchMapIterator::ADD_KEY_LIST); |
|
59
|
|
|
$this->assertEquals(array(7 => 3, 8 => 4, 2 => 7), iterator_to_array($mappedInput)); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|