2016-09-15 18:09:01 +00:00
|
|
|
<?php
|
|
|
|
|
2020-12-04 21:46:34 +00:00
|
|
|
require_once('test_base.php');
|
2016-09-15 18:09:01 +00:00
|
|
|
require_once('test_util.php');
|
|
|
|
|
|
|
|
use Google\Protobuf\Internal\RepeatedField;
|
|
|
|
use Google\Protobuf\Internal\GPBType;
|
|
|
|
use Foo\TestMessage;
|
2018-05-23 23:43:30 +00:00
|
|
|
use Foo\TestMessage\Sub;
|
2016-09-15 18:09:01 +00:00
|
|
|
|
2020-12-04 21:46:34 +00:00
|
|
|
class ArrayTest extends TestBase
|
2016-09-15 18:09:01 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
#########################################################
|
|
|
|
# Test int32 field.
|
|
|
|
#########################################################
|
|
|
|
|
|
|
|
public function testInt32()
|
|
|
|
{
|
|
|
|
$arr = new RepeatedField(GPBType::INT32);
|
|
|
|
|
|
|
|
// Test append.
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = MAX_INT32;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(MAX_INT32, $arr[0]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = MIN_INT32;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(MIN_INT32, $arr[1]);
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = 1.1;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(1, $arr[2]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = MAX_INT32_FLOAT;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(MAX_INT32, $arr[3]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = MAX_INT32_FLOAT;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(MAX_INT32, $arr[4]);
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = '2';
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(2, $arr[5]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = '3.1';
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(3, $arr[6]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = MAX_INT32_STRING;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(MAX_INT32, $arr[7]);
|
|
|
|
|
|
|
|
$this->assertEquals(8, count($arr));
|
|
|
|
|
|
|
|
for ($i = 0; $i < count($arr); $i++) {
|
|
|
|
$arr[$i] = 0;
|
|
|
|
$this->assertSame(0, $arr[$i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test set.
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[0] = MAX_INT32;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(MAX_INT32, $arr[0]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[1] = MIN_INT32;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(MIN_INT32, $arr[1]);
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[2] = 1.1;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(1, $arr[2]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[3] = MAX_INT32_FLOAT;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(MAX_INT32, $arr[3]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[4] = MAX_INT32_FLOAT;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(MAX_INT32, $arr[4]);
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[5] = '2';
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(2, $arr[5]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[6] = '3.1';
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(3, $arr[6]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[7] = MAX_INT32_STRING;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(MAX_INT32, $arr[7]);
|
2016-11-08 19:38:34 +00:00
|
|
|
|
|
|
|
// Test foreach.
|
|
|
|
$arr = new RepeatedField(GPBType::INT32);
|
|
|
|
for ($i = 0; $i < 3; $i++) {
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = $i;
|
2016-11-08 19:38:34 +00:00
|
|
|
}
|
|
|
|
$i = 0;
|
|
|
|
foreach ($arr as $val) {
|
|
|
|
$this->assertSame($i++, $val);
|
|
|
|
}
|
|
|
|
$this->assertSame(3, $i);
|
2016-09-15 18:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#########################################################
|
|
|
|
# Test uint32 field.
|
|
|
|
#########################################################
|
|
|
|
|
|
|
|
public function testUint32()
|
|
|
|
{
|
|
|
|
$arr = new RepeatedField(GPBType::UINT32);
|
|
|
|
|
|
|
|
// Test append.
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = MAX_UINT32;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(-1, $arr[0]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = -1;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(-1, $arr[1]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = MIN_UINT32;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(MIN_UINT32, $arr[2]);
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = 1.1;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(1, $arr[3]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = MAX_UINT32_FLOAT;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(-1, $arr[4]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = -1.0;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(-1, $arr[5]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = MIN_UINT32_FLOAT;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(MIN_UINT32, $arr[6]);
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = '2';
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(2, $arr[7]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = '3.1';
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(3, $arr[8]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = MAX_UINT32_STRING;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(-1, $arr[9]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = '-1.0';
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(-1, $arr[10]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = MIN_UINT32_STRING;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(MIN_UINT32, $arr[11]);
|
|
|
|
|
|
|
|
$this->assertEquals(12, count($arr));
|
|
|
|
|
|
|
|
for ($i = 0; $i < count($arr); $i++) {
|
|
|
|
$arr[$i] = 0;
|
|
|
|
$this->assertSame(0, $arr[$i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test set.
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[0] = MAX_UINT32;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(-1, $arr[0]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[1] = -1;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(-1, $arr[1]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[2] = MIN_UINT32;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(MIN_UINT32, $arr[2]);
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[3] = 1.1;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(1, $arr[3]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[4] = MAX_UINT32_FLOAT;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(-1, $arr[4]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[5] = -1.0;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(-1, $arr[5]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[6] = MIN_UINT32_FLOAT;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(MIN_UINT32, $arr[6]);
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[7] = '2';
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(2, $arr[7]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[8] = '3.1';
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(3, $arr[8]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[9] = MAX_UINT32_STRING;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(-1, $arr[9]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[10] = '-1.0';
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(-1, $arr[10]);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[11] = MIN_UINT32_STRING;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(MIN_UINT32, $arr[11]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#########################################################
|
|
|
|
# Test int64 field.
|
|
|
|
#########################################################
|
|
|
|
|
|
|
|
public function testInt64()
|
|
|
|
{
|
|
|
|
$arr = new RepeatedField(GPBType::INT64);
|
|
|
|
|
|
|
|
// Test append.
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = MAX_INT64;
|
|
|
|
$arr[] = MIN_INT64;
|
|
|
|
$arr[] = 1.1;
|
|
|
|
$arr[] = '2';
|
|
|
|
$arr[] = '3.1';
|
|
|
|
$arr[] = MAX_INT64_STRING;
|
|
|
|
$arr[] = MIN_INT64_STRING;
|
2016-10-26 00:27:05 +00:00
|
|
|
if (PHP_INT_SIZE == 4) {
|
|
|
|
$this->assertSame(MAX_INT64, $arr[0]);
|
|
|
|
$this->assertSame(MIN_INT64, $arr[1]);
|
|
|
|
$this->assertSame('1', $arr[2]);
|
|
|
|
$this->assertSame('2', $arr[3]);
|
|
|
|
$this->assertSame('3', $arr[4]);
|
|
|
|
$this->assertSame(MAX_INT64_STRING, $arr[5]);
|
|
|
|
$this->assertSame(MIN_INT64_STRING, $arr[6]);
|
|
|
|
} else {
|
|
|
|
$this->assertSame(MAX_INT64, $arr[0]);
|
|
|
|
$this->assertSame(MIN_INT64, $arr[1]);
|
|
|
|
$this->assertSame(1, $arr[2]);
|
|
|
|
$this->assertSame(2, $arr[3]);
|
|
|
|
$this->assertSame(3, $arr[4]);
|
|
|
|
$this->assertSame(MAX_INT64, $arr[5]);
|
|
|
|
$this->assertSame(MIN_INT64, $arr[6]);
|
|
|
|
}
|
|
|
|
|
2016-09-15 18:09:01 +00:00
|
|
|
|
|
|
|
$this->assertEquals(7, count($arr));
|
|
|
|
|
|
|
|
for ($i = 0; $i < count($arr); $i++) {
|
|
|
|
$arr[$i] = 0;
|
2016-10-26 00:27:05 +00:00
|
|
|
if (PHP_INT_SIZE == 4) {
|
|
|
|
$this->assertSame('0', $arr[$i]);
|
|
|
|
} else {
|
|
|
|
$this->assertSame(0, $arr[$i]);
|
|
|
|
}
|
2016-09-15 18:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test set.
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[0] = MAX_INT64;
|
|
|
|
$arr[1] = MIN_INT64;
|
|
|
|
$arr[2] = 1.1;
|
|
|
|
$arr[3] = '2';
|
|
|
|
$arr[4] = '3.1';
|
|
|
|
$arr[5] = MAX_INT64_STRING;
|
|
|
|
$arr[6] = MIN_INT64_STRING;
|
2016-10-26 00:27:05 +00:00
|
|
|
|
|
|
|
if (PHP_INT_SIZE == 4) {
|
|
|
|
$this->assertSame(MAX_INT64_STRING, $arr[0]);
|
|
|
|
$this->assertSame(MIN_INT64_STRING, $arr[1]);
|
|
|
|
$this->assertSame('1', $arr[2]);
|
|
|
|
$this->assertSame('2', $arr[3]);
|
|
|
|
$this->assertSame('3', $arr[4]);
|
|
|
|
$this->assertSame(MAX_INT64_STRING, $arr[5]);
|
|
|
|
$this->assertEquals(MIN_INT64_STRING, $arr[6]);
|
|
|
|
} else {
|
|
|
|
$this->assertSame(MAX_INT64, $arr[0]);
|
|
|
|
$this->assertSame(MIN_INT64, $arr[1]);
|
|
|
|
$this->assertSame(1, $arr[2]);
|
|
|
|
$this->assertSame(2, $arr[3]);
|
|
|
|
$this->assertSame(3, $arr[4]);
|
|
|
|
$this->assertSame(MAX_INT64, $arr[5]);
|
|
|
|
$this->assertEquals(MIN_INT64, $arr[6]);
|
|
|
|
}
|
2016-09-15 18:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#########################################################
|
|
|
|
# Test uint64 field.
|
|
|
|
#########################################################
|
|
|
|
|
|
|
|
public function testUint64()
|
|
|
|
{
|
|
|
|
$arr = new RepeatedField(GPBType::UINT64);
|
|
|
|
|
|
|
|
// Test append.
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = MAX_UINT64;
|
|
|
|
$arr[] = 1.1;
|
|
|
|
$arr[] = '2';
|
|
|
|
$arr[] = '3.1';
|
|
|
|
$arr[] = MAX_UINT64_STRING;
|
2016-09-15 18:09:01 +00:00
|
|
|
|
2016-10-26 00:27:05 +00:00
|
|
|
if (PHP_INT_SIZE == 4) {
|
|
|
|
$this->assertSame(MAX_UINT64_STRING, $arr[0]);
|
|
|
|
$this->assertSame('1', $arr[1]);
|
|
|
|
$this->assertSame('2', $arr[2]);
|
|
|
|
$this->assertSame('3', $arr[3]);
|
|
|
|
$this->assertSame(MAX_UINT64_STRING, $arr[4]);
|
|
|
|
} else {
|
|
|
|
$this->assertSame(MAX_UINT64, $arr[0]);
|
|
|
|
$this->assertSame(1, $arr[1]);
|
|
|
|
$this->assertSame(2, $arr[2]);
|
|
|
|
$this->assertSame(3, $arr[3]);
|
|
|
|
$this->assertSame(MAX_UINT64, $arr[4]);
|
|
|
|
$this->assertSame(5, count($arr));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertSame(5, count($arr));
|
2016-09-15 18:09:01 +00:00
|
|
|
|
|
|
|
for ($i = 0; $i < count($arr); $i++) {
|
|
|
|
$arr[$i] = 0;
|
2016-10-26 00:27:05 +00:00
|
|
|
if (PHP_INT_SIZE == 4) {
|
|
|
|
$this->assertSame('0', $arr[$i]);
|
|
|
|
} else {
|
|
|
|
$this->assertSame(0, $arr[$i]);
|
|
|
|
}
|
2016-09-15 18:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test set.
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[0] = MAX_UINT64;
|
|
|
|
$arr[1] = 1.1;
|
|
|
|
$arr[2] = '2';
|
|
|
|
$arr[3] = '3.1';
|
|
|
|
$arr[4] = MAX_UINT64_STRING;
|
2016-10-26 00:27:05 +00:00
|
|
|
|
|
|
|
if (PHP_INT_SIZE == 4) {
|
|
|
|
$this->assertSame(MAX_UINT64_STRING, $arr[0]);
|
|
|
|
$this->assertSame('1', $arr[1]);
|
|
|
|
$this->assertSame('2', $arr[2]);
|
|
|
|
$this->assertSame('3', $arr[3]);
|
|
|
|
$this->assertSame(MAX_UINT64_STRING, $arr[4]);
|
|
|
|
} else {
|
|
|
|
$this->assertSame(MAX_UINT64, $arr[0]);
|
|
|
|
$this->assertSame(1, $arr[1]);
|
|
|
|
$this->assertSame(2, $arr[2]);
|
|
|
|
$this->assertSame(3, $arr[3]);
|
|
|
|
$this->assertSame(MAX_UINT64, $arr[4]);
|
|
|
|
}
|
2016-09-15 18:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#########################################################
|
|
|
|
# Test float field.
|
|
|
|
#########################################################
|
|
|
|
|
|
|
|
public function testFloat()
|
|
|
|
{
|
|
|
|
$arr = new RepeatedField(GPBType::FLOAT);
|
|
|
|
|
|
|
|
// Test append.
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = 1;
|
2020-12-04 21:46:34 +00:00
|
|
|
$this->assertFloatEquals(1.0, $arr[0], MAX_FLOAT_DIFF);
|
2016-09-15 18:09:01 +00:00
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = 1.1;
|
2020-12-04 21:46:34 +00:00
|
|
|
$this->assertFloatEquals(1.1, $arr[1], MAX_FLOAT_DIFF);
|
2016-09-15 18:09:01 +00:00
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = '2';
|
2020-12-04 21:46:34 +00:00
|
|
|
$this->assertFloatEquals(2.0, $arr[2], MAX_FLOAT_DIFF);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = '3.1';
|
2020-12-04 21:46:34 +00:00
|
|
|
$this->assertFloatEquals(3.1, $arr[3], MAX_FLOAT_DIFF);
|
2016-09-15 18:09:01 +00:00
|
|
|
|
|
|
|
$this->assertEquals(4, count($arr));
|
|
|
|
|
|
|
|
for ($i = 0; $i < count($arr); $i++) {
|
|
|
|
$arr[$i] = 0;
|
|
|
|
$this->assertSame(0.0, $arr[$i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test set.
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[0] = 1;
|
2020-12-04 21:46:34 +00:00
|
|
|
$this->assertFloatEquals(1.0, $arr[0], MAX_FLOAT_DIFF);
|
2016-09-15 18:09:01 +00:00
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[1] = 1.1;
|
2020-12-04 21:46:34 +00:00
|
|
|
$this->assertFloatEquals(1.1, $arr[1], MAX_FLOAT_DIFF);
|
2016-09-15 18:09:01 +00:00
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[2] = '2';
|
2020-12-04 21:46:34 +00:00
|
|
|
$this->assertFloatEquals(2.0, $arr[2], MAX_FLOAT_DIFF);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[3] = '3.1';
|
2020-12-04 21:46:34 +00:00
|
|
|
$this->assertFloatEquals(3.1, $arr[3], MAX_FLOAT_DIFF);
|
2016-09-15 18:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#########################################################
|
|
|
|
# Test double field.
|
|
|
|
#########################################################
|
|
|
|
|
|
|
|
public function testDouble()
|
|
|
|
{
|
|
|
|
$arr = new RepeatedField(GPBType::DOUBLE);
|
|
|
|
|
|
|
|
// Test append.
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = 1;
|
2020-12-04 21:46:34 +00:00
|
|
|
$this->assertFloatEquals(1.0, $arr[0], MAX_FLOAT_DIFF);
|
2016-09-15 18:09:01 +00:00
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = 1.1;
|
2020-12-04 21:46:34 +00:00
|
|
|
$this->assertFloatEquals(1.1, $arr[1], MAX_FLOAT_DIFF);
|
2016-09-15 18:09:01 +00:00
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = '2';
|
2020-12-04 21:46:34 +00:00
|
|
|
$this->assertFloatEquals(2.0, $arr[2], MAX_FLOAT_DIFF);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = '3.1';
|
2020-12-04 21:46:34 +00:00
|
|
|
$this->assertFloatEquals(3.1, $arr[3], MAX_FLOAT_DIFF);
|
2016-09-15 18:09:01 +00:00
|
|
|
|
|
|
|
$this->assertEquals(4, count($arr));
|
|
|
|
|
|
|
|
for ($i = 0; $i < count($arr); $i++) {
|
|
|
|
$arr[$i] = 0;
|
|
|
|
$this->assertSame(0.0, $arr[$i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test set.
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[0] = 1;
|
2020-12-04 21:46:34 +00:00
|
|
|
$this->assertFloatEquals(1.0, $arr[0], MAX_FLOAT_DIFF);
|
2016-09-15 18:09:01 +00:00
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[1] = 1.1;
|
2020-12-04 21:46:34 +00:00
|
|
|
$this->assertFloatEquals(1.1, $arr[1], MAX_FLOAT_DIFF);
|
2016-09-15 18:09:01 +00:00
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[2] = '2';
|
2020-12-04 21:46:34 +00:00
|
|
|
$this->assertFloatEquals(2.0, $arr[2], MAX_FLOAT_DIFF);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[3] = '3.1';
|
2020-12-04 21:46:34 +00:00
|
|
|
$this->assertFloatEquals(3.1, $arr[3], MAX_FLOAT_DIFF);
|
2016-09-15 18:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#########################################################
|
|
|
|
# Test bool field.
|
|
|
|
#########################################################
|
|
|
|
|
|
|
|
public function testBool()
|
|
|
|
{
|
|
|
|
$arr = new RepeatedField(GPBType::BOOL);
|
|
|
|
|
|
|
|
// Test append.
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = true;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(true, $arr[0]);
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = -1;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(true, $arr[1]);
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = 1.1;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(true, $arr[2]);
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = '';
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(false, $arr[3]);
|
|
|
|
|
|
|
|
$this->assertEquals(4, count($arr));
|
|
|
|
|
|
|
|
for ($i = 0; $i < count($arr); $i++) {
|
|
|
|
$arr[$i] = 0;
|
|
|
|
$this->assertSame(false, $arr[$i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test set.
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[0] = true;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(true, $arr[0]);
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[1] = -1;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(true, $arr[1]);
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[2] = 1.1;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(true, $arr[2]);
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[3] = '';
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(false, $arr[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#########################################################
|
|
|
|
# Test string field.
|
|
|
|
#########################################################
|
|
|
|
|
|
|
|
public function testString()
|
|
|
|
{
|
|
|
|
$arr = new RepeatedField(GPBType::STRING);
|
|
|
|
|
|
|
|
// Test append.
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = 'abc';
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame('abc', $arr[0]);
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = 1;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame('1', $arr[1]);
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = 1.1;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame('1.1', $arr[2]);
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = true;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame('1', $arr[3]);
|
|
|
|
|
|
|
|
$this->assertEquals(4, count($arr));
|
|
|
|
|
|
|
|
for ($i = 0; $i < count($arr); $i++) {
|
|
|
|
$arr[$i] = '';
|
|
|
|
$this->assertSame('', $arr[$i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test set.
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[0] = 'abc';
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame('abc', $arr[0]);
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[1] = 1;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame('1', $arr[1]);
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[2] = 1.1;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame('1.1', $arr[2]);
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[3] = true;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame('1', $arr[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#########################################################
|
|
|
|
# Test message field.
|
|
|
|
#########################################################
|
|
|
|
|
|
|
|
public function testMessage()
|
|
|
|
{
|
2018-05-23 23:43:30 +00:00
|
|
|
$arr = new RepeatedField(GPBType::MESSAGE, Sub::class);
|
2016-09-15 18:09:01 +00:00
|
|
|
|
|
|
|
// Test append.
|
2018-05-23 23:43:30 +00:00
|
|
|
$sub_m = new Sub();
|
2016-09-15 18:09:01 +00:00
|
|
|
$sub_m->setA(1);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = $sub_m;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(1, $arr[0]->getA());
|
|
|
|
|
2017-04-19 23:23:51 +00:00
|
|
|
$this->assertEquals(1, count($arr));
|
2016-09-15 18:09:01 +00:00
|
|
|
|
|
|
|
// Test set.
|
2018-05-23 23:43:30 +00:00
|
|
|
$sub_m = new Sub();
|
2017-04-19 23:23:51 +00:00
|
|
|
$sub_m->setA(2);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[0] = $sub_m;
|
2017-04-19 23:23:51 +00:00
|
|
|
$this->assertSame(2, $arr[0]->getA());
|
2017-07-25 07:49:16 +00:00
|
|
|
|
|
|
|
// Test foreach.
|
2018-05-23 23:43:30 +00:00
|
|
|
$arr = new RepeatedField(GPBType::MESSAGE, Sub::class);
|
2017-07-25 07:49:16 +00:00
|
|
|
for ($i = 0; $i < 3; $i++) {
|
2018-05-23 23:43:30 +00:00
|
|
|
$arr[] = new Sub();
|
2017-07-25 07:49:16 +00:00
|
|
|
$arr[$i]->setA($i);
|
|
|
|
}
|
|
|
|
$i = 0;
|
|
|
|
foreach ($arr as $val) {
|
|
|
|
$this->assertSame($i++, $val->getA());
|
|
|
|
}
|
|
|
|
$this->assertSame(3, $i);
|
2016-09-15 18:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#########################################################
|
|
|
|
# Test offset type
|
|
|
|
#########################################################
|
|
|
|
|
|
|
|
public function testOffset()
|
|
|
|
{
|
|
|
|
$arr = new RepeatedField(GPBType::INT32);
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = 0;
|
2016-09-15 18:09:01 +00:00
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[0] = 1;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(1, $arr[0]);
|
|
|
|
$this->assertSame(1, count($arr));
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr['0'] = 2;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(2, $arr['0']);
|
|
|
|
$this->assertSame(2, $arr[0]);
|
|
|
|
$this->assertSame(1, count($arr));
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[0.0] = 3;
|
2016-09-15 18:09:01 +00:00
|
|
|
$this->assertSame(3, $arr[0.0]);
|
|
|
|
$this->assertSame(1, count($arr));
|
|
|
|
}
|
|
|
|
|
2016-10-05 17:54:39 +00:00
|
|
|
public function testInsertRemoval()
|
|
|
|
{
|
|
|
|
$arr = new RepeatedField(GPBType::INT32);
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = 0;
|
|
|
|
$arr[] = 1;
|
|
|
|
$arr[] = 2;
|
2016-10-05 17:54:39 +00:00
|
|
|
$this->assertSame(3, count($arr));
|
|
|
|
|
|
|
|
unset($arr[2]);
|
|
|
|
$this->assertSame(2, count($arr));
|
|
|
|
$this->assertSame(0, $arr[0]);
|
|
|
|
$this->assertSame(1, $arr[1]);
|
|
|
|
|
2017-06-30 19:14:09 +00:00
|
|
|
$arr[] = 3;
|
2016-10-05 17:54:39 +00:00
|
|
|
$this->assertSame(3, count($arr));
|
|
|
|
$this->assertSame(0, $arr[0]);
|
|
|
|
$this->assertSame(1, $arr[1]);
|
|
|
|
$this->assertSame(3, $arr[2]);
|
|
|
|
}
|
2016-09-15 18:09:01 +00:00
|
|
|
|
2019-01-22 22:02:44 +00:00
|
|
|
#########################################################
|
|
|
|
# Test reference in array
|
|
|
|
#########################################################
|
|
|
|
|
2019-01-23 20:44:20 +00:00
|
|
|
public function testArrayElementIsReferenceInSetters()
|
2019-01-22 22:02:44 +00:00
|
|
|
{
|
2019-01-23 20:44:20 +00:00
|
|
|
// Bool elements
|
|
|
|
$values = [true];
|
|
|
|
array_walk($values, function (&$value) {});
|
2019-01-22 22:02:44 +00:00
|
|
|
$m = new TestMessage();
|
2019-01-23 20:44:20 +00:00
|
|
|
$m->setRepeatedBool($values);
|
2019-01-22 22:02:44 +00:00
|
|
|
|
2019-01-23 20:44:20 +00:00
|
|
|
// Int32 elements
|
|
|
|
$values = [1];
|
|
|
|
array_walk($values, function (&$value) {});
|
|
|
|
$m = new TestMessage();
|
|
|
|
$m->setRepeatedInt32($values);
|
2019-01-22 22:02:44 +00:00
|
|
|
|
2019-01-23 20:44:20 +00:00
|
|
|
// Double elements
|
|
|
|
$values = [1.0];
|
|
|
|
array_walk($values, function (&$value) {});
|
|
|
|
$m = new TestMessage();
|
|
|
|
$m->setRepeatedDouble($values);
|
2019-01-22 22:02:44 +00:00
|
|
|
|
2019-01-23 20:44:20 +00:00
|
|
|
// String elements
|
|
|
|
$values = ['a'];
|
|
|
|
array_walk($values, function (&$value) {});
|
|
|
|
$m = new TestMessage();
|
|
|
|
$m->setRepeatedString($values);
|
2019-01-22 22:02:44 +00:00
|
|
|
|
2019-01-23 20:44:20 +00:00
|
|
|
// Message elements
|
2019-01-22 22:02:44 +00:00
|
|
|
$m = new TestMessage();
|
2019-01-23 20:44:20 +00:00
|
|
|
$subs = [1, 2];
|
|
|
|
foreach ($subs as &$sub) {
|
|
|
|
$sub = new Sub(['a' => $sub]);
|
|
|
|
}
|
|
|
|
$m->setRepeatedMessage($subs);
|
2020-12-04 21:46:34 +00:00
|
|
|
|
|
|
|
$this->assertTrue(true);
|
2019-01-22 22:02:44 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 18:09:01 +00:00
|
|
|
#########################################################
|
|
|
|
# Test memory leak
|
|
|
|
#########################################################
|
|
|
|
|
2017-07-25 07:49:16 +00:00
|
|
|
public function testCycleLeak()
|
|
|
|
{
|
2021-05-14 06:16:49 +00:00
|
|
|
if (getenv("USE_ZEND_ALLOC") === "0") {
|
|
|
|
// If we are disabling Zend's internal allocator (as we do for
|
|
|
|
// Valgrind tests, for example) then memory_get_usage() will not
|
|
|
|
// return a useful value.
|
|
|
|
$this->markTestSkipped();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-25 07:49:16 +00:00
|
|
|
gc_collect_cycles();
|
|
|
|
$arr = new RepeatedField(GPBType::MESSAGE, TestMessage::class);
|
|
|
|
$arr[] = new TestMessage;
|
|
|
|
$arr[0]->SetRepeatedRecursive($arr);
|
|
|
|
|
|
|
|
// Clean up memory before test.
|
|
|
|
gc_collect_cycles();
|
|
|
|
$start = memory_get_usage();
|
|
|
|
unset($arr);
|
|
|
|
|
|
|
|
// Explicitly trigger garbage collection.
|
|
|
|
gc_collect_cycles();
|
|
|
|
|
|
|
|
$end = memory_get_usage();
|
|
|
|
$this->assertLessThan($start, $end);
|
|
|
|
}
|
2020-09-22 18:44:52 +00:00
|
|
|
|
2021-05-14 23:48:45 +00:00
|
|
|
#########################################################
|
|
|
|
# Test incorrect types
|
|
|
|
#########################################################
|
|
|
|
|
|
|
|
public function testAppendNull()
|
|
|
|
{
|
|
|
|
$this->expectException(TypeError::class);
|
|
|
|
$arr = new RepeatedField(GPBType::MESSAGE, TestMessage::class);
|
|
|
|
$arr[] = null;
|
|
|
|
}
|
|
|
|
|
2020-09-22 18:44:52 +00:00
|
|
|
#########################################################
|
|
|
|
# Test equality
|
|
|
|
#########################################################
|
|
|
|
|
|
|
|
public function testEquality()
|
|
|
|
{
|
|
|
|
$arr = new RepeatedField(GPBType::INT32);
|
|
|
|
$arr2 = new RepeatedField(GPBType::INT32);
|
|
|
|
|
|
|
|
$this->assertTrue($arr == $arr2);
|
|
|
|
|
|
|
|
$arr[] = 0;
|
|
|
|
$arr[] = 1;
|
|
|
|
$arr[] = 2;
|
|
|
|
|
|
|
|
$this->assertFalse($arr == $arr2);
|
|
|
|
|
|
|
|
$arr2[] = 0;
|
|
|
|
$arr2[] = 1;
|
|
|
|
$arr2[] = 2;
|
|
|
|
|
|
|
|
$this->assertTrue($arr == $arr2);
|
|
|
|
|
|
|
|
// Arrays of different types always compare false.
|
|
|
|
$this->assertFalse(new RepeatedField(GPBType::INT32) ==
|
|
|
|
new RepeatedField(GPBType::INT64));
|
|
|
|
$this->assertFalse(
|
|
|
|
new RepeatedField(GPBType::MESSAGE, TestMessage::class) ==
|
|
|
|
new RepeatedField(GPBType::MESSAGE, Sub::class));
|
|
|
|
}
|
2021-02-02 17:15:10 +00:00
|
|
|
|
|
|
|
#########################################################
|
|
|
|
# Test clone
|
|
|
|
#########################################################
|
|
|
|
|
|
|
|
public function testClone()
|
|
|
|
{
|
|
|
|
$arr = new RepeatedField(GPBType::MESSAGE, TestMessage::class);
|
|
|
|
$arr[] = new TestMessage;
|
|
|
|
$arr2 = clone $arr;
|
|
|
|
$this->assertSame($arr[0], $arr2[0]);
|
|
|
|
}
|
2016-09-15 18:09:01 +00:00
|
|
|
}
|