array_test.php 24 KB

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