2019-12-24 01:52:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Psl\Tests\Arr;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2019-12-25 00:35:14 +01:00
|
|
|
use Psl\Arr;
|
|
|
|
use Psl\Str;
|
2019-12-24 01:52:07 +01:00
|
|
|
|
2020-10-15 10:18:03 +02:00
|
|
|
final class SortByTest extends TestCase
|
2019-12-24 01:52:07 +01:00
|
|
|
{
|
2019-12-25 00:35:14 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider provideData
|
|
|
|
*/
|
2020-08-27 16:14:37 +02:00
|
|
|
public function testSortBy(array $expected, array $array, callable $scalar_fun, ?callable $comp = null): void
|
2019-12-25 00:35:14 +01:00
|
|
|
{
|
2020-10-15 10:18:03 +02:00
|
|
|
static::assertSame($expected, Arr\sort_by($array, $scalar_fun, $comp));
|
2019-12-25 00:35:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideData(): array
|
|
|
|
{
|
2020-09-05 17:23:37 +02:00
|
|
|
$a = [1, 2];
|
|
|
|
$b = [1, 2, 3, 4];
|
|
|
|
$c = ['a' => 'foo', 'b' => 'bar', 'c' => 'baz', 'd' => 'qux', 'e' => 'lax'];
|
|
|
|
$expected = [$a, $b, $c];
|
|
|
|
$array = [$b, $c, $a];
|
2020-08-27 16:14:37 +02:00
|
|
|
$scalar_fun =
|
|
|
|
/**
|
|
|
|
* @param array<array-key, string|int> $arr
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*
|
|
|
|
* @psalm-pure
|
|
|
|
*/
|
2020-10-15 10:18:03 +02:00
|
|
|
static fn ($arr) => Arr\count($arr);
|
2019-12-25 00:35:14 +01:00
|
|
|
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
$expected,
|
2020-08-27 16:14:37 +02:00
|
|
|
$array,
|
2019-12-25 00:35:14 +01:00
|
|
|
$scalar_fun,
|
|
|
|
],
|
|
|
|
|
|
|
|
[
|
|
|
|
['a', 'b', 'c', 'd'],
|
|
|
|
['d', 'a', 'b', 'c'],
|
2020-08-27 16:14:37 +02:00
|
|
|
/**
|
|
|
|
* @param string $v
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*
|
|
|
|
* @psalm-pure
|
|
|
|
*/
|
2020-10-15 10:18:03 +02:00
|
|
|
static fn ($v) => $v,
|
2019-12-25 00:35:14 +01:00
|
|
|
],
|
|
|
|
|
|
|
|
[
|
|
|
|
['a'],
|
|
|
|
['a'],
|
2020-08-27 16:14:37 +02:00
|
|
|
/**
|
|
|
|
* @param string $v
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*
|
|
|
|
* @psalm-pure
|
|
|
|
*/
|
2020-10-15 10:18:03 +02:00
|
|
|
static fn ($v) => $v,
|
2019-12-25 00:35:14 +01:00
|
|
|
],
|
|
|
|
|
|
|
|
[
|
|
|
|
['d', 'c', 'b', 'a'],
|
|
|
|
['d', 'a', 'b', 'c'],
|
2020-08-27 16:14:37 +02:00
|
|
|
/**
|
|
|
|
* @param string $v
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*
|
|
|
|
* @psalm-pure
|
|
|
|
*/
|
2020-10-15 10:18:03 +02:00
|
|
|
static fn ($v) => $v,
|
2020-08-27 16:14:37 +02:00
|
|
|
/**
|
|
|
|
* @param string $a
|
|
|
|
* @param string $b
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*
|
|
|
|
* @psalm-pure
|
|
|
|
*/
|
2020-10-15 10:18:03 +02:00
|
|
|
static fn (string $a, string $b) => Str\ord($a) > Str\ord($b) ? -1 : 1,
|
2019-12-25 00:35:14 +01:00
|
|
|
],
|
|
|
|
|
|
|
|
[
|
|
|
|
['bar', 'qux'],
|
|
|
|
['foo' => 'bar', 'baz' => 'qux'],
|
2020-08-27 16:14:37 +02:00
|
|
|
/**
|
|
|
|
* @param string $v
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*
|
|
|
|
* @psalm-pure
|
|
|
|
*/
|
2020-10-15 10:18:03 +02:00
|
|
|
static fn ($v) => $v,
|
2019-12-25 00:35:14 +01:00
|
|
|
],
|
|
|
|
|
|
|
|
[
|
|
|
|
['jumped', 'the', 'quick', 'brown', 'fox'],
|
|
|
|
['the', 'quick', 'brown', 'fox', 'jumped'],
|
2020-08-27 16:14:37 +02:00
|
|
|
/**
|
|
|
|
* @param string $v
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*
|
|
|
|
* @psalm-pure
|
|
|
|
*/
|
2020-10-15 10:18:03 +02:00
|
|
|
static fn ($v) => Str\Byte\reverse($v),
|
2019-12-25 00:35:14 +01:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2019-12-24 01:52:07 +01:00
|
|
|
}
|