1
0
mirror of https://github.com/pmmp/ext-encoding.git synced 2024-11-23 13:36:25 +00:00
ext-encoding/tests/read-byte-array.phpt
Dylan K. Taylor 9a5d74ab2a
Separate read and write offsets
this isn't an ideal solution, but it's the easiest one available and gives time to figure out next steps for the API design.
2024-01-30 14:25:44 +00:00

55 lines
1.2 KiB
PHP

--TEST--
Test that ByteBuffer::readByteArray() works as expected
--FILE--
<?php
use pmmp\encoding\ByteBuffer;
use pmmp\encoding\DataDecodeException;
$buffer = new ByteBuffer("");
//read with no bytes available
try{
$buffer->readByteArray(1);
}catch(DataDecodeException $e){
echo $e->getMessage() . PHP_EOL;
}
$buffer = new ByteBuffer("abcde");
var_dump($buffer->readByteArray(3));
var_dump($buffer->readByteArray(1));
try{
$buffer->readByteArray(2);
}catch(DataDecodeException $e){
echo $e->getMessage() . PHP_EOL;
}
$buffer = new ByteBuffer("abcde");
try{
$buffer->readByteArray(-1);
}catch(\ValueError $e){
echo $e->getMessage() . PHP_EOL;
}
//ensure offset is updated properly
$buffer->setReadOffset(1);
var_dump($buffer->readByteArray(2));
var_dump($buffer->getReadOffset());
//read with bytes, but all before the buffer start
$buffer->setReadOffset(5);
try{
$buffer->readByteArray(2);
}catch(DataDecodeException $e){
echo $e->getMessage() . PHP_EOL;
}
--EXPECT--
Need at least 1 bytes, but only have 0 bytes
string(3) "abc"
string(1) "d"
Need at least 2 bytes, but only have 1 bytes
Length cannot be negative
string(2) "bc"
int(3)
Need at least 2 bytes, but only have 0 bytes