array_test.php 24 KB

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