Skip to content

Commit

Permalink
tests: Add SA tests for Random and Shuffle operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Oct 4, 2021
1 parent ef61f57 commit cefe0f5
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
67 changes: 67 additions & 0 deletions tests/static-analysis/random.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

include __DIR__ . '/../../vendor/autoload.php';

use loophp\collection\Collection;
use loophp\collection\Contract\Collection as CollectionInterface;

/**
* @param CollectionInterface<int, int> $collection
*/
function random_checkIntList(CollectionInterface $collection): void
{
}
/**
* @param CollectionInterface<int, string> $collection
*/
function random_checkStringList(CollectionInterface $collection): void
{
}
/**
* @param CollectionInterface<int, bool> $collection
*/
function random_checkBoolList(CollectionInterface $collection): void
{
}
/**
* @param CollectionInterface<string, string> $collection
*/
function random_checkStringMap(CollectionInterface $collection): void
{
}

$intGenerator = static function (): Generator {
while (true) {
yield mt_rand();
}
};

$boolGenerator = static function (): Generator {
while (true) {
yield (0 === mt_rand() % 2) ? true : false;
}
};

$stringGenerator = static function (): Generator {
while (true) {
yield chr(random_int(0, 255));
}
};

$stringStringGenerator = static function (): Generator {
while (true) {
yield chr(random_int(0, 255)) => chr(random_int(0, 255));
}
};

random_checkIntList(Collection::fromIterable($intGenerator())->random());
random_checkStringList(Collection::fromIterable($stringGenerator())->random());
random_checkBoolList(Collection::fromIterable($boolGenerator())->random());
random_checkStringMap(Collection::fromIterable($stringStringGenerator())->random());
67 changes: 67 additions & 0 deletions tests/static-analysis/shuffle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

include __DIR__ . '/../../vendor/autoload.php';

use loophp\collection\Collection;
use loophp\collection\Contract\Collection as CollectionInterface;

/**
* @param CollectionInterface<int, int> $collection
*/
function shuffle_checkIntList(CollectionInterface $collection): void
{
}
/**
* @param CollectionInterface<int, string> $collection
*/
function shuffle_checkStringList(CollectionInterface $collection): void
{
}
/**
* @param CollectionInterface<int, bool> $collection
*/
function shuffle_checkBoolList(CollectionInterface $collection): void
{
}
/**
* @param CollectionInterface<string, string> $collection
*/
function shuffle_checkStringMap(CollectionInterface $collection): void
{
}

$intGenerator = static function (): Generator {
while (true) {
yield mt_rand();
}
};

$boolGenerator = static function (): Generator {
while (true) {
yield (0 === mt_rand() % 2) ? true : false;
}
};

$stringGenerator = static function (): Generator {
while (true) {
yield chr(random_int(0, 255));
}
};

$stringStringGenerator = static function (): Generator {
while (true) {
yield chr(random_int(0, 255)) => chr(random_int(0, 255));
}
};

shuffle_checkIntList(Collection::fromIterable($intGenerator())->shuffle());
shuffle_checkStringList(Collection::fromIterable($stringGenerator())->shuffle());
shuffle_checkBoolList(Collection::fromIterable($boolGenerator())->shuffle());
shuffle_checkStringMap(Collection::fromIterable($stringStringGenerator())->shuffle());

0 comments on commit cefe0f5

Please sign in to comment.