File: phpUnionVectorTest.php

package info (click to toggle)
golang-github-google-flatbuffers 24.12.23-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 17,704 kB
  • sloc: cpp: 53,217; python: 6,900; cs: 5,566; java: 4,370; php: 1,460; javascript: 1,061; xml: 1,016; sh: 886; makefile: 13
file content (109 lines) | stat: -rw-r--r-- 3,906 bytes parent folder | download | duplicates (16)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php

require join(DIRECTORY_SEPARATOR, array(dirname(dirname(__FILE__)), "php", "Constants.php"));
require join(DIRECTORY_SEPARATOR, array(dirname(dirname(__FILE__)), "php", "ByteBuffer.php"));
require join(DIRECTORY_SEPARATOR, array(dirname(dirname(__FILE__)), "php", "FlatbufferBuilder.php"));
require join(DIRECTORY_SEPARATOR, array(dirname(dirname(__FILE__)), "php", "Table.php"));
require join(DIRECTORY_SEPARATOR, array(dirname(dirname(__FILE__)), "php", "Struct.php"));

require join(DIRECTORY_SEPARATOR, array(dirname(__FILE__), "php", 'Attacker.php'));
require join(DIRECTORY_SEPARATOR, array(dirname(__FILE__), "php", 'BookReader.php'));
require join(DIRECTORY_SEPARATOR, array(dirname(__FILE__), "php", 'Character.php'));
require join(DIRECTORY_SEPARATOR, array(dirname(__FILE__), "php", 'Movie.php'));

class Assert {
    public function ok($result, $message = "") {
        if (!$result){
            throw new Exception(!empty($message) ? $message : "{$result} is not true.");
        }
    }

    public function Equal($result, $expected, $message = "") {
        if ($result != $expected) {
            throw new Exception(!empty($message) ? $message : "given the result {$result} is not equals as {$expected}");
        }
    }


    public function strictEqual($result, $expected, $message = "") {
        if ($result !== $expected) {
            throw new Exception(!empty($message) ? $message : "given the result {$result} is not strict equals as {$expected}");
        }
    }

    public function Throws($class, Callable $callback) {
        try {
            $callback();

            throw new \Exception("passed statement don't throw an exception.");
        } catch (\Exception $e) {
            if (get_class($e) != get_class($class)) {
                throw new Exception("passed statement doesn't throw " . get_class($class) . ". throwws " . get_class($e));
            }
        }
    }
}

function main()
{
    $assert = new Assert();

    $fbb = new Google\FlatBuffers\FlatBufferBuilder(1);

    $charTypes = [
        Character::Belle,
        Character::MuLan,
        Character::BookFan,
    ];

    Attacker::startAttacker($fbb);
    Attacker::addSwordAttackDamage($fbb, 5);
    $attackerOffset = Attacker::endAttacker($fbb);

    $charTypesOffset = Movie::createCharactersTypeVector($fbb, $charTypes);
    $charsOffset = Movie::createCharactersVector(
        $fbb,
        [
            BookReader::createBookReader($fbb, 7),
            $attackerOffset,
            BookReader::createBookReader($fbb, 2),
        ]
    );

    Movie::startMovie($fbb);
    Movie::addCharactersType($fbb, $charTypesOffset);
    Movie::addCharacters($fbb, $charsOffset);
    Movie::finishMovieBuffer($fbb, Movie::endMovie($fbb));

    $buf = Google\FlatBuffers\ByteBuffer::wrap($fbb->dataBuffer()->data());

    $movie = Movie::getRootAsMovie($buf);

    $assert->strictEqual($movie->getCharactersTypeLength(), count($charTypes));
    $assert->strictEqual($movie->getCharactersLength(), $movie->getCharactersTypeLength());

    for ($i = 0; $i < count($charTypes); ++$i) {
        $assert->strictEqual($movie->getCharactersType($i), $charTypes[$i]);
    }

    $bookReader7 = $movie->getCharacters(0, new BookReader());
    $assert->strictEqual($bookReader7->getBooksRead(), 7);

    $attacker = $movie->getCharacters(1, new Attacker());
    $assert->strictEqual($attacker->getSwordAttackDamage(), 5);

    $bookReader2 = $movie->getCharacters(2, new BookReader());
    $assert->strictEqual($bookReader2->getBooksRead(), 2);
}

try {
    main();
    exit(0);
} catch(Exception $e) {
    printf("Fatal error: Uncaught exception '%s' with message '%s. in %s:%d\n", get_class($e), $e->getMessage(), $e->getFile(), $e->getLine());
    printf("Stack trace:\n");
    echo $e->getTraceAsString() . PHP_EOL;
    printf("  thrown in in %s:%d\n", $e->getFile(), $e->getLine());

    die(-1);
}