OLD | NEW |
(Empty) | |
| 1 <?php |
| 2 |
| 3 require_once('test.pb.php'); |
| 4 require_once('test_util.php'); |
| 5 |
| 6 use Google\Protobuf\Internal\RepeatedField; |
| 7 use Google\Protobuf\Internal\GPBType; |
| 8 use Foo\TestMessage; |
| 9 use Foo\TestMessage_Sub; |
| 10 |
| 11 class RepeatedFieldTest extends PHPUnit_Framework_TestCase |
| 12 { |
| 13 |
| 14 ######################################################### |
| 15 # Test int32 field. |
| 16 ######################################################### |
| 17 |
| 18 public function testInt32() |
| 19 { |
| 20 $arr = new RepeatedField(GPBType::INT32); |
| 21 |
| 22 // Test append. |
| 23 $arr []= MAX_INT32; |
| 24 $this->assertSame(MAX_INT32, $arr[0]); |
| 25 $arr []= MIN_INT32; |
| 26 $this->assertSame(MIN_INT32, $arr[1]); |
| 27 |
| 28 $arr []= 1.1; |
| 29 $this->assertSame(1, $arr[2]); |
| 30 $arr []= MAX_INT32_FLOAT; |
| 31 $this->assertSame(MAX_INT32, $arr[3]); |
| 32 $arr []= MAX_INT32_FLOAT; |
| 33 $this->assertSame(MAX_INT32, $arr[4]); |
| 34 |
| 35 $arr []= '2'; |
| 36 $this->assertSame(2, $arr[5]); |
| 37 $arr []= '3.1'; |
| 38 $this->assertSame(3, $arr[6]); |
| 39 $arr []= MAX_INT32_STRING; |
| 40 $this->assertSame(MAX_INT32, $arr[7]); |
| 41 |
| 42 $this->assertEquals(8, count($arr)); |
| 43 |
| 44 for ($i = 0; $i < count($arr); $i++) { |
| 45 $arr[$i] = 0; |
| 46 $this->assertSame(0, $arr[$i]); |
| 47 } |
| 48 |
| 49 // Test set. |
| 50 $arr [0]= MAX_INT32; |
| 51 $this->assertSame(MAX_INT32, $arr[0]); |
| 52 $arr [1]= MIN_INT32; |
| 53 $this->assertSame(MIN_INT32, $arr[1]); |
| 54 |
| 55 $arr [2]= 1.1; |
| 56 $this->assertSame(1, $arr[2]); |
| 57 $arr [3]= MAX_INT32_FLOAT; |
| 58 $this->assertSame(MAX_INT32, $arr[3]); |
| 59 $arr [4]= MAX_INT32_FLOAT; |
| 60 $this->assertSame(MAX_INT32, $arr[4]); |
| 61 |
| 62 $arr [5]= '2'; |
| 63 $this->assertSame(2, $arr[5]); |
| 64 $arr [6]= '3.1'; |
| 65 $this->assertSame(3, $arr[6]); |
| 66 $arr [7]= MAX_INT32_STRING; |
| 67 $this->assertSame(MAX_INT32, $arr[7]); |
| 68 |
| 69 // Test foreach. |
| 70 $arr = new RepeatedField(GPBType::INT32); |
| 71 for ($i = 0; $i < 3; $i++) { |
| 72 $arr []= $i; |
| 73 } |
| 74 $i = 0; |
| 75 foreach ($arr as $val) { |
| 76 $this->assertSame($i++, $val); |
| 77 } |
| 78 $this->assertSame(3, $i); |
| 79 } |
| 80 |
| 81 /** |
| 82 * @expectedException PHPUnit_Framework_Error |
| 83 */ |
| 84 public function testInt32AppendStringFail() |
| 85 { |
| 86 $arr = new RepeatedField(GPBType::INT32); |
| 87 $arr []= 'abc'; |
| 88 } |
| 89 |
| 90 /** |
| 91 * @expectedException PHPUnit_Framework_Error |
| 92 */ |
| 93 public function testInt32SetStringFail() |
| 94 { |
| 95 $arr = new RepeatedField(GPBType::INT32); |
| 96 $arr []= 0; |
| 97 $arr [0]= 'abc'; |
| 98 } |
| 99 |
| 100 /** |
| 101 * @expectedException PHPUnit_Framework_Error |
| 102 */ |
| 103 public function testInt32AppendMessageFail() |
| 104 { |
| 105 $arr = new RepeatedField(GPBType::INT32); |
| 106 $arr []= new TestMessage_Sub(); |
| 107 } |
| 108 |
| 109 /** |
| 110 * @expectedException PHPUnit_Framework_Error |
| 111 */ |
| 112 public function testInt32SetMessageFail() |
| 113 { |
| 114 $arr = new RepeatedField(GPBType::INT32); |
| 115 $arr []= 0; |
| 116 $arr [0]= new TestMessage_Sub(); |
| 117 } |
| 118 |
| 119 ######################################################### |
| 120 # Test uint32 field. |
| 121 ######################################################### |
| 122 |
| 123 public function testUint32() |
| 124 { |
| 125 $arr = new RepeatedField(GPBType::UINT32); |
| 126 |
| 127 // Test append. |
| 128 $arr []= MAX_UINT32; |
| 129 $this->assertSame(-1, $arr[0]); |
| 130 $arr []= -1; |
| 131 $this->assertSame(-1, $arr[1]); |
| 132 $arr []= MIN_UINT32; |
| 133 $this->assertSame(MIN_UINT32, $arr[2]); |
| 134 |
| 135 $arr []= 1.1; |
| 136 $this->assertSame(1, $arr[3]); |
| 137 $arr []= MAX_UINT32_FLOAT; |
| 138 $this->assertSame(-1, $arr[4]); |
| 139 $arr []= -1.0; |
| 140 $this->assertSame(-1, $arr[5]); |
| 141 $arr []= MIN_UINT32_FLOAT; |
| 142 $this->assertSame(MIN_UINT32, $arr[6]); |
| 143 |
| 144 $arr []= '2'; |
| 145 $this->assertSame(2, $arr[7]); |
| 146 $arr []= '3.1'; |
| 147 $this->assertSame(3, $arr[8]); |
| 148 $arr []= MAX_UINT32_STRING; |
| 149 $this->assertSame(-1, $arr[9]); |
| 150 $arr []= '-1.0'; |
| 151 $this->assertSame(-1, $arr[10]); |
| 152 $arr []= MIN_UINT32_STRING; |
| 153 $this->assertSame(MIN_UINT32, $arr[11]); |
| 154 |
| 155 $this->assertEquals(12, count($arr)); |
| 156 |
| 157 for ($i = 0; $i < count($arr); $i++) { |
| 158 $arr[$i] = 0; |
| 159 $this->assertSame(0, $arr[$i]); |
| 160 } |
| 161 |
| 162 // Test set. |
| 163 $arr [0]= MAX_UINT32; |
| 164 $this->assertSame(-1, $arr[0]); |
| 165 $arr [1]= -1; |
| 166 $this->assertSame(-1, $arr[1]); |
| 167 $arr [2]= MIN_UINT32; |
| 168 $this->assertSame(MIN_UINT32, $arr[2]); |
| 169 |
| 170 $arr [3]= 1.1; |
| 171 $this->assertSame(1, $arr[3]); |
| 172 $arr [4]= MAX_UINT32_FLOAT; |
| 173 $this->assertSame(-1, $arr[4]); |
| 174 $arr [5]= -1.0; |
| 175 $this->assertSame(-1, $arr[5]); |
| 176 $arr [6]= MIN_UINT32_FLOAT; |
| 177 $this->assertSame(MIN_UINT32, $arr[6]); |
| 178 |
| 179 $arr [7]= '2'; |
| 180 $this->assertSame(2, $arr[7]); |
| 181 $arr [8]= '3.1'; |
| 182 $this->assertSame(3, $arr[8]); |
| 183 $arr [9]= MAX_UINT32_STRING; |
| 184 $this->assertSame(-1, $arr[9]); |
| 185 $arr [10]= '-1.0'; |
| 186 $this->assertSame(-1, $arr[10]); |
| 187 $arr [11]= MIN_UINT32_STRING; |
| 188 $this->assertSame(MIN_UINT32, $arr[11]); |
| 189 } |
| 190 |
| 191 /** |
| 192 * @expectedException PHPUnit_Framework_Error |
| 193 */ |
| 194 public function testUint32AppendStringFail() |
| 195 { |
| 196 $arr = new RepeatedField(GPBType::UINT32); |
| 197 $arr []= 'abc'; |
| 198 } |
| 199 |
| 200 /** |
| 201 * @expectedException PHPUnit_Framework_Error |
| 202 */ |
| 203 public function testUint32SetStringFail() |
| 204 { |
| 205 $arr = new RepeatedField(GPBType::UINT32); |
| 206 $arr []= 0; |
| 207 $arr [0]= 'abc'; |
| 208 } |
| 209 |
| 210 /** |
| 211 * @expectedException PHPUnit_Framework_Error |
| 212 */ |
| 213 public function testUint32AppendMessageFail() |
| 214 { |
| 215 $arr = new RepeatedField(GPBType::UINT32); |
| 216 $arr []= new TestMessage_Sub(); |
| 217 } |
| 218 |
| 219 /** |
| 220 * @expectedException PHPUnit_Framework_Error |
| 221 */ |
| 222 public function testUint32SetMessageFail() |
| 223 { |
| 224 $arr = new RepeatedField(GPBType::UINT32); |
| 225 $arr []= 0; |
| 226 $arr [0]= new TestMessage_Sub(); |
| 227 } |
| 228 |
| 229 ######################################################### |
| 230 # Test int64 field. |
| 231 ######################################################### |
| 232 |
| 233 public function testInt64() |
| 234 { |
| 235 $arr = new RepeatedField(GPBType::INT64); |
| 236 |
| 237 // Test append. |
| 238 $arr []= MAX_INT64; |
| 239 $arr []= MIN_INT64; |
| 240 $arr []= 1.1; |
| 241 $arr []= '2'; |
| 242 $arr []= '3.1'; |
| 243 $arr []= MAX_INT64_STRING; |
| 244 $arr []= MIN_INT64_STRING; |
| 245 if (PHP_INT_SIZE == 4) { |
| 246 $this->assertSame(MAX_INT64, $arr[0]); |
| 247 $this->assertSame(MIN_INT64, $arr[1]); |
| 248 $this->assertSame('1', $arr[2]); |
| 249 $this->assertSame('2', $arr[3]); |
| 250 $this->assertSame('3', $arr[4]); |
| 251 $this->assertSame(MAX_INT64_STRING, $arr[5]); |
| 252 $this->assertSame(MIN_INT64_STRING, $arr[6]); |
| 253 } else { |
| 254 $this->assertSame(MAX_INT64, $arr[0]); |
| 255 $this->assertSame(MIN_INT64, $arr[1]); |
| 256 $this->assertSame(1, $arr[2]); |
| 257 $this->assertSame(2, $arr[3]); |
| 258 $this->assertSame(3, $arr[4]); |
| 259 $this->assertSame(MAX_INT64, $arr[5]); |
| 260 $this->assertSame(MIN_INT64, $arr[6]); |
| 261 } |
| 262 |
| 263 |
| 264 $this->assertEquals(7, count($arr)); |
| 265 |
| 266 for ($i = 0; $i < count($arr); $i++) { |
| 267 $arr[$i] = 0; |
| 268 if (PHP_INT_SIZE == 4) { |
| 269 $this->assertSame('0', $arr[$i]); |
| 270 } else { |
| 271 $this->assertSame(0, $arr[$i]); |
| 272 } |
| 273 } |
| 274 |
| 275 // Test set. |
| 276 $arr [0]= MAX_INT64; |
| 277 $arr [1]= MIN_INT64; |
| 278 $arr [2]= 1.1; |
| 279 $arr [3]= '2'; |
| 280 $arr [4]= '3.1'; |
| 281 $arr [5]= MAX_INT64_STRING; |
| 282 $arr [6]= MIN_INT64_STRING; |
| 283 |
| 284 if (PHP_INT_SIZE == 4) { |
| 285 $this->assertSame(MAX_INT64_STRING, $arr[0]); |
| 286 $this->assertSame(MIN_INT64_STRING, $arr[1]); |
| 287 $this->assertSame('1', $arr[2]); |
| 288 $this->assertSame('2', $arr[3]); |
| 289 $this->assertSame('3', $arr[4]); |
| 290 $this->assertSame(MAX_INT64_STRING, $arr[5]); |
| 291 $this->assertEquals(MIN_INT64_STRING, $arr[6]); |
| 292 } else { |
| 293 $this->assertSame(MAX_INT64, $arr[0]); |
| 294 $this->assertSame(MIN_INT64, $arr[1]); |
| 295 $this->assertSame(1, $arr[2]); |
| 296 $this->assertSame(2, $arr[3]); |
| 297 $this->assertSame(3, $arr[4]); |
| 298 $this->assertSame(MAX_INT64, $arr[5]); |
| 299 $this->assertEquals(MIN_INT64, $arr[6]); |
| 300 } |
| 301 } |
| 302 |
| 303 /** |
| 304 * @expectedException PHPUnit_Framework_Error |
| 305 */ |
| 306 public function testInt64AppendStringFail() |
| 307 { |
| 308 $arr = new RepeatedField(GPBType::INT64); |
| 309 $arr []= 'abc'; |
| 310 } |
| 311 |
| 312 /** |
| 313 * @expectedException PHPUnit_Framework_Error |
| 314 */ |
| 315 public function testInt64SetStringFail() |
| 316 { |
| 317 $arr = new RepeatedField(GPBType::INT64); |
| 318 $arr []= 0; |
| 319 $arr [0]= 'abc'; |
| 320 } |
| 321 |
| 322 /** |
| 323 * @expectedException PHPUnit_Framework_Error |
| 324 */ |
| 325 public function testInt64AppendMessageFail() |
| 326 { |
| 327 $arr = new RepeatedField(GPBType::INT64); |
| 328 $arr []= new TestMessage_Sub(); |
| 329 } |
| 330 |
| 331 /** |
| 332 * @expectedException PHPUnit_Framework_Error |
| 333 */ |
| 334 public function testInt64SetMessageFail() |
| 335 { |
| 336 $arr = new RepeatedField(GPBType::INT64); |
| 337 $arr []= 0; |
| 338 $arr [0]= new TestMessage_Sub(); |
| 339 } |
| 340 |
| 341 ######################################################### |
| 342 # Test uint64 field. |
| 343 ######################################################### |
| 344 |
| 345 public function testUint64() |
| 346 { |
| 347 $arr = new RepeatedField(GPBType::UINT64); |
| 348 |
| 349 // Test append. |
| 350 $arr []= MAX_UINT64; |
| 351 $arr []= 1.1; |
| 352 $arr []= '2'; |
| 353 $arr []= '3.1'; |
| 354 $arr []= MAX_UINT64_STRING; |
| 355 |
| 356 if (PHP_INT_SIZE == 4) { |
| 357 $this->assertSame(MAX_UINT64_STRING, $arr[0]); |
| 358 $this->assertSame('1', $arr[1]); |
| 359 $this->assertSame('2', $arr[2]); |
| 360 $this->assertSame('3', $arr[3]); |
| 361 $this->assertSame(MAX_UINT64_STRING, $arr[4]); |
| 362 } else { |
| 363 $this->assertSame(MAX_UINT64, $arr[0]); |
| 364 $this->assertSame(1, $arr[1]); |
| 365 $this->assertSame(2, $arr[2]); |
| 366 $this->assertSame(3, $arr[3]); |
| 367 $this->assertSame(MAX_UINT64, $arr[4]); |
| 368 $this->assertSame(5, count($arr)); |
| 369 } |
| 370 |
| 371 $this->assertSame(5, count($arr)); |
| 372 |
| 373 for ($i = 0; $i < count($arr); $i++) { |
| 374 $arr[$i] = 0; |
| 375 if (PHP_INT_SIZE == 4) { |
| 376 $this->assertSame('0', $arr[$i]); |
| 377 } else { |
| 378 $this->assertSame(0, $arr[$i]); |
| 379 } |
| 380 } |
| 381 |
| 382 // Test set. |
| 383 $arr [0]= MAX_UINT64; |
| 384 $arr [1]= 1.1; |
| 385 $arr [2]= '2'; |
| 386 $arr [3]= '3.1'; |
| 387 $arr [4]= MAX_UINT64_STRING; |
| 388 |
| 389 if (PHP_INT_SIZE == 4) { |
| 390 $this->assertSame(MAX_UINT64_STRING, $arr[0]); |
| 391 $this->assertSame('1', $arr[1]); |
| 392 $this->assertSame('2', $arr[2]); |
| 393 $this->assertSame('3', $arr[3]); |
| 394 $this->assertSame(MAX_UINT64_STRING, $arr[4]); |
| 395 } else { |
| 396 $this->assertSame(MAX_UINT64, $arr[0]); |
| 397 $this->assertSame(1, $arr[1]); |
| 398 $this->assertSame(2, $arr[2]); |
| 399 $this->assertSame(3, $arr[3]); |
| 400 $this->assertSame(MAX_UINT64, $arr[4]); |
| 401 } |
| 402 } |
| 403 |
| 404 /** |
| 405 * @expectedException PHPUnit_Framework_Error |
| 406 */ |
| 407 public function testUint64AppendStringFail() |
| 408 { |
| 409 $arr = new RepeatedField(GPBType::UINT64); |
| 410 $arr []= 'abc'; |
| 411 } |
| 412 |
| 413 /** |
| 414 * @expectedException PHPUnit_Framework_Error |
| 415 */ |
| 416 public function testUint64SetStringFail() |
| 417 { |
| 418 $arr = new RepeatedField(GPBType::UINT64); |
| 419 $arr []= 0; |
| 420 $arr [0]= 'abc'; |
| 421 } |
| 422 |
| 423 /** |
| 424 * @expectedException PHPUnit_Framework_Error |
| 425 */ |
| 426 public function testUint64AppendMessageFail() |
| 427 { |
| 428 $arr = new RepeatedField(GPBType::UINT64); |
| 429 $arr []= new TestMessage_Sub(); |
| 430 } |
| 431 |
| 432 /** |
| 433 * @expectedException PHPUnit_Framework_Error |
| 434 */ |
| 435 public function testUint64SetMessageFail() |
| 436 { |
| 437 $arr = new RepeatedField(GPBType::UINT64); |
| 438 $arr []= 0; |
| 439 $arr [0]= new TestMessage_Sub(); |
| 440 } |
| 441 |
| 442 ######################################################### |
| 443 # Test float field. |
| 444 ######################################################### |
| 445 |
| 446 public function testFloat() |
| 447 { |
| 448 $arr = new RepeatedField(GPBType::FLOAT); |
| 449 |
| 450 // Test append. |
| 451 $arr []= 1; |
| 452 $this->assertEquals(1.0, $arr[0], '', MAX_FLOAT_DIFF); |
| 453 |
| 454 $arr []= 1.1; |
| 455 $this->assertEquals(1.1, $arr[1], '', MAX_FLOAT_DIFF); |
| 456 |
| 457 $arr []= '2'; |
| 458 $this->assertEquals(2.0, $arr[2], '', MAX_FLOAT_DIFF); |
| 459 $arr []= '3.1'; |
| 460 $this->assertEquals(3.1, $arr[3], '', MAX_FLOAT_DIFF); |
| 461 |
| 462 $this->assertEquals(4, count($arr)); |
| 463 |
| 464 for ($i = 0; $i < count($arr); $i++) { |
| 465 $arr[$i] = 0; |
| 466 $this->assertSame(0.0, $arr[$i]); |
| 467 } |
| 468 |
| 469 // Test set. |
| 470 $arr [0]= 1; |
| 471 $this->assertEquals(1.0, $arr[0], '', MAX_FLOAT_DIFF); |
| 472 |
| 473 $arr [1]= 1.1; |
| 474 $this->assertEquals(1.1, $arr[1], '', MAX_FLOAT_DIFF); |
| 475 |
| 476 $arr [2]= '2'; |
| 477 $this->assertEquals(2.0, $arr[2], '', MAX_FLOAT_DIFF); |
| 478 $arr [3]= '3.1'; |
| 479 $this->assertEquals(3.1, $arr[3], '', MAX_FLOAT_DIFF); |
| 480 } |
| 481 |
| 482 /** |
| 483 * @expectedException PHPUnit_Framework_Error |
| 484 */ |
| 485 public function testFloatAppendStringFail() |
| 486 { |
| 487 $arr = new RepeatedField(GPBType::FLOAT); |
| 488 $arr []= 'abc'; |
| 489 } |
| 490 |
| 491 /** |
| 492 * @expectedException PHPUnit_Framework_Error |
| 493 */ |
| 494 public function testFloatSetStringFail() |
| 495 { |
| 496 $arr = new RepeatedField(GPBType::FLOAT); |
| 497 $arr []= 0.0; |
| 498 $arr [0]= 'abc'; |
| 499 } |
| 500 |
| 501 /** |
| 502 * @expectedException PHPUnit_Framework_Error |
| 503 */ |
| 504 public function testFloatAppendMessageFail() |
| 505 { |
| 506 $arr = new RepeatedField(GPBType::FLOAT); |
| 507 $arr []= new TestMessage_Sub(); |
| 508 } |
| 509 |
| 510 /** |
| 511 * @expectedException PHPUnit_Framework_Error |
| 512 */ |
| 513 public function testFloatSetMessageFail() |
| 514 { |
| 515 $arr = new RepeatedField(GPBType::FLOAT); |
| 516 $arr []= 0.0; |
| 517 $arr [0]= new TestMessage_Sub(); |
| 518 } |
| 519 |
| 520 ######################################################### |
| 521 # Test double field. |
| 522 ######################################################### |
| 523 |
| 524 public function testDouble() |
| 525 { |
| 526 $arr = new RepeatedField(GPBType::DOUBLE); |
| 527 |
| 528 // Test append. |
| 529 $arr []= 1; |
| 530 $this->assertEquals(1.0, $arr[0], '', MAX_FLOAT_DIFF); |
| 531 |
| 532 $arr []= 1.1; |
| 533 $this->assertEquals(1.1, $arr[1], '', MAX_FLOAT_DIFF); |
| 534 |
| 535 $arr []= '2'; |
| 536 $this->assertEquals(2.0, $arr[2], '', MAX_FLOAT_DIFF); |
| 537 $arr []= '3.1'; |
| 538 $this->assertEquals(3.1, $arr[3], '', MAX_FLOAT_DIFF); |
| 539 |
| 540 $this->assertEquals(4, count($arr)); |
| 541 |
| 542 for ($i = 0; $i < count($arr); $i++) { |
| 543 $arr[$i] = 0; |
| 544 $this->assertSame(0.0, $arr[$i]); |
| 545 } |
| 546 |
| 547 // Test set. |
| 548 $arr [0]= 1; |
| 549 $this->assertEquals(1.0, $arr[0], '', MAX_FLOAT_DIFF); |
| 550 |
| 551 $arr [1]= 1.1; |
| 552 $this->assertEquals(1.1, $arr[1], '', MAX_FLOAT_DIFF); |
| 553 |
| 554 $arr [2]= '2'; |
| 555 $this->assertEquals(2.0, $arr[2], '', MAX_FLOAT_DIFF); |
| 556 $arr [3]= '3.1'; |
| 557 $this->assertEquals(3.1, $arr[3], '', MAX_FLOAT_DIFF); |
| 558 } |
| 559 |
| 560 /** |
| 561 * @expectedException PHPUnit_Framework_Error |
| 562 */ |
| 563 public function testDoubleAppendStringFail() |
| 564 { |
| 565 $arr = new RepeatedField(GPBType::DOUBLE); |
| 566 $arr []= 'abc'; |
| 567 } |
| 568 |
| 569 /** |
| 570 * @expectedException PHPUnit_Framework_Error |
| 571 */ |
| 572 public function testDoubleSetStringFail() |
| 573 { |
| 574 $arr = new RepeatedField(GPBType::DOUBLE); |
| 575 $arr []= 0.0; |
| 576 $arr [0]= 'abc'; |
| 577 } |
| 578 |
| 579 /** |
| 580 * @expectedException PHPUnit_Framework_Error |
| 581 */ |
| 582 public function testDoubleAppendMessageFail() |
| 583 { |
| 584 $arr = new RepeatedField(GPBType::DOUBLE); |
| 585 $arr []= new TestMessage_Sub(); |
| 586 } |
| 587 |
| 588 /** |
| 589 * @expectedException PHPUnit_Framework_Error |
| 590 */ |
| 591 public function testDoubleSetMessageFail() |
| 592 { |
| 593 $arr = new RepeatedField(GPBType::DOUBLE); |
| 594 $arr []= 0.0; |
| 595 $arr [0]= new TestMessage_Sub(); |
| 596 } |
| 597 |
| 598 ######################################################### |
| 599 # Test bool field. |
| 600 ######################################################### |
| 601 |
| 602 public function testBool() |
| 603 { |
| 604 $arr = new RepeatedField(GPBType::BOOL); |
| 605 |
| 606 // Test append. |
| 607 $arr []= true; |
| 608 $this->assertSame(true, $arr[0]); |
| 609 |
| 610 $arr []= -1; |
| 611 $this->assertSame(true, $arr[1]); |
| 612 |
| 613 $arr []= 1.1; |
| 614 $this->assertSame(true, $arr[2]); |
| 615 |
| 616 $arr []= ''; |
| 617 $this->assertSame(false, $arr[3]); |
| 618 |
| 619 $this->assertEquals(4, count($arr)); |
| 620 |
| 621 for ($i = 0; $i < count($arr); $i++) { |
| 622 $arr[$i] = 0; |
| 623 $this->assertSame(false, $arr[$i]); |
| 624 } |
| 625 |
| 626 // Test set. |
| 627 $arr [0]= true; |
| 628 $this->assertSame(true, $arr[0]); |
| 629 |
| 630 $arr [1]= -1; |
| 631 $this->assertSame(true, $arr[1]); |
| 632 |
| 633 $arr [2]= 1.1; |
| 634 $this->assertSame(true, $arr[2]); |
| 635 |
| 636 $arr [3]= ''; |
| 637 $this->assertSame(false, $arr[3]); |
| 638 } |
| 639 |
| 640 /** |
| 641 * @expectedException PHPUnit_Framework_Error |
| 642 */ |
| 643 public function testBoolAppendMessageFail() |
| 644 { |
| 645 $arr = new RepeatedField(GPBType::BOOL); |
| 646 $arr []= new TestMessage_Sub(); |
| 647 } |
| 648 |
| 649 /** |
| 650 * @expectedException PHPUnit_Framework_Error |
| 651 */ |
| 652 public function testBoolSetMessageFail() |
| 653 { |
| 654 $arr = new RepeatedField(GPBType::BOOL); |
| 655 $arr []= true; |
| 656 $arr [0]= new TestMessage_Sub(); |
| 657 } |
| 658 |
| 659 ######################################################### |
| 660 # Test string field. |
| 661 ######################################################### |
| 662 |
| 663 public function testString() |
| 664 { |
| 665 $arr = new RepeatedField(GPBType::STRING); |
| 666 |
| 667 // Test append. |
| 668 $arr []= 'abc'; |
| 669 $this->assertSame('abc', $arr[0]); |
| 670 |
| 671 $arr []= 1; |
| 672 $this->assertSame('1', $arr[1]); |
| 673 |
| 674 $arr []= 1.1; |
| 675 $this->assertSame('1.1', $arr[2]); |
| 676 |
| 677 $arr []= true; |
| 678 $this->assertSame('1', $arr[3]); |
| 679 |
| 680 $this->assertEquals(4, count($arr)); |
| 681 |
| 682 for ($i = 0; $i < count($arr); $i++) { |
| 683 $arr[$i] = ''; |
| 684 $this->assertSame('', $arr[$i]); |
| 685 } |
| 686 |
| 687 // Test set. |
| 688 $arr [0]= 'abc'; |
| 689 $this->assertSame('abc', $arr[0]); |
| 690 |
| 691 $arr [1]= 1; |
| 692 $this->assertSame('1', $arr[1]); |
| 693 |
| 694 $arr [2]= 1.1; |
| 695 $this->assertSame('1.1', $arr[2]); |
| 696 |
| 697 $arr [3]= true; |
| 698 $this->assertSame('1', $arr[3]); |
| 699 } |
| 700 |
| 701 /** |
| 702 * @expectedException PHPUnit_Framework_Error |
| 703 */ |
| 704 public function testStringAppendMessageFail() |
| 705 { |
| 706 $arr = new RepeatedField(GPBType::STRING); |
| 707 $arr []= new TestMessage_Sub(); |
| 708 } |
| 709 |
| 710 /** |
| 711 * @expectedException PHPUnit_Framework_Error |
| 712 */ |
| 713 public function testStringSetMessageFail() |
| 714 { |
| 715 $arr = new RepeatedField(GPBType::STRING); |
| 716 $arr []= 'abc'; |
| 717 $arr [0]= new TestMessage_Sub(); |
| 718 } |
| 719 |
| 720 /** |
| 721 * @expectedException PHPUnit_Framework_Error |
| 722 */ |
| 723 public function testStringAppendInvalidUTF8Fail() |
| 724 { |
| 725 $arr = new RepeatedField(GPBType::STRING); |
| 726 $hex = hex2bin("ff"); |
| 727 $arr []= $hex; |
| 728 } |
| 729 |
| 730 /** |
| 731 * @expectedException PHPUnit_Framework_Error |
| 732 */ |
| 733 public function testStringSetInvalidUTF8Fail() |
| 734 { |
| 735 $arr = new RepeatedField(GPBType::STRING); |
| 736 $arr []= 'abc'; |
| 737 $hex = hex2bin("ff"); |
| 738 $arr [0]= $hex; |
| 739 } |
| 740 |
| 741 ######################################################### |
| 742 # Test message field. |
| 743 ######################################################### |
| 744 |
| 745 public function testMessage() |
| 746 { |
| 747 $arr = new RepeatedField(GPBType::MESSAGE, TestMessage_Sub::class); |
| 748 |
| 749 // Test append. |
| 750 $sub_m = new TestMessage_Sub(); |
| 751 $sub_m->setA(1); |
| 752 $arr []= $sub_m; |
| 753 $this->assertSame(1, $arr[0]->getA()); |
| 754 |
| 755 $null = null; |
| 756 $arr []= $null; |
| 757 $this->assertNull($arr[1]); |
| 758 |
| 759 $this->assertEquals(2, count($arr)); |
| 760 |
| 761 for ($i = 0; $i < count($arr); $i++) { |
| 762 $arr[$i] = $null; |
| 763 $this->assertNull($arr[$i]); |
| 764 } |
| 765 |
| 766 // Test set. |
| 767 $arr [0]= $sub_m; |
| 768 $this->assertSame(1, $arr[0]->getA()); |
| 769 |
| 770 $arr [1]= $null; |
| 771 $this->assertNull($arr[1]); |
| 772 } |
| 773 |
| 774 /** |
| 775 * @expectedException PHPUnit_Framework_Error |
| 776 */ |
| 777 public function testMessageAppendIntFail() |
| 778 { |
| 779 $arr = new RepeatedField(GPBType::MESSAGE, TestMessage_Sub::class); |
| 780 $arr []= 1; |
| 781 } |
| 782 |
| 783 /** |
| 784 * @expectedException PHPUnit_Framework_Error |
| 785 */ |
| 786 public function testMessageSetIntFail() |
| 787 { |
| 788 $arr = new RepeatedField(GPBType::MESSAGE, TestMessage_Sub::class); |
| 789 $arr []= new TestMessage_Sub; |
| 790 $arr [0]= 'abc'; |
| 791 } |
| 792 |
| 793 /** |
| 794 * @expectedException PHPUnit_Framework_Error |
| 795 */ |
| 796 public function testMessageAppendStringFail() |
| 797 { |
| 798 $arr = new RepeatedField(GPBType::MESSAGE, TestMessage_Sub::class); |
| 799 $arr []= 'abc'; |
| 800 } |
| 801 |
| 802 /** |
| 803 * @expectedException PHPUnit_Framework_Error |
| 804 */ |
| 805 public function testMessageSetStringFail() |
| 806 { |
| 807 $arr = new RepeatedField(GPBType::MESSAGE, TestMessage_Sub::class); |
| 808 $arr []= new TestMessage_Sub; |
| 809 $arr [0]= 'abc'; |
| 810 } |
| 811 |
| 812 /** |
| 813 * @expectedException PHPUnit_Framework_Error |
| 814 */ |
| 815 public function testMessageAppendOtherMessageFail() |
| 816 { |
| 817 $arr = new RepeatedField(GPBType::MESSAGE, TestMessage_Sub::class); |
| 818 $arr []= new TestMessage; |
| 819 } |
| 820 |
| 821 ######################################################### |
| 822 # Test offset type |
| 823 ######################################################### |
| 824 |
| 825 public function testOffset() |
| 826 { |
| 827 $arr = new RepeatedField(GPBType::INT32); |
| 828 $arr []= 0; |
| 829 |
| 830 $arr [0]= 1; |
| 831 $this->assertSame(1, $arr[0]); |
| 832 $this->assertSame(1, count($arr)); |
| 833 |
| 834 $arr ['0']= 2; |
| 835 $this->assertSame(2, $arr['0']); |
| 836 $this->assertSame(2, $arr[0]); |
| 837 $this->assertSame(1, count($arr)); |
| 838 |
| 839 $arr [0.0]= 3; |
| 840 $this->assertSame(3, $arr[0.0]); |
| 841 $this->assertSame(1, count($arr)); |
| 842 } |
| 843 |
| 844 public function testInsertRemoval() |
| 845 { |
| 846 $arr = new RepeatedField(GPBType::INT32); |
| 847 |
| 848 $arr []= 0; |
| 849 $arr []= 1; |
| 850 $arr []= 2; |
| 851 $this->assertSame(3, count($arr)); |
| 852 |
| 853 unset($arr[2]); |
| 854 $this->assertSame(2, count($arr)); |
| 855 $this->assertSame(0, $arr[0]); |
| 856 $this->assertSame(1, $arr[1]); |
| 857 |
| 858 $arr [] = 3; |
| 859 $this->assertSame(3, count($arr)); |
| 860 $this->assertSame(0, $arr[0]); |
| 861 $this->assertSame(1, $arr[1]); |
| 862 $this->assertSame(3, $arr[2]); |
| 863 } |
| 864 |
| 865 /** |
| 866 * @expectedException PHPUnit_Framework_Error |
| 867 */ |
| 868 public function testRemoveMiddleFail() |
| 869 { |
| 870 $arr = new RepeatedField(GPBType::INT32); |
| 871 |
| 872 $arr []= 0; |
| 873 $arr []= 1; |
| 874 $arr []= 2; |
| 875 $this->assertSame(3, count($arr)); |
| 876 |
| 877 unset($arr[1]); |
| 878 } |
| 879 |
| 880 /** |
| 881 * @expectedException PHPUnit_Framework_Error |
| 882 */ |
| 883 public function testRemoveEmptyFail() |
| 884 { |
| 885 $arr = new RepeatedField(GPBType::INT32); |
| 886 |
| 887 unset($arr[0]); |
| 888 } |
| 889 |
| 890 /** |
| 891 * @expectedException PHPUnit_Framework_Error |
| 892 */ |
| 893 public function testMessageOffsetFail() |
| 894 { |
| 895 $arr = new RepeatedField(GPBType::INT32); |
| 896 $arr []= 0; |
| 897 $arr [new TestMessage_Sub()]= 0; |
| 898 } |
| 899 |
| 900 /** |
| 901 * @expectedException PHPUnit_Framework_Error |
| 902 */ |
| 903 public function testStringOffsetFail() |
| 904 { |
| 905 $arr = new RepeatedField(GPBType::INT32); |
| 906 $arr []= 0; |
| 907 $arr ['abc']= 0; |
| 908 } |
| 909 |
| 910 /** |
| 911 * @expectedException PHPUnit_Framework_Error |
| 912 */ |
| 913 public function testSetNonExistedOffsetFail() |
| 914 { |
| 915 $arr = new RepeatedField(GPBType::INT32); |
| 916 $arr [0]= 0; |
| 917 } |
| 918 |
| 919 ######################################################### |
| 920 # Test memory leak |
| 921 ######################################################### |
| 922 |
| 923 public function testCycleLeak() |
| 924 { |
| 925 $arr = new RepeatedField(GPBType::MESSAGE, TestMessage::class); |
| 926 $arr []= new TestMessage; |
| 927 $arr[0]->SetRepeatedRecursive($arr); |
| 928 |
| 929 // Clean up memory before test. |
| 930 gc_collect_cycles(); |
| 931 $start = memory_get_usage(); |
| 932 unset($arr); |
| 933 |
| 934 // Explicitly trigger garbage collection. |
| 935 gc_collect_cycles(); |
| 936 |
| 937 $end = memory_get_usage(); |
| 938 $this->assertLessThan($start, $end); |
| 939 } |
| 940 } |
OLD | NEW |