map_field_test.php 19 KB

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