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

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

Powered by Google App Engine
This is Rietveld 408576698