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