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 = 0x7fffffff | 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; } } } }