2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-12-16 00:28:22 +01:00
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
2017-05-02 07:22:53 +02:00
|
|
|
use Amp\Delayed;
|
2017-04-27 17:51:06 +02:00
|
|
|
use Amp\Iterator;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Loop;
|
2017-04-28 06:53:04 +02:00
|
|
|
use Amp\PHPUnit\TestException;
|
2017-03-14 00:52:57 +01:00
|
|
|
use Amp\Producer;
|
2017-05-02 07:22:53 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2017-05-02 07:22:53 +02:00
|
|
|
class MergeTest extends TestCase {
|
2017-03-14 00:52:57 +01:00
|
|
|
public function getArrays() {
|
2016-12-16 00:28:22 +01:00
|
|
|
return [
|
2017-03-14 00:52:57 +01:00
|
|
|
[[\range(1, 3), \range(4, 6)], [1, 4, 2, 5, 3, 6]],
|
|
|
|
[[\range(1, 5), \range(6, 8)], [1, 6, 2, 7, 3, 8, 4, 5]],
|
|
|
|
[[\range(1, 4), \range(5, 10)], [1, 5, 2, 6, 3, 7, 4, 8, 9, 10]],
|
2016-12-16 00:28:22 +01:00
|
|
|
];
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
/**
|
2017-03-14 00:52:57 +01:00
|
|
|
* @dataProvider getArrays
|
2016-12-16 00:28:22 +01:00
|
|
|
*
|
2017-04-27 17:51:06 +02:00
|
|
|
* @param array $iterators
|
2016-12-16 00:28:22 +01:00
|
|
|
* @param array $expected
|
|
|
|
*/
|
2017-04-27 17:51:06 +02:00
|
|
|
public function testMerge(array $iterators, array $expected) {
|
|
|
|
Loop::run(function () use ($iterators, $expected) {
|
|
|
|
$iterators = \array_map(function (array $iterator): Iterator {
|
|
|
|
return Iterator\fromIterable($iterator);
|
|
|
|
}, $iterators);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-04-27 17:51:06 +02:00
|
|
|
$iterator = Iterator\merge($iterators);
|
2017-03-14 00:52:57 +01:00
|
|
|
|
2017-04-27 17:51:06 +02:00
|
|
|
while (yield $iterator->advance()) {
|
|
|
|
$this->assertSame(\array_shift($expected), $iterator->getCurrent());
|
2017-04-27 17:32:53 +02:00
|
|
|
}
|
2016-12-16 00:28:22 +01:00
|
|
|
});
|
2017-04-27 17:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testMerge
|
|
|
|
*/
|
|
|
|
public function testMergeWithDelayedEmits() {
|
|
|
|
Loop::run(function () {
|
2017-04-27 17:51:06 +02:00
|
|
|
$iterators = [];
|
2017-05-02 07:22:53 +02:00
|
|
|
$values1 = [new Delayed(10, 1), new Delayed(50, 2), new Delayed(70, 3)];
|
|
|
|
$values2 = [new Delayed(20, 4), new Delayed(40, 5), new Delayed(60, 6)];
|
2017-04-27 17:32:53 +02:00
|
|
|
$expected = [1, 4, 5, 2, 6, 3];
|
|
|
|
|
2017-04-27 17:51:06 +02:00
|
|
|
$iterators[] = new Producer(function (callable $emit) use ($values1) {
|
2017-04-27 17:32:53 +02:00
|
|
|
foreach ($values1 as $value) {
|
|
|
|
yield $emit($value);
|
|
|
|
}
|
|
|
|
});
|
2017-03-14 00:52:57 +01:00
|
|
|
|
2017-04-27 17:51:06 +02:00
|
|
|
$iterators[] = new Producer(function (callable $emit) use ($values2) {
|
2017-04-27 17:32:53 +02:00
|
|
|
foreach ($values2 as $value) {
|
|
|
|
yield $emit($value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-04-27 17:51:06 +02:00
|
|
|
$iterator = Iterator\merge($iterators);
|
2017-04-27 17:32:53 +02:00
|
|
|
|
2017-04-27 17:51:06 +02:00
|
|
|
while (yield $iterator->advance()) {
|
|
|
|
$this->assertSame(\array_shift($expected), $iterator->getCurrent());
|
2017-04-27 17:32:53 +02:00
|
|
|
}
|
|
|
|
});
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
/**
|
|
|
|
* @depends testMerge
|
|
|
|
*/
|
2017-05-01 07:32:56 +02:00
|
|
|
public function testMergeWithFailedIterator() {
|
2017-04-27 17:32:53 +02:00
|
|
|
Loop::run(function () {
|
2017-04-28 06:53:04 +02:00
|
|
|
$exception = new TestException;
|
2017-01-04 02:10:27 +01:00
|
|
|
$producer = new Producer(function (callable $emit) use ($exception) {
|
2016-12-16 00:28:22 +01:00
|
|
|
yield $emit(1); // Emit once before failing.
|
|
|
|
throw $exception;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-04-27 17:51:06 +02:00
|
|
|
$iterator = Iterator\merge([$producer, Iterator\fromIterable(\range(1, 5))]);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-04-27 17:32:53 +02:00
|
|
|
try {
|
2017-04-27 17:51:06 +02:00
|
|
|
while (yield $iterator->advance());
|
2017-04-28 06:53:04 +02:00
|
|
|
$this->fail("The exception used to fail the iterator should be thrown from advance()");
|
|
|
|
} catch (TestException $reason) {
|
2017-04-27 17:32:53 +02:00
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
2016-12-16 00:28:22 +01:00
|
|
|
});
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
/**
|
2017-04-23 19:08:40 +02:00
|
|
|
* @expectedException \TypeError
|
2016-12-16 00:28:22 +01:00
|
|
|
*/
|
2017-05-01 07:32:56 +02:00
|
|
|
public function testNonIterator() {
|
2017-04-27 17:51:06 +02:00
|
|
|
Iterator\merge([1]);
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
|
|
|
}
|