1ffe3c632Sopenharmony_ci<?php 2ffe3c632Sopenharmony_ci 3ffe3c632Sopenharmony_cirequire_once('test_util.php'); 4ffe3c632Sopenharmony_ci 5ffe3c632Sopenharmony_ciuse Google\Protobuf\Internal\RepeatedField; 6ffe3c632Sopenharmony_ciuse Google\Protobuf\Internal\GPBType; 7ffe3c632Sopenharmony_ciuse Foo\TestMessage; 8ffe3c632Sopenharmony_ciuse Foo\TestMessage\Sub; 9ffe3c632Sopenharmony_ci 10ffe3c632Sopenharmony_ciclass ArrayTest extends \PHPUnit\Framework\TestCase 11ffe3c632Sopenharmony_ci{ 12ffe3c632Sopenharmony_ci 13ffe3c632Sopenharmony_ci ######################################################### 14ffe3c632Sopenharmony_ci # Test int32 field. 15ffe3c632Sopenharmony_ci ######################################################### 16ffe3c632Sopenharmony_ci 17ffe3c632Sopenharmony_ci public function testInt32() 18ffe3c632Sopenharmony_ci { 19ffe3c632Sopenharmony_ci $arr = new RepeatedField(GPBType::INT32); 20ffe3c632Sopenharmony_ci 21ffe3c632Sopenharmony_ci // Test append. 22ffe3c632Sopenharmony_ci $arr[] = MAX_INT32; 23ffe3c632Sopenharmony_ci $this->assertSame(MAX_INT32, $arr[0]); 24ffe3c632Sopenharmony_ci $arr[] = MIN_INT32; 25ffe3c632Sopenharmony_ci $this->assertSame(MIN_INT32, $arr[1]); 26ffe3c632Sopenharmony_ci 27ffe3c632Sopenharmony_ci $arr[] = 1.1; 28ffe3c632Sopenharmony_ci $this->assertSame(1, $arr[2]); 29ffe3c632Sopenharmony_ci $arr[] = MAX_INT32_FLOAT; 30ffe3c632Sopenharmony_ci $this->assertSame(MAX_INT32, $arr[3]); 31ffe3c632Sopenharmony_ci $arr[] = MAX_INT32_FLOAT; 32ffe3c632Sopenharmony_ci $this->assertSame(MAX_INT32, $arr[4]); 33ffe3c632Sopenharmony_ci 34ffe3c632Sopenharmony_ci $arr[] = '2'; 35ffe3c632Sopenharmony_ci $this->assertSame(2, $arr[5]); 36ffe3c632Sopenharmony_ci $arr[] = '3.1'; 37ffe3c632Sopenharmony_ci $this->assertSame(3, $arr[6]); 38ffe3c632Sopenharmony_ci $arr[] = MAX_INT32_STRING; 39ffe3c632Sopenharmony_ci $this->assertSame(MAX_INT32, $arr[7]); 40ffe3c632Sopenharmony_ci 41ffe3c632Sopenharmony_ci $this->assertEquals(8, count($arr)); 42ffe3c632Sopenharmony_ci 43ffe3c632Sopenharmony_ci for ($i = 0; $i < count($arr); $i++) { 44ffe3c632Sopenharmony_ci $arr[$i] = 0; 45ffe3c632Sopenharmony_ci $this->assertSame(0, $arr[$i]); 46ffe3c632Sopenharmony_ci } 47ffe3c632Sopenharmony_ci 48ffe3c632Sopenharmony_ci // Test set. 49ffe3c632Sopenharmony_ci $arr[0] = MAX_INT32; 50ffe3c632Sopenharmony_ci $this->assertSame(MAX_INT32, $arr[0]); 51ffe3c632Sopenharmony_ci $arr[1] = MIN_INT32; 52ffe3c632Sopenharmony_ci $this->assertSame(MIN_INT32, $arr[1]); 53ffe3c632Sopenharmony_ci 54ffe3c632Sopenharmony_ci $arr[2] = 1.1; 55ffe3c632Sopenharmony_ci $this->assertSame(1, $arr[2]); 56ffe3c632Sopenharmony_ci $arr[3] = MAX_INT32_FLOAT; 57ffe3c632Sopenharmony_ci $this->assertSame(MAX_INT32, $arr[3]); 58ffe3c632Sopenharmony_ci $arr[4] = MAX_INT32_FLOAT; 59ffe3c632Sopenharmony_ci $this->assertSame(MAX_INT32, $arr[4]); 60ffe3c632Sopenharmony_ci 61ffe3c632Sopenharmony_ci $arr[5] = '2'; 62ffe3c632Sopenharmony_ci $this->assertSame(2, $arr[5]); 63ffe3c632Sopenharmony_ci $arr[6] = '3.1'; 64ffe3c632Sopenharmony_ci $this->assertSame(3, $arr[6]); 65ffe3c632Sopenharmony_ci $arr[7] = MAX_INT32_STRING; 66ffe3c632Sopenharmony_ci $this->assertSame(MAX_INT32, $arr[7]); 67ffe3c632Sopenharmony_ci 68ffe3c632Sopenharmony_ci // Test foreach. 69ffe3c632Sopenharmony_ci $arr = new RepeatedField(GPBType::INT32); 70ffe3c632Sopenharmony_ci for ($i = 0; $i < 3; $i++) { 71ffe3c632Sopenharmony_ci $arr[] = $i; 72ffe3c632Sopenharmony_ci } 73ffe3c632Sopenharmony_ci $i = 0; 74ffe3c632Sopenharmony_ci foreach ($arr as $val) { 75ffe3c632Sopenharmony_ci $this->assertSame($i++, $val); 76ffe3c632Sopenharmony_ci } 77ffe3c632Sopenharmony_ci $this->assertSame(3, $i); 78ffe3c632Sopenharmony_ci } 79ffe3c632Sopenharmony_ci 80ffe3c632Sopenharmony_ci ######################################################### 81ffe3c632Sopenharmony_ci # Test uint32 field. 82ffe3c632Sopenharmony_ci ######################################################### 83ffe3c632Sopenharmony_ci 84ffe3c632Sopenharmony_ci public function testUint32() 85ffe3c632Sopenharmony_ci { 86ffe3c632Sopenharmony_ci $arr = new RepeatedField(GPBType::UINT32); 87ffe3c632Sopenharmony_ci 88ffe3c632Sopenharmony_ci // Test append. 89ffe3c632Sopenharmony_ci $arr[] = MAX_UINT32; 90ffe3c632Sopenharmony_ci $this->assertSame(-1, $arr[0]); 91ffe3c632Sopenharmony_ci $arr[] = -1; 92ffe3c632Sopenharmony_ci $this->assertSame(-1, $arr[1]); 93ffe3c632Sopenharmony_ci $arr[] = MIN_UINT32; 94ffe3c632Sopenharmony_ci $this->assertSame(MIN_UINT32, $arr[2]); 95ffe3c632Sopenharmony_ci 96ffe3c632Sopenharmony_ci $arr[] = 1.1; 97ffe3c632Sopenharmony_ci $this->assertSame(1, $arr[3]); 98ffe3c632Sopenharmony_ci $arr[] = MAX_UINT32_FLOAT; 99ffe3c632Sopenharmony_ci $this->assertSame(-1, $arr[4]); 100ffe3c632Sopenharmony_ci $arr[] = -1.0; 101ffe3c632Sopenharmony_ci $this->assertSame(-1, $arr[5]); 102ffe3c632Sopenharmony_ci $arr[] = MIN_UINT32_FLOAT; 103ffe3c632Sopenharmony_ci $this->assertSame(MIN_UINT32, $arr[6]); 104ffe3c632Sopenharmony_ci 105ffe3c632Sopenharmony_ci $arr[] = '2'; 106ffe3c632Sopenharmony_ci $this->assertSame(2, $arr[7]); 107ffe3c632Sopenharmony_ci $arr[] = '3.1'; 108ffe3c632Sopenharmony_ci $this->assertSame(3, $arr[8]); 109ffe3c632Sopenharmony_ci $arr[] = MAX_UINT32_STRING; 110ffe3c632Sopenharmony_ci $this->assertSame(-1, $arr[9]); 111ffe3c632Sopenharmony_ci $arr[] = '-1.0'; 112ffe3c632Sopenharmony_ci $this->assertSame(-1, $arr[10]); 113ffe3c632Sopenharmony_ci $arr[] = MIN_UINT32_STRING; 114ffe3c632Sopenharmony_ci $this->assertSame(MIN_UINT32, $arr[11]); 115ffe3c632Sopenharmony_ci 116ffe3c632Sopenharmony_ci $this->assertEquals(12, count($arr)); 117ffe3c632Sopenharmony_ci 118ffe3c632Sopenharmony_ci for ($i = 0; $i < count($arr); $i++) { 119ffe3c632Sopenharmony_ci $arr[$i] = 0; 120ffe3c632Sopenharmony_ci $this->assertSame(0, $arr[$i]); 121ffe3c632Sopenharmony_ci } 122ffe3c632Sopenharmony_ci 123ffe3c632Sopenharmony_ci // Test set. 124ffe3c632Sopenharmony_ci $arr[0] = MAX_UINT32; 125ffe3c632Sopenharmony_ci $this->assertSame(-1, $arr[0]); 126ffe3c632Sopenharmony_ci $arr[1] = -1; 127ffe3c632Sopenharmony_ci $this->assertSame(-1, $arr[1]); 128ffe3c632Sopenharmony_ci $arr[2] = MIN_UINT32; 129ffe3c632Sopenharmony_ci $this->assertSame(MIN_UINT32, $arr[2]); 130ffe3c632Sopenharmony_ci 131ffe3c632Sopenharmony_ci $arr[3] = 1.1; 132ffe3c632Sopenharmony_ci $this->assertSame(1, $arr[3]); 133ffe3c632Sopenharmony_ci $arr[4] = MAX_UINT32_FLOAT; 134ffe3c632Sopenharmony_ci $this->assertSame(-1, $arr[4]); 135ffe3c632Sopenharmony_ci $arr[5] = -1.0; 136ffe3c632Sopenharmony_ci $this->assertSame(-1, $arr[5]); 137ffe3c632Sopenharmony_ci $arr[6] = MIN_UINT32_FLOAT; 138ffe3c632Sopenharmony_ci $this->assertSame(MIN_UINT32, $arr[6]); 139ffe3c632Sopenharmony_ci 140ffe3c632Sopenharmony_ci $arr[7] = '2'; 141ffe3c632Sopenharmony_ci $this->assertSame(2, $arr[7]); 142ffe3c632Sopenharmony_ci $arr[8] = '3.1'; 143ffe3c632Sopenharmony_ci $this->assertSame(3, $arr[8]); 144ffe3c632Sopenharmony_ci $arr[9] = MAX_UINT32_STRING; 145ffe3c632Sopenharmony_ci $this->assertSame(-1, $arr[9]); 146ffe3c632Sopenharmony_ci $arr[10] = '-1.0'; 147ffe3c632Sopenharmony_ci $this->assertSame(-1, $arr[10]); 148ffe3c632Sopenharmony_ci $arr[11] = MIN_UINT32_STRING; 149ffe3c632Sopenharmony_ci $this->assertSame(MIN_UINT32, $arr[11]); 150ffe3c632Sopenharmony_ci } 151ffe3c632Sopenharmony_ci 152ffe3c632Sopenharmony_ci ######################################################### 153ffe3c632Sopenharmony_ci # Test int64 field. 154ffe3c632Sopenharmony_ci ######################################################### 155ffe3c632Sopenharmony_ci 156ffe3c632Sopenharmony_ci public function testInt64() 157ffe3c632Sopenharmony_ci { 158ffe3c632Sopenharmony_ci $arr = new RepeatedField(GPBType::INT64); 159ffe3c632Sopenharmony_ci 160ffe3c632Sopenharmony_ci // Test append. 161ffe3c632Sopenharmony_ci $arr[] = MAX_INT64; 162ffe3c632Sopenharmony_ci $arr[] = MIN_INT64; 163ffe3c632Sopenharmony_ci $arr[] = 1.1; 164ffe3c632Sopenharmony_ci $arr[] = '2'; 165ffe3c632Sopenharmony_ci $arr[] = '3.1'; 166ffe3c632Sopenharmony_ci $arr[] = MAX_INT64_STRING; 167ffe3c632Sopenharmony_ci $arr[] = MIN_INT64_STRING; 168ffe3c632Sopenharmony_ci if (PHP_INT_SIZE == 4) { 169ffe3c632Sopenharmony_ci $this->assertSame(MAX_INT64, $arr[0]); 170ffe3c632Sopenharmony_ci $this->assertSame(MIN_INT64, $arr[1]); 171ffe3c632Sopenharmony_ci $this->assertSame('1', $arr[2]); 172ffe3c632Sopenharmony_ci $this->assertSame('2', $arr[3]); 173ffe3c632Sopenharmony_ci $this->assertSame('3', $arr[4]); 174ffe3c632Sopenharmony_ci $this->assertSame(MAX_INT64_STRING, $arr[5]); 175ffe3c632Sopenharmony_ci $this->assertSame(MIN_INT64_STRING, $arr[6]); 176ffe3c632Sopenharmony_ci } else { 177ffe3c632Sopenharmony_ci $this->assertSame(MAX_INT64, $arr[0]); 178ffe3c632Sopenharmony_ci $this->assertSame(MIN_INT64, $arr[1]); 179ffe3c632Sopenharmony_ci $this->assertSame(1, $arr[2]); 180ffe3c632Sopenharmony_ci $this->assertSame(2, $arr[3]); 181ffe3c632Sopenharmony_ci $this->assertSame(3, $arr[4]); 182ffe3c632Sopenharmony_ci $this->assertSame(MAX_INT64, $arr[5]); 183ffe3c632Sopenharmony_ci $this->assertSame(MIN_INT64, $arr[6]); 184ffe3c632Sopenharmony_ci } 185ffe3c632Sopenharmony_ci 186ffe3c632Sopenharmony_ci 187ffe3c632Sopenharmony_ci $this->assertEquals(7, count($arr)); 188ffe3c632Sopenharmony_ci 189ffe3c632Sopenharmony_ci for ($i = 0; $i < count($arr); $i++) { 190ffe3c632Sopenharmony_ci $arr[$i] = 0; 191ffe3c632Sopenharmony_ci if (PHP_INT_SIZE == 4) { 192ffe3c632Sopenharmony_ci $this->assertSame('0', $arr[$i]); 193ffe3c632Sopenharmony_ci } else { 194ffe3c632Sopenharmony_ci $this->assertSame(0, $arr[$i]); 195ffe3c632Sopenharmony_ci } 196ffe3c632Sopenharmony_ci } 197ffe3c632Sopenharmony_ci 198ffe3c632Sopenharmony_ci // Test set. 199ffe3c632Sopenharmony_ci $arr[0] = MAX_INT64; 200ffe3c632Sopenharmony_ci $arr[1] = MIN_INT64; 201ffe3c632Sopenharmony_ci $arr[2] = 1.1; 202ffe3c632Sopenharmony_ci $arr[3] = '2'; 203ffe3c632Sopenharmony_ci $arr[4] = '3.1'; 204ffe3c632Sopenharmony_ci $arr[5] = MAX_INT64_STRING; 205ffe3c632Sopenharmony_ci $arr[6] = MIN_INT64_STRING; 206ffe3c632Sopenharmony_ci 207ffe3c632Sopenharmony_ci if (PHP_INT_SIZE == 4) { 208ffe3c632Sopenharmony_ci $this->assertSame(MAX_INT64_STRING, $arr[0]); 209ffe3c632Sopenharmony_ci $this->assertSame(MIN_INT64_STRING, $arr[1]); 210ffe3c632Sopenharmony_ci $this->assertSame('1', $arr[2]); 211ffe3c632Sopenharmony_ci $this->assertSame('2', $arr[3]); 212ffe3c632Sopenharmony_ci $this->assertSame('3', $arr[4]); 213ffe3c632Sopenharmony_ci $this->assertSame(MAX_INT64_STRING, $arr[5]); 214ffe3c632Sopenharmony_ci $this->assertEquals(MIN_INT64_STRING, $arr[6]); 215ffe3c632Sopenharmony_ci } else { 216ffe3c632Sopenharmony_ci $this->assertSame(MAX_INT64, $arr[0]); 217ffe3c632Sopenharmony_ci $this->assertSame(MIN_INT64, $arr[1]); 218ffe3c632Sopenharmony_ci $this->assertSame(1, $arr[2]); 219ffe3c632Sopenharmony_ci $this->assertSame(2, $arr[3]); 220ffe3c632Sopenharmony_ci $this->assertSame(3, $arr[4]); 221ffe3c632Sopenharmony_ci $this->assertSame(MAX_INT64, $arr[5]); 222ffe3c632Sopenharmony_ci $this->assertEquals(MIN_INT64, $arr[6]); 223ffe3c632Sopenharmony_ci } 224ffe3c632Sopenharmony_ci } 225ffe3c632Sopenharmony_ci 226ffe3c632Sopenharmony_ci ######################################################### 227ffe3c632Sopenharmony_ci # Test uint64 field. 228ffe3c632Sopenharmony_ci ######################################################### 229ffe3c632Sopenharmony_ci 230ffe3c632Sopenharmony_ci public function testUint64() 231ffe3c632Sopenharmony_ci { 232ffe3c632Sopenharmony_ci $arr = new RepeatedField(GPBType::UINT64); 233ffe3c632Sopenharmony_ci 234ffe3c632Sopenharmony_ci // Test append. 235ffe3c632Sopenharmony_ci $arr[] = MAX_UINT64; 236ffe3c632Sopenharmony_ci $arr[] = 1.1; 237ffe3c632Sopenharmony_ci $arr[] = '2'; 238ffe3c632Sopenharmony_ci $arr[] = '3.1'; 239ffe3c632Sopenharmony_ci $arr[] = MAX_UINT64_STRING; 240ffe3c632Sopenharmony_ci 241ffe3c632Sopenharmony_ci if (PHP_INT_SIZE == 4) { 242ffe3c632Sopenharmony_ci $this->assertSame(MAX_UINT64_STRING, $arr[0]); 243ffe3c632Sopenharmony_ci $this->assertSame('1', $arr[1]); 244ffe3c632Sopenharmony_ci $this->assertSame('2', $arr[2]); 245ffe3c632Sopenharmony_ci $this->assertSame('3', $arr[3]); 246ffe3c632Sopenharmony_ci $this->assertSame(MAX_UINT64_STRING, $arr[4]); 247ffe3c632Sopenharmony_ci } else { 248ffe3c632Sopenharmony_ci $this->assertSame(MAX_UINT64, $arr[0]); 249ffe3c632Sopenharmony_ci $this->assertSame(1, $arr[1]); 250ffe3c632Sopenharmony_ci $this->assertSame(2, $arr[2]); 251ffe3c632Sopenharmony_ci $this->assertSame(3, $arr[3]); 252ffe3c632Sopenharmony_ci $this->assertSame(MAX_UINT64, $arr[4]); 253ffe3c632Sopenharmony_ci $this->assertSame(5, count($arr)); 254ffe3c632Sopenharmony_ci } 255ffe3c632Sopenharmony_ci 256ffe3c632Sopenharmony_ci $this->assertSame(5, count($arr)); 257ffe3c632Sopenharmony_ci 258ffe3c632Sopenharmony_ci for ($i = 0; $i < count($arr); $i++) { 259ffe3c632Sopenharmony_ci $arr[$i] = 0; 260ffe3c632Sopenharmony_ci if (PHP_INT_SIZE == 4) { 261ffe3c632Sopenharmony_ci $this->assertSame('0', $arr[$i]); 262ffe3c632Sopenharmony_ci } else { 263ffe3c632Sopenharmony_ci $this->assertSame(0, $arr[$i]); 264ffe3c632Sopenharmony_ci } 265ffe3c632Sopenharmony_ci } 266ffe3c632Sopenharmony_ci 267ffe3c632Sopenharmony_ci // Test set. 268ffe3c632Sopenharmony_ci $arr[0] = MAX_UINT64; 269ffe3c632Sopenharmony_ci $arr[1] = 1.1; 270ffe3c632Sopenharmony_ci $arr[2] = '2'; 271ffe3c632Sopenharmony_ci $arr[3] = '3.1'; 272ffe3c632Sopenharmony_ci $arr[4] = MAX_UINT64_STRING; 273ffe3c632Sopenharmony_ci 274ffe3c632Sopenharmony_ci if (PHP_INT_SIZE == 4) { 275ffe3c632Sopenharmony_ci $this->assertSame(MAX_UINT64_STRING, $arr[0]); 276ffe3c632Sopenharmony_ci $this->assertSame('1', $arr[1]); 277ffe3c632Sopenharmony_ci $this->assertSame('2', $arr[2]); 278ffe3c632Sopenharmony_ci $this->assertSame('3', $arr[3]); 279ffe3c632Sopenharmony_ci $this->assertSame(MAX_UINT64_STRING, $arr[4]); 280ffe3c632Sopenharmony_ci } else { 281ffe3c632Sopenharmony_ci $this->assertSame(MAX_UINT64, $arr[0]); 282ffe3c632Sopenharmony_ci $this->assertSame(1, $arr[1]); 283ffe3c632Sopenharmony_ci $this->assertSame(2, $arr[2]); 284ffe3c632Sopenharmony_ci $this->assertSame(3, $arr[3]); 285ffe3c632Sopenharmony_ci $this->assertSame(MAX_UINT64, $arr[4]); 286ffe3c632Sopenharmony_ci } 287ffe3c632Sopenharmony_ci } 288ffe3c632Sopenharmony_ci 289ffe3c632Sopenharmony_ci ######################################################### 290ffe3c632Sopenharmony_ci # Test float field. 291ffe3c632Sopenharmony_ci ######################################################### 292ffe3c632Sopenharmony_ci 293ffe3c632Sopenharmony_ci public function testFloat() 294ffe3c632Sopenharmony_ci { 295ffe3c632Sopenharmony_ci $arr = new RepeatedField(GPBType::FLOAT); 296ffe3c632Sopenharmony_ci 297ffe3c632Sopenharmony_ci // Test append. 298ffe3c632Sopenharmony_ci $arr[] = 1; 299ffe3c632Sopenharmony_ci $this->assertEquals(1.0, $arr[0], '', MAX_FLOAT_DIFF); 300ffe3c632Sopenharmony_ci 301ffe3c632Sopenharmony_ci $arr[] = 1.1; 302ffe3c632Sopenharmony_ci $this->assertEquals(1.1, $arr[1], '', MAX_FLOAT_DIFF); 303ffe3c632Sopenharmony_ci 304ffe3c632Sopenharmony_ci $arr[] = '2'; 305ffe3c632Sopenharmony_ci $this->assertEquals(2.0, $arr[2], '', MAX_FLOAT_DIFF); 306ffe3c632Sopenharmony_ci $arr[] = '3.1'; 307ffe3c632Sopenharmony_ci $this->assertEquals(3.1, $arr[3], '', MAX_FLOAT_DIFF); 308ffe3c632Sopenharmony_ci 309ffe3c632Sopenharmony_ci $this->assertEquals(4, count($arr)); 310ffe3c632Sopenharmony_ci 311ffe3c632Sopenharmony_ci for ($i = 0; $i < count($arr); $i++) { 312ffe3c632Sopenharmony_ci $arr[$i] = 0; 313ffe3c632Sopenharmony_ci $this->assertSame(0.0, $arr[$i]); 314ffe3c632Sopenharmony_ci } 315ffe3c632Sopenharmony_ci 316ffe3c632Sopenharmony_ci // Test set. 317ffe3c632Sopenharmony_ci $arr[0] = 1; 318ffe3c632Sopenharmony_ci $this->assertEquals(1.0, $arr[0], '', MAX_FLOAT_DIFF); 319ffe3c632Sopenharmony_ci 320ffe3c632Sopenharmony_ci $arr[1] = 1.1; 321ffe3c632Sopenharmony_ci $this->assertEquals(1.1, $arr[1], '', MAX_FLOAT_DIFF); 322ffe3c632Sopenharmony_ci 323ffe3c632Sopenharmony_ci $arr[2] = '2'; 324ffe3c632Sopenharmony_ci $this->assertEquals(2.0, $arr[2], '', MAX_FLOAT_DIFF); 325ffe3c632Sopenharmony_ci $arr[3] = '3.1'; 326ffe3c632Sopenharmony_ci $this->assertEquals(3.1, $arr[3], '', MAX_FLOAT_DIFF); 327ffe3c632Sopenharmony_ci } 328ffe3c632Sopenharmony_ci 329ffe3c632Sopenharmony_ci ######################################################### 330ffe3c632Sopenharmony_ci # Test double field. 331ffe3c632Sopenharmony_ci ######################################################### 332ffe3c632Sopenharmony_ci 333ffe3c632Sopenharmony_ci public function testDouble() 334ffe3c632Sopenharmony_ci { 335ffe3c632Sopenharmony_ci $arr = new RepeatedField(GPBType::DOUBLE); 336ffe3c632Sopenharmony_ci 337ffe3c632Sopenharmony_ci // Test append. 338ffe3c632Sopenharmony_ci $arr[] = 1; 339ffe3c632Sopenharmony_ci $this->assertEquals(1.0, $arr[0], '', MAX_FLOAT_DIFF); 340ffe3c632Sopenharmony_ci 341ffe3c632Sopenharmony_ci $arr[] = 1.1; 342ffe3c632Sopenharmony_ci $this->assertEquals(1.1, $arr[1], '', MAX_FLOAT_DIFF); 343ffe3c632Sopenharmony_ci 344ffe3c632Sopenharmony_ci $arr[] = '2'; 345ffe3c632Sopenharmony_ci $this->assertEquals(2.0, $arr[2], '', MAX_FLOAT_DIFF); 346ffe3c632Sopenharmony_ci $arr[] = '3.1'; 347ffe3c632Sopenharmony_ci $this->assertEquals(3.1, $arr[3], '', MAX_FLOAT_DIFF); 348ffe3c632Sopenharmony_ci 349ffe3c632Sopenharmony_ci $this->assertEquals(4, count($arr)); 350ffe3c632Sopenharmony_ci 351ffe3c632Sopenharmony_ci for ($i = 0; $i < count($arr); $i++) { 352ffe3c632Sopenharmony_ci $arr[$i] = 0; 353ffe3c632Sopenharmony_ci $this->assertSame(0.0, $arr[$i]); 354ffe3c632Sopenharmony_ci } 355ffe3c632Sopenharmony_ci 356ffe3c632Sopenharmony_ci // Test set. 357ffe3c632Sopenharmony_ci $arr[0] = 1; 358ffe3c632Sopenharmony_ci $this->assertEquals(1.0, $arr[0], '', MAX_FLOAT_DIFF); 359ffe3c632Sopenharmony_ci 360ffe3c632Sopenharmony_ci $arr[1] = 1.1; 361ffe3c632Sopenharmony_ci $this->assertEquals(1.1, $arr[1], '', MAX_FLOAT_DIFF); 362ffe3c632Sopenharmony_ci 363ffe3c632Sopenharmony_ci $arr[2] = '2'; 364ffe3c632Sopenharmony_ci $this->assertEquals(2.0, $arr[2], '', MAX_FLOAT_DIFF); 365ffe3c632Sopenharmony_ci $arr[3] = '3.1'; 366ffe3c632Sopenharmony_ci $this->assertEquals(3.1, $arr[3], '', MAX_FLOAT_DIFF); 367ffe3c632Sopenharmony_ci } 368ffe3c632Sopenharmony_ci 369ffe3c632Sopenharmony_ci ######################################################### 370ffe3c632Sopenharmony_ci # Test bool field. 371ffe3c632Sopenharmony_ci ######################################################### 372ffe3c632Sopenharmony_ci 373ffe3c632Sopenharmony_ci public function testBool() 374ffe3c632Sopenharmony_ci { 375ffe3c632Sopenharmony_ci $arr = new RepeatedField(GPBType::BOOL); 376ffe3c632Sopenharmony_ci 377ffe3c632Sopenharmony_ci // Test append. 378ffe3c632Sopenharmony_ci $arr[] = true; 379ffe3c632Sopenharmony_ci $this->assertSame(true, $arr[0]); 380ffe3c632Sopenharmony_ci 381ffe3c632Sopenharmony_ci $arr[] = -1; 382ffe3c632Sopenharmony_ci $this->assertSame(true, $arr[1]); 383ffe3c632Sopenharmony_ci 384ffe3c632Sopenharmony_ci $arr[] = 1.1; 385ffe3c632Sopenharmony_ci $this->assertSame(true, $arr[2]); 386ffe3c632Sopenharmony_ci 387ffe3c632Sopenharmony_ci $arr[] = ''; 388ffe3c632Sopenharmony_ci $this->assertSame(false, $arr[3]); 389ffe3c632Sopenharmony_ci 390ffe3c632Sopenharmony_ci $this->assertEquals(4, count($arr)); 391ffe3c632Sopenharmony_ci 392ffe3c632Sopenharmony_ci for ($i = 0; $i < count($arr); $i++) { 393ffe3c632Sopenharmony_ci $arr[$i] = 0; 394ffe3c632Sopenharmony_ci $this->assertSame(false, $arr[$i]); 395ffe3c632Sopenharmony_ci } 396ffe3c632Sopenharmony_ci 397ffe3c632Sopenharmony_ci // Test set. 398ffe3c632Sopenharmony_ci $arr[0] = true; 399ffe3c632Sopenharmony_ci $this->assertSame(true, $arr[0]); 400ffe3c632Sopenharmony_ci 401ffe3c632Sopenharmony_ci $arr[1] = -1; 402ffe3c632Sopenharmony_ci $this->assertSame(true, $arr[1]); 403ffe3c632Sopenharmony_ci 404ffe3c632Sopenharmony_ci $arr[2] = 1.1; 405ffe3c632Sopenharmony_ci $this->assertSame(true, $arr[2]); 406ffe3c632Sopenharmony_ci 407ffe3c632Sopenharmony_ci $arr[3] = ''; 408ffe3c632Sopenharmony_ci $this->assertSame(false, $arr[3]); 409ffe3c632Sopenharmony_ci } 410ffe3c632Sopenharmony_ci 411ffe3c632Sopenharmony_ci ######################################################### 412ffe3c632Sopenharmony_ci # Test string field. 413ffe3c632Sopenharmony_ci ######################################################### 414ffe3c632Sopenharmony_ci 415ffe3c632Sopenharmony_ci public function testString() 416ffe3c632Sopenharmony_ci { 417ffe3c632Sopenharmony_ci $arr = new RepeatedField(GPBType::STRING); 418ffe3c632Sopenharmony_ci 419ffe3c632Sopenharmony_ci // Test append. 420ffe3c632Sopenharmony_ci $arr[] = 'abc'; 421ffe3c632Sopenharmony_ci $this->assertSame('abc', $arr[0]); 422ffe3c632Sopenharmony_ci 423ffe3c632Sopenharmony_ci $arr[] = 1; 424ffe3c632Sopenharmony_ci $this->assertSame('1', $arr[1]); 425ffe3c632Sopenharmony_ci 426ffe3c632Sopenharmony_ci $arr[] = 1.1; 427ffe3c632Sopenharmony_ci $this->assertSame('1.1', $arr[2]); 428ffe3c632Sopenharmony_ci 429ffe3c632Sopenharmony_ci $arr[] = true; 430ffe3c632Sopenharmony_ci $this->assertSame('1', $arr[3]); 431ffe3c632Sopenharmony_ci 432ffe3c632Sopenharmony_ci $this->assertEquals(4, count($arr)); 433ffe3c632Sopenharmony_ci 434ffe3c632Sopenharmony_ci for ($i = 0; $i < count($arr); $i++) { 435ffe3c632Sopenharmony_ci $arr[$i] = ''; 436ffe3c632Sopenharmony_ci $this->assertSame('', $arr[$i]); 437ffe3c632Sopenharmony_ci } 438ffe3c632Sopenharmony_ci 439ffe3c632Sopenharmony_ci // Test set. 440ffe3c632Sopenharmony_ci $arr[0] = 'abc'; 441ffe3c632Sopenharmony_ci $this->assertSame('abc', $arr[0]); 442ffe3c632Sopenharmony_ci 443ffe3c632Sopenharmony_ci $arr[1] = 1; 444ffe3c632Sopenharmony_ci $this->assertSame('1', $arr[1]); 445ffe3c632Sopenharmony_ci 446ffe3c632Sopenharmony_ci $arr[2] = 1.1; 447ffe3c632Sopenharmony_ci $this->assertSame('1.1', $arr[2]); 448ffe3c632Sopenharmony_ci 449ffe3c632Sopenharmony_ci $arr[3] = true; 450ffe3c632Sopenharmony_ci $this->assertSame('1', $arr[3]); 451ffe3c632Sopenharmony_ci } 452ffe3c632Sopenharmony_ci 453ffe3c632Sopenharmony_ci ######################################################### 454ffe3c632Sopenharmony_ci # Test message field. 455ffe3c632Sopenharmony_ci ######################################################### 456ffe3c632Sopenharmony_ci 457ffe3c632Sopenharmony_ci public function testMessage() 458ffe3c632Sopenharmony_ci { 459ffe3c632Sopenharmony_ci $arr = new RepeatedField(GPBType::MESSAGE, Sub::class); 460ffe3c632Sopenharmony_ci 461ffe3c632Sopenharmony_ci // Test append. 462ffe3c632Sopenharmony_ci $sub_m = new Sub(); 463ffe3c632Sopenharmony_ci $sub_m->setA(1); 464ffe3c632Sopenharmony_ci $arr[] = $sub_m; 465ffe3c632Sopenharmony_ci $this->assertSame(1, $arr[0]->getA()); 466ffe3c632Sopenharmony_ci 467ffe3c632Sopenharmony_ci $this->assertEquals(1, count($arr)); 468ffe3c632Sopenharmony_ci 469ffe3c632Sopenharmony_ci // Test set. 470ffe3c632Sopenharmony_ci $sub_m = new Sub(); 471ffe3c632Sopenharmony_ci $sub_m->setA(2); 472ffe3c632Sopenharmony_ci $arr[0] = $sub_m; 473ffe3c632Sopenharmony_ci $this->assertSame(2, $arr[0]->getA()); 474ffe3c632Sopenharmony_ci 475ffe3c632Sopenharmony_ci // Test foreach. 476ffe3c632Sopenharmony_ci $arr = new RepeatedField(GPBType::MESSAGE, Sub::class); 477ffe3c632Sopenharmony_ci for ($i = 0; $i < 3; $i++) { 478ffe3c632Sopenharmony_ci $arr[] = new Sub(); 479ffe3c632Sopenharmony_ci $arr[$i]->setA($i); 480ffe3c632Sopenharmony_ci } 481ffe3c632Sopenharmony_ci $i = 0; 482ffe3c632Sopenharmony_ci foreach ($arr as $val) { 483ffe3c632Sopenharmony_ci $this->assertSame($i++, $val->getA()); 484ffe3c632Sopenharmony_ci } 485ffe3c632Sopenharmony_ci $this->assertSame(3, $i); 486ffe3c632Sopenharmony_ci } 487ffe3c632Sopenharmony_ci 488ffe3c632Sopenharmony_ci ######################################################### 489ffe3c632Sopenharmony_ci # Test offset type 490ffe3c632Sopenharmony_ci ######################################################### 491ffe3c632Sopenharmony_ci 492ffe3c632Sopenharmony_ci public function testOffset() 493ffe3c632Sopenharmony_ci { 494ffe3c632Sopenharmony_ci $arr = new RepeatedField(GPBType::INT32); 495ffe3c632Sopenharmony_ci $arr[] = 0; 496ffe3c632Sopenharmony_ci 497ffe3c632Sopenharmony_ci $arr[0] = 1; 498ffe3c632Sopenharmony_ci $this->assertSame(1, $arr[0]); 499ffe3c632Sopenharmony_ci $this->assertSame(1, count($arr)); 500ffe3c632Sopenharmony_ci 501ffe3c632Sopenharmony_ci $arr['0'] = 2; 502ffe3c632Sopenharmony_ci $this->assertSame(2, $arr['0']); 503ffe3c632Sopenharmony_ci $this->assertSame(2, $arr[0]); 504ffe3c632Sopenharmony_ci $this->assertSame(1, count($arr)); 505ffe3c632Sopenharmony_ci 506ffe3c632Sopenharmony_ci $arr[0.0] = 3; 507ffe3c632Sopenharmony_ci $this->assertSame(3, $arr[0.0]); 508ffe3c632Sopenharmony_ci $this->assertSame(1, count($arr)); 509ffe3c632Sopenharmony_ci } 510ffe3c632Sopenharmony_ci 511ffe3c632Sopenharmony_ci public function testInsertRemoval() 512ffe3c632Sopenharmony_ci { 513ffe3c632Sopenharmony_ci $arr = new RepeatedField(GPBType::INT32); 514ffe3c632Sopenharmony_ci 515ffe3c632Sopenharmony_ci $arr[] = 0; 516ffe3c632Sopenharmony_ci $arr[] = 1; 517ffe3c632Sopenharmony_ci $arr[] = 2; 518ffe3c632Sopenharmony_ci $this->assertSame(3, count($arr)); 519ffe3c632Sopenharmony_ci 520ffe3c632Sopenharmony_ci unset($arr[2]); 521ffe3c632Sopenharmony_ci $this->assertSame(2, count($arr)); 522ffe3c632Sopenharmony_ci $this->assertSame(0, $arr[0]); 523ffe3c632Sopenharmony_ci $this->assertSame(1, $arr[1]); 524ffe3c632Sopenharmony_ci 525ffe3c632Sopenharmony_ci $arr[] = 3; 526ffe3c632Sopenharmony_ci $this->assertSame(3, count($arr)); 527ffe3c632Sopenharmony_ci $this->assertSame(0, $arr[0]); 528ffe3c632Sopenharmony_ci $this->assertSame(1, $arr[1]); 529ffe3c632Sopenharmony_ci $this->assertSame(3, $arr[2]); 530ffe3c632Sopenharmony_ci } 531ffe3c632Sopenharmony_ci 532ffe3c632Sopenharmony_ci ######################################################### 533ffe3c632Sopenharmony_ci # Test reference in array 534ffe3c632Sopenharmony_ci ######################################################### 535ffe3c632Sopenharmony_ci 536ffe3c632Sopenharmony_ci public function testArrayElementIsReferenceInSetters() 537ffe3c632Sopenharmony_ci { 538ffe3c632Sopenharmony_ci // Bool elements 539ffe3c632Sopenharmony_ci $values = [true]; 540ffe3c632Sopenharmony_ci array_walk($values, function (&$value) {}); 541ffe3c632Sopenharmony_ci $m = new TestMessage(); 542ffe3c632Sopenharmony_ci $m->setRepeatedBool($values); 543ffe3c632Sopenharmony_ci 544ffe3c632Sopenharmony_ci // Int32 elements 545ffe3c632Sopenharmony_ci $values = [1]; 546ffe3c632Sopenharmony_ci array_walk($values, function (&$value) {}); 547ffe3c632Sopenharmony_ci $m = new TestMessage(); 548ffe3c632Sopenharmony_ci $m->setRepeatedInt32($values); 549ffe3c632Sopenharmony_ci 550ffe3c632Sopenharmony_ci // Double elements 551ffe3c632Sopenharmony_ci $values = [1.0]; 552ffe3c632Sopenharmony_ci array_walk($values, function (&$value) {}); 553ffe3c632Sopenharmony_ci $m = new TestMessage(); 554ffe3c632Sopenharmony_ci $m->setRepeatedDouble($values); 555ffe3c632Sopenharmony_ci 556ffe3c632Sopenharmony_ci // String elements 557ffe3c632Sopenharmony_ci $values = ['a']; 558ffe3c632Sopenharmony_ci array_walk($values, function (&$value) {}); 559ffe3c632Sopenharmony_ci $m = new TestMessage(); 560ffe3c632Sopenharmony_ci $m->setRepeatedString($values); 561ffe3c632Sopenharmony_ci 562ffe3c632Sopenharmony_ci // Message elements 563ffe3c632Sopenharmony_ci $m = new TestMessage(); 564ffe3c632Sopenharmony_ci $subs = [1, 2]; 565ffe3c632Sopenharmony_ci foreach ($subs as &$sub) { 566ffe3c632Sopenharmony_ci $sub = new Sub(['a' => $sub]); 567ffe3c632Sopenharmony_ci } 568ffe3c632Sopenharmony_ci $m->setRepeatedMessage($subs); 569ffe3c632Sopenharmony_ci } 570ffe3c632Sopenharmony_ci 571ffe3c632Sopenharmony_ci ######################################################### 572ffe3c632Sopenharmony_ci # Test memory leak 573ffe3c632Sopenharmony_ci ######################################################### 574ffe3c632Sopenharmony_ci 575ffe3c632Sopenharmony_ci public function testCycleLeak() 576ffe3c632Sopenharmony_ci { 577ffe3c632Sopenharmony_ci gc_collect_cycles(); 578ffe3c632Sopenharmony_ci $arr = new RepeatedField(GPBType::MESSAGE, TestMessage::class); 579ffe3c632Sopenharmony_ci $arr[] = new TestMessage; 580ffe3c632Sopenharmony_ci $arr[0]->SetRepeatedRecursive($arr); 581ffe3c632Sopenharmony_ci 582ffe3c632Sopenharmony_ci // Clean up memory before test. 583ffe3c632Sopenharmony_ci gc_collect_cycles(); 584ffe3c632Sopenharmony_ci $start = memory_get_usage(); 585ffe3c632Sopenharmony_ci unset($arr); 586ffe3c632Sopenharmony_ci 587ffe3c632Sopenharmony_ci // Explicitly trigger garbage collection. 588ffe3c632Sopenharmony_ci gc_collect_cycles(); 589ffe3c632Sopenharmony_ci 590ffe3c632Sopenharmony_ci $end = memory_get_usage(); 591ffe3c632Sopenharmony_ci $this->assertLessThan($start, $end); 592ffe3c632Sopenharmony_ci } 593ffe3c632Sopenharmony_ci} 594