array_test.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. <?php
  2. require_once('test_util.php');
  3. use Google\Protobuf\Internal\RepeatedField;
  4. use Google\Protobuf\Internal\GPBType;
  5. use Foo\TestMessage;
  6. use Foo\TestMessage\Sub;
  7. class RepeatedFieldTest extends \PHPUnit\Framework\TestCase
  8. {
  9. #########################################################
  10. # Test int32 field.
  11. #########################################################
  12. public function testInt32()
  13. {
  14. $arr = new RepeatedField(GPBType::INT32);
  15. // Test append.
  16. $arr[] = MAX_INT32;
  17. $this->assertSame(MAX_INT32, $arr[0]);
  18. $arr[] = MIN_INT32;
  19. $this->assertSame(MIN_INT32, $arr[1]);
  20. $arr[] = 1.1;
  21. $this->assertSame(1, $arr[2]);
  22. $arr[] = MAX_INT32_FLOAT;
  23. $this->assertSame(MAX_INT32, $arr[3]);
  24. $arr[] = MAX_INT32_FLOAT;
  25. $this->assertSame(MAX_INT32, $arr[4]);
  26. $arr[] = '2';
  27. $this->assertSame(2, $arr[5]);
  28. $arr[] = '3.1';
  29. $this->assertSame(3, $arr[6]);
  30. $arr[] = MAX_INT32_STRING;
  31. $this->assertSame(MAX_INT32, $arr[7]);
  32. $this->assertEquals(8, count($arr));
  33. for ($i = 0; $i < count($arr); $i++) {
  34. $arr[$i] = 0;
  35. $this->assertSame(0, $arr[$i]);
  36. }
  37. // Test set.
  38. $arr[0] = MAX_INT32;
  39. $this->assertSame(MAX_INT32, $arr[0]);
  40. $arr[1] = MIN_INT32;
  41. $this->assertSame(MIN_INT32, $arr[1]);
  42. $arr[2] = 1.1;
  43. $this->assertSame(1, $arr[2]);
  44. $arr[3] = MAX_INT32_FLOAT;
  45. $this->assertSame(MAX_INT32, $arr[3]);
  46. $arr[4] = MAX_INT32_FLOAT;
  47. $this->assertSame(MAX_INT32, $arr[4]);
  48. $arr[5] = '2';
  49. $this->assertSame(2, $arr[5]);
  50. $arr[6] = '3.1';
  51. $this->assertSame(3, $arr[6]);
  52. $arr[7] = MAX_INT32_STRING;
  53. $this->assertSame(MAX_INT32, $arr[7]);
  54. // Test foreach.
  55. $arr = new RepeatedField(GPBType::INT32);
  56. for ($i = 0; $i < 3; $i++) {
  57. $arr[] = $i;
  58. }
  59. $i = 0;
  60. foreach ($arr as $val) {
  61. $this->assertSame($i++, $val);
  62. }
  63. $this->assertSame(3, $i);
  64. }
  65. #########################################################
  66. # Test uint32 field.
  67. #########################################################
  68. public function testUint32()
  69. {
  70. $arr = new RepeatedField(GPBType::UINT32);
  71. // Test append.
  72. $arr[] = MAX_UINT32;
  73. $this->assertSame(-1, $arr[0]);
  74. $arr[] = -1;
  75. $this->assertSame(-1, $arr[1]);
  76. $arr[] = MIN_UINT32;
  77. $this->assertSame(MIN_UINT32, $arr[2]);
  78. $arr[] = 1.1;
  79. $this->assertSame(1, $arr[3]);
  80. $arr[] = MAX_UINT32_FLOAT;
  81. $this->assertSame(-1, $arr[4]);
  82. $arr[] = -1.0;
  83. $this->assertSame(-1, $arr[5]);
  84. $arr[] = MIN_UINT32_FLOAT;
  85. $this->assertSame(MIN_UINT32, $arr[6]);
  86. $arr[] = '2';
  87. $this->assertSame(2, $arr[7]);
  88. $arr[] = '3.1';
  89. $this->assertSame(3, $arr[8]);
  90. $arr[] = MAX_UINT32_STRING;
  91. $this->assertSame(-1, $arr[9]);
  92. $arr[] = '-1.0';
  93. $this->assertSame(-1, $arr[10]);
  94. $arr[] = MIN_UINT32_STRING;
  95. $this->assertSame(MIN_UINT32, $arr[11]);
  96. $this->assertEquals(12, count($arr));
  97. for ($i = 0; $i < count($arr); $i++) {
  98. $arr[$i] = 0;
  99. $this->assertSame(0, $arr[$i]);
  100. }
  101. // Test set.
  102. $arr[0] = MAX_UINT32;
  103. $this->assertSame(-1, $arr[0]);
  104. $arr[1] = -1;
  105. $this->assertSame(-1, $arr[1]);
  106. $arr[2] = MIN_UINT32;
  107. $this->assertSame(MIN_UINT32, $arr[2]);
  108. $arr[3] = 1.1;
  109. $this->assertSame(1, $arr[3]);
  110. $arr[4] = MAX_UINT32_FLOAT;
  111. $this->assertSame(-1, $arr[4]);
  112. $arr[5] = -1.0;
  113. $this->assertSame(-1, $arr[5]);
  114. $arr[6] = MIN_UINT32_FLOAT;
  115. $this->assertSame(MIN_UINT32, $arr[6]);
  116. $arr[7] = '2';
  117. $this->assertSame(2, $arr[7]);
  118. $arr[8] = '3.1';
  119. $this->assertSame(3, $arr[8]);
  120. $arr[9] = MAX_UINT32_STRING;
  121. $this->assertSame(-1, $arr[9]);
  122. $arr[10] = '-1.0';
  123. $this->assertSame(-1, $arr[10]);
  124. $arr[11] = MIN_UINT32_STRING;
  125. $this->assertSame(MIN_UINT32, $arr[11]);
  126. }
  127. #########################################################
  128. # Test int64 field.
  129. #########################################################
  130. public function testInt64()
  131. {
  132. $arr = new RepeatedField(GPBType::INT64);
  133. // Test append.
  134. $arr[] = MAX_INT64;
  135. $arr[] = MIN_INT64;
  136. $arr[] = 1.1;
  137. $arr[] = '2';
  138. $arr[] = '3.1';
  139. $arr[] = MAX_INT64_STRING;
  140. $arr[] = MIN_INT64_STRING;
  141. if (PHP_INT_SIZE == 4) {
  142. $this->assertSame(MAX_INT64, $arr[0]);
  143. $this->assertSame(MIN_INT64, $arr[1]);
  144. $this->assertSame('1', $arr[2]);
  145. $this->assertSame('2', $arr[3]);
  146. $this->assertSame('3', $arr[4]);
  147. $this->assertSame(MAX_INT64_STRING, $arr[5]);
  148. $this->assertSame(MIN_INT64_STRING, $arr[6]);
  149. } else {
  150. $this->assertSame(MAX_INT64, $arr[0]);
  151. $this->assertSame(MIN_INT64, $arr[1]);
  152. $this->assertSame(1, $arr[2]);
  153. $this->assertSame(2, $arr[3]);
  154. $this->assertSame(3, $arr[4]);
  155. $this->assertSame(MAX_INT64, $arr[5]);
  156. $this->assertSame(MIN_INT64, $arr[6]);
  157. }
  158. $this->assertEquals(7, count($arr));
  159. for ($i = 0; $i < count($arr); $i++) {
  160. $arr[$i] = 0;
  161. if (PHP_INT_SIZE == 4) {
  162. $this->assertSame('0', $arr[$i]);
  163. } else {
  164. $this->assertSame(0, $arr[$i]);
  165. }
  166. }
  167. // Test set.
  168. $arr[0] = MAX_INT64;
  169. $arr[1] = MIN_INT64;
  170. $arr[2] = 1.1;
  171. $arr[3] = '2';
  172. $arr[4] = '3.1';
  173. $arr[5] = MAX_INT64_STRING;
  174. $arr[6] = MIN_INT64_STRING;
  175. if (PHP_INT_SIZE == 4) {
  176. $this->assertSame(MAX_INT64_STRING, $arr[0]);
  177. $this->assertSame(MIN_INT64_STRING, $arr[1]);
  178. $this->assertSame('1', $arr[2]);
  179. $this->assertSame('2', $arr[3]);
  180. $this->assertSame('3', $arr[4]);
  181. $this->assertSame(MAX_INT64_STRING, $arr[5]);
  182. $this->assertEquals(MIN_INT64_STRING, $arr[6]);
  183. } else {
  184. $this->assertSame(MAX_INT64, $arr[0]);
  185. $this->assertSame(MIN_INT64, $arr[1]);
  186. $this->assertSame(1, $arr[2]);
  187. $this->assertSame(2, $arr[3]);
  188. $this->assertSame(3, $arr[4]);
  189. $this->assertSame(MAX_INT64, $arr[5]);
  190. $this->assertEquals(MIN_INT64, $arr[6]);
  191. }
  192. }
  193. #########################################################
  194. # Test uint64 field.
  195. #########################################################
  196. public function testUint64()
  197. {
  198. $arr = new RepeatedField(GPBType::UINT64);
  199. // Test append.
  200. $arr[] = MAX_UINT64;
  201. $arr[] = 1.1;
  202. $arr[] = '2';
  203. $arr[] = '3.1';
  204. $arr[] = MAX_UINT64_STRING;
  205. if (PHP_INT_SIZE == 4) {
  206. $this->assertSame(MAX_UINT64_STRING, $arr[0]);
  207. $this->assertSame('1', $arr[1]);
  208. $this->assertSame('2', $arr[2]);
  209. $this->assertSame('3', $arr[3]);
  210. $this->assertSame(MAX_UINT64_STRING, $arr[4]);
  211. } else {
  212. $this->assertSame(MAX_UINT64, $arr[0]);
  213. $this->assertSame(1, $arr[1]);
  214. $this->assertSame(2, $arr[2]);
  215. $this->assertSame(3, $arr[3]);
  216. $this->assertSame(MAX_UINT64, $arr[4]);
  217. $this->assertSame(5, count($arr));
  218. }
  219. $this->assertSame(5, count($arr));
  220. for ($i = 0; $i < count($arr); $i++) {
  221. $arr[$i] = 0;
  222. if (PHP_INT_SIZE == 4) {
  223. $this->assertSame('0', $arr[$i]);
  224. } else {
  225. $this->assertSame(0, $arr[$i]);
  226. }
  227. }
  228. // Test set.
  229. $arr[0] = MAX_UINT64;
  230. $arr[1] = 1.1;
  231. $arr[2] = '2';
  232. $arr[3] = '3.1';
  233. $arr[4] = MAX_UINT64_STRING;
  234. if (PHP_INT_SIZE == 4) {
  235. $this->assertSame(MAX_UINT64_STRING, $arr[0]);
  236. $this->assertSame('1', $arr[1]);
  237. $this->assertSame('2', $arr[2]);
  238. $this->assertSame('3', $arr[3]);
  239. $this->assertSame(MAX_UINT64_STRING, $arr[4]);
  240. } else {
  241. $this->assertSame(MAX_UINT64, $arr[0]);
  242. $this->assertSame(1, $arr[1]);
  243. $this->assertSame(2, $arr[2]);
  244. $this->assertSame(3, $arr[3]);
  245. $this->assertSame(MAX_UINT64, $arr[4]);
  246. }
  247. }
  248. #########################################################
  249. # Test float field.
  250. #########################################################
  251. public function testFloat()
  252. {
  253. $arr = new RepeatedField(GPBType::FLOAT);
  254. // Test append.
  255. $arr[] = 1;
  256. $this->assertEquals(1.0, $arr[0], '', MAX_FLOAT_DIFF);
  257. $arr[] = 1.1;
  258. $this->assertEquals(1.1, $arr[1], '', MAX_FLOAT_DIFF);
  259. $arr[] = '2';
  260. $this->assertEquals(2.0, $arr[2], '', MAX_FLOAT_DIFF);
  261. $arr[] = '3.1';
  262. $this->assertEquals(3.1, $arr[3], '', MAX_FLOAT_DIFF);
  263. $this->assertEquals(4, count($arr));
  264. for ($i = 0; $i < count($arr); $i++) {
  265. $arr[$i] = 0;
  266. $this->assertSame(0.0, $arr[$i]);
  267. }
  268. // Test set.
  269. $arr[0] = 1;
  270. $this->assertEquals(1.0, $arr[0], '', MAX_FLOAT_DIFF);
  271. $arr[1] = 1.1;
  272. $this->assertEquals(1.1, $arr[1], '', MAX_FLOAT_DIFF);
  273. $arr[2] = '2';
  274. $this->assertEquals(2.0, $arr[2], '', MAX_FLOAT_DIFF);
  275. $arr[3] = '3.1';
  276. $this->assertEquals(3.1, $arr[3], '', MAX_FLOAT_DIFF);
  277. }
  278. #########################################################
  279. # Test double field.
  280. #########################################################
  281. public function testDouble()
  282. {
  283. $arr = new RepeatedField(GPBType::DOUBLE);
  284. // Test append.
  285. $arr[] = 1;
  286. $this->assertEquals(1.0, $arr[0], '', MAX_FLOAT_DIFF);
  287. $arr[] = 1.1;
  288. $this->assertEquals(1.1, $arr[1], '', MAX_FLOAT_DIFF);
  289. $arr[] = '2';
  290. $this->assertEquals(2.0, $arr[2], '', MAX_FLOAT_DIFF);
  291. $arr[] = '3.1';
  292. $this->assertEquals(3.1, $arr[3], '', MAX_FLOAT_DIFF);
  293. $this->assertEquals(4, count($arr));
  294. for ($i = 0; $i < count($arr); $i++) {
  295. $arr[$i] = 0;
  296. $this->assertSame(0.0, $arr[$i]);
  297. }
  298. // Test set.
  299. $arr[0] = 1;
  300. $this->assertEquals(1.0, $arr[0], '', MAX_FLOAT_DIFF);
  301. $arr[1] = 1.1;
  302. $this->assertEquals(1.1, $arr[1], '', MAX_FLOAT_DIFF);
  303. $arr[2] = '2';
  304. $this->assertEquals(2.0, $arr[2], '', MAX_FLOAT_DIFF);
  305. $arr[3] = '3.1';
  306. $this->assertEquals(3.1, $arr[3], '', MAX_FLOAT_DIFF);
  307. }
  308. #########################################################
  309. # Test bool field.
  310. #########################################################
  311. public function testBool()
  312. {
  313. $arr = new RepeatedField(GPBType::BOOL);
  314. // Test append.
  315. $arr[] = true;
  316. $this->assertSame(true, $arr[0]);
  317. $arr[] = -1;
  318. $this->assertSame(true, $arr[1]);
  319. $arr[] = 1.1;
  320. $this->assertSame(true, $arr[2]);
  321. $arr[] = '';
  322. $this->assertSame(false, $arr[3]);
  323. $this->assertEquals(4, count($arr));
  324. for ($i = 0; $i < count($arr); $i++) {
  325. $arr[$i] = 0;
  326. $this->assertSame(false, $arr[$i]);
  327. }
  328. // Test set.
  329. $arr[0] = true;
  330. $this->assertSame(true, $arr[0]);
  331. $arr[1] = -1;
  332. $this->assertSame(true, $arr[1]);
  333. $arr[2] = 1.1;
  334. $this->assertSame(true, $arr[2]);
  335. $arr[3] = '';
  336. $this->assertSame(false, $arr[3]);
  337. }
  338. #########################################################
  339. # Test string field.
  340. #########################################################
  341. public function testString()
  342. {
  343. $arr = new RepeatedField(GPBType::STRING);
  344. // Test append.
  345. $arr[] = 'abc';
  346. $this->assertSame('abc', $arr[0]);
  347. $arr[] = 1;
  348. $this->assertSame('1', $arr[1]);
  349. $arr[] = 1.1;
  350. $this->assertSame('1.1', $arr[2]);
  351. $arr[] = true;
  352. $this->assertSame('1', $arr[3]);
  353. $this->assertEquals(4, count($arr));
  354. for ($i = 0; $i < count($arr); $i++) {
  355. $arr[$i] = '';
  356. $this->assertSame('', $arr[$i]);
  357. }
  358. // Test set.
  359. $arr[0] = 'abc';
  360. $this->assertSame('abc', $arr[0]);
  361. $arr[1] = 1;
  362. $this->assertSame('1', $arr[1]);
  363. $arr[2] = 1.1;
  364. $this->assertSame('1.1', $arr[2]);
  365. $arr[3] = true;
  366. $this->assertSame('1', $arr[3]);
  367. }
  368. #########################################################
  369. # Test message field.
  370. #########################################################
  371. public function testMessage()
  372. {
  373. $arr = new RepeatedField(GPBType::MESSAGE, Sub::class);
  374. // Test append.
  375. $sub_m = new Sub();
  376. $sub_m->setA(1);
  377. $arr[] = $sub_m;
  378. $this->assertSame(1, $arr[0]->getA());
  379. $this->assertEquals(1, count($arr));
  380. // Test set.
  381. $sub_m = new Sub();
  382. $sub_m->setA(2);
  383. $arr[0] = $sub_m;
  384. $this->assertSame(2, $arr[0]->getA());
  385. // Test foreach.
  386. $arr = new RepeatedField(GPBType::MESSAGE, Sub::class);
  387. for ($i = 0; $i < 3; $i++) {
  388. $arr[] = new Sub();
  389. $arr[$i]->setA($i);
  390. }
  391. $i = 0;
  392. foreach ($arr as $val) {
  393. $this->assertSame($i++, $val->getA());
  394. }
  395. $this->assertSame(3, $i);
  396. }
  397. #########################################################
  398. # Test offset type
  399. #########################################################
  400. public function testOffset()
  401. {
  402. $arr = new RepeatedField(GPBType::INT32);
  403. $arr[] = 0;
  404. $arr[0] = 1;
  405. $this->assertSame(1, $arr[0]);
  406. $this->assertSame(1, count($arr));
  407. $arr['0'] = 2;
  408. $this->assertSame(2, $arr['0']);
  409. $this->assertSame(2, $arr[0]);
  410. $this->assertSame(1, count($arr));
  411. $arr[0.0] = 3;
  412. $this->assertSame(3, $arr[0.0]);
  413. $this->assertSame(1, count($arr));
  414. }
  415. public function testInsertRemoval()
  416. {
  417. $arr = new RepeatedField(GPBType::INT32);
  418. $arr[] = 0;
  419. $arr[] = 1;
  420. $arr[] = 2;
  421. $this->assertSame(3, count($arr));
  422. unset($arr[2]);
  423. $this->assertSame(2, count($arr));
  424. $this->assertSame(0, $arr[0]);
  425. $this->assertSame(1, $arr[1]);
  426. $arr[] = 3;
  427. $this->assertSame(3, count($arr));
  428. $this->assertSame(0, $arr[0]);
  429. $this->assertSame(1, $arr[1]);
  430. $this->assertSame(3, $arr[2]);
  431. }
  432. #########################################################
  433. # Test reference in array
  434. #########################################################
  435. public function testArrayElementIsReferenceInSetters()
  436. {
  437. // Bool elements
  438. $values = [true];
  439. array_walk($values, function (&$value) {});
  440. $m = new TestMessage();
  441. $m->setRepeatedBool($values);
  442. // Int32 elements
  443. $values = [1];
  444. array_walk($values, function (&$value) {});
  445. $m = new TestMessage();
  446. $m->setRepeatedInt32($values);
  447. // Double elements
  448. $values = [1.0];
  449. array_walk($values, function (&$value) {});
  450. $m = new TestMessage();
  451. $m->setRepeatedDouble($values);
  452. // String elements
  453. $values = ['a'];
  454. array_walk($values, function (&$value) {});
  455. $m = new TestMessage();
  456. $m->setRepeatedString($values);
  457. // Message elements
  458. $m = new TestMessage();
  459. $subs = [1, 2];
  460. foreach ($subs as &$sub) {
  461. $sub = new Sub(['a' => $sub]);
  462. }
  463. $m->setRepeatedMessage($subs);
  464. }
  465. #########################################################
  466. # Test memory leak
  467. #########################################################
  468. public function testCycleLeak()
  469. {
  470. gc_collect_cycles();
  471. $arr = new RepeatedField(GPBType::MESSAGE, TestMessage::class);
  472. $arr[] = new TestMessage;
  473. $arr[0]->SetRepeatedRecursive($arr);
  474. // Clean up memory before test.
  475. gc_collect_cycles();
  476. $start = memory_get_usage();
  477. unset($arr);
  478. // Explicitly trigger garbage collection.
  479. gc_collect_cycles();
  480. $end = memory_get_usage();
  481. $this->assertLessThan($start, $end);
  482. }
  483. }