Exemplo n.º 1
0
function testByteBuffer(Assert $assert)
{
    //Test: ByteBuffer_Length_MatchesBufferLength
    $buffer = str_repeat("", 100);
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $assert->Equal($uut->capacity(), strlen($buffer));
    //Test: ByteBuffer_PutBytePopulatesBufferAtZeroOffset
    $buffer = "";
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $uut->putByte(0, "c");
    // 99
    $assert->Equal("c", $uut->_buffer[0]);
    // don't share buffer as php user might confuse reference.
    //Test: ByteBuffer_PutByteCannotPutAtOffsetPastLength
    $buffer = "";
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $assert->Throws(new OutOfRangeException(), function () use($uut) {
        $uut->putByte(1, "c");
        // 99
    });
    //Test: ByteBuffer_PutShortPopulatesBufferCorrectly
    $buffer = str_repeat("", 2);
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $uut->putShort(0, 1);
    // Ensure Endiannes was written correctly
    $assert->Equal(chr(0x1), $uut->_buffer[0]);
    $assert->Equal(chr(0x0), $uut->_buffer[1]);
    $buffer = str_repeat("", 2);
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $uut->putShort(0, -32768);
    // Ensure Endiannes was written correctly
    $assert->Equal(chr(0x0), $uut->_buffer[0]);
    $assert->Equal(chr(0x80), $uut->_buffer[1]);
    //Test: ByteBuffer_PutShortCannotPutAtOffsetPastLength
    $buffer = "";
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $assert->Throws(new OutOfRangeException(), function () use($uut) {
        $uut->putShort(2, "c");
        // 99
    });
    //Test: ByteBuffer_PutShortChecksLength
    $buffer = "";
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $assert->Throws(new OutOfRangeException(), function () use($uut) {
        $uut->putShort(0, "c");
        // 99
    });
    //Test: ByteBuffer_PutShortChecksLengthAndOffset
    $buffer = str_repeat("", 2);
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $assert->Throws(new OutOfRangeException(), function () use($uut) {
        $uut->putShort(1, "c");
        // 99
    });
    //Test: ByteBuffer_PutIntPopulatesBufferCorrectly
    $buffer = str_repeat("", 4);
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $uut->putInt(0, 0xa0b0c0d);
    $assert->Equal(chr(0xd), $uut->_buffer[0]);
    $assert->Equal(chr(0xc), $uut->_buffer[1]);
    $assert->Equal(chr(0xb), $uut->_buffer[2]);
    $assert->Equal(chr(0xa), $uut->_buffer[3]);
    $buffer = str_repeat("", 4);
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $uut->putInt(0, -2147483648);
    $assert->Equal(chr(0x0), $uut->_buffer[0]);
    $assert->Equal(chr(0x0), $uut->_buffer[1]);
    $assert->Equal(chr(0x0), $uut->_buffer[2]);
    $assert->Equal(chr(0x80), $uut->_buffer[3]);
    //Test: ByteBuffer_PutIntCannotPutAtOffsetPastLength
    $buffer = str_repeat("", 4);
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $assert->Throws(new OutOfRangeException(), function () use($uut) {
        $uut->putInt(2, 0xa0b0c0d);
    });
    //Test: ByteBuffer_PutIntChecksLength
    $buffer = str_repeat("", 1);
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $assert->Throws(new OutOfRangeException(), function () use($uut) {
        $uut->putInt(0, 0xa0b0c0d);
    });
    //Test: ByteBuffer_PutIntChecksLengthAndOffset
    $buffer = str_repeat("", 4);
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $assert->Throws(new OutOfRangeException(), function () use($uut) {
        $uut->putInt(2, 0xa0b0c0d);
    });
    if (PHP_INT_SIZE > 4) {
        //Test: ByteBuffer_PutLongPopulatesBufferCorrectly
        $buffer = str_repeat("", 8);
        $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
        $uut->putLong(0, 0x10203040a0b0c0d);
        $assert->Equal(chr(0xd), $uut->_buffer[0]);
        $assert->Equal(chr(0xc), $uut->_buffer[1]);
        $assert->Equal(chr(0xb), $uut->_buffer[2]);
        $assert->Equal(chr(0xa), $uut->_buffer[3]);
        $assert->Equal(chr(0x4), $uut->_buffer[4]);
        $assert->Equal(chr(0x3), $uut->_buffer[5]);
        $assert->Equal(chr(0x2), $uut->_buffer[6]);
        $assert->Equal(chr(0x1), $uut->_buffer[7]);
        //Test: ByteBuffer_PutLongCannotPutAtOffsetPastLength
        $buffer = str_repeat("", 8);
        $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
        $assert->Throws(new OutOfRangeException(), function () use($uut) {
            $uut->putLong(2, 0x10203040a0b0c0d);
        });
        //Test: ByteBuffer_PutLongCannotPutAtOffsetPastLength
        $buffer = str_repeat("", 1);
        $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
        $assert->Throws(new OutOfRangeException(), function () use($uut) {
            $uut->putLong(0, 0x10203040a0b0c0d);
        });
        //Test: ByteBuffer_PutLongChecksLengthAndOffset
        $buffer = str_repeat("", 8);
        $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
        $assert->Throws(new OutOfRangeException(), function () use($uut) {
            $uut->putLong(2, 0x10203040a0b0c0d);
        });
    }
    //Test: ByteBuffer_GetByteReturnsCorrectData
    $buffer = str_repeat("", 1);
    $buffer[0] = "c";
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $assert->Equal("c", $uut->get(0));
    //Test: ByteBuffer_GetByteChecksOffset
    $buffer = str_repeat("", 1);
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $assert->Throws(new OutOfRangeException(), function () use($uut) {
        $uut->get(1);
    });
    //Test: ByteBuffer_GetShortReturnsCorrectData
    $buffer = str_repeat("", 2);
    $buffer[0] = chr(0x1);
    $buffer[1] = chr(0x0);
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $assert->Equal(1, $uut->getShort(0));
    //Test: ByteBuffer_GetShortReturnsCorrectData (signed value)
    $buffer = str_repeat("", 2);
    $buffer[0] = chr(0x0);
    $buffer[1] = chr(0x80);
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $assert->Equal(-32768, $uut->getShort(0));
    //Test: ByteBuffer_GetShortChecksOffset
    $buffer = str_repeat("", 2);
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $assert->Throws(new OutOfRangeException(), function () use($uut) {
        $uut->getShort(2);
    });
    //Test: ByteBuffer_GetShortChecksLength
    $buffer = str_repeat("", 2);
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $assert->Throws(new OutOfRangeException(), function () use($uut) {
        $uut->getShort(1);
    });
    //Test: ByteBuffer_GetIntReturnsCorrectData
    $buffer = str_repeat("", 4);
    $buffer[0] = chr(0xd);
    $buffer[1] = chr(0xc);
    $buffer[2] = chr(0xb);
    $buffer[3] = chr(0xa);
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $assert->Equal(0xa0b0c0d, $uut->getInt(0));
    $buffer = str_repeat("", 4);
    $buffer[0] = chr(0x0);
    $buffer[1] = chr(0x0);
    $buffer[2] = chr(0x0);
    $buffer[3] = chr(0x80);
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $assert->Equal(-2147483648, $uut->getInt(0));
    //Test: ByteBuffer_GetIntChecksOffset
    $buffer = str_repeat("", 4);
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $assert->Throws(new OutOfRangeException(), function () use($uut) {
        $uut->getInt(4);
    });
    //Test: ByteBuffer_GetIntChecksLength
    $buffer = str_repeat("", 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("", 8);
        $buffer[0] = chr(0xd);
        $buffer[1] = chr(0xc);
        $buffer[2] = chr(0xb);
        $buffer[3] = chr(0xa);
        $buffer[4] = chr(0x4);
        $buffer[5] = chr(0x3);
        $buffer[6] = chr(0x2);
        $buffer[7] = chr(0x1);
        $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
        $assert->Equal(0x10203040a0b0c0d, $uut->getLong(0));
        //Test: Signed Long
        $buffer = str_repeat("", 8);
        $buffer[0] = chr(0x0);
        $buffer[1] = chr(0x0);
        $buffer[2] = chr(0x0);
        $buffer[3] = chr(0x0);
        $buffer[4] = chr(0x0);
        $buffer[5] = chr(0x0);
        $buffer[6] = chr(0x0);
        $buffer[7] = chr(0x80);
        $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
        $assert->Equal(-1 << 63, $uut->getLong(0));
    }
    //Test: ByteBuffer_GetLongChecksOffset
    $buffer = str_repeat("", 8);
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $assert->Throws(new OutOfRangeException(), function () use($uut) {
        $uut->getLong(8);
    });
    //Test: ByteBuffer_GetLongChecksLength
    $buffer = str_repeat("", 7);
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $assert->Throws(new OutOfRangeException(), function () use($uut) {
        $uut->getLong(0);
    });
    //Test: big endian
    $buffer = str_repeat("", 2);
    // 0xFF 0x00
    // Little Endian: 255
    // Big Endian: 65280
    $buffer[0] = chr(0xff);
    $buffer[1] = chr(0x0);
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $assert->Equal(65280, $uut->readLittleEndian(0, 2, true));
    $buffer = str_repeat("", 4);
    $buffer[0] = chr(0xd);
    $buffer[1] = chr(0xc);
    $buffer[2] = chr(0xb);
    $buffer[3] = chr(0xa);
    $uut = Google\FlatBuffers\ByteBuffer::wrap($buffer);
    $assert->Equal(0xd0c0b0a, $uut->readLittleEndian(0, 4, true));
}