1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace itertools; |
4
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class LookAheadIteratorTest extends PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
/** @test */ |
11
|
|
|
public function testBasicIteration() |
12
|
|
|
{ |
13
|
|
|
$it = new LookAheadIterator(range(0, 10)); |
14
|
|
|
$this->assertEquals(range(0, 10), iterator_to_array($it), 'The LookAheadIterator should not modify the inner iterator'); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** @test */ |
18
|
|
|
public function testSimpleLookAheads() |
19
|
|
|
{ |
20
|
|
|
$it = new LookAheadIterator(range(0, 10)); |
21
|
|
|
$i = 0; |
22
|
|
|
foreach($it as $key => $value) { |
23
|
|
|
$this->assertEquals($i, $value); |
24
|
|
|
if($key < 10) { |
25
|
|
|
$this->assertEquals($it->getNext(), $i + 1); |
26
|
|
|
} |
27
|
|
|
$this->assertEquals($i, $value); |
28
|
|
|
$this->assertEquals($i, $key); |
29
|
|
|
$i += 1; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** @test */ |
34
|
|
|
public function testLookAheadWithoutRewind() |
35
|
|
|
{ |
36
|
|
|
$it = new LookAheadIterator(range(0, 10)); |
37
|
|
|
$this->assertEquals(10, $it->getNext(10)); |
38
|
|
|
$this->assertEquals(range(0, 10), iterator_to_array($it)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** @test */ |
42
|
|
|
public function testKeyLookAhead() |
43
|
|
|
{ |
44
|
|
|
$it = new LookAheadIterator(range(1, 10)); |
45
|
|
|
$this->assertEquals(9, $it->getNextKey(9)); |
46
|
|
|
$this->assertEquals(10, $it->getNext(9)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @test |
51
|
|
|
* @expectedException OutOfBoundsException |
52
|
|
|
*/ |
53
|
|
|
public function testOutOfBounds() |
54
|
|
|
{ |
55
|
|
|
$it = new LookAheadIterator(array()); |
56
|
|
|
$it->getNext(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** @test */ |
60
|
|
|
public function testHasNext() |
61
|
|
|
{ |
62
|
|
|
$it = new LookAheadIterator(range(0, 10)); |
63
|
|
|
$i = 0; |
64
|
|
|
foreach($it as $e) { |
65
|
|
|
$this->assertEquals($it->hasNext(), $e < 10); |
66
|
|
|
$i += 1; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|