packages/sql-faker/tests/Unit/Generation/Choice/ByteChoicesTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\SqlFaker\Generation\Choice;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\TestCase;
9use SqlFaker\Generation\Choice\ByteChoices;
10
11#[CoversClass(ByteChoices::class)]
12#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFaker\Grammar\Model\Grammar::class)]
13#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFaker\Grammar\Model\Production::class)]
14#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFaker\Grammar\Model\ProductionRule::class)]
15#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFaker\Grammar\Model\Terminal::class)]
16#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFaker\Grammar\Model\NonTerminal::class)]
17#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFaker\Generation\Coverage\GrammarCoverageInventory::class)]
18#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFaker\Generation\Coverage\CoverageException::class)]
19#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFaker\Generation\Coverage\GrammarCoverage::class)]
20#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFaker\Generation\Coverage\GeneratorRevision::class)]
21#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFaker\Generation\Coverage\CoverageSnapshotStore::class)]
22#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFaker\Generation\Coverage\SnapshotValidation::class)]
23#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFaker\Generation\Coverage\GenerationTrace::class)]
24#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFaker\Generation\Coverage\CoverageSets::class)]
25#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFaker\Generation\Derivation\CompletionCosts::class)]
26#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFaker\Generation\Derivation\DerivationNode::class)]
27#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFaker\Generation\Coverage\LexicalObservation::class)]
28final class ByteChoicesTest extends TestCase
29{
30    public function testIndexConsumesLittleEndianChoicesWiderThanOneByte(): void
31    {
32        $choices = new ByteChoices("\x00\x01\xff");
33        self::assertSame(256, $choices->index(300));
34        self::assertSame(0, $choices->index(3));
35        self::assertNull($choices->index(2));
36    }
37
38    public function testIndexDoesNotUseAnIncompleteChoice(): void
39    {
40        $choices = new ByteChoices("\xff");
41        self::assertNull($choices->index(257));
42        self::assertNull($choices->index(2));
43    }
44
45    public function testWidthUsesTheMinimumNumberOfBytesWithoutAssuming256Alternatives(): void
46    {
47        self::assertSame(1, ByteChoices::width(1));
48        self::assertSame(1, ByteChoices::width(256));
49        self::assertSame(2, ByteChoices::width(257));
50        self::assertSame(3, ByteChoices::width(65537));
51    }
52
53
54    public function testIndexReducesUnsignedEightByteValuesWithoutIntegerOverflow(): void
55    {
56        self::assertSame(1, (new ByteChoices(str_repeat("\xff", 8)))->index(PHP_INT_MAX));
57    }
58}
59