packages/sql-faker/tests/Unit/MySql/Generation/Value/RadixDomainTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\SqlFaker\MySql\Generation\Value;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\UsesClass;
9use PHPUnit\Framework\TestCase;
10use SqlFaker\Generation\Value\CharacterDomain;
11use SqlFaker\Generation\Value\SequenceDomain;
12use SqlFaker\Generation\Value\WordDomain;
13use SqlFaker\MySql\Generation\Value\RadixDomain;
14
15#[CoversClass(RadixDomain::class)]
16#[UsesClass(CharacterDomain::class)]
17#[UsesClass(WordDomain::class)]
18#[UsesClass(SequenceDomain::class)]
19final class RadixDomainTest extends TestCase
20{
21 public function testChooseConstructsEvenHexAndWholeAsciiBytes(): void
22 {
23 $ordinary = new RadixDomain('0123456789abcdefABCDEF', '0x', ['X', 'x'], 2, 32);
24 self::assertSame('0x0', $ordinary->choose(static fn (int $count): int => 0));
25 self::assertSame("X'" . str_repeat('F', 32) . "'", $ordinary->choose(static fn (int $count): int => $count - 1));
26 $ascii = new RadixDomain('01', '0b', ['B', 'b'], 1, 64, true);
27 self::assertSame('0b00000000', $ascii->choose(static fn (int $count): int => 0));
28 self::assertSame("B'" . str_repeat('01111111', 8) . "'", $ascii->choose(static fn (int $count): int => $count - 1));
29 }
30
31 public function testMatchKeepsLexicalFormsIndependentOfSamplingBudgets(): void
32 {
33 $domain = new RadixDomain('0123456789abcdefABCDEF', '0x', ['X', 'x'], 2, 2, true);
34 self::assertSame([3, 4], $domain->match('0xff!'));
35 self::assertSame([5], $domain->match("x'ff'"));
36 self::assertContains(8, $domain->match('0xabcdef'));
37 self::assertSame([], $domain->match("X'f'"));
38 self::assertSame([], $domain->match('0x'));
39 }
40
41 public function testDigitsDecodesOnlyCompleteMatchingForms(): void
42 {
43 $domain = new RadixDomain('0123456789abcdefABCDEF', '0x', ['X', 'x'], 2, 32);
44 self::assertSame('0f', $domain->digits('0x0f'));
45 self::assertSame('0f', $domain->digits("x'0f'"));
46 self::assertSame('', $domain->digits("X''"));
47 self::assertNull($domain->digits("X'0f'junk"));
48 self::assertNull($domain->digits('0xfg'));
49 }
50}
51