PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/corlib/Test/System.Collections/NewArrayListTest.cs

https://bitbucket.org/danipen/mono
C# | 1414 lines | 1064 code | 281 blank | 69 comment | 55 complexity | 97926cfe9749501c481b37447c751712 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. // NewArrayListTest.cs
  2. //
  3. // Unit tests for System.Collections.ArrayList
  4. //
  5. // Copyright (c) 2003 Thong (Tum) Nguyen [tum@veridicus.com]
  6. //
  7. // Released under the MIT License:
  8. //
  9. // http://www.opensource.org/licenses/mit-license.html
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  12. // software and associated documentation files (the "Software"), to deal in the
  13. // Software without restriction, including without limitation the rights to use, copy,
  14. // modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
  15. // and to permit persons to whom the Software is furnished to do so, subject to the
  16. // following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be included in all copies
  19. // or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  22. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  24. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  25. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  26. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  27. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. // Author's comment: Source code formatting has been changed by request to match
  30. // Mono's formatting style. I personally use BSD-style formatting.
  31. //
  32. using System;
  33. using System.Collections;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Collections
  36. {
  37. /// <summary>
  38. /// Some test cases for the new ArrayList implementation.
  39. /// </summary>
  40. [TestFixture]
  41. public class NewArrayListTest
  42. {
  43. private object[] c_TestData = new Object[] {0,1,2,3,4,5,6,7,8,9};
  44. private void VerifyContains(IList list, IList values, string message)
  45. {
  46. if (values.Count != list.Count)
  47. {
  48. Assert.Fail (message);
  49. }
  50. for (int i = 0; i < list.Count; i++)
  51. {
  52. if (list[i] == null && values[i] == null)
  53. {
  54. continue;
  55. }
  56. if ((list[i] == null || values[i] == null) || !list[i].Equals(values[i]))
  57. {
  58. Assert.Fail (message);
  59. }
  60. }
  61. }
  62. private void PrivateTestSort(ArrayList arrayList)
  63. {
  64. Random random = new Random(1027);
  65. // Sort arrays of lengths up to 200
  66. for (int i = 1; i < 200; i++)
  67. {
  68. for (int j = 0; j < i; j++)
  69. {
  70. arrayList.Add(random.Next(0, 1000));
  71. }
  72. arrayList.Sort();
  73. for (int j = 1; j < i; j++)
  74. {
  75. if ((int)arrayList[j] < (int)arrayList[j - 1])
  76. {
  77. Assert.Fail("ArrayList.Sort()");
  78. return;
  79. }
  80. }
  81. arrayList.Clear();
  82. }
  83. }
  84. [Test]
  85. public void TestSortStandard()
  86. {
  87. PrivateTestSort(new ArrayList());
  88. }
  89. [Test]
  90. public void TestSortSynchronized()
  91. {
  92. PrivateTestSort(ArrayList.Synchronized(new ArrayList()));
  93. }
  94. [Test]
  95. public void TestSortAdapter()
  96. {
  97. PrivateTestSort(ArrayList.Adapter(new ArrayList()));
  98. }
  99. [Test]
  100. public void TestSortGetRange()
  101. {
  102. PrivateTestSort(new ArrayList().GetRange(0, 0));
  103. }
  104. private void PrivateTestIndexOf(ArrayList arrayList)
  105. {
  106. int x;
  107. arrayList.AddRange(c_TestData);
  108. for (int i = 0; i < 10; i++)
  109. {
  110. x = arrayList.IndexOf(i);
  111. Assert.IsTrue(x == i, "ArrayList.IndexOf(" + i + ")");
  112. }
  113. try
  114. {
  115. arrayList.IndexOf(0, 10, 1);
  116. Assert.Fail("ArrayList.IndexOf(0, 10, 1)");
  117. }
  118. catch (ArgumentOutOfRangeException)
  119. {
  120. }
  121. try
  122. {
  123. arrayList.IndexOf(0, 0, -1);
  124. Assert.Fail("ArrayList.IndexOf(0, 10, 1)");
  125. }
  126. catch (ArgumentOutOfRangeException)
  127. {
  128. }
  129. try
  130. {
  131. arrayList.IndexOf(0, -1, -1);
  132. Assert.Fail("ArrayList.IndexOf(0, 10, 1)");
  133. }
  134. catch (ArgumentOutOfRangeException)
  135. {
  136. }
  137. try
  138. {
  139. arrayList.IndexOf(0, 9, 10);
  140. Assert.Fail("ArrayList.IndexOf(0, 10, 1)");
  141. }
  142. catch (ArgumentOutOfRangeException)
  143. {
  144. }
  145. try
  146. {
  147. arrayList.IndexOf(0, 0, 10);
  148. }
  149. catch (ArgumentOutOfRangeException)
  150. {
  151. Assert.Fail("ArrayList.IndexOf(0, 10, 1)");
  152. }
  153. try
  154. {
  155. arrayList.IndexOf(0, 0, 11);
  156. Assert.Fail("ArrayList.IndexOf(0, 10, 1)");
  157. }
  158. catch (ArgumentOutOfRangeException)
  159. {
  160. }
  161. // LastIndexOf
  162. for (int i = 0; i < 10; i++)
  163. {
  164. x = arrayList.LastIndexOf(i);
  165. Assert.IsTrue(x == i, "ArrayList.LastIndexOf(" + i + ")");
  166. }
  167. try
  168. {
  169. arrayList.IndexOf(0, 10, 1);
  170. Assert.Fail("ArrayList.LastIndexOf(0, 10, 1)");
  171. }
  172. catch (ArgumentOutOfRangeException)
  173. {
  174. }
  175. try
  176. {
  177. arrayList.IndexOf(0, 0, -1);
  178. Assert.Fail("ArrayList.LastIndexOf(0, 10, 1)");
  179. }
  180. catch (ArgumentOutOfRangeException)
  181. {
  182. }
  183. try
  184. {
  185. arrayList.LastIndexOf(0, -1, -1);
  186. Assert.Fail("ArrayList.LastIndexOf(0, 10, 1)");
  187. }
  188. catch (ArgumentOutOfRangeException)
  189. {
  190. }
  191. try
  192. {
  193. arrayList.LastIndexOf(0, 9, 10);
  194. }
  195. catch (ArgumentOutOfRangeException)
  196. {
  197. Assert.Fail("ArrayList.LastIndexOf(0, 10, 1)");
  198. }
  199. try
  200. {
  201. arrayList.LastIndexOf(0, 0, 10);
  202. Assert.Fail("ArrayList.LastIndexOf(0, 10, 1)");
  203. }
  204. catch (ArgumentOutOfRangeException)
  205. {
  206. }
  207. try
  208. {
  209. arrayList.LastIndexOf(0, 0, 11);
  210. Assert.Fail("ArrayList.LastIndexOf(0, 10, 1)");
  211. }
  212. catch (ArgumentOutOfRangeException)
  213. {
  214. }
  215. }
  216. private void PrivateTestAddRange(ArrayList arrayList)
  217. {
  218. arrayList.AddRange(c_TestData);
  219. arrayList.AddRange(c_TestData);
  220. VerifyContains(arrayList, new object[] {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9}, "VerifyContains");
  221. }
  222. [Test]
  223. public void TestAddRangeStandard()
  224. {
  225. PrivateTestAddRange(new ArrayList());
  226. }
  227. [Test]
  228. public void TestAddRangeSynchronized()
  229. {
  230. PrivateTestAddRange(ArrayList.Synchronized(new ArrayList()));
  231. }
  232. [Test]
  233. public void TestAddRangeAdapter()
  234. {
  235. PrivateTestAddRange(ArrayList.Adapter(new ArrayList()));
  236. }
  237. [Test]
  238. public void TestAddRangeGetRange()
  239. {
  240. PrivateTestAddRange(new ArrayList().GetRange(0, 0));
  241. }
  242. [Test]
  243. public void TestIndexOfStandard()
  244. {
  245. PrivateTestIndexOf(new ArrayList());
  246. }
  247. [Test]
  248. public void TestIndexOfSynchronized()
  249. {
  250. PrivateTestIndexOf(ArrayList.Synchronized(new ArrayList()));
  251. }
  252. [Test]
  253. public void TestIndexOfAdapter()
  254. {
  255. PrivateTestIndexOf(ArrayList.Adapter(new ArrayList()));
  256. }
  257. [Test]
  258. public void TestIndexOfGetRange()
  259. {
  260. PrivateTestIndexOf(new ArrayList().GetRange(0, 0));
  261. }
  262. [Test]
  263. public void TestReadOnly()
  264. {
  265. ArrayList arrayList, readOnlyList;
  266. arrayList = new ArrayList();
  267. readOnlyList = ArrayList.ReadOnly(arrayList);
  268. arrayList.AddRange(c_TestData);
  269. // Make sure the readOnlyList is a wrapper and not a clone.
  270. arrayList.Add(10);
  271. Assert.IsTrue(readOnlyList.Count == 11, "readOnlyList.Count == 11");
  272. try
  273. {
  274. readOnlyList.Add(0);
  275. Assert.Fail("readOnlyList.Add(0)");
  276. }
  277. catch (NotSupportedException)
  278. {
  279. }
  280. try
  281. {
  282. readOnlyList.AddRange(c_TestData);
  283. Assert.Fail("readOnlyList.AddRange(c_TestData)");
  284. }
  285. catch (NotSupportedException)
  286. {
  287. }
  288. try
  289. {
  290. readOnlyList.BinarySearch(1);
  291. }
  292. catch (NotSupportedException)
  293. {
  294. Assert.Fail("readOnlyList.BinarySearch(1)");
  295. }
  296. try
  297. {
  298. int x = readOnlyList.Capacity;
  299. }
  300. catch (NotSupportedException)
  301. {
  302. Assert.Fail("readOnlyList.Capacity");
  303. }
  304. try
  305. {
  306. readOnlyList.Clear();
  307. Assert.Fail("readOnlyList.Clear()");
  308. }
  309. catch (NotSupportedException)
  310. {
  311. }
  312. try
  313. {
  314. readOnlyList.Clone();
  315. }
  316. catch (NotSupportedException)
  317. {
  318. Assert.Fail("readOnlyList.Clone()");
  319. }
  320. try
  321. {
  322. readOnlyList.Contains(1);
  323. }
  324. catch (NotSupportedException)
  325. {
  326. Assert.Fail("readOnlyList.Contains");
  327. }
  328. try
  329. {
  330. readOnlyList.CopyTo(new object[readOnlyList.Count]);
  331. }
  332. catch (NotSupportedException)
  333. {
  334. Assert.Fail("readOnlyList.CopyTo(new Array(readOnlyList.Count))");
  335. }
  336. try
  337. {
  338. foreach (object o in readOnlyList)
  339. {
  340. o.ToString();
  341. }
  342. }
  343. catch (NotSupportedException)
  344. {
  345. Assert.Fail("readOnlyList.GetEnumerator()");
  346. }
  347. try
  348. {
  349. readOnlyList.GetRange(0, 1);
  350. }
  351. catch (NotSupportedException)
  352. {
  353. Assert.Fail("readOnlyList.GetRange(0, 1)");
  354. }
  355. try
  356. {
  357. readOnlyList.IndexOf(1);
  358. }
  359. catch (NotSupportedException)
  360. {
  361. Assert.Fail("readOnlyList.readOnlyList.IndexOf(1)");
  362. }
  363. try
  364. {
  365. readOnlyList[0] = 0;
  366. Assert.Fail("readOnlyList[0] = 0");
  367. }
  368. catch (NotSupportedException)
  369. {
  370. }
  371. try
  372. {
  373. readOnlyList.IndexOf(0);
  374. }
  375. catch (NotSupportedException)
  376. {
  377. Assert.Fail("readOnlyList.IndexOf(0)");
  378. }
  379. try
  380. {
  381. readOnlyList.InsertRange(0, new object[] {1,2});
  382. Assert.Fail("readOnlyList.InsertRange(0, new object[] {1,2})");
  383. }
  384. catch (NotSupportedException)
  385. {
  386. }
  387. try
  388. {
  389. readOnlyList.LastIndexOf(1111);
  390. }
  391. catch (NotSupportedException)
  392. {
  393. Assert.Fail("readOnlyList.LastIndexOf(1)");
  394. }
  395. try
  396. {
  397. readOnlyList.Remove(1);
  398. Assert.Fail("readOnlyList.Remove(1)");
  399. }
  400. catch (NotSupportedException)
  401. {
  402. }
  403. try
  404. {
  405. readOnlyList.RemoveAt(1);
  406. Assert.Fail("readOnlyList.RemoveAt(1)");
  407. }
  408. catch (NotSupportedException)
  409. {
  410. }
  411. try
  412. {
  413. readOnlyList.RemoveRange(0, 1);
  414. Assert.Fail("readOnlyList.RemoveRange(0, 1)");
  415. }
  416. catch (NotSupportedException)
  417. {
  418. }
  419. try
  420. {
  421. readOnlyList.Reverse();
  422. Assert.Fail("readOnlyList.Reverse()");
  423. }
  424. catch (NotSupportedException)
  425. {
  426. }
  427. try
  428. {
  429. readOnlyList.SetRange(0, new Object[] {0, 1});
  430. Assert.Fail("readOnlyList.SetRange(0, new Object[] {0, 1})");
  431. }
  432. catch (NotSupportedException)
  433. {
  434. }
  435. try
  436. {
  437. readOnlyList.Sort();
  438. Assert.Fail("readOnlyList.Sort()");
  439. }
  440. catch (NotSupportedException)
  441. {
  442. }
  443. try
  444. {
  445. readOnlyList.ToArray();
  446. }
  447. catch (NotSupportedException)
  448. {
  449. Assert.Fail("readOnlyList.ToArray()");
  450. }
  451. try
  452. {
  453. readOnlyList.TrimToSize();
  454. Assert.Fail("readOnlyList.TrimToSize()");
  455. }
  456. catch (NotSupportedException)
  457. {
  458. }
  459. }
  460. [Test]
  461. public void TestFixedSize()
  462. {
  463. ArrayList arrayList, fixedSizeList;
  464. arrayList = new ArrayList();
  465. fixedSizeList = ArrayList.FixedSize(arrayList);
  466. arrayList.AddRange(c_TestData);
  467. // Make sure the fixedSizeList is a wrapper and not a clone.
  468. arrayList.Add(10);
  469. Assert.IsTrue(fixedSizeList.Count == 11, "fixedSizeList.Count == 11");
  470. try
  471. {
  472. fixedSizeList.Add(0);
  473. Assert.Fail("fixedSizeList.Add(0)");
  474. }
  475. catch (NotSupportedException)
  476. {
  477. }
  478. try
  479. {
  480. fixedSizeList.Remove(0);
  481. Assert.Fail("fixedSizeList.Remove(0)");
  482. }
  483. catch (NotSupportedException)
  484. {
  485. }
  486. try
  487. {
  488. fixedSizeList.RemoveAt(0);
  489. Assert.Fail("fixedSizeList.RemoveAt(0)");
  490. }
  491. catch (NotSupportedException)
  492. {
  493. }
  494. try
  495. {
  496. fixedSizeList.Clear();
  497. Assert.Fail("fixedSizeList.Clear()");
  498. }
  499. catch (NotSupportedException)
  500. {
  501. }
  502. try
  503. {
  504. fixedSizeList[0] = 0;
  505. }
  506. catch (NotSupportedException)
  507. {
  508. Assert.Fail("fixedSizeList[0] = 0");
  509. }
  510. try
  511. {
  512. fixedSizeList.Clear();
  513. Assert.Fail("fixedSizeList.Clear()");
  514. }
  515. catch (NotSupportedException)
  516. {
  517. }
  518. try
  519. {
  520. fixedSizeList.Contains(1);
  521. }
  522. catch (NotSupportedException)
  523. {
  524. Assert.Fail("fixedSizeList.Contains");
  525. }
  526. try
  527. {
  528. int x = fixedSizeList.Count;
  529. }
  530. catch (NotSupportedException)
  531. {
  532. Assert.Fail("fixedSizeList.Count");
  533. }
  534. try
  535. {
  536. fixedSizeList.GetRange(0, 1);
  537. }
  538. catch (NotSupportedException)
  539. {
  540. Assert.Fail("fixedSizeList.GetRange(0, 1)");
  541. }
  542. try
  543. {
  544. fixedSizeList.IndexOf(0);
  545. }
  546. catch (NotSupportedException)
  547. {
  548. Assert.Fail("fixedSizeList.IndexOf(0)");
  549. }
  550. try
  551. {
  552. fixedSizeList.InsertRange(0, new object[] {1,2});
  553. Assert.Fail("fixedSizeList.InsertRange(0, new object[] {1,2})");
  554. }
  555. catch (NotSupportedException)
  556. {
  557. }
  558. try
  559. {
  560. fixedSizeList.Reverse();
  561. }
  562. catch (NotSupportedException)
  563. {
  564. Assert.Fail("fixedSizeList.Reverse()");
  565. }
  566. try
  567. {
  568. fixedSizeList.SetRange(0, new Object[] {0, 1});
  569. }
  570. catch (NotSupportedException)
  571. {
  572. Assert.Fail("fixedSizeList.SetRange(0, new Object[] {0, 1})");
  573. }
  574. try
  575. {
  576. fixedSizeList.Sort();
  577. }
  578. catch (NotSupportedException)
  579. {
  580. Assert.Fail("fixedSizeList.Sort()");
  581. }
  582. try
  583. {
  584. fixedSizeList.ToArray();
  585. }
  586. catch (NotSupportedException)
  587. {
  588. Assert.Fail("fixedSizeList.ToArray()");
  589. }
  590. try
  591. {
  592. fixedSizeList.TrimToSize();
  593. Assert.Fail("fixedSizeList.TrimToSize()");
  594. }
  595. catch (NotSupportedException)
  596. {
  597. }
  598. try
  599. {
  600. fixedSizeList.Clone();
  601. }
  602. catch (NotSupportedException)
  603. {
  604. Assert.Fail("fixedSizeList.Clone()");
  605. }
  606. try
  607. {
  608. fixedSizeList.AddRange(c_TestData);
  609. Assert.Fail("fixedSizeList.AddRange(c_TestData)");
  610. }
  611. catch (NotSupportedException)
  612. {
  613. }
  614. }
  615. private void PrivateTestClone(ArrayList arrayList)
  616. {
  617. ArrayList arrayList2;
  618. arrayList.AddRange(c_TestData);
  619. arrayList2 = (ArrayList)arrayList.Clone();
  620. VerifyContains(arrayList2, c_TestData, "arrayList.Clone()");
  621. }
  622. [Test]
  623. public void TestCloneStandard()
  624. {
  625. PrivateTestClone(new ArrayList());
  626. }
  627. [Test]
  628. public void TestCloneSynchronized()
  629. {
  630. PrivateTestClone(ArrayList.Synchronized(new ArrayList()));
  631. }
  632. [Test]
  633. public void TestCloneAdapter()
  634. {
  635. PrivateTestClone(ArrayList.Adapter(new ArrayList()));
  636. }
  637. [Test]
  638. public void TestCloneGetRange()
  639. {
  640. PrivateTestClone(new ArrayList().GetRange(0, 0));
  641. }
  642. private void PrivateTestCopyTo(ArrayList arrayList)
  643. {
  644. object[] array;
  645. arrayList.AddRange(c_TestData);
  646. array = new Object[arrayList.Count];
  647. arrayList.CopyTo(array);
  648. VerifyContains(array, new object[] {0,1,2,3,4,5,6,7,8,9}, "ArrayList.CopyTo(array)");
  649. array = new Object[3];
  650. arrayList.CopyTo(0, array, 0, 3);
  651. VerifyContains(array, new object[] {0,1,2}, "ArrayList.CopyTo(0, array, 0, 3)");
  652. array = new Object[4];
  653. arrayList.CopyTo(0, array, 1, 3);
  654. VerifyContains(array, new object[] {null,0, 1, 2}, "ArrayList.CopyTo(0, array, 1, 3)");
  655. array = new object[10];
  656. arrayList.CopyTo(3, array, 3, 5);
  657. VerifyContains(array, new object[] {null, null, null, 3, 4, 5, 6, 7, null, null}, "VerifyContains(array, ...)");
  658. }
  659. [Test]
  660. public void TestCopyToStandard()
  661. {
  662. PrivateTestCopyTo(new ArrayList());
  663. }
  664. [Test]
  665. public void TestCopyToSynchronized()
  666. {
  667. PrivateTestCopyTo(ArrayList.Synchronized(new ArrayList()));
  668. }
  669. [Test]
  670. public void TestCopyToAdapter()
  671. {
  672. PrivateTestCopyTo(ArrayList.Adapter(new ArrayList()));
  673. }
  674. [Test]
  675. public void TestCopyToGetRange()
  676. {
  677. PrivateTestCopyTo(new ArrayList().GetRange(0, 0));
  678. }
  679. private void PrivateTestSetCapacity(ArrayList arrayList)
  680. {
  681. int x;
  682. arrayList.AddRange(c_TestData);
  683. x = arrayList.Capacity;
  684. arrayList.Capacity = x * 2;
  685. Assert.IsTrue(arrayList.Capacity == x * 2, "arrayList.Capacity == x * 2");
  686. VerifyContains(arrayList, c_TestData, "VerifyContains(arrayList, c_TestData)");
  687. }
  688. [Test]
  689. public void TestSetCapacity()
  690. {
  691. PrivateTestSetCapacity(new ArrayList());
  692. }
  693. [Test]
  694. public void TestSetCapacitySynchronized()
  695. {
  696. PrivateTestSetCapacity(ArrayList.Synchronized(new ArrayList()));
  697. }
  698. [Test]
  699. public void TestCapacityExpands()
  700. {
  701. ArrayList arrayList = new ArrayList(10);
  702. arrayList.AddRange(c_TestData);
  703. Assert.IsTrue(arrayList.Capacity == 10, "arrayList.Capacity == 10");
  704. arrayList.Add(10);
  705. Assert.IsTrue(arrayList.Capacity == 20, "arrayList.Capacity == 20");
  706. VerifyContains(arrayList, new object[] {0,1,2,3,4,5,6,7,8,9,10}, "VerifyContains");
  707. }
  708. private void PrivateTestBinarySearch(ArrayList arrayList)
  709. {
  710. // Try searching with different size lists...
  711. for (int x = 0; x < 10; x++)
  712. {
  713. for (int i = 0; i < x; i++)
  714. {
  715. arrayList.Add(i);
  716. }
  717. for (int i = 0; i < x; i++)
  718. {
  719. int y;
  720. y = arrayList.BinarySearch(i);
  721. }
  722. }
  723. arrayList.Clear();
  724. arrayList.Add(new object());
  725. try
  726. {
  727. arrayList.BinarySearch(new object());
  728. Assert.Fail("1: Binary search on object that doesn't support IComparable.");
  729. }
  730. catch (ArgumentException)
  731. {
  732. }
  733. catch (InvalidOperationException)
  734. {
  735. // LAMESPEC: ArrayList.BinarySearch() on MS.NET throws InvalidOperationException
  736. }
  737. try
  738. {
  739. arrayList.BinarySearch(1);
  740. Assert.Fail("2: Binary search on incompatible object.");
  741. }
  742. catch (ArgumentException)
  743. {
  744. }
  745. catch (InvalidOperationException)
  746. {
  747. // LAMESPEC: ArrayList.BinarySearch() on MS.NET throws InvalidOperationException
  748. }
  749. arrayList.Clear();
  750. for (int i = 0; i < 100; i++)
  751. {
  752. arrayList.Add(1);
  753. }
  754. Assert.IsTrue(arrayList.BinarySearch(1) == 49, "BinarySearch should start in middle.");
  755. Assert.IsTrue(arrayList.BinarySearch(0, 0, 0, Comparer.Default) == -1, "arrayList.BinarySearch(0, 0, 0, Comparer.Default)");
  756. }
  757. [Test]
  758. public void TestBinarySearchStandard()
  759. {
  760. PrivateTestBinarySearch(new ArrayList());
  761. }
  762. [Test]
  763. public void TestBinarySearchSynchronized()
  764. {
  765. PrivateTestBinarySearch(ArrayList.Synchronized(new ArrayList()));
  766. }
  767. [Test]
  768. public void TestBinarySearchAdapter()
  769. {
  770. PrivateTestBinarySearch(ArrayList.Adapter(new ArrayList()));
  771. }
  772. [Test]
  773. public void TestBinarySearchGetRange()
  774. {
  775. PrivateTestBinarySearch(new ArrayList().GetRange(0, 0));
  776. }
  777. private void PrivateTestRemoveAt(ArrayList arrayList)
  778. {
  779. arrayList.Add(1);
  780. arrayList.Add(2);
  781. arrayList.Add(3);
  782. arrayList.Add(4);
  783. arrayList.Add(5);
  784. arrayList.Remove(2);
  785. VerifyContains(arrayList, new object[] {1, 3, 4, 5},
  786. "Remove element failed.");
  787. arrayList.RemoveAt(0);
  788. VerifyContains(arrayList, new object[] {3, 4, 5},
  789. "RemoveAt at start failed.");
  790. arrayList.RemoveAt(2);
  791. VerifyContains(arrayList, new object[] {3, 4},
  792. "RemoveAt at end failed.");
  793. }
  794. [Test]
  795. public void TestRemoveAtStandard()
  796. {
  797. PrivateTestRemoveAt(new ArrayList());
  798. }
  799. [Test]
  800. public void TestRemoveAtSynchronized()
  801. {
  802. PrivateTestRemoveAt(ArrayList.Synchronized(new ArrayList()));
  803. }
  804. [Test]
  805. public void TestRemoveAtAdapter()
  806. {
  807. PrivateTestRemoveAt(ArrayList.Adapter(new ArrayList()));
  808. }
  809. [Test]
  810. public void TestRemoveAtGetRange()
  811. {
  812. PrivateTestRemoveAt(new ArrayList().GetRange(0, 0));
  813. }
  814. private void PrivateTestRemoveRange(ArrayList arrayList)
  815. {
  816. arrayList.AddRange(c_TestData);
  817. arrayList.RemoveRange(0, 3);
  818. VerifyContains(arrayList, new object[] { 3, 4, 5, 6, 7, 8, 9 },
  819. "RemoveRange at start failed.");
  820. arrayList.RemoveRange(4, 3);
  821. VerifyContains(arrayList, new object[] { 3, 4, 5, 6 },
  822. "RemoveRange at start failed.");
  823. arrayList.RemoveRange(2, 1);
  824. VerifyContains(arrayList, new object[] { 3, 4, 6 },
  825. "RemoveRange in middle failed.");
  826. }
  827. [Test]
  828. public void TestRemoveRangeStandard()
  829. {
  830. PrivateTestRemoveRange(new ArrayList());
  831. }
  832. [Test]
  833. public void TestRemoveRangeSynchronized()
  834. {
  835. PrivateTestRemoveRange(ArrayList.Synchronized(new ArrayList()));
  836. }
  837. [Test]
  838. public void TestRemoveRangeAdapter()
  839. {
  840. PrivateTestRemoveRange(ArrayList.Adapter(new ArrayList()));
  841. }
  842. [Test]
  843. public void TestRemoveRangeGetRange()
  844. {
  845. PrivateTestRemoveRange(new ArrayList().GetRange(0, 0));
  846. }
  847. private void PrivateTestInsert(ArrayList arrayList)
  848. {
  849. arrayList.Add(1);
  850. arrayList.Add(2);
  851. arrayList.Add(3);
  852. arrayList.Add(4);
  853. arrayList.Insert(0, 1);
  854. VerifyContains(arrayList, new object[] {1, 1, 2, 3, 4}, "Insert at beginning failed.");
  855. arrayList.Insert(5, 5);
  856. VerifyContains(arrayList, new object[] {1, 1, 2, 3, 4, 5}, "Insert at end failed.");
  857. arrayList.Insert(3, 7);
  858. VerifyContains(arrayList, new object[] {1, 1, 2, 7, 3, 4, 5}, "Insert in middle failed.");
  859. }
  860. [Test]
  861. public void TestInsertStandard()
  862. {
  863. PrivateTestInsert(new ArrayList());
  864. }
  865. [Test]
  866. public void TestInsertAdapter()
  867. {
  868. PrivateTestInsert(ArrayList.Adapter(new ArrayList()));
  869. }
  870. [Test]
  871. public void TestInsertSynchronized()
  872. {
  873. PrivateTestInsert(ArrayList.Synchronized(new ArrayList()));
  874. }
  875. [Test]
  876. public void TestInsertGetRange()
  877. {
  878. PrivateTestInsert(new ArrayList().GetRange(0, 0));
  879. }
  880. private void PrivateTestGetRange(ArrayList arrayList)
  881. {
  882. ArrayList rangeList;
  883. arrayList.AddRange(c_TestData);
  884. rangeList = arrayList.GetRange(3, 5);
  885. Assert.IsTrue(rangeList.Count == 5, "rangeList.Count == 5");
  886. this.VerifyContains(rangeList, new object[] {3,4,5,6,7}, "1: VerifyContains(rangeList)");
  887. //FIXME: If items are removed from the Range, one may not iterate over it on .NET
  888. /*
  889. rangeList.Remove(7);
  890. this.VerifyContains(a2, new object[] {3,4,5,6}, "2: VerifyContains(rangeList)");
  891. rangeList.RemoveAt(0);
  892. this.VerifyContains(a3, new object[] {4,5,6}, "3: VerifyContains(rangeList)");
  893. rangeList.Add(7);
  894. rangeList.Add(6);
  895. rangeList.Add(3);
  896. rangeList.Add(11);
  897. Assert.IsTrue(rangeList.LastIndexOf(6) == 4, "rangeList.LastIndexOf(6) == 4");
  898. rangeList.Sort();
  899. this.VerifyContains(arrayList, new object[] {0, 1, 2, 3, 4, 5, 6, 6, 7, 11, 8, 9}, "4: VerifyContains(rangeList)");
  900. */
  901. }
  902. [Test]
  903. public void TestGetRangeStandard()
  904. {
  905. PrivateTestGetRange(new ArrayList());
  906. }
  907. [Test]
  908. public void TestGetRangeAdapter()
  909. {
  910. PrivateTestGetRange(ArrayList.Adapter(new ArrayList()));
  911. }
  912. [Test]
  913. public void TestGetRangeSynchronized()
  914. {
  915. PrivateTestGetRange(ArrayList.Synchronized(new ArrayList()));
  916. }
  917. [Test]
  918. public void TestGetRangeGetRange()
  919. {
  920. PrivateTestGetRange(new ArrayList().GetRange(0, 0));
  921. }
  922. private void PrivateTestEnumeratorWithRange(ArrayList arrayList)
  923. {
  924. IEnumerator enumerator;
  925. arrayList.AddRange(c_TestData);
  926. int x;
  927. // Test with the range 1 - 3
  928. enumerator = arrayList.GetEnumerator(1, 3);
  929. x = 1;
  930. while (enumerator.MoveNext())
  931. {
  932. Assert.IsTrue((int)enumerator.Current == x, "enumerator.Current == x");
  933. x++;
  934. }
  935. enumerator.Reset();
  936. x = 1;
  937. while (enumerator.MoveNext())
  938. {
  939. Assert.IsTrue((int)enumerator.Current == x, "enumerator.Current == x");
  940. x++;
  941. }
  942. // Test with a range covering the whole list.
  943. enumerator = arrayList.GetEnumerator(0, arrayList.Count);
  944. x = 0;
  945. while (enumerator.MoveNext())
  946. {
  947. Assert.IsTrue((int)enumerator.Current == x, "enumerator.Current == x");
  948. x++;
  949. }
  950. enumerator.Reset();
  951. x = 0;
  952. while (enumerator.MoveNext())
  953. {
  954. Assert.IsTrue((int)enumerator.Current == x, "enumerator.Current == x");
  955. x++;
  956. }
  957. // Test with a range covering nothing.
  958. enumerator = arrayList.GetEnumerator(arrayList.Count, 0);
  959. Assert.IsTrue(!enumerator.MoveNext(), "!enumerator.MoveNext()");
  960. enumerator.Reset();
  961. Assert.IsTrue(!enumerator.MoveNext(), "!enumerator.MoveNext()");
  962. }
  963. [Test]
  964. public void TestEnumeratorWithRangeStandard()
  965. {
  966. PrivateTestEnumeratorWithRange(new ArrayList());
  967. }
  968. [Test]
  969. public void TestEnumeratorWithRangeSynchronized()
  970. {
  971. PrivateTestEnumeratorWithRange(ArrayList.Synchronized(new ArrayList()));
  972. }
  973. [Test]
  974. public void TestEnumeratorWithRangeAdapter()
  975. {
  976. PrivateTestEnumeratorWithRange(ArrayList.Adapter(new ArrayList()));
  977. }
  978. [Test]
  979. public void TestEnumeratorWithRangeGetRange()
  980. {
  981. PrivateTestEnumeratorWithRange(new ArrayList().GetRange(0, 0));
  982. }
  983. private void PrivateTestEnumerator(ArrayList arrayList)
  984. {
  985. int x = 0;
  986. arrayList.AddRange(c_TestData);
  987. x = 0;
  988. foreach (object o in arrayList)
  989. {
  990. if (!o.Equals(x))
  991. {
  992. Assert.Fail("Arraylist.GetEnumerator()");
  993. break;
  994. }
  995. x++;
  996. }
  997. IEnumerator enumerator;
  998. enumerator = arrayList.GetEnumerator();
  999. enumerator.MoveNext();
  1000. Assert.IsTrue((int)enumerator.Current == 0, "enumerator.Current == 0");
  1001. // Invalidate the enumerator.
  1002. arrayList.Add(10);
  1003. try
  1004. {
  1005. // According to the spec this should still work even though the enumerator is invalid.
  1006. Assert.IsTrue((int)enumerator.Current == 0, "enumerator.Current == 0");
  1007. }
  1008. catch (InvalidOperationException)
  1009. {
  1010. Assert.IsTrue(false, "enumerator.Current should not fail.");
  1011. }
  1012. try
  1013. {
  1014. // This should throw an InvalidOperationException.
  1015. enumerator.MoveNext();
  1016. Assert.IsTrue(false, "enumerator.Current should fail.");
  1017. }
  1018. catch (InvalidOperationException)
  1019. {
  1020. }
  1021. }
  1022. [Test]
  1023. public void TestEnumeratorStandard()
  1024. {
  1025. PrivateTestEnumerator(new ArrayList());
  1026. }
  1027. [Test]
  1028. public void TestEnumeratorSynchronized()
  1029. {
  1030. PrivateTestEnumerator(ArrayList.Synchronized(new ArrayList()));
  1031. }
  1032. [Test]
  1033. public void TestEnumeratorAdapter()
  1034. {
  1035. PrivateTestEnumerator(ArrayList.Adapter(new ArrayList()));
  1036. }
  1037. [Test]
  1038. public void TestEnumeratorGetRange()
  1039. {
  1040. PrivateTestEnumerator(new ArrayList().GetRange(0, 0));
  1041. }
  1042. private void PrivateTestReverse(ArrayList arrayList)
  1043. {
  1044. ArrayList arrayList2;
  1045. for (int x = 1; x < 100; x ++)
  1046. {
  1047. arrayList2 = (ArrayList)arrayList.Clone();
  1048. for (int i = 0; i < x; i++)
  1049. {
  1050. arrayList2.Add(i);
  1051. }
  1052. arrayList2.Reverse();
  1053. bool ok = true;
  1054. // Check that reverse did reverse the adapter.
  1055. for (int i = 0; i < x; i++)
  1056. {
  1057. if ((int)arrayList2[i] != x - i - 1)
  1058. {
  1059. ok = false;
  1060. break;
  1061. }
  1062. }
  1063. Assert.IsTrue (ok, String.Format("Reverse on arrayList failed on list with {0} items.", x));
  1064. }
  1065. }
  1066. [Test]
  1067. public void TestReverseStandard()
  1068. {
  1069. PrivateTestReverse(new ArrayList());
  1070. }
  1071. [Test]
  1072. public void TestReverseAdapter()
  1073. {
  1074. ArrayList arrayList = new ArrayList();
  1075. ArrayList adapter = ArrayList.Adapter(arrayList);
  1076. PrivateTestReverse(adapter);
  1077. VerifyContains(adapter, arrayList, "Changing adapter didn't change ArrayList.");
  1078. }
  1079. [Test]
  1080. public void TestReverseSynchronized()
  1081. {
  1082. PrivateTestReverse(ArrayList.Synchronized(new ArrayList()));
  1083. }
  1084. [Test]
  1085. public void TestReverseGetRange()
  1086. {
  1087. PrivateTestReverse(new ArrayList().GetRange(0,0));
  1088. }
  1089. [Test]
  1090. public void TestIterator ()
  1091. {
  1092. ArrayList a = new ArrayList ();
  1093. a.Add (1);
  1094. a.Add (2);
  1095. a.Add (3);
  1096. int total = 0;
  1097. foreach (int b in a)
  1098. total += b;
  1099. Assert.IsTrue (total == 6, "Count should be 6");
  1100. }
  1101. [Test]
  1102. public void TestIteratorObjects ()
  1103. {
  1104. ArrayList a = new ArrayList ();
  1105. a.Add (1);
  1106. a.Add (null);
  1107. a.Add (3);
  1108. int total = 0;
  1109. int count = 0;
  1110. bool found_null = false;
  1111. foreach (object b in a){
  1112. count++;
  1113. if (b == null){
  1114. if (found_null)
  1115. Assert.IsTrue (false, "Should only find one null");
  1116. found_null = true;
  1117. } else {
  1118. total += (int) b;
  1119. }
  1120. }
  1121. Assert.IsTrue (found_null, "Should fine one null");
  1122. Assert.IsTrue (total == 4, "Total should be 4");
  1123. Assert.IsTrue (count == 3, "Count should be 3");
  1124. }
  1125. }
  1126. }