packages/sql-parser/tests/Unit/Automaton/DigraphTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Automaton;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\Attributes\UsesClass;
10use PHPUnit\Framework\TestCase;
11use SqlParser\Automaton\Bitset;
12use SqlParser\Automaton\Digraph;
13
14#[CoversClass(Digraph::class)]
15#[UsesClass(Bitset::class)]
16#[Small]
17final class DigraphTest extends TestCase
18{
19    public function testCloseUnionsAlongEdgesAndAcrossCycles(): void
20    {
21        $sets = [[1], [2], [4], [8]];
22        $closed = (new Digraph())->close(4, [0 => [1], 1 => [2], 2 => [1], 3 => []], $sets);
23
24        self::assertSame([1 | 2 | 4], $closed[0]);
25        self::assertSame([2 | 4], $closed[1]);
26        self::assertSame([2 | 4], $closed[2]);
27        self::assertSame([8], $closed[3]);
28    }
29
30    public function testTraverseClosesOnlyWhatTheRootReaches(): void
31    {
32        $sets = [[1], [2], [4]];
33        $marks = [0, 0, 0];
34        $stack = [];
35        (new Digraph())->traverse(1, [0 => [1], 1 => [2], 2 => []], $sets, $marks, $stack);
36
37        self::assertSame([[1], [6], [4]], $sets);
38        self::assertSame(0, $marks[0]);
39        self::assertSame(PHP_INT_MAX, $marks[1]);
40        self::assertSame([], $stack);
41    }
42}
43