123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593 |
- <?php
- require_once('test_util.php');
- use Google\Protobuf\Internal\RepeatedField;
- use Google\Protobuf\Internal\GPBType;
- use Foo\TestMessage;
- use Foo\TestMessage\Sub;
- class RepeatedFieldTest extends \PHPUnit\Framework\TestCase
- {
- #########################################################
- # Test int32 field.
- #########################################################
- public function testInt32()
- {
- $arr = new RepeatedField(GPBType::INT32);
- // Test append.
- $arr[] = MAX_INT32;
- $this->assertSame(MAX_INT32, $arr[0]);
- $arr[] = MIN_INT32;
- $this->assertSame(MIN_INT32, $arr[1]);
- $arr[] = 1.1;
- $this->assertSame(1, $arr[2]);
- $arr[] = MAX_INT32_FLOAT;
- $this->assertSame(MAX_INT32, $arr[3]);
- $arr[] = MAX_INT32_FLOAT;
- $this->assertSame(MAX_INT32, $arr[4]);
- $arr[] = '2';
- $this->assertSame(2, $arr[5]);
- $arr[] = '3.1';
- $this->assertSame(3, $arr[6]);
- $arr[] = MAX_INT32_STRING;
- $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.
- $arr[0] = MAX_INT32;
- $this->assertSame(MAX_INT32, $arr[0]);
- $arr[1] = MIN_INT32;
- $this->assertSame(MIN_INT32, $arr[1]);
- $arr[2] = 1.1;
- $this->assertSame(1, $arr[2]);
- $arr[3] = MAX_INT32_FLOAT;
- $this->assertSame(MAX_INT32, $arr[3]);
- $arr[4] = MAX_INT32_FLOAT;
- $this->assertSame(MAX_INT32, $arr[4]);
- $arr[5] = '2';
- $this->assertSame(2, $arr[5]);
- $arr[6] = '3.1';
- $this->assertSame(3, $arr[6]);
- $arr[7] = MAX_INT32_STRING;
- $this->assertSame(MAX_INT32, $arr[7]);
- // Test foreach.
- $arr = new RepeatedField(GPBType::INT32);
- for ($i = 0; $i < 3; $i++) {
- $arr[] = $i;
- }
- $i = 0;
- foreach ($arr as $val) {
- $this->assertSame($i++, $val);
- }
- $this->assertSame(3, $i);
- }
- #########################################################
- # Test uint32 field.
- #########################################################
- public function testUint32()
- {
- $arr = new RepeatedField(GPBType::UINT32);
- // Test append.
- $arr[] = MAX_UINT32;
- $this->assertSame(-1, $arr[0]);
- $arr[] = -1;
- $this->assertSame(-1, $arr[1]);
- $arr[] = MIN_UINT32;
- $this->assertSame(MIN_UINT32, $arr[2]);
- $arr[] = 1.1;
- $this->assertSame(1, $arr[3]);
- $arr[] = MAX_UINT32_FLOAT;
- $this->assertSame(-1, $arr[4]);
- $arr[] = -1.0;
- $this->assertSame(-1, $arr[5]);
- $arr[] = MIN_UINT32_FLOAT;
- $this->assertSame(MIN_UINT32, $arr[6]);
- $arr[] = '2';
- $this->assertSame(2, $arr[7]);
- $arr[] = '3.1';
- $this->assertSame(3, $arr[8]);
- $arr[] = MAX_UINT32_STRING;
- $this->assertSame(-1, $arr[9]);
- $arr[] = '-1.0';
- $this->assertSame(-1, $arr[10]);
- $arr[] = MIN_UINT32_STRING;
- $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.
- $arr[0] = MAX_UINT32;
- $this->assertSame(-1, $arr[0]);
- $arr[1] = -1;
- $this->assertSame(-1, $arr[1]);
- $arr[2] = MIN_UINT32;
- $this->assertSame(MIN_UINT32, $arr[2]);
- $arr[3] = 1.1;
- $this->assertSame(1, $arr[3]);
- $arr[4] = MAX_UINT32_FLOAT;
- $this->assertSame(-1, $arr[4]);
- $arr[5] = -1.0;
- $this->assertSame(-1, $arr[5]);
- $arr[6] = MIN_UINT32_FLOAT;
- $this->assertSame(MIN_UINT32, $arr[6]);
- $arr[7] = '2';
- $this->assertSame(2, $arr[7]);
- $arr[8] = '3.1';
- $this->assertSame(3, $arr[8]);
- $arr[9] = MAX_UINT32_STRING;
- $this->assertSame(-1, $arr[9]);
- $arr[10] = '-1.0';
- $this->assertSame(-1, $arr[10]);
- $arr[11] = MIN_UINT32_STRING;
- $this->assertSame(MIN_UINT32, $arr[11]);
- }
- #########################################################
- # Test int64 field.
- #########################################################
- public function testInt64()
- {
- $arr = new RepeatedField(GPBType::INT64);
- // Test append.
- $arr[] = MAX_INT64;
- $arr[] = MIN_INT64;
- $arr[] = 1.1;
- $arr[] = '2';
- $arr[] = '3.1';
- $arr[] = MAX_INT64_STRING;
- $arr[] = MIN_INT64_STRING;
- 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]);
- }
- $this->assertEquals(7, count($arr));
- for ($i = 0; $i < count($arr); $i++) {
- $arr[$i] = 0;
- if (PHP_INT_SIZE == 4) {
- $this->assertSame('0', $arr[$i]);
- } else {
- $this->assertSame(0, $arr[$i]);
- }
- }
- // Test set.
- $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;
- 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]);
- }
- }
- #########################################################
- # Test uint64 field.
- #########################################################
- public function testUint64()
- {
- $arr = new RepeatedField(GPBType::UINT64);
- // Test append.
- $arr[] = MAX_UINT64;
- $arr[] = 1.1;
- $arr[] = '2';
- $arr[] = '3.1';
- $arr[] = MAX_UINT64_STRING;
- 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));
- for ($i = 0; $i < count($arr); $i++) {
- $arr[$i] = 0;
- if (PHP_INT_SIZE == 4) {
- $this->assertSame('0', $arr[$i]);
- } else {
- $this->assertSame(0, $arr[$i]);
- }
- }
- // Test set.
- $arr[0] = MAX_UINT64;
- $arr[1] = 1.1;
- $arr[2] = '2';
- $arr[3] = '3.1';
- $arr[4] = MAX_UINT64_STRING;
- 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]);
- }
- }
- #########################################################
- # Test float field.
- #########################################################
- public function testFloat()
- {
- $arr = new RepeatedField(GPBType::FLOAT);
- // Test append.
- $arr[] = 1;
- $this->assertEquals(1.0, $arr[0], '', MAX_FLOAT_DIFF);
- $arr[] = 1.1;
- $this->assertEquals(1.1, $arr[1], '', MAX_FLOAT_DIFF);
- $arr[] = '2';
- $this->assertEquals(2.0, $arr[2], '', MAX_FLOAT_DIFF);
- $arr[] = '3.1';
- $this->assertEquals(3.1, $arr[3], '', MAX_FLOAT_DIFF);
- $this->assertEquals(4, count($arr));
- for ($i = 0; $i < count($arr); $i++) {
- $arr[$i] = 0;
- $this->assertSame(0.0, $arr[$i]);
- }
- // Test set.
- $arr[0] = 1;
- $this->assertEquals(1.0, $arr[0], '', MAX_FLOAT_DIFF);
- $arr[1] = 1.1;
- $this->assertEquals(1.1, $arr[1], '', MAX_FLOAT_DIFF);
- $arr[2] = '2';
- $this->assertEquals(2.0, $arr[2], '', MAX_FLOAT_DIFF);
- $arr[3] = '3.1';
- $this->assertEquals(3.1, $arr[3], '', MAX_FLOAT_DIFF);
- }
- #########################################################
- # Test double field.
- #########################################################
- public function testDouble()
- {
- $arr = new RepeatedField(GPBType::DOUBLE);
- // Test append.
- $arr[] = 1;
- $this->assertEquals(1.0, $arr[0], '', MAX_FLOAT_DIFF);
- $arr[] = 1.1;
- $this->assertEquals(1.1, $arr[1], '', MAX_FLOAT_DIFF);
- $arr[] = '2';
- $this->assertEquals(2.0, $arr[2], '', MAX_FLOAT_DIFF);
- $arr[] = '3.1';
- $this->assertEquals(3.1, $arr[3], '', MAX_FLOAT_DIFF);
- $this->assertEquals(4, count($arr));
- for ($i = 0; $i < count($arr); $i++) {
- $arr[$i] = 0;
- $this->assertSame(0.0, $arr[$i]);
- }
- // Test set.
- $arr[0] = 1;
- $this->assertEquals(1.0, $arr[0], '', MAX_FLOAT_DIFF);
- $arr[1] = 1.1;
- $this->assertEquals(1.1, $arr[1], '', MAX_FLOAT_DIFF);
- $arr[2] = '2';
- $this->assertEquals(2.0, $arr[2], '', MAX_FLOAT_DIFF);
- $arr[3] = '3.1';
- $this->assertEquals(3.1, $arr[3], '', MAX_FLOAT_DIFF);
- }
- #########################################################
- # Test bool field.
- #########################################################
- public function testBool()
- {
- $arr = new RepeatedField(GPBType::BOOL);
- // Test append.
- $arr[] = true;
- $this->assertSame(true, $arr[0]);
- $arr[] = -1;
- $this->assertSame(true, $arr[1]);
- $arr[] = 1.1;
- $this->assertSame(true, $arr[2]);
- $arr[] = '';
- $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.
- $arr[0] = true;
- $this->assertSame(true, $arr[0]);
- $arr[1] = -1;
- $this->assertSame(true, $arr[1]);
- $arr[2] = 1.1;
- $this->assertSame(true, $arr[2]);
- $arr[3] = '';
- $this->assertSame(false, $arr[3]);
- }
- #########################################################
- # Test string field.
- #########################################################
- public function testString()
- {
- $arr = new RepeatedField(GPBType::STRING);
- // Test append.
- $arr[] = 'abc';
- $this->assertSame('abc', $arr[0]);
- $arr[] = 1;
- $this->assertSame('1', $arr[1]);
- $arr[] = 1.1;
- $this->assertSame('1.1', $arr[2]);
- $arr[] = true;
- $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.
- $arr[0] = 'abc';
- $this->assertSame('abc', $arr[0]);
- $arr[1] = 1;
- $this->assertSame('1', $arr[1]);
- $arr[2] = 1.1;
- $this->assertSame('1.1', $arr[2]);
- $arr[3] = true;
- $this->assertSame('1', $arr[3]);
- }
- #########################################################
- # Test message field.
- #########################################################
- public function testMessage()
- {
- $arr = new RepeatedField(GPBType::MESSAGE, Sub::class);
- // Test append.
- $sub_m = new Sub();
- $sub_m->setA(1);
- $arr[] = $sub_m;
- $this->assertSame(1, $arr[0]->getA());
- $this->assertEquals(1, count($arr));
- // Test set.
- $sub_m = new Sub();
- $sub_m->setA(2);
- $arr[0] = $sub_m;
- $this->assertSame(2, $arr[0]->getA());
- // Test foreach.
- $arr = new RepeatedField(GPBType::MESSAGE, Sub::class);
- for ($i = 0; $i < 3; $i++) {
- $arr[] = new Sub();
- $arr[$i]->setA($i);
- }
- $i = 0;
- foreach ($arr as $val) {
- $this->assertSame($i++, $val->getA());
- }
- $this->assertSame(3, $i);
- }
- #########################################################
- # Test offset type
- #########################################################
- public function testOffset()
- {
- $arr = new RepeatedField(GPBType::INT32);
- $arr[] = 0;
- $arr[0] = 1;
- $this->assertSame(1, $arr[0]);
- $this->assertSame(1, count($arr));
- $arr['0'] = 2;
- $this->assertSame(2, $arr['0']);
- $this->assertSame(2, $arr[0]);
- $this->assertSame(1, count($arr));
- $arr[0.0] = 3;
- $this->assertSame(3, $arr[0.0]);
- $this->assertSame(1, count($arr));
- }
- public function testInsertRemoval()
- {
- $arr = new RepeatedField(GPBType::INT32);
- $arr[] = 0;
- $arr[] = 1;
- $arr[] = 2;
- $this->assertSame(3, count($arr));
- unset($arr[2]);
- $this->assertSame(2, count($arr));
- $this->assertSame(0, $arr[0]);
- $this->assertSame(1, $arr[1]);
- $arr[] = 3;
- $this->assertSame(3, count($arr));
- $this->assertSame(0, $arr[0]);
- $this->assertSame(1, $arr[1]);
- $this->assertSame(3, $arr[2]);
- }
- #########################################################
- # Test reference in array
- #########################################################
- public function testArrayElementIsReferenceInSetters()
- {
- // Bool elements
- $values = [true];
- array_walk($values, function (&$value) {});
- $m = new TestMessage();
- $m->setRepeatedBool($values);
- // Int32 elements
- $values = [1];
- array_walk($values, function (&$value) {});
- $m = new TestMessage();
- $m->setRepeatedInt32($values);
- // Double elements
- $values = [1.0];
- array_walk($values, function (&$value) {});
- $m = new TestMessage();
- $m->setRepeatedDouble($values);
- // String elements
- $values = ['a'];
- array_walk($values, function (&$value) {});
- $m = new TestMessage();
- $m->setRepeatedString($values);
- // Message elements
- $m = new TestMessage();
- $subs = [1, 2];
- foreach ($subs as &$sub) {
- $sub = new Sub(['a' => $sub]);
- }
- $m->setRepeatedMessage($subs);
- }
- #########################################################
- # Test memory leak
- #########################################################
- public function testCycleLeak()
- {
- 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);
- }
- }
|