File: Random.generate%28%29.phpt

package info (click to toggle)
php-nette-utils 4.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,420 kB
  • sloc: php: 4,069; xml: 12; makefile: 4
file content (51 lines) | stat: -rw-r--r-- 1,314 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php

/**
 * Test: Nette\Utils\Random::generate()
 */

declare(strict_types=1);

use Nette\Utils\Random;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


Assert::same(10, strlen(Random::generate()));
Assert::same(5, strlen(Random::generate(5)));
Assert::same(200, strlen(Random::generate(200)));

Assert::truthy(preg_match('#^[0-9a-z]+$#', Random::generate()));
Assert::truthy(preg_match('#^[0-9]+$#', Random::generate(1000, '0-9')));
Assert::truthy(preg_match('#^[0a-z12]+$#', Random::generate(1000, '0a-z12')));
Assert::truthy(preg_match('#^[-a]+$#', Random::generate(1000, '-a')));

Assert::exception(
	fn() => Random::generate(0),
	Nette\InvalidArgumentException::class,
	'Length must be greater than zero.',
);

Assert::exception(
	fn() => Random::generate(1, '000'),
	Nette\InvalidArgumentException::class,
	'Character list must contain at least two chars.',
);


// frequency check
$phpdbgLog = defined('PHPDBG_VERSION') && @phpdbg_end_oplog(); // memory leak workaround
$length = (int) 1e6;
$delta = 0.1;
$s = Nette\Utils\Random::generate($length, "\x01-\xFF");
$freq = count_chars($s);
Assert::same(0, $freq[0]);
for ($i = 1; $i < 255; $i++) {
	Assert::true($freq[$i] < $length / 255 * (1 + $delta) && $freq[$i] > $length / 255 * (1 - $delta));
}

if ($phpdbgLog) {
	phpdbg_start_oplog();
}