summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorShuhei Taunma <chobieee@gmail.com>2015-11-05 16:19:28 +0900
committerShuhei Tanuma <chobieee@gmail.com>2015-11-18 00:26:39 +0900
commit5ce86826718c938e38b0f57e4bc5840fe15a1565 (patch)
tree7b34312db70588595b515c21fd8df36814cf27dd /tests
parent3f1c4b41f650a5d4e9f89f606f01ba0a6a164029 (diff)
downloadflatbuffers-5ce86826718c938e38b0f57e4bc5840fe15a1565.tar.gz
flatbuffers-5ce86826718c938e38b0f57e4bc5840fe15a1565.tar.bz2
flatbuffers-5ce86826718c938e38b0f57e4bc5840fe15a1565.zip
(PHP) add experimental support for PHP language.
* codegen for all basic features: WIP (probably implemented all basic feature) * JSON parsing: NO * Simple mutation: NO * Reflection: NO * Buffer verifier: NO (will be add later) * Testing: basic: Yes * Testing: fuzz: Yes * Performance: Not bad * Platform: Supported Linux, OS X, Windows (has 32bit integer limitation) * Engine Unity: No flatc --php monster_test.fbs <?php //include neccessary files. $fbb = new Google\FlatBuffers\FlatBufferBuilder(1); $str = $fbb->createString("monster"); \MyGame\Example\Monster::startMonster($fbb); \MyGame\Example\Monster::addHp($fbb, 80); \MyGame\Example\Monster::addName($fbb, $str); $mon = \MyGame\Example\Monster::endMonster($fbb); $fbb->finish($mon); echo $fbb->sizedByteArray(); PHP 5.4 higher Currently, we do not register this library to packagist as still experimental and versioning problem. If you intended to use flatbuffers with composer. add repostiories section to composer.json like below. "repositories": [{ "type": "vcs", "url": "https://github.com/google/flatbuffers" }], and just put google/flatbuffers. "require": { "google/flatbuffers": "*" } * PHP's integer is platform dependant. we strongly recommend use 64bit machine and don't use uint, ulong types as prevent overflow issue. ref: http://php.net/manual/en/language.types.integer.php * php don't support float type. floating point numbers are always parsed as double precision internally. ref: http://php.net/manual/en/language.types.float.php * ByteBuffer is little bit slow implemnentation due to many chr/ord function calls. Especially encoding objects. This is expected performance as PHP5 has parsing arguments overhead. probably we'll add C-extension. Basically, PHP implementation respects Java and C# implementation. Note: ByteBuffer and FlatBuffersBuilder class are not intended to use other purposes. we may change internal API foreseeable future. PSR-2, PSR-4 standards. Implemented simple assertion class (respect JavaScript testcase implementation) as we prefer small code base. this also keeps CI iteration speed. we'll choose phpunit or something when the test cases grown.
Diffstat (limited to 'tests')
-rw-r--r--tests/MyGame/Example/Any.php25
-rw-r--r--tests/MyGame/Example/Color.php25
-rw-r--r--tests/MyGame/Example/Monster.php759
-rw-r--r--tests/MyGame/Example/Stat.php136
-rw-r--r--tests/MyGame/Example/Test.php53
-rw-r--r--tests/MyGame/Example/TestSimpleTableWithEnum.php99
-rw-r--r--tests/MyGame/Example/Vec3.php96
-rw-r--r--tests/generate_code.bat2
-rw-r--r--tests/generate_code.sh2
-rw-r--r--tests/phpTest.php609
10 files changed, 1805 insertions, 1 deletions
diff --git a/tests/MyGame/Example/Any.php b/tests/MyGame/Example/Any.php
new file mode 100644
index 00000000..d35bfd63
--- /dev/null
+++ b/tests/MyGame/Example/Any.php
@@ -0,0 +1,25 @@
+<?php
+// automatically generated, do not modify
+
+namespace MyGame\Example;
+
+class Any
+{
+ const NONE = 0;
+ const Monster = 1;
+ const TestSimpleTableWithEnum = 2;
+
+ private static $names = array(
+ "NONE",
+ "Monster",
+ "TestSimpleTableWithEnum",
+ );
+
+ public static function Name($e)
+ {
+ if (!isset(self::$names[$e])) {
+ throw new \Exception();
+ }
+ return self::$names[$e];
+ }
+}
diff --git a/tests/MyGame/Example/Color.php b/tests/MyGame/Example/Color.php
new file mode 100644
index 00000000..9de0e68f
--- /dev/null
+++ b/tests/MyGame/Example/Color.php
@@ -0,0 +1,25 @@
+<?php
+// automatically generated, do not modify
+
+namespace MyGame\Example;
+
+class Color
+{
+ const Red = 1;
+ const Green = 2;
+ const Blue = 8;
+
+ private static $names = array(
+ "Red",
+ "Green",
+ "Blue",
+ );
+
+ public static function Name($e)
+ {
+ if (!isset(self::$names[$e])) {
+ throw new \Exception();
+ }
+ return self::$names[$e];
+ }
+}
diff --git a/tests/MyGame/Example/Monster.php b/tests/MyGame/Example/Monster.php
new file mode 100644
index 00000000..94a0df7d
--- /dev/null
+++ b/tests/MyGame/Example/Monster.php
@@ -0,0 +1,759 @@
+<?php
+// automatically generated, do not modify
+
+namespace MyGame\Example;
+
+use \Google\FlatBuffers\Struct;
+use \Google\FlatBuffers\Table;
+use \Google\FlatBuffers\ByteBuffer;
+use \Google\FlatBuffers\FlatBufferBuilder;
+
+/// an example documentation comment: monster object
+class Monster extends Table
+{
+ /**
+ * @param ByteBuffer $bb
+ * @return Monster
+ */
+ public static function getRootAsMonster(ByteBuffer $bb)
+ {
+ $obj = new Monster();
+ return ($obj->init($bb->getInt($bb->getPosition()) + $bb->getPosition(), $bb));
+ }
+
+ public static function MonsterIdentifier()
+ {
+ return "MONS";
+ }
+
+ public static function MonsterBufferHasIdentifier(ByteBuffer $buf)
+ {
+ return self::__has_identifier($buf, self::MonsterIdentifier());
+ }
+
+ public static function MonsterExtension()
+ {
+ return "mon";
+ }
+
+ /**
+ * @param int $_i offset
+ * @param ByteBuffer $_bb
+ * @return Monster
+ **/
+ public function init($_i, ByteBuffer $_bb)
+ {
+ $this->bb_pos = $_i;
+ $this->bb = $_bb;
+ return $this;
+ }
+
+ public function getPos()
+ {
+ $obj = new Vec3();
+ $o = $this->__offset(4);
+ return $o != 0 ? $obj->init($o + $this->bb_pos, $this->bb) : 0;
+ }
+
+ /**
+ * @return short
+ */
+ public function getMana()
+ {
+ $o = $this->__offset(6);
+ return $o != 0 ? $this->bb->getShort($o + $this->bb_pos) : 150;
+ }
+
+ /**
+ * @return short
+ */
+ public function getHp()
+ {
+ $o = $this->__offset(8);
+ return $o != 0 ? $this->bb->getShort($o + $this->bb_pos) : 100;
+ }
+
+ public function getName()
+ {
+ $o = $this->__offset(10);
+ return $o != 0 ? $this->__string($o + $this->bb_pos) : null;
+ }
+
+ /**
+ * @param int offset
+ * @return byte
+ */
+ public function getInventory($j)
+ {
+ $o = $this->__offset(14);
+ return $o != 0 ? $this->bb->getByte($this->__vector($o) + $j * 1) : 0;
+ }
+
+ /**
+ * @return int
+ */
+ public function getInventoryLength()
+ {
+ $o = $this->__offset(14);
+ return $o != 0 ? $this->__vector_len($o) : 0;
+ }
+
+ /**
+ * @return sbyte
+ */
+ public function getColor()
+ {
+ $o = $this->__offset(16);
+ return $o != 0 ? $this->bb->getSbyte($o + $this->bb_pos) : \MyGame\Example\Color::Blue;
+ }
+
+ /**
+ * @return byte
+ */
+ public function getTestType()
+ {
+ $o = $this->__offset(18);
+ return $o != 0 ? $this->bb->getByte($o + $this->bb_pos) : \MyGame\Example\Any::NONE;
+ }
+
+ /**
+ * @returnint
+ */
+ public function getTest($obj)
+ {
+ $o = $this->__offset(20);
+ return $o != 0 ? $this->__union($obj, $o) : null;
+ }
+
+ /**
+ * @returnVectorOffset
+ */
+ public function getTest4($j)
+ {
+ $o = $this->__offset(22);
+ $obj = new Test();
+ return $o != 0 ? $obj->init($this->__vector($o) + $j *4, $this->bb) : null;
+ }
+
+ /**
+ * @return int
+ */
+ public function getTest4Length()
+ {
+ $o = $this->__offset(22);
+ return $o != 0 ? $this->__vector_len($o) : 0;
+ }
+
+ /**
+ * @param int offset
+ * @return string
+ */
+ public function getTestarrayofstring($j)
+ {
+ $o = $this->__offset(24);
+ return $o != 0 ? $this->__string($this->__vector($o) + $j * 4) : 0;
+ }
+
+ /**
+ * @return int
+ */
+ public function getTestarrayofstringLength()
+ {
+ $o = $this->__offset(24);
+ return $o != 0 ? $this->__vector_len($o) : 0;
+ }
+
+/// an example documentation comment: this will end up in the generated code
+/// multiline too
+ /**
+ * @returnVectorOffset
+ */
+ public function getTestarrayoftables($j)
+ {
+ $o = $this->__offset(26);
+ $obj = new Monster();
+ return $o != 0 ? $obj->init($this->__indirect($this->__vector($o) + $j * 4), $this->bb) : null;
+ }
+
+ /**
+ * @return int
+ */
+ public function getTestarrayoftablesLength()
+ {
+ $o = $this->__offset(26);
+ return $o != 0 ? $this->__vector_len($o) : 0;
+ }
+
+ public function getEnemy()
+ {
+ $obj = new Monster();
+ $o = $this->__offset(28);
+ return $o != 0 ? $obj->init($o + $this->bb_pos, $this->bb) : 0;
+ }
+
+ /**
+ * @param int offset
+ * @return byte
+ */
+ public function getTestnestedflatbuffer($j)
+ {
+ $o = $this->__offset(30);
+ return $o != 0 ? $this->bb->getByte($this->__vector($o) + $j * 1) : 0;
+ }
+
+ /**
+ * @return int
+ */
+ public function getTestnestedflatbufferLength()
+ {
+ $o = $this->__offset(30);
+ return $o != 0 ? $this->__vector_len($o) : 0;
+ }
+
+ public function getTestempty()
+ {
+ $obj = new Stat();
+ $o = $this->__offset(32);
+ return $o != 0 ? $obj->init($o + $this->bb_pos, $this->bb) : 0;
+ }
+
+ /**
+ * @return bool
+ */
+ public function getTestbool()
+ {
+ $o = $this->__offset(34);
+ return $o != 0 ? $this->bb->getBool($o + $this->bb_pos) : false;
+ }
+
+ /**
+ * @return int
+ */
+ public function getTesthashs32Fnv1()
+ {
+ $o = $this->__offset(36);
+ return $o != 0 ? $this->bb->getInt($o + $this->bb_pos) : 0;
+ }
+
+ /**
+ * @return uint
+ */
+ public function getTesthashu32Fnv1()
+ {
+ $o = $this->__offset(38);
+ return $o != 0 ? $this->bb->getUint($o + $this->bb_pos) : 0;
+ }
+
+ /**
+ * @return long
+ */
+ public function getTesthashs64Fnv1()
+ {
+ $o = $this->__offset(40);
+ return $o != 0 ? $this->bb->getLong($o + $this->bb_pos) : 0;
+ }
+
+ /**
+ * @return ulong
+ */
+ public function getTesthashu64Fnv1()
+ {
+ $o = $this->__offset(42);
+ return $o != 0 ? $this->bb->getUlong($o + $this->bb_pos) : 0;
+ }
+
+ /**
+ * @return int
+ */
+ public function getTesthashs32Fnv1a()
+ {
+ $o = $this->__offset(44);
+ return $o != 0 ? $this->bb->getInt($o + $this->bb_pos) : 0;
+ }
+
+ /**
+ * @return uint
+ */
+ public function getTesthashu32Fnv1a()
+ {
+ $o = $this->__offset(46);
+ return $o != 0 ? $this->bb->getUint($o + $this->bb_pos) : 0;
+ }
+
+ /**
+ * @return long
+ */
+ public function getTesthashs64Fnv1a()
+ {
+ $o = $this->__offset(48);
+ return $o != 0 ? $this->bb->getLong($o + $this->bb_pos) : 0;
+ }
+
+ /**
+ * @return ulong
+ */
+ public function getTesthashu64Fnv1a()
+ {
+ $o = $this->__offset(50);
+ return $o != 0 ? $this->bb->getUlong($o + $this->bb_pos) : 0;
+ }
+
+ /**
+ * @param int offset
+ * @return bool
+ */
+ public function getTestarrayofbools($j)
+ {
+ $o = $this->__offset(52);
+ return $o != 0 ? $this->bb->getBool($this->__vector($o) + $j * 1) : 0;
+ }
+
+ /**
+ * @return int
+ */
+ public function getTestarrayofboolsLength()
+ {
+ $o = $this->__offset(52);
+ return $o != 0 ? $this->__vector_len($o) : 0;
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @return void
+ */
+ public static function startMonster(FlatBufferBuilder $builder)
+ {
+ $builder->StartObject(25);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @return Monster
+ */
+ public static function createMonster(FlatBufferBuilder $builder, $pos, $mana, $hp, $name, $inventory, $color, $test_type, $test, $test4, $testarrayofstring, $testarrayoftables, $enemy, $testnestedflatbuffer, $testempty, $testbool, $testhashs32_fnv1, $testhashu32_fnv1, $testhashs64_fnv1, $testhashu64_fnv1, $testhashs32_fnv1a, $testhashu32_fnv1a, $testhashs64_fnv1a, $testhashu64_fnv1a, $testarrayofbools)
+ {
+ $builder->startObject(25);
+ self::addPos($builder, $pos);
+ self::addMana($builder, $mana);
+ self::addHp($builder, $hp);
+ self::addName($builder, $name);
+ self::addInventory($builder, $inventory);
+ self::addColor($builder, $color);
+ self::addTestType($builder, $test_type);
+ self::addTest($builder, $test);
+ self::addTest4($builder, $test4);
+ self::addTestarrayofstring($builder, $testarrayofstring);
+ self::addTestarrayoftables($builder, $testarrayoftables);
+ self::addEnemy($builder, $enemy);
+ self::addTestnestedflatbuffer($builder, $testnestedflatbuffer);
+ self::addTestempty($builder, $testempty);
+ self::addTestbool($builder, $testbool);
+ self::addTesthashs32Fnv1($builder, $testhashs32_fnv1);
+ self::addTesthashu32Fnv1($builder, $testhashu32_fnv1);
+ self::addTesthashs64Fnv1($builder, $testhashs64_fnv1);
+ self::addTesthashu64Fnv1($builder, $testhashu64_fnv1);
+ self::addTesthashs32Fnv1a($builder, $testhashs32_fnv1a);
+ self::addTesthashu32Fnv1a($builder, $testhashu32_fnv1a);
+ self::addTesthashs64Fnv1a($builder, $testhashs64_fnv1a);
+ self::addTesthashu64Fnv1a($builder, $testhashu64_fnv1a);
+ self::addTestarrayofbools($builder, $testarrayofbools);
+ $o = $builder->endObject();
+ $builder->required($o, 10); // name
+ return $o;
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param int
+ * @return void
+ */
+ public static function addPos(FlatBufferBuilder $builder, $pos)
+ {
+ $builder->addStructX(0, $pos, 0);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param short
+ * @return void
+ */
+ public static function addMana(FlatBufferBuilder $builder, $mana)
+ {
+ $builder->addShortX(1, $mana, 150);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param short
+ * @return void
+ */
+ public static function addHp(FlatBufferBuilder $builder, $hp)
+ {
+ $builder->addShortX(2, $hp, 100);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param StringOffset
+ * @return void
+ */
+ public static function addName(FlatBufferBuilder $builder, $name)
+ {
+ $builder->addOffsetX(3, $name, 0);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param VectorOffset
+ * @return void
+ */
+ public static function addInventory(FlatBufferBuilder $builder, $inventory)
+ {
+ $builder->addOffsetX(5, $inventory, 0);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param array offset array
+ * @return int vector offset
+ */
+ public static function createInventoryVector(FlatBufferBuilder $builder, array $data)
+ {
+ $builder->startVector(1, count($data), 1);
+ for ($i = count($data) - 1; $i >= 0; $i--) {
+ $builder->addByte($data[$i]);
+ }
+ return $builder->endVector();
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param int $numElems
+ * @return void
+ */
+ public static function startInventoryVector(FlatBufferBuilder $builder, $numElems)
+ {
+ $builder->startVector(1, $numElems, 1);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param sbyte
+ * @return void
+ */
+ public static function addColor(FlatBufferBuilder $builder, $color)
+ {
+ $builder->addSbyteX(6, $color, 8);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param byte
+ * @return void
+ */
+ public static function addTestType(FlatBufferBuilder $builder, $testType)
+ {
+ $builder->addByteX(7, $testType, 0);
+ }
+
+ public static function addTest(FlatBufferBuilder $builder, $offset)
+ {
+ $builder->addOffsetX(8, $offset, 0);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param VectorOffset
+ * @return void
+ */
+ public static function addTest4(FlatBufferBuilder $builder, $test4)
+ {
+ $builder->addOffsetX(9, $test4, 0);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param array offset array
+ * @return int vector offset
+ */
+ public static function createTest4Vector(FlatBufferBuilder $builder, array $data)
+ {
+ $builder->startVector(4, count($data), 2);
+ for ($i = count($data) - 1; $i >= 0; $i--) {
+ $builder->addOffset($data[$i]);
+ }
+ return $builder->endVector();
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param int $numElems
+ * @return void
+ */
+ public static function startTest4Vector(FlatBufferBuilder $builder, $numElems)
+ {
+ $builder->startVector(4, $numElems, 2);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param VectorOffset
+ * @return void
+ */
+ public static function addTestarrayofstring(FlatBufferBuilder $builder, $testarrayofstring)
+ {
+ $builder->addOffsetX(10, $testarrayofstring, 0);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param array offset array
+ * @return int vector offset
+ */
+ public static function createTestarrayofstringVector(FlatBufferBuilder $builder, array $data)
+ {
+ $builder->startVector(4, count($data), 4);
+ for ($i = count($data) - 1; $i >= 0; $i--) {
+ $builder->addOffset($data[$i]);
+ }
+ return $builder->endVector();
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param int $numElems
+ * @return void
+ */
+ public static function startTestarrayofstringVector(FlatBufferBuilder $builder, $numElems)
+ {
+ $builder->startVector(4, $numElems, 4);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param VectorOffset
+ * @return void
+ */
+ public static function addTestarrayoftables(FlatBufferBuilder $builder, $testarrayoftables)
+ {
+ $builder->addOffsetX(11, $testarrayoftables, 0);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param array offset array
+ * @return int vector offset
+ */
+ public static function createTestarrayoftablesVector(FlatBufferBuilder $builder, array $data)
+ {
+ $builder->startVector(4, count($data), 4);
+ for ($i = count($data) - 1; $i >= 0; $i--) {
+ $builder->addOffset($data[$i]);
+ }
+ return $builder->endVector();
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param int $numElems
+ * @return void
+ */
+ public static function startTestarrayoftablesVector(FlatBufferBuilder $builder, $numElems)
+ {
+ $builder->startVector(4, $numElems, 4);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param int
+ * @return void
+ */
+ public static function addEnemy(FlatBufferBuilder $builder, $enemy)
+ {
+ $builder->addOffsetX(12, $enemy, 0);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param VectorOffset
+ * @return void
+ */
+ public static function addTestnestedflatbuffer(FlatBufferBuilder $builder, $testnestedflatbuffer)
+ {
+ $builder->addOffsetX(13, $testnestedflatbuffer, 0);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param array offset array
+ * @return int vector offset
+ */
+ public static function createTestnestedflatbufferVector(FlatBufferBuilder $builder, array $data)
+ {
+ $builder->startVector(1, count($data), 1);
+ for ($i = count($data) - 1; $i >= 0; $i--) {
+ $builder->addByte($data[$i]);
+ }
+ return $builder->endVector();
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param int $numElems
+ * @return void
+ */
+ public static function startTestnestedflatbufferVector(FlatBufferBuilder $builder, $numElems)
+ {
+ $builder->startVector(1, $numElems, 1);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param int
+ * @return void
+ */
+ public static function addTestempty(FlatBufferBuilder $builder, $testempty)
+ {
+ $builder->addOffsetX(14, $testempty, 0);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param bool
+ * @return void
+ */
+ public static function addTestbool(FlatBufferBuilder $builder, $testbool)
+ {
+ $builder->addBoolX(15, $testbool, false);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param int
+ * @return void
+ */
+ public static function addTesthashs32Fnv1(FlatBufferBuilder $builder, $testhashs32Fnv1)
+ {
+ $builder->addIntX(16, $testhashs32Fnv1, 0);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param uint
+ * @return void
+ */
+ public static function addTesthashu32Fnv1(FlatBufferBuilder $builder, $testhashu32Fnv1)
+ {
+ $builder->addUintX(17, $testhashu32Fnv1, 0);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param long
+ * @return void
+ */
+ public static function addTesthashs64Fnv1(FlatBufferBuilder $builder, $testhashs64Fnv1)
+ {
+ $builder->addLongX(18, $testhashs64Fnv1, 0);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param ulong
+ * @return void
+ */
+ public static function addTesthashu64Fnv1(FlatBufferBuilder $builder, $testhashu64Fnv1)
+ {
+ $builder->addUlongX(19, $testhashu64Fnv1, 0);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param int
+ * @return void
+ */
+ public static function addTesthashs32Fnv1a(FlatBufferBuilder $builder, $testhashs32Fnv1a)
+ {
+ $builder->addIntX(20, $testhashs32Fnv1a, 0);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param uint
+ * @return void
+ */
+ public static function addTesthashu32Fnv1a(FlatBufferBuilder $builder, $testhashu32Fnv1a)
+ {
+ $builder->addUintX(21, $testhashu32Fnv1a, 0);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param long
+ * @return void
+ */
+ public static function addTesthashs64Fnv1a(FlatBufferBuilder $builder, $testhashs64Fnv1a)
+ {
+ $builder->addLongX(22, $testhashs64Fnv1a, 0);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param ulong
+ * @return void
+ */
+ public static function addTesthashu64Fnv1a(FlatBufferBuilder $builder, $testhashu64Fnv1a)
+ {
+ $builder->addUlongX(23, $testhashu64Fnv1a, 0);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param VectorOffset
+ * @return void
+ */
+ public static function addTestarrayofbools(FlatBufferBuilder $builder, $testarrayofbools)
+ {
+ $builder->addOffsetX(24, $testarrayofbools, 0);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param array offset array
+ * @return int vector offset
+ */
+ public static function createTestarrayofboolsVector(FlatBufferBuilder $builder, array $data)
+ {
+ $builder->startVector(1, count($data), 1);
+ for ($i = count($data) - 1; $i >= 0; $i--) {
+ $builder->addBool($data[$i]);
+ }
+ return $builder->endVector();
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param int $numElems
+ * @return void
+ */
+ public static function startTestarrayofboolsVector(FlatBufferBuilder $builder, $numElems)
+ {
+ $builder->startVector(1, $numElems, 1);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @return int table offset
+ */
+ public static function endMonster(FlatBufferBuilder $builder)
+ {
+ $o = $builder->endObject();
+ $builder->required($o, 10); // name
+ return $o;
+ }
+
+ public static function finishMonsterBuffer(FlatBufferBuilder $builder, $offset)
+ {
+ $builder->finish($offset, "MONS");
+ }
+}
diff --git a/tests/MyGame/Example/Stat.php b/tests/MyGame/Example/Stat.php
new file mode 100644
index 00000000..c959ac7b
--- /dev/null
+++ b/tests/MyGame/Example/Stat.php
@@ -0,0 +1,136 @@
+<?php
+// automatically generated, do not modify
+
+namespace MyGame\Example;
+
+use \Google\FlatBuffers\Struct;
+use \Google\FlatBuffers\Table;
+use \Google\FlatBuffers\ByteBuffer;
+use \Google\FlatBuffers\FlatBufferBuilder;
+
+class Stat extends Table
+{
+ /**
+ * @param ByteBuffer $bb
+ * @return Stat
+ */
+ public static function getRootAsStat(ByteBuffer $bb)
+ {
+ $obj = new Stat();
+ return ($obj->init($bb->getInt($bb->getPosition()) + $bb->getPosition(), $bb));
+ }
+
+ public static function StatIdentifier()
+ {
+ return "MONS";
+ }
+
+ public static function StatBufferHasIdentifier(ByteBuffer $buf)
+ {
+ return self::__has_identifier($buf, self::StatIdentifier());
+ }
+
+ public static function StatExtension()
+ {
+ return "mon";
+ }
+
+ /**
+ * @param int $_i offset
+ * @param ByteBuffer $_bb
+ * @return Stat
+ **/
+ public function init($_i, ByteBuffer $_bb)
+ {
+ $this->bb_pos = $_i;
+ $this->bb = $_bb;
+ return $this;
+ }
+
+ public function getId()
+ {
+ $o = $this->__offset(4);
+ return $o != 0 ? $this->__string($o + $this->bb_pos) : null;
+ }
+
+ /**
+ * @return long
+ */
+ public function getVal()
+ {
+ $o = $this->__offset(6);
+ return $o != 0 ? $this->bb->getLong($o + $this->bb_pos) : 0;
+ }
+
+ /**
+ * @return ushort
+ */
+ public function getCount()
+ {
+ $o = $this->__offset(8);
+ return $o != 0 ? $this->bb->getUshort($o + $this->bb_pos) : 0;
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @return void
+ */
+ public static function startStat(FlatBufferBuilder $builder)
+ {
+ $builder->StartObject(3);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @return Stat
+ */
+ public static function createStat(FlatBufferBuilder $builder, $id, $val, $count)
+ {
+ $builder->startObject(3);
+ self::addId($builder, $id);
+ self::addVal($builder, $val);
+ self::addCount($builder, $count);
+ $o = $builder->endObject();
+ return $o;
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param StringOffset
+ * @return void
+ */
+ public static function addId(FlatBufferBuilder $builder, $id)
+ {
+ $builder->addOffsetX(0, $id, 0);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param long
+ * @return void
+ */
+ public static function addVal(FlatBufferBuilder $builder, $val)
+ {
+ $builder->addLongX(1, $val, 0);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param ushort
+ * @return void
+ */
+ public static function addCount(FlatBufferBuilder $builder, $count)
+ {
+ $builder->addUshortX(2, $count, 0);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @return int table offset
+ */
+ public static function endStat(FlatBufferBuilder $builder)
+ {
+ $o = $builder->endObject();
+ return $o;
+ }
+}
diff --git a/tests/MyGame/Example/Test.php b/tests/MyGame/Example/Test.php
new file mode 100644
index 00000000..acc838a3
--- /dev/null
+++ b/tests/MyGame/Example/Test.php
@@ -0,0 +1,53 @@
+<?php
+// automatically generated, do not modify
+
+namespace MyGame\Example;
+
+use \Google\FlatBuffers\Struct;
+use \Google\FlatBuffers\Table;
+use \Google\FlatBuffers\ByteBuffer;
+use \Google\FlatBuffers\FlatBufferBuilder;
+
+class Test extends Struct
+{
+ /**
+ * @param int $_i offset
+ * @param ByteBuffer $_bb
+ * @return Test
+ **/
+ public function init($_i, ByteBuffer $_bb)
+ {
+ $this->bb_pos = $_i;
+ $this->bb = $_bb;
+ return $this;
+ }
+
+ /**
+ * @return short
+ */
+ public function GetA()
+ {
+ return $this->bb->getShort($this->bb_pos + 0);
+ }
+
+ /**
+ * @return sbyte
+ */
+ public function GetB()
+ {
+ return $this->bb->getSbyte($this->bb_pos + 2);
+ }
+
+
+ /**
+ * @return int offset
+ */
+ public static function createTest(FlatBufferBuilder $builder, $a, $b)
+ {
+ $builder->prep(2, 4);
+ $builder->pad(1);
+ $builder->putSbyte($b);
+ $builder->putShort($a);
+ return $builder->offset();
+ }
+}
diff --git a/tests/MyGame/Example/TestSimpleTableWithEnum.php b/tests/MyGame/Example/TestSimpleTableWithEnum.php
new file mode 100644
index 00000000..6181461e
--- /dev/null
+++ b/tests/MyGame/Example/TestSimpleTableWithEnum.php
@@ -0,0 +1,99 @@
+<?php
+// automatically generated, do not modify
+
+namespace MyGame\Example;
+
+use \Google\FlatBuffers\Struct;
+use \Google\FlatBuffers\Table;
+use \Google\FlatBuffers\ByteBuffer;
+use \Google\FlatBuffers\FlatBufferBuilder;
+
+class TestSimpleTableWithEnum extends Table
+{
+ /**
+ * @param ByteBuffer $bb
+ * @return TestSimpleTableWithEnum
+ */
+ public static function getRootAsTestSimpleTableWithEnum(ByteBuffer $bb)
+ {
+ $obj = new TestSimpleTableWithEnum();
+ return ($obj->init($bb->getInt($bb->getPosition()) + $bb->getPosition(), $bb));
+ }
+
+ public static function TestSimpleTableWithEnumIdentifier()
+ {
+ return "MONS";
+ }
+
+ public static function TestSimpleTableWithEnumBufferHasIdentifier(ByteBuffer $buf)
+ {
+ return self::__has_identifier($buf, self::TestSimpleTableWithEnumIdentifier());
+ }
+
+ public static function TestSimpleTableWithEnumExtension()
+ {
+ return "mon";
+ }
+
+ /**
+ * @param int $_i offset
+ * @param ByteBuffer $_bb
+ * @return TestSimpleTableWithEnum
+ **/
+ public function init($_i, ByteBuffer $_bb)
+ {
+ $this->bb_pos = $_i;
+ $this->bb = $_bb;
+ return $this;
+ }
+
+ /**
+ * @return sbyte
+ */
+ public function getColor()
+ {
+ $o = $this->__offset(4);
+ return $o != 0 ? $this->bb->getSbyte($o + $this->bb_pos) : \MyGame\Example\Color::Green;
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @return void
+ */
+ public static function startTestSimpleTableWithEnum(FlatBufferBuilder $builder)
+ {
+ $builder->StartObject(1);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @return TestSimpleTableWithEnum
+ */
+ public static function createTestSimpleTableWithEnum(FlatBufferBuilder $builder, $color)
+ {
+ $builder->startObject(1);
+ self::addColor($builder, $color);
+ $o = $builder->endObject();
+ return $o;
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @param sbyte
+ * @return void
+ */
+ public static function addColor(FlatBufferBuilder $builder, $color)
+ {
+ $builder->addSbyteX(0, $color, 2);
+ }
+
+ /**
+ * @param FlatBufferBuilder $builder
+ * @return int table offset
+ */
+ public static function endTestSimpleTableWithEnum(FlatBufferBuilder $builder)
+ {
+ $o = $builder->endObject();
+ return $o;
+ }
+}
diff --git a/tests/MyGame/Example/Vec3.php b/tests/MyGame/Example/Vec3.php
new file mode 100644
index 00000000..022254ce
--- /dev/null
+++ b/tests/MyGame/Example/Vec3.php
@@ -0,0 +1,96 @@
+<?php
+// automatically generated, do not modify
+
+namespace MyGame\Example;
+
+use \Google\FlatBuffers\Struct;
+use \Google\FlatBuffers\Table;
+use \Google\FlatBuffers\ByteBuffer;
+use \Google\FlatBuffers\FlatBufferBuilder;
+
+class Vec3 extends Struct
+{
+ /**
+ * @param int $_i offset
+ * @param ByteBuffer $_bb
+ * @return Vec3
+ **/
+ public function init($_i, ByteBuffer $_bb)
+ {
+ $this->bb_pos = $_i;
+ $this->bb = $_bb;
+ return $this;
+ }
+
+ /**
+ * @return float
+ */
+ public function GetX()
+ {
+ return $this->bb->getFloat($this->bb_pos + 0);
+ }
+
+ /**
+ * @return float
+ */
+ public function GetY()
+ {
+ return $this->bb->getFloat($this->bb_pos + 4);
+ }
+
+ /**
+ * @return float
+ */
+ public function GetZ()
+ {
+ return $this->bb->getFloat($this->bb_pos + 8);
+ }
+
+ /**
+ * @return double
+ */
+ public function GetTest1()
+ {
+ return $this->bb->getDouble($this->bb_pos + 16);
+ }
+
+ /**
+ * @return sbyte
+ */
+ public function GetTest2()
+ {
+ return $this->bb->getSbyte($this->bb_pos + 24);
+ }
+
+ /**
+ * @return Test
+ */
+ public function getTest3()
+ {
+ $obj = new Test();
+ $obj->init($this->bb_pos + 26, $this->bb);
+ return $obj;
+ }
+
+
+ /**
+ * @return int offset
+ */
+ public static function createVec3(FlatBufferBuilder $builder, $x, $y, $z, $test1, $test2, $test3_a, $test3_b)
+ {
+ $builder->prep(16, 32);
+ $builder->pad(2);
+ $builder->prep(2, 4);
+ $builder->pad(1);
+ $builder->putSbyte($test3_b);
+ $builder->putShort($test3_a);
+ $builder->pad(1);
+ $builder->putSbyte($test2);
+ $builder->putDouble($test1);
+ $builder->pad(4);
+ $builder->putFloat($z);
+ $builder->putFloat($y);
+ $builder->putFloat($x);
+ return $builder->offset();
+ }
+}
diff --git a/tests/generate_code.bat b/tests/generate_code.bat
new file mode 100644
index 00000000..75ac1433
--- /dev/null
+++ b/tests/generate_code.bat
@@ -0,0 +1,2 @@
+..\flatc.exe -c -j -n -g -b -p --php -s --gen-mutable --no-includes monster_test.fbs monsterdata_test.json
+..\flatc.exe -b --schema monster_test.fbs
diff --git a/tests/generate_code.sh b/tests/generate_code.sh
index 3e96712f..91158bff 100644
--- a/tests/generate_code.sh
+++ b/tests/generate_code.sh
@@ -1,2 +1,2 @@
-../flatc -c -j -n -g -b -p -s --gen-mutable --no-includes monster_test.fbs monsterdata_test.json
+../flatc -c -j -n -g -b -p -s --php --gen-mutable --no-includes monster_test.fbs monsterdata_test.json
../flatc -b --schema monster_test.fbs
diff --git a/tests/phpTest.php b/tests/phpTest.php
new file mode 100644
index 00000000..bace87db
--- /dev/null
+++ b/tests/phpTest.php
@@ -0,0 +1,609 @@
+<?php
+// manual load for testing. please use PSR style autoloader when you use flatbuffers.
+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"));
+foreach (glob(join(DIRECTORY_SEPARATOR, array(dirname(__FILE__), "MyGame", "Example", "*.php"))) as $file) {
+ require $file;
+}
+
+function main()
+{
+ /// Begin Test
+ $assert = new Assert();
+
+ // First, let's test reading a FlatBuffer generated by C++ code:
+ // This file was generated from monsterdata_test.json
+
+ // Now test it:
+ $data = file_get_contents('monsterdata_test.mon');
+ $bb = Google\FlatBuffers\ByteBuffer::wrap($data);
+ test_buffer($assert, $bb);
+
+ // Second, let's create a FlatBuffer from scratch in JavaScript, and test it also.
+ // We use an initial size of 1 to exercise the reallocation algorithm,
+ // normally a size larger than the typical FlatBuffer you generate would be
+ // better for performance.
+ $fbb = new Google\FlatBuffers\FlatBufferBuilder(1);
+
+ // We set up the same values as monsterdata.json:
+ $str = $fbb->createString("MyMonster");
+
+ $inv = \MyGame\Example\Monster::CreateInventoryVector($fbb, array(0, 1, 2, 3, 4));
+
+ $fred = $fbb->createString('Fred');
+ \MyGame\Example\Monster::StartMonster($fbb);
+ \MyGame\Example\Monster::AddName($fbb, $fred);
+ $mon2 = \MyGame\Example\Monster::EndMonster($fbb);
+
+ \MyGame\Example\Monster::StartTest4Vector($fbb, 2);
+ \MyGame\Example\Test::CreateTest($fbb, 10, 20);
+ \MyGame\Example\Test::CreateTest($fbb, 30, 40);
+ $test4 = $fbb->endVector();
+
+ $testArrayOfString = \MyGame\Example\Monster::CreateTestarrayofstringVector($fbb, array(
+ $fbb->createString('test1'),
+ $fbb->createString('test2')
+ ));
+
+ \MyGame\Example\Monster::StartMonster($fbb);
+ \MyGame\Example\Monster::AddPos($fbb, \MyGame\Example\Vec3::CreateVec3($fbb,
+ 1.0, 2.0, 3.0, //float
+ 3.0, // double
+ \MyGame\Example\Color::Green,
+ 5, //short
+ 6));
+ \MyGame\Example\Monster::AddHp($fbb, 80);
+ \MyGame\Example\Monster::AddName($fbb, $str);
+ \MyGame\Example\Monster::AddInventory($fbb, $inv);
+ \MyGame\Example\Monster::AddTestType($fbb, \MyGame\Example\Any::Monster);
+ \MyGame\Example\Monster::AddTest($fbb, $mon2);
+ \MyGame\Example\Monster::AddTest4($fbb, $test4);
+ \MyGame\Example\Monster::AddTestarrayofstring($fbb, $testArrayOfString);
+ \MyGame\Example\Monster::AddTestbool($fbb, false);
+ $mon = \MyGame\Example\Monster::EndMonster($fbb);
+
+ \MyGame\Example\Monster::FinishMonsterBuffer($fbb, $mon);
+
+ // Test it:
+ test_buffer($assert, $fbb->dataBuffer());
+
+ testByteBuffer($assert);
+ fuzzTest1($assert);
+// testUnicode($assert);
+
+ echo 'FlatBuffers php test: completed successfully' . PHP_EOL;
+}
+
+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);
+}
+
+function test_buffer(Assert $assert, Google\FlatBuffers\ByteBuffer $bb) {
+
+ $assert->ok(MyGame\Example\Monster::MonsterBufferHasIdentifier($bb));
+ $monster = \MyGame\Example\Monster::GetRootAsMonster($bb);
+
+ $assert->strictEqual($monster->GetHp(), 80);
+ $assert->strictEqual($monster->GetMana(), 150); // default
+
+ $assert->strictEqual($monster->GetName(), 'MyMonster');
+
+ $pos = $monster->GetPos();
+ $assert->strictEqual($pos->GetX(), 1.0);
+ $assert->strictEqual($pos->GetY(), 2.0);
+ $assert->strictEqual($pos->GetZ(), 3.0);
+
+ $assert->Equal($pos->GetTest1(), 3.0);
+ $assert->strictEqual($pos->GetTest2(), \MyGame\Example\Color::Green);
+
+ $t = $pos->GetTest3();
+ $assert->strictEqual($t->GetA(), 5);
+ $assert->strictEqual($t->GetB(), 6);
+ $assert->strictEqual($monster->GetTestType(), \MyGame\Example\Any::Monster);
+
+ $monster2 = new \MyGame\Example\Monster();
+ $assert->strictEqual($monster->GetTest($monster2) != null, true);
+ $assert->strictEqual($monster2->GetName(), 'Fred');
+
+ $assert->strictEqual($monster->GetInventoryLength(), 5);
+ $invsum = 0;
+ for ($i = 0; $i < $monster->GetInventoryLength(); $i++) {
+ $invsum += $monster->GetInventory($i);
+ }
+ $assert->strictEqual($invsum, 10);
+
+
+ $test_0 = $monster->GetTest4(0);
+ $test_1 = $monster->GetTest4(1);
+ $assert->strictEqual($monster->GetTest4Length(), 2);
+ $assert->strictEqual($test_0->GetA() + $test_0->GetB() + $test_1->GetA() + $test_1->GetB(), 100);
+
+ $assert->strictEqual($monster->GetTestarrayofstringLength(), 2);
+ $assert->strictEqual($monster->GetTestarrayofstring(0), 'test1');
+ $assert->strictEqual($monster->GetTestarrayofstring(1), 'test2');
+ $assert->strictEqual($monster->GetTestbool(), false);
+}
+
+//function testUnicode(Assert $assert) {
+// // missing unicode_test.mon, implemented later
+// $correct = file_get_contents('unicode_test.mon');
+// $json = json_decode(file_get_contents('unicode_test.json'));
+//
+// // Test reading
+// $bb = flatbuffers\ByteBuffer::Wrap($correct);
+// $monster = \MyGame\Example\Monster::GetRootAsMonster($bb);
+// $assert->strictEqual($monster->GetName(), $json["name"]);
+//
+// //$assert->deepEqual(new Buffer(monster.name(flatbuffers.Encoding.UTF8_BYTES)), new Buffer(json.name));
+// //assert.strictEqual(monster.testarrayoftablesLength(), json.testarrayoftables.length);
+// foreach ($json["testarrayoftables"]as $i => $table) {
+// $value = $monster->GetTestArrayOfTables($i);
+// $assert->strictEqual($value->GetName(), $table["name"]);
+// //assert.deepEqual(new Buffer(value.name(flatbuffers.Encoding.UTF8_BYTES)), new Buffer(table.name));
+// }
+// $assert->strictEqual($monster->GetTestarrayofstringLength(), $json["testarrayofstring"]["length"]);
+// foreach ($json["testarrayofstring"] as $i => $string) {
+// $assert->strictEqual($monster->GetTestarrayofstring($i), $string);
+// //assert.deepEqual(new Buffer(monster.testarrayofstring(i, flatbuffers.Encoding.UTF8_BYTES)), new Buffer(string));
+// }
+//
+// // Test writing
+// $fbb = new FlatBuffers\FlatBufferBuilder(1);
+// $name = $fbb->CreateString($json["name"]);
+// $testarrayoftablesOffsets = array_map(function($table) use($fbb) {
+// $name = $fbb->CreateString($table["name"]);
+// \MyGame\Example\Monster::StartMonster($fbb);
+// \MyGame\Example\Monster::AddName($fbb, $name);
+// return \MyGame\Example\Monster::EndMonster($fbb);
+// }, $json["testarrayoftables"]);
+// $testarrayoftablesOffset = \MyGame\Example\Monster::CreateTestarrayoftablesVector($fbb,
+// $testarrayoftablesOffsets);
+//// $testarrayofstringOffset = \MyGame\Example\Monster::CreateTestarrayofstringVector($fbb,
+//// $json["testarrayofstring"].map(function(string) { return fbb.createString(string); }));
+//
+// \MyGame\Example\Monster::startMonster($fbb);
+// \MyGame\Example\Monster::addTestarrayofstring($fbb, $testarrayoftablesOffset);
+// \MyGame\Example\Monster::addTestarrayoftables($fbb, $testarrayoftablesOffset);
+// \MyGame\Example\Monster::addName($fbb, $name);
+// \MyGame\Example\Monster::finishMonsterBuffer($fbb, \MyGame\Example\Monster::endMonster($fbb));
+// //;assert.deepEqual(new Buffer(fbb.asUint8Array()), correct);
+//}
+
+// Low level stress/fuzz test: serialize/deserialize a variety of
+// different kinds of data in different combinations
+function fuzzTest1(Assert $assert)
+{
+
+ // Values we're testing against: chosen to ensure no bits get chopped
+ // off anywhere, and also be different from eachother.
+ $bool_val = true;
+ $char_val = -127; // 0x81
+ $uchar_val = 0xFF;
+ $short_val = -32222; // 0x8222;
+ $ushort_val = 0xFEEE;
+ $int_val = 0x83333333 | 0;
+ // for now
+ $uint_val = 1;
+ $long_val = 2;
+ $ulong_val = 3;
+
+// var uint_val = 0xFDDDDDDD;
+// var long_val = new flatbuffers.Long(0x44444444, 0x84444444);
+// var ulong_val = new flatbuffers.Long(0xCCCCCCCC, 0xFCCCCCCC);
+
+ $float_val = 3.14159;
+ $double_val = 3.14159265359;
+
+ $test_values_max = 11;
+ $fields_per_object = 4;
+ // current implementation is not good at encoding.
+ $num_fuzz_objects = 1000;
+ $builder = new Google\FlatBuffers\FlatBufferBuilder(1);
+
+ // can't use same implementation due to PHP_INTMAX overflow issue.
+ // we use mt_rand function to reproduce fuzzy test.
+ mt_srand(48271);
+ $objects = array();
+ // Generate num_fuzz_objects random objects each consisting of
+ // fields_per_object fields, each of a random type.
+ for ($i = 0; $i < $num_fuzz_objects; $i++) {
+ $builder->startObject($fields_per_object);
+ for ($f = 0; $f < $fields_per_object; $f++) {
+ $choice = mt_rand() % $test_values_max;
+ switch ($choice) {
+ case 0:
+ $builder->addBoolX($f, $bool_val, 0);
+ break;
+ case 1:
+ $builder->addByteX($f, $char_val, 0);
+ break;
+ case 2:
+ $builder->addSbyteX($f, $uchar_val, 0);
+ break;
+ case 3:
+ $builder->addShortX($f, $short_val, 0);
+ break;
+ case 4:
+ $builder->addUshortX($f, $ushort_val, 0);
+ break;
+ case 5:
+ $builder->addIntX($f, $int_val, 0);
+ break;
+ case 6:
+ $builder->addUintX($f, $uint_val, 0);
+ break;
+ case 7:
+ $builder->addLongX($f, $long_val, 0);
+ break;
+ case 8:
+ $builder->addUlongX($f, $ulong_val, 0);
+ break;
+ case 9:
+ $builder->addFloatX($f, $float_val, 0);
+ break;
+ case 10:
+ $builder->addDoubleX($f, $double_val, 0);
+ break;
+ }
+ }
+ $objects[] = $builder->endObject();
+ }
+ $builder->prep(8, 0); // Align whole buffer.
+
+ mt_srand(48271); // Reset
+ $builder->finish($objects[count($objects) - 1]);
+
+ $view = Google\FlatBuffers\ByteBuffer::wrap($builder->sizedByteArray());
+ for ($i = 0; $i < $num_fuzz_objects; $i++) {
+ $offset = $view->capacity() - $objects[$i];
+ for ($f = 0; $f < $fields_per_object; $f++) {
+ $choice = mt_rand() % $test_values_max;
+ $vtable_offset = fieldIndexToOffset($f);
+ $vtable = $offset - $view->getInt($offset);
+ $assert->ok($vtable_offset < $view->getShort($vtable));
+ $field_offset = $offset + $view->getShort($vtable + $vtable_offset);
+ switch ($choice) {
+ case 0:
+ $assert->strictEqual(!!$view->getBool($field_offset), $bool_val);
+ break;
+ case 1:
+ $assert->strictEqual($view->getSbyte($field_offset), $char_val);
+ break;
+ case 2:
+ $assert->strictEqual($view->getByte($field_offset), $uchar_val);
+ break;
+ case 3:
+ $assert->strictEqual($view->getShort($field_offset), $short_val);
+ break;
+ case 4:
+ $assert->strictEqual($view->getUShort($field_offset), $ushort_val);
+ break;
+ case 5:
+ $assert->strictEqual($view->getInt($field_offset), $int_val);
+ break;
+ case 6:
+ $assert->strictEqual($view->getUint($field_offset), $uint_val);
+ break;
+ case 7:
+ if (PHP_INT_SIZE <= 4) break;
+ $assert->strictEqual($view->getLong($field_offset), $long_val);
+ break;
+ case 8:
+ if (PHP_INT_SIZE <= 4) break;
+ $assert->strictEqual($view->getUlong($field_offset), $ulong_val);
+ break;
+ case 9:
+ $assert->strictEqual(floor($view->getFloat($field_offset)), floor($float_val));
+ break;
+ case 10:
+ $assert->strictEqual($view->getDouble($field_offset), $double_val);
+ break;
+ }
+ }
+ }
+}
+
+function fieldIndexToOffset($field_id) {
+ // Should correspond to what EndTable() below builds up.
+ $fixed_fields = 2; // Vtable size and Object Size.
+ return ($field_id + $fixed_fields) * 2;
+}
+
+function testByteBuffer(Assert $assert) {
+
+ //Test: ByteBuffer_Length_MatchesBufferLength
+ $buffer = str_repeat("\0", 100);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Equal($uut->capacity(), strlen($buffer));
+
+ //Test: ByteBuffer_PutBytePopulatesBufferAtZeroOffset
+ $buffer = "\0";
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $uut->putByte(0, "\x63"); // 99
+ $assert->Equal("\x63", $uut->_buffer[0]); // don't share buffer as php user might confuse reference.
+
+ //Test: ByteBuffer_PutByteCannotPutAtOffsetPastLength
+ $buffer = "\0";
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Throws(new OutOfRangeException(), function() use ($uut) {
+ $uut->putByte(1, "\x63"); // 99
+ });
+
+ //Test: ByteBuffer_PutShortPopulatesBufferCorrectly
+ $buffer = str_repeat("\0", 2);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $uut->putShort(0, 1);
+
+ // Ensure Endiannes was written correctly
+ $assert->Equal(chr(0x01), $uut->_buffer[0]);
+ $assert->Equal(chr(0x00), $uut->_buffer[1]);
+
+ $buffer = str_repeat("\0", 2);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $uut->putShort(0, -32768);
+
+ // Ensure Endiannes was written correctly
+ $assert->Equal(chr(0x00), $uut->_buffer[0]);
+ $assert->Equal(chr(0x80), $uut->_buffer[1]);
+
+ //Test: ByteBuffer_PutShortCannotPutAtOffsetPastLength
+ $buffer = "\0";
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Throws(new OutOfRangeException(), function() use ($uut) {
+ $uut->putShort(2, "\x63"); // 99
+ });
+
+ //Test: ByteBuffer_PutShortChecksLength
+ $buffer = "\0";
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Throws(new OutOfRangeException(), function() use ($uut) {
+ $uut->putShort(0, "\x63"); // 99
+ });
+
+ //Test: ByteBuffer_PutShortChecksLengthAndOffset
+ $buffer = str_repeat("\0", 2);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Throws(new OutOfRangeException(), function() use ($uut) {
+ $uut->putShort(1, "\x63"); // 99
+ });
+
+ //Test: ByteBuffer_PutIntPopulatesBufferCorrectly
+ $buffer = str_repeat("\0", 4);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $uut->putInt(0, 0x0A0B0C0D);
+ $assert->Equal(chr(0x0D), $uut->_buffer[0]);
+ $assert->Equal(chr(0x0C), $uut->_buffer[1]);
+ $assert->Equal(chr(0x0B), $uut->_buffer[2]);
+ $assert->Equal(chr(0x0A), $uut->_buffer[3]);
+
+ $buffer = str_repeat("\0", 4);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $uut->putInt(0, -2147483648);
+ $assert->Equal(chr(0x00), $uut->_buffer[0]);
+ $assert->Equal(chr(0x00), $uut->_buffer[1]);
+ $assert->Equal(chr(0x00), $uut->_buffer[2]);
+ $assert->Equal(chr(0x80), $uut->_buffer[3]);
+
+ //Test: ByteBuffer_PutIntCannotPutAtOffsetPastLength
+ $buffer = str_repeat("\0", 4);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Throws(new OutOfRangeException(), function() use ($uut) {
+ $uut->putInt(2, 0x0A0B0C0D);
+ });
+
+ //Test: ByteBuffer_PutIntChecksLength
+ $buffer = str_repeat("\0", 1);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Throws(new OutOfRangeException(), function() use ($uut) {
+ $uut->putInt(0, 0x0A0B0C0D);
+ });
+
+ //Test: ByteBuffer_PutIntChecksLengthAndOffset
+ $buffer = str_repeat("\0", 4);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Throws(new OutOfRangeException(), function() use ($uut) {
+ $uut->putInt(2, 0x0A0B0C0D);
+ });
+
+ if (PHP_INT_SIZE > 4) {
+ //Test: ByteBuffer_PutLongPopulatesBufferCorrectly
+ $buffer = str_repeat("\0", 8);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $uut->putLong(0, 0x010203040A0B0C0D);
+ $assert->Equal(chr(0x0D), $uut->_buffer[0]);
+ $assert->Equal(chr(0x0C), $uut->_buffer[1]);
+ $assert->Equal(chr(0x0B), $uut->_buffer[2]);
+ $assert->Equal(chr(0x0A), $uut->_buffer[3]);
+ $assert->Equal(chr(0x04), $uut->_buffer[4]);
+ $assert->Equal(chr(0x03), $uut->_buffer[5]);
+ $assert->Equal(chr(0x02), $uut->_buffer[6]);
+ $assert->Equal(chr(0x01), $uut->_buffer[7]);
+
+ //Test: ByteBuffer_PutLongCannotPutAtOffsetPastLength
+ $buffer = str_repeat("\0", 8);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Throws(new OutOfRangeException(), function() use ($uut) {
+ $uut->putLong(2, 0x010203040A0B0C0D);
+ });
+
+ //Test: ByteBuffer_PutLongCannotPutAtOffsetPastLength
+ $buffer = str_repeat("\0", 1);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Throws(new OutOfRangeException(), function() use ($uut) {
+ $uut->putLong(0, 0x010203040A0B0C0D);
+ });
+
+
+ //Test: ByteBuffer_PutLongChecksLengthAndOffset
+ $buffer = str_repeat("\0", 8);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Throws(new OutOfRangeException(), function() use ($uut) {
+ $uut->putLong(2, 0x010203040A0B0C0D);
+ });
+ }
+
+ //Test: ByteBuffer_GetByteReturnsCorrectData
+ $buffer = str_repeat("\0", 1);
+ $buffer[0] = "\x63";
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Equal("\x63", $uut->get(0));
+
+ //Test: ByteBuffer_GetByteChecksOffset
+ $buffer = str_repeat("\0", 1);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Throws(new OutOfRangeException(), function() use ($uut) {
+ $uut->get(1);
+ });
+
+ //Test: ByteBuffer_GetShortReturnsCorrectData
+ $buffer = str_repeat("\0", 2);
+ $buffer[0] = chr(0x01);
+ $buffer[1] = chr(0x00);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Equal(1, $uut->getShort(0));
+
+ //Test: ByteBuffer_GetShortReturnsCorrectData (signed value)
+ $buffer = str_repeat("\0", 2);
+ $buffer[0] = chr(0x00);
+ $buffer[1] = chr(0x80);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Equal(-32768, $uut->getShort(0));
+
+ //Test: ByteBuffer_GetShortChecksOffset
+ $buffer = str_repeat("\0", 2);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Throws(new OutOfRangeException(), function() use ($uut) {
+ $uut->getShort(2);
+ });
+
+ //Test: ByteBuffer_GetShortChecksLength
+ $buffer = str_repeat("\0", 2);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Throws(new OutOfRangeException(), function() use ($uut) {
+ $uut->getShort(1);
+ });
+
+ //Test: ByteBuffer_GetIntReturnsCorrectData
+ $buffer = str_repeat("\0", 4);
+ $buffer[0] = chr(0x0D);
+ $buffer[1] = chr(0x0C);
+ $buffer[2] = chr(0x0B);
+ $buffer[3] = chr(0x0A);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Equal(0x0A0B0C0D, $uut->getInt(0));
+
+ $buffer = str_repeat("\0", 4);
+ $buffer[0] = chr(0x00);
+ $buffer[1] = chr(0x00);
+ $buffer[2] = chr(0x00);
+ $buffer[3] = chr(0x80);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Equal(-2147483648, $uut->getInt(0));
+
+ //Test: ByteBuffer_GetIntChecksOffset
+ $buffer = str_repeat("\0", 4);
+
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Throws(new OutOfRangeException(), function() use ($uut) {
+ $uut->getInt(4);
+ });
+
+ //Test: ByteBuffer_GetIntChecksLength
+ $buffer = str_repeat("\0", 2);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Throws(new OutOfRangeException(), function() use ($uut) {
+ $uut->getInt(0);
+ });
+
+ if (PHP_INT_SIZE > 4) {
+ //Test: ByteBuffer_GetLongReturnsCorrectData
+ $buffer = str_repeat("\0", 8);
+ $buffer[0] = chr(0x0D);
+ $buffer[1] = chr(0x0C);
+ $buffer[2] = chr(0x0B);
+ $buffer[3] = chr(0x0A);
+ $buffer[4] = chr(0x04);
+ $buffer[5] = chr(0x03);
+ $buffer[6] = chr(0x02);
+ $buffer[7] = chr(0x01);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Equal(0x010203040A0B0C0D, $uut->getLong(0));
+ }
+
+ //Test: ByteBuffer_GetLongChecksOffset
+ $buffer = str_repeat("\0", 8);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Throws(new OutOfRangeException(), function() use ($uut) {
+ $uut->getLong(8);
+ });
+
+ //Test: ByteBuffer_GetLongChecksLength
+ $buffer = str_repeat("\0", 7);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Throws(new OutOfRangeException(), function() use ($uut) {
+ $uut->getLong(0);
+ });
+
+ //Test: big endian
+ $buffer = str_repeat("\0", 2);
+ // 0xFF 0x00
+ // Little Endian: 255
+ // Big Endian: 65280
+ $buffer[0] = chr(0xff);
+ $buffer[1] = chr(0x00);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Equal(65280, $uut->readLittleEndian(0, 2, true));
+
+ $buffer = str_repeat("\0", 4);
+ $buffer[0] = chr(0x0D);
+ $buffer[1] = chr(0x0C);
+ $buffer[2] = chr(0x0B);
+ $buffer[3] = chr(0x0A);
+ $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
+ $assert->Equal(0x0D0C0B0A, $uut->readLittleEndian(0, 4, true));
+
+}
+
+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));
+ }
+ }
+ }
+}