Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: third_party/protobuf/php/tests/map_field_test.php

Issue 2495533002: third_party/protobuf: Update to HEAD (83d681ee2c) (Closed)
Patch Set: Update to new HEAD (b7632464b4) + restore GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 <?php
2
3 require_once('test.pb.php');
4 require_once('test_util.php');
5
6 use Google\Protobuf\Internal\GPBType;
7 use Google\Protobuf\Internal\MapField;
8 use Foo\TestMessage;
9 use Foo\TestMessage_Sub;
10
11 class MapFieldTest extends PHPUnit_Framework_TestCase {
12
13 #########################################################
14 # Test int32 field.
15 #########################################################
16
17 public function testInt32() {
18 $arr = new MapField(GPBType::INT32, GPBType::INT32);
19
20 // Test integer argument.
21 $arr[MAX_INT32] = MAX_INT32;
22 $this->assertSame(MAX_INT32, $arr[MAX_INT32]);
23 $arr[MIN_INT32] = MIN_INT32;
24 $this->assertSame(MIN_INT32, $arr[MIN_INT32]);
25 $this->assertEquals(2, count($arr));
26 $this->assertTrue(isset($arr[MAX_INT32]));
27 $this->assertTrue(isset($arr[MIN_INT32]));
28 unset($arr[MAX_INT32]);
29 unset($arr[MIN_INT32]);
30 $this->assertEquals(0, count($arr));
31
32 // Test float argument.
33 $arr[1.9] = 1.9;
34 $arr[2.1] = 2.1;
35 $this->assertSame(1, $arr[1]);
36 $this->assertSame(2, $arr[2]);
37 $arr[MAX_INT32_FLOAT] = MAX_INT32_FLOAT;
38 $this->assertSame(MAX_INT32, $arr[MAX_INT32]);
39 $arr[MIN_INT32_FLOAT] = MIN_INT32_FLOAT;
40 $this->assertSame(MIN_INT32, $arr[MIN_INT32]);
41 $this->assertEquals(4, count($arr));
42 unset($arr[1.9]);
43 unset($arr[2.9]);
44 unset($arr[MAX_INT32_FLOAT]);
45 unset($arr[MIN_INT32_FLOAT]);
46 $this->assertEquals(0, count($arr));
47
48 // Test string argument.
49 $arr['2'] = '2';
50 $this->assertSame(2, $arr[2]);
51 $arr['3.1'] = '3.1';
52 $this->assertSame(3, $arr[3]);
53 $arr[MAX_INT32_STRING] = MAX_INT32_STRING;
54 $this->assertSame(MAX_INT32, $arr[MAX_INT32]);
55 $this->assertEquals(3, count($arr));
56 unset($arr['2']);
57 unset($arr['3.1']);
58 unset($arr[MAX_INT32_STRING]);
59 $this->assertEquals(0, count($arr));
60 }
61
62 /**
63 * @expectedException PHPUnit_Framework_Error
64 */
65 public function testInt32SetStringKeyFail()
66 {
67 $arr = new MapField(GPBType::INT32, GPBType::INT32);
68 $arr ['abc']= 0;
69 }
70
71 /**
72 * @expectedException PHPUnit_Framework_Error
73 */
74 public function testInt32SetStringValueFail()
75 {
76 $arr = new MapField(GPBType::INT32, GPBType::INT32);
77 $arr [0]= 'abc';
78 }
79
80 /**
81 * @expectedException PHPUnit_Framework_Error
82 */
83 public function testInt32SetMessageKeyFail()
84 {
85 $arr = new MapField(GPBType::INT32, GPBType::INT32);
86 $arr [new TestMessage_Sub()]= 0;
87 }
88
89 /**
90 * @expectedException PHPUnit_Framework_Error
91 */
92 public function testInt32SetMessageValueFail()
93 {
94 $arr = new MapField(GPBType::INT32, GPBType::INT32);
95 $arr [0]= new TestMessage_Sub();
96 }
97
98 #########################################################
99 # Test uint32 field.
100 #########################################################
101
102 public function testUint32() {
103 $arr = new MapField(GPBType::UINT32, GPBType::UINT32);
104
105 // Test integer argument.
106 $arr[MAX_UINT32] = MAX_UINT32;
107 $this->assertSame(-1, $arr[-1]);
108 $this->assertEquals(1, count($arr));
109 unset($arr[MAX_UINT32]);
110 $this->assertEquals(0, count($arr));
111
112 $arr[-1] = -1;
113 $this->assertSame(-1, $arr[-1]);
114 $arr[MIN_UINT32] = MIN_UINT32;
115 $this->assertSame(MIN_UINT32, $arr[MIN_UINT32]);
116 $this->assertEquals(2, count($arr));
117 unset($arr[-1]);
118 unset($arr[MIN_UINT32]);
119 $this->assertEquals(0, count($arr));
120
121 // Test float argument.
122 $arr[MAX_UINT32_FLOAT] = MAX_UINT32_FLOAT;
123 $this->assertSame(-1, $arr[-1]);
124 $this->assertEquals(1, count($arr));
125 unset($arr[MAX_UINT32_FLOAT]);
126 $this->assertEquals(0, count($arr));
127
128 $arr[3.1] = 3.1;
129 $this->assertSame(3, $arr[3]);
130 $arr[-1.0] = -1.0;
131 $this->assertSame(-1, $arr[-1]);
132 $arr[MIN_UINT32_FLOAT] = MIN_UINT32_FLOAT;
133 $this->assertSame(MIN_UINT32, $arr[MIN_UINT32]);
134 $this->assertEquals(3, count($arr));
135 unset($arr[3.1]);
136 unset($arr[-1.0]);
137 unset($arr[MIN_UINT32_FLOAT]);
138 $this->assertEquals(0, count($arr));
139
140 // Test string argument.
141 $arr[MAX_UINT32_STRING] = MAX_UINT32_STRING;
142 $this->assertSame(-1, $arr[-1]);
143 $this->assertEquals(1, count($arr));
144 unset($arr[MAX_UINT32_STRING]);
145 $this->assertEquals(0, count($arr));
146
147 $arr['7'] = '7';
148 $this->assertSame(7, $arr[7]);
149 $arr['3.1'] = '3.1';
150 $this->assertSame(3, $arr[3]);
151 $arr['-1.0'] = '-1.0';
152 $this->assertSame(-1, $arr[-1]);
153 $arr[MIN_UINT32_STRING] = MIN_UINT32_STRING;
154 $this->assertSame(MIN_UINT32, $arr[MIN_UINT32]);
155 $this->assertEquals(4, count($arr));
156 unset($arr['7']);
157 unset($arr['3.1']);
158 unset($arr['-1.0']);
159 unset($arr[MIN_UINT32_STRING]);
160 $this->assertEquals(0, count($arr));
161 }
162
163 /**
164 * @expectedException PHPUnit_Framework_Error
165 */
166 public function testUint32SetStringKeyFail()
167 {
168 $arr = new MapField(GPBType::UINT32, GPBType::UINT32);
169 $arr ['abc']= 0;
170 }
171
172 /**
173 * @expectedException PHPUnit_Framework_Error
174 */
175 public function testUint32SetStringValueFail()
176 {
177 $arr = new MapField(GPBType::UINT32, GPBType::UINT32);
178 $arr [0]= 'abc';
179 }
180
181 /**
182 * @expectedException PHPUnit_Framework_Error
183 */
184 public function testUint32SetMessageKeyFail()
185 {
186 $arr = new MapField(GPBType::UINT32, GPBType::UINT32);
187 $arr [new TestMessage_Sub()]= 0;
188 }
189
190 /**
191 * @expectedException PHPUnit_Framework_Error
192 */
193 public function testUint32SetMessageValueFail()
194 {
195 $arr = new MapField(GPBType::UINT32, GPBType::UINT32);
196 $arr [0]= new TestMessage_Sub();
197 }
198
199 #########################################################
200 # Test int64 field.
201 #########################################################
202
203 public function testInt64() {
204 $arr = new MapField(GPBType::INT64, GPBType::INT64);
205
206 // Test integer argument.
207 $arr[MAX_INT64] = MAX_INT64;
208 $arr[MIN_INT64] = MIN_INT64;
209 if (PHP_INT_SIZE == 4) {
210 $this->assertSame(MAX_INT64_STRING, $arr[MAX_INT64_STRING]);
211 $this->assertSame(MIN_INT64_STRING, $arr[MIN_INT64_STRING]);
212 } else {
213 $this->assertSame(MAX_INT64, $arr[MAX_INT64]);
214 $this->assertSame(MIN_INT64, $arr[MIN_INT64]);
215 }
216 $this->assertEquals(2, count($arr));
217 unset($arr[MAX_INT64]);
218 unset($arr[MIN_INT64]);
219 $this->assertEquals(0, count($arr));
220
221 // Test float argument.
222 $arr[1.1] = 1.1;
223 if (PHP_INT_SIZE == 4) {
224 $this->assertSame('1', $arr['1']);
225 } else {
226 $this->assertSame(1, $arr[1]);
227 }
228 $this->assertEquals(1, count($arr));
229 unset($arr[1.1]);
230 $this->assertEquals(0, count($arr));
231
232 // Test string argument.
233 $arr['2'] = '2';
234 $arr['3.1'] = '3.1';
235 $arr[MAX_INT64_STRING] = MAX_INT64_STRING;
236 $arr[MIN_INT64_STRING] = MIN_INT64_STRING;
237 if (PHP_INT_SIZE == 4) {
238 $this->assertSame('2', $arr['2']);
239 $this->assertSame('3', $arr['3']);
240 $this->assertSame(MAX_INT64_STRING, $arr[MAX_INT64_STRING]);
241 $this->assertSame(MIN_INT64_STRING, $arr[MIN_INT64_STRING]);
242 } else {
243 $this->assertSame(2, $arr[2]);
244 $this->assertSame(3, $arr[3]);
245 $this->assertSame(MAX_INT64, $arr[MAX_INT64]);
246 $this->assertSame(MIN_INT64, $arr[MIN_INT64]);
247 }
248 $this->assertEquals(4, count($arr));
249 unset($arr['2']);
250 unset($arr['3.1']);
251 unset($arr[MAX_INT64_STRING]);
252 unset($arr[MIN_INT64_STRING]);
253 $this->assertEquals(0, count($arr));
254 }
255
256 /**
257 * @expectedException PHPUnit_Framework_Error
258 */
259 public function testInt64SetStringKeyFail()
260 {
261 $arr = new MapField(GPBType::INT64, GPBType::INT64);
262 $arr ['abc']= 0;
263 }
264
265 /**
266 * @expectedException PHPUnit_Framework_Error
267 */
268 public function testInt64SetStringValueFail()
269 {
270 $arr = new MapField(GPBType::INT64, GPBType::INT64);
271 $arr [0]= 'abc';
272 }
273
274 /**
275 * @expectedException PHPUnit_Framework_Error
276 */
277 public function testInt64SetMessageKeyFail()
278 {
279 $arr = new MapField(GPBType::INT64, GPBType::INT64);
280 $arr [new TestMessage_Sub()]= 0;
281 }
282
283 /**
284 * @expectedException PHPUnit_Framework_Error
285 */
286 public function testInt64SetMessageValueFail()
287 {
288 $arr = new MapField(GPBType::INT64, GPBType::INT64);
289 $arr [0]= new TestMessage_Sub();
290 }
291
292 #########################################################
293 # Test uint64 field.
294 #########################################################
295
296 public function testUint64() {
297 $arr = new MapField(GPBType::UINT64, GPBType::UINT64);
298
299 // Test integer argument.
300 $arr[MAX_UINT64] = MAX_UINT64;
301 if (PHP_INT_SIZE == 4) {
302 $this->assertSame(MAX_UINT64_STRING, $arr[MAX_UINT64_STRING]);
303 } else {
304 $this->assertSame(MAX_UINT64, $arr[MAX_UINT64]);
305 }
306 $this->assertEquals(1, count($arr));
307 unset($arr[MAX_UINT64]);
308 $this->assertEquals(0, count($arr));
309
310 // Test float argument.
311 $arr[1.1] = 1.1;
312 if (PHP_INT_SIZE == 4) {
313 $this->assertSame('1', $arr['1']);
314 } else {
315 $this->assertSame(1, $arr[1]);
316 }
317 $this->assertEquals(1, count($arr));
318 unset($arr[1.1]);
319 $this->assertEquals(0, count($arr));
320
321 // Test string argument.
322 $arr['2'] = '2';
323 $arr['3.1'] = '3.1';
324 $arr[MAX_UINT64_STRING] = MAX_UINT64_STRING;
325
326 if (PHP_INT_SIZE == 4) {
327 $this->assertSame('2', $arr['2']);
328 $this->assertSame('3', $arr['3']);
329 $this->assertSame(MAX_UINT64_STRING, $arr[MAX_UINT64_STRING]);
330 } else {
331 $this->assertSame(2, $arr[2]);
332 $this->assertSame(3, $arr[3]);
333 $this->assertSame(MAX_UINT64, $arr[MAX_UINT64]);
334 }
335
336 $this->assertEquals(3, count($arr));
337 unset($arr['2']);
338 unset($arr['3.1']);
339 unset($arr[MAX_UINT64_STRING]);
340 $this->assertEquals(0, count($arr));
341 }
342
343 /**
344 * @expectedException PHPUnit_Framework_Error
345 */
346 public function testUint64SetStringKeyFail()
347 {
348 $arr = new MapField(GPBType::UINT64, GPBType::UINT64);
349 $arr ['abc']= 0;
350 }
351
352 /**
353 * @expectedException PHPUnit_Framework_Error
354 */
355 public function testUint64SetStringValueFail()
356 {
357 $arr = new MapField(GPBType::UINT64, GPBType::UINT64);
358 $arr [0]= 'abc';
359 }
360
361 /**
362 * @expectedException PHPUnit_Framework_Error
363 */
364 public function testUint64SetMessageKeyFail()
365 {
366 $arr = new MapField(GPBType::UINT64, GPBType::UINT64);
367 $arr [new TestMessage_Sub()]= 0;
368 }
369
370 /**
371 * @expectedException PHPUnit_Framework_Error
372 */
373 public function testUint64SetMessageValueFail()
374 {
375 $arr = new MapField(GPBType::UINT64, GPBType::UINT64);
376 $arr [0]= new TestMessage_Sub();
377 }
378
379 #########################################################
380 # Test float field.
381 #########################################################
382
383 public function testFloat() {
384 $arr = new MapField(GPBType::INT32, GPBType::FLOAT);
385
386 // Test set.
387 $arr[0] = 1;
388 $this->assertEquals(1.0, $arr[0], '', MAX_FLOAT_DIFF);
389
390 $arr[1] = 1.1;
391 $this->assertEquals(1.1, $arr[1], '', MAX_FLOAT_DIFF);
392
393 $arr[2] = '2';
394 $this->assertEquals(2.0, $arr[2], '', MAX_FLOAT_DIFF);
395 $arr[3] = '3.1';
396 $this->assertEquals(3.1, $arr[3], '', MAX_FLOAT_DIFF);
397
398 $this->assertEquals(4, count($arr));
399 }
400
401 /**
402 * @expectedException PHPUnit_Framework_Error
403 */
404 public function testFloatSetStringValueFail()
405 {
406 $arr = new MapField(GPBType::INT64, GPBType::FLOAT);
407 $arr [0]= 'abc';
408 }
409
410 /**
411 * @expectedException PHPUnit_Framework_Error
412 */
413 public function testFloatSetMessageValueFail()
414 {
415 $arr = new MapField(GPBType::INT64, GPBType::FLOAT);
416 $arr [0]= new TestMessage_Sub();
417 }
418
419 #########################################################
420 # Test double field.
421 #########################################################
422
423 public function testDouble() {
424 $arr = new MapField(GPBType::INT32, GPBType::DOUBLE);
425
426 // Test set.
427 $arr[0] = 1;
428 $this->assertEquals(1.0, $arr[0], '', MAX_FLOAT_DIFF);
429
430 $arr[1] = 1.1;
431 $this->assertEquals(1.1, $arr[1], '', MAX_FLOAT_DIFF);
432
433 $arr[2] = '2';
434 $this->assertEquals(2.0, $arr[2], '', MAX_FLOAT_DIFF);
435 $arr[3] = '3.1';
436 $this->assertEquals(3.1, $arr[3], '', MAX_FLOAT_DIFF);
437
438 $this->assertEquals(4, count($arr));
439 }
440
441 /**
442 * @expectedException PHPUnit_Framework_Error
443 */
444 public function testDoubleSetStringValueFail()
445 {
446 $arr = new MapField(GPBType::INT64, GPBType::DOUBLE);
447 $arr [0]= 'abc';
448 }
449
450 /**
451 * @expectedException PHPUnit_Framework_Error
452 */
453 public function testDoubleSetMessageValueFail()
454 {
455 $arr = new MapField(GPBType::INT64, GPBType::DOUBLE);
456 $arr [0]= new TestMessage_Sub();
457 }
458
459 #########################################################
460 # Test bool field.
461 #########################################################
462
463 public function testBool() {
464 $arr = new MapField(GPBType::BOOL, GPBType::BOOL);
465
466 // Test boolean.
467 $arr[True] = True;
468 $this->assertSame(True, $arr[True]);
469 $this->assertEquals(1, count($arr));
470 unset($arr[True]);
471 $this->assertEquals(0, count($arr));
472
473 $arr[False] = False;
474 $this->assertSame(False, $arr[False]);
475 $this->assertEquals(1, count($arr));
476 unset($arr[False]);
477 $this->assertEquals(0, count($arr));
478
479 // Test integer.
480 $arr[-1] = -1;
481 $this->assertSame(True, $arr[True]);
482 $this->assertEquals(1, count($arr));
483 unset($arr[-1]);
484 $this->assertEquals(0, count($arr));
485
486 $arr[0] = 0;
487 $this->assertSame(False, $arr[False]);
488 $this->assertEquals(1, count($arr));
489 unset($arr[0]);
490 $this->assertEquals(0, count($arr));
491
492 // Test float.
493 $arr[1.1] = 1.1;
494 $this->assertSame(True, $arr[True]);
495 $this->assertEquals(1, count($arr));
496 unset($arr[1.1]);
497 $this->assertEquals(0, count($arr));
498
499 $arr[0.0] = 0.0;
500 $this->assertSame(False, $arr[False]);
501 $this->assertEquals(1, count($arr));
502 unset($arr[0.0]);
503 $this->assertEquals(0, count($arr));
504
505 // Test string.
506 $arr['a'] = 'a';
507 $this->assertSame(True, $arr[True]);
508 $this->assertEquals(1, count($arr));
509 unset($arr['a']);
510 $this->assertEquals(0, count($arr));
511
512 $arr[''] = '';
513 $this->assertSame(False, $arr[False]);
514 $this->assertEquals(1, count($arr));
515 unset($arr['']);
516 $this->assertEquals(0, count($arr));
517 }
518
519 /**
520 * @expectedException PHPUnit_Framework_Error
521 */
522 public function testBoolSetMessageKeyFail()
523 {
524 $arr = new MapField(GPBType::BOOL, GPBType::BOOL);
525 $arr [new TestMessage_Sub()]= true;
526 }
527
528 /**
529 * @expectedException PHPUnit_Framework_Error
530 */
531 public function testBoolSetMessageValueFail()
532 {
533 $arr = new MapField(GPBType::BOOL, GPBType::BOOL);
534 $arr [true]= new TestMessage_Sub();
535 }
536
537 #########################################################
538 # Test string field.
539 #########################################################
540
541 public function testString() {
542 $arr = new MapField(GPBType::STRING, GPBType::STRING);
543
544 // Test set.
545 $arr['abc'] = 'abc';
546 $this->assertSame('abc', $arr['abc']);
547 $this->assertEquals(1, count($arr));
548 unset($arr['abc']);
549 $this->assertEquals(0, count($arr));
550
551 $arr[1] = 1;
552 $this->assertSame('1', $arr['1']);
553 $this->assertEquals(1, count($arr));
554 unset($arr[1]);
555 $this->assertEquals(0, count($arr));
556
557 $arr[1.1] = 1.1;
558 $this->assertSame('1.1', $arr['1.1']);
559 $this->assertEquals(1, count($arr));
560 unset($arr[1.1]);
561 $this->assertEquals(0, count($arr));
562
563 $arr[True] = True;
564 $this->assertSame('1', $arr['1']);
565 $this->assertEquals(1, count($arr));
566 unset($arr[True]);
567 $this->assertEquals(0, count($arr));
568 }
569
570 /**
571 * @expectedException PHPUnit_Framework_Error
572 */
573 public function testStringSetInvalidUTF8KeyFail()
574 {
575 $arr = new MapField(GPBType::STRING, GPBType::STRING);
576 $arr[hex2bin("ff")]= 'abc';
577 }
578
579 /**
580 * @expectedException PHPUnit_Framework_Error
581 */
582 public function testStringSetInvalidUTF8ValueFail()
583 {
584 $arr = new MapField(GPBType::STRING, GPBType::STRING);
585 $arr ['abc']= hex2bin("ff");
586 }
587
588 /**
589 * @expectedException PHPUnit_Framework_Error
590 */
591 public function testStringSetMessageKeyFail()
592 {
593 $arr = new MapField(GPBType::STRING, GPBType::STRING);
594 $arr [new TestMessage_Sub()]= 'abc';
595 }
596
597 /**
598 * @expectedException PHPUnit_Framework_Error
599 */
600 public function testStringSetMessageValueFail()
601 {
602 $arr = new MapField(GPBType::STRING, GPBType::STRING);
603 $arr ['abc']= new TestMessage_Sub();
604 }
605
606 #########################################################
607 # Test message field.
608 #########################################################
609
610 public function testMessage() {
611 $arr = new MapField(GPBType::INT32,
612 GPBType::MESSAGE, TestMessage_Sub::class);
613
614 // Test append.
615 $sub_m = new TestMessage_Sub();
616 $sub_m->setA(1);
617 $arr[0] = $sub_m;
618 $this->assertSame(1, $arr[0]->getA());
619
620 $null = NULL;
621 $arr[1] = $null;
622 $this->assertNull($arr[1]);
623
624 $this->assertEquals(2, count($arr));
625 }
626
627 /**
628 * @expectedException PHPUnit_Framework_Error
629 */
630 public function testMessageSetIntValueFail()
631 {
632 $arr =
633 new MapField(GPBType::INT32, GPBType::MESSAGE, TestMessage::class);
634 $arr[0] = 0;
635 }
636
637 /**
638 * @expectedException PHPUnit_Framework_Error
639 */
640 public function testMessageSetStringValueFail()
641 {
642 $arr =
643 new MapField(GPBType::INT32, GPBType::MESSAGE, TestMessage::class);
644 $arr[0] = 'abc';
645 }
646
647 /**
648 * @expectedException PHPUnit_Framework_Error
649 */
650 public function testMessageSetOtherMessageValueFail()
651 {
652 $arr =
653 new MapField(GPBType::INT32, GPBType::MESSAGE, TestMessage::class);
654 $arr[0] = new TestMessage_Sub();
655 }
656
657 #########################################################
658 # Test memory leak
659 #########################################################
660
661 // TODO(teboring): Add it back.
662 // public function testCycleLeak()
663 // {
664 // $arr = new MapField(GPBType::INT32,
665 // GPBType::MESSAGE, TestMessage::class);
666 // $arr [0]= new TestMessage;
667 // $arr[0]->SetMapRecursive($arr);
668
669 // // Clean up memory before test.
670 // gc_collect_cycles();
671 // $start = memory_get_usage();
672 // unset($arr);
673
674 // // Explicitly trigger garbage collection.
675 // gc_collect_cycles();
676
677 // $end = memory_get_usage();
678 // $this->assertLessThan($start, $end);
679 // }
680 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698