PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/danipen/mono
C# | 1865 lines | 1687 code | 98 blank | 80 comment | 25 complexity | c9c91782d42dbdae0a47b3a4576b11cd 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

Large files files are truncated, but you can click here to view the full file

  1. // ArrayListTest.cs - NUnit Test Cases for the System.Collections.ArrayList class
  2. //
  3. // David Brandt (bucky@keystreams.com)
  4. //
  5. // (C) Ximian, Inc. http://www.ximian.com
  6. // Copyright (C) 2005 Novell (http://www.novell.com)
  7. //
  8. using System;
  9. using System.Collections;
  10. using NUnit.Framework;
  11. namespace MonoTests.System.Collections
  12. {
  13. [TestFixture]
  14. public class ArrayListTest
  15. {
  16. [Test]
  17. public void TestCtor ()
  18. {
  19. {
  20. ArrayList al1 = new ArrayList ();
  21. Assert.IsNotNull (al1, "no basic ArrayList");
  22. }
  23. {
  24. bool errorThrown = false;
  25. try {
  26. ArrayList a = new ArrayList (null);
  27. } catch (ArgumentNullException) {
  28. errorThrown = true;
  29. }
  30. Assert.IsTrue (errorThrown, "null icollection error not thrown");
  31. }
  32. {
  33. // what can I say? I like chars. [--DB]
  34. char [] coll = { 'a', 'b', 'c', 'd' };
  35. ArrayList al1 = new ArrayList (coll);
  36. Assert.IsNotNull (al1, "no icollection ArrayList");
  37. for (int i = 0; i < coll.Length; i++) {
  38. Assert.AreEqual (coll [i], al1 [i], i + " not ctor'ed properly.");
  39. }
  40. }
  41. {
  42. try {
  43. Char [,] c1 = new Char [2, 2];
  44. ArrayList al1 = new ArrayList (c1);
  45. Assert.Fail ("Should fail with multi-dimensional array in constructor.");
  46. } catch (RankException) {
  47. }
  48. }
  49. {
  50. bool errorThrown = false;
  51. try {
  52. ArrayList a = new ArrayList (-1);
  53. } catch (ArgumentOutOfRangeException) {
  54. errorThrown = true;
  55. }
  56. Assert.IsTrue (errorThrown, "negative capacity error not thrown");
  57. }
  58. }
  59. [Test]
  60. public void TestCapacity ()
  61. {
  62. #if NET_2_0
  63. int default_capacity = 4;
  64. int unspecified_capacity = 0;
  65. #else
  66. int default_capacity = 16;
  67. int unspecified_capacity = 16;
  68. #endif
  69. for (int i = 1; i < 100; i++) {
  70. ArrayList al1 = new ArrayList (i);
  71. Assert.AreEqual (i, al1.Capacity, "Bad capacity of " + i);
  72. }
  73. {
  74. ArrayList al1 = new ArrayList (0);
  75. // LAMESPEC:
  76. // Assert.AreEqual (// 16, al1.Capacity, "Bad capacity when set to 0");
  77. al1.Add ("?");
  78. Assert.AreEqual (default_capacity, al1.Capacity, "Bad capacity when set to 0");
  79. }
  80. {
  81. ArrayList al1 = new ArrayList ();
  82. Assert.AreEqual (unspecified_capacity, al1.Capacity, "Bad default capacity");
  83. }
  84. }
  85. [Test]
  86. public void TestCount ()
  87. {
  88. {
  89. ArrayList al1 = new ArrayList ();
  90. Assert.AreEqual (0, al1.Count, "Bad initial count");
  91. for (int i = 1; i <= 100; i++) {
  92. al1.Add (i);
  93. Assert.AreEqual (i, al1.Count, "Bad count " + i);
  94. }
  95. }
  96. for (int i = 0; i < 100; i++) {
  97. char [] coll = new Char [i];
  98. ArrayList al1 = new ArrayList (coll);
  99. Assert.AreEqual (i, al1.Count, "Bad count for " + i);
  100. }
  101. }
  102. [Test]
  103. public void TestIsFixed ()
  104. {
  105. ArrayList al1 = new ArrayList ();
  106. Assert.IsTrue (!al1.IsFixedSize, "should not be fixed by default");
  107. ArrayList al2 = ArrayList.FixedSize (al1);
  108. Assert.IsTrue (al2.IsFixedSize, "fixed-size wrapper not working");
  109. }
  110. [Test]
  111. public void TestIsReadOnly ()
  112. {
  113. ArrayList al1 = new ArrayList ();
  114. Assert.IsTrue (!al1.IsReadOnly, "should not be ReadOnly by default");
  115. ArrayList al2 = ArrayList.ReadOnly (al1);
  116. Assert.IsTrue (al2.IsReadOnly, "read-only wrapper not working");
  117. }
  118. [Test]
  119. public void TestIsSynchronized ()
  120. {
  121. ArrayList al1 = new ArrayList ();
  122. Assert.IsTrue (!al1.IsSynchronized, "should not be synchronized by default");
  123. ArrayList al2 = ArrayList.Synchronized (al1);
  124. Assert.IsTrue (al2.IsSynchronized, "synchronized wrapper not working");
  125. }
  126. [Test]
  127. public void TestItem ()
  128. {
  129. ArrayList al1 = new ArrayList ();
  130. {
  131. bool errorThrown = false;
  132. try {
  133. object o = al1 [-1];
  134. } catch (ArgumentOutOfRangeException) {
  135. errorThrown = true;
  136. }
  137. Assert.IsTrue (errorThrown, "negative item error not thrown");
  138. }
  139. {
  140. bool errorThrown = false;
  141. try {
  142. object o = al1 [1];
  143. } catch (ArgumentOutOfRangeException) {
  144. errorThrown = true;
  145. }
  146. Assert.IsTrue (errorThrown, "past-end item error not thrown");
  147. }
  148. for (int i = 0; i <= 100; i++) {
  149. al1.Add (i);
  150. }
  151. for (int i = 0; i <= 100; i++) {
  152. Assert.AreEqual (i, al1 [i], "item not fetched for " + i);
  153. }
  154. }
  155. [Test]
  156. public void TestAdapter ()
  157. {
  158. {
  159. bool errorThrown = false;
  160. try {
  161. ArrayList al1 = ArrayList.Adapter (null);
  162. } catch (ArgumentNullException) {
  163. errorThrown = true;
  164. } catch (Exception e) {
  165. Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString ());
  166. }
  167. Assert.IsTrue (errorThrown, "null adapter error not thrown");
  168. }
  169. {
  170. char [] list = { 'a', 'b', 'c', 'd' };
  171. ArrayList al1 = ArrayList.Adapter (list);
  172. Assert.IsNotNull (al1, "Couldn't get an adapter");
  173. for (int i = 0; i < list.Length; i++) {
  174. Assert.AreEqual (list [i], al1 [i], "adapter not adapting");
  175. }
  176. list [0] = 'z';
  177. for (int i = 0; i < list.Length; i++) {
  178. Assert.AreEqual (list [i], al1 [i], "adapter not adapting");
  179. }
  180. }
  181. // Test Binary Search
  182. {
  183. bool errorThrown = false;
  184. try {
  185. String [] s1 = { "This", "is", "a", "test" };
  186. ArrayList al1 = ArrayList.Adapter (s1);
  187. al1.BinarySearch (42);
  188. } catch (InvalidOperationException) {
  189. // this is what .NET throws
  190. errorThrown = true;
  191. } catch (ArgumentException) {
  192. // this is what the docs say it should throw
  193. errorThrown = true;
  194. } catch (Exception e) {
  195. Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString ());
  196. }
  197. Assert.IsTrue (errorThrown, "search-for-wrong-type error not thrown");
  198. }
  199. {
  200. char [] arr = { 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd' };
  201. ArrayList al1 = ArrayList.Adapter (arr);
  202. Assert.IsTrue (al1.BinarySearch ('c') >= 3, "couldn't find elem #1");
  203. Assert.IsTrue (al1.BinarySearch ('c') < 6, "couldn't find elem #2");
  204. }
  205. {
  206. char [] arr = { 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e' };
  207. ArrayList al1 = ArrayList.Adapter (arr);
  208. Assert.AreEqual (-4, al1.BinarySearch ('c'), "couldn't find next-higher elem");
  209. }
  210. {
  211. char [] arr = { 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd' };
  212. ArrayList al1 = ArrayList.Adapter (arr);
  213. Assert.AreEqual (-9, al1.BinarySearch ('e'), "couldn't find end");
  214. }
  215. // Sort
  216. {
  217. char [] starter = { 'd', 'b', 'f', 'e', 'a', 'c' };
  218. ArrayList al1 = ArrayList.Adapter (starter);
  219. al1.Sort ();
  220. Assert.AreEqual ('a', al1 [0], "Should be sorted");
  221. Assert.AreEqual ('b', al1 [1], "Should be sorted");
  222. Assert.AreEqual ('c', al1 [2], "Should be sorted");
  223. Assert.AreEqual ('d', al1 [3], "Should be sorted");
  224. Assert.AreEqual ('e', al1 [4], "Should be sorted");
  225. Assert.AreEqual ('f', al1 [5], "Should be sorted");
  226. }
  227. // TODO - test other adapter types?
  228. }
  229. [Test]
  230. public void TestAdd ()
  231. {
  232. {
  233. bool errorThrown = false;
  234. try {
  235. ArrayList al1 =
  236. ArrayList.FixedSize (new ArrayList ());
  237. al1.Add ("Hi!");
  238. } catch (NotSupportedException) {
  239. errorThrown = true;
  240. } catch (Exception e) {
  241. Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString ());
  242. }
  243. Assert.IsTrue (errorThrown, "add to fixed size error not thrown");
  244. }
  245. {
  246. bool errorThrown = false;
  247. try {
  248. ArrayList al1 =
  249. ArrayList.ReadOnly (new ArrayList ());
  250. al1.Add ("Hi!");
  251. } catch (NotSupportedException) {
  252. errorThrown = true;
  253. } catch (Exception e) {
  254. Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString ());
  255. }
  256. Assert.IsTrue (errorThrown, "add to read only error not thrown");
  257. }
  258. {
  259. ArrayList al1 = new ArrayList ();
  260. for (int i = 1; i <= 100; i++) {
  261. al1.Add (i);
  262. Assert.AreEqual (i, al1.Count, "add failed " + i);
  263. Assert.AreEqual (i, al1 [i - 1], "add failed " + i);
  264. }
  265. }
  266. {
  267. string [] strArray = new string [] { };
  268. ArrayList al1 = new ArrayList (strArray);
  269. al1.Add ("Hi!");
  270. al1.Add ("Hi!");
  271. Assert.AreEqual (2, al1.Count, "add failed");
  272. }
  273. }
  274. [Test]
  275. public void TestAddRange ()
  276. {
  277. {
  278. bool errorThrown = false;
  279. try {
  280. ArrayList al1 =
  281. ArrayList.FixedSize (new ArrayList ());
  282. String [] s1 = { "Hi!" };
  283. al1.AddRange (s1);
  284. } catch (NotSupportedException) {
  285. errorThrown = true;
  286. } catch (Exception e) {
  287. Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString ());
  288. }
  289. Assert.IsTrue (errorThrown, "add to fixed size error not thrown");
  290. }
  291. {
  292. bool errorThrown = false;
  293. try {
  294. ArrayList al1 =
  295. ArrayList.ReadOnly (new ArrayList ());
  296. String [] s1 = { "Hi!" };
  297. al1.AddRange (s1);
  298. } catch (NotSupportedException) {
  299. errorThrown = true;
  300. } catch (Exception e) {
  301. Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString ());
  302. }
  303. Assert.IsTrue (errorThrown, "add to read only error not thrown");
  304. }
  305. {
  306. bool errorThrown = false;
  307. try {
  308. ArrayList al1 = new ArrayList ();
  309. al1.AddRange (null);
  310. } catch (ArgumentNullException) {
  311. errorThrown = true;
  312. } catch (Exception e) {
  313. Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString ());
  314. }
  315. Assert.IsTrue (errorThrown, "add to read only error not thrown");
  316. }
  317. {
  318. ArrayList a1 = new ArrayList ();
  319. Assert.AreEqual (0, a1.Count, "ArrayList should start empty");
  320. char [] coll = { 'a', 'b', 'c' };
  321. a1.AddRange (coll);
  322. Assert.AreEqual (3, a1.Count, "ArrayList has wrong elements");
  323. a1.AddRange (coll);
  324. Assert.AreEqual (6, a1.Count, "ArrayList has wrong elements");
  325. }
  326. {
  327. ArrayList list = new ArrayList ();
  328. for (int i = 0; i < 100; i++) {
  329. list.Add (1);
  330. }
  331. Assert.AreEqual (49, list.BinarySearch (1), "BinarySearch off-by-one bug");
  332. }
  333. }
  334. [Test]
  335. public void TestBinarySearch ()
  336. {
  337. {
  338. bool errorThrown = false;
  339. try {
  340. ArrayList al1 = new ArrayList ();
  341. String [] s1 = { "This", "is", "a", "test" };
  342. al1.AddRange (s1);
  343. al1.BinarySearch (42);
  344. } catch (InvalidOperationException) {
  345. // this is what .NET throws
  346. errorThrown = true;
  347. } catch (ArgumentException) {
  348. // this is what the docs say it should throw
  349. errorThrown = true;
  350. } catch (Exception e) {
  351. Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString ());
  352. }
  353. Assert.IsTrue (errorThrown, "search-for-wrong-type error not thrown");
  354. }
  355. {
  356. char [] arr = { 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd' };
  357. ArrayList al1 = new ArrayList (arr);
  358. Assert.IsTrue (al1.BinarySearch ('c') >= 3, "couldn't find elem #1");
  359. Assert.IsTrue (al1.BinarySearch ('c') < 6, "couldn't find elem #2");
  360. }
  361. {
  362. char [] arr = { 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e' };
  363. ArrayList al1 = new ArrayList (arr);
  364. Assert.AreEqual (-4, al1.BinarySearch ('c'), "couldn't find next-higher elem");
  365. }
  366. {
  367. char [] arr = { 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd' };
  368. ArrayList al1 = new ArrayList (arr);
  369. Assert.AreEqual (-9, al1.BinarySearch ('e'), "couldn't find end");
  370. }
  371. }
  372. [Test]
  373. [ExpectedException (typeof (ArgumentException))]
  374. public void BinarySearch_IndexOverflow ()
  375. {
  376. ArrayList al = new ArrayList ();
  377. al.Add (this);
  378. al.BinarySearch (Int32.MaxValue, 1, this, null);
  379. }
  380. [Test]
  381. [ExpectedException (typeof (ArgumentException))]
  382. public void BinarySearch_CountOverflow ()
  383. {
  384. ArrayList al = new ArrayList ();
  385. al.Add (this);
  386. al.BinarySearch (1, Int32.MaxValue, this, null);
  387. }
  388. [Test]
  389. public void BinarySearch_Null ()
  390. {
  391. ArrayList al = new ArrayList ();
  392. al.Add (this);
  393. Assert.AreEqual (-1, al.BinarySearch (null), "null");
  394. }
  395. // TODO - BinarySearch with IComparer
  396. [Test]
  397. public void TestClear ()
  398. {
  399. {
  400. bool errorThrown = false;
  401. try {
  402. ArrayList al1 =
  403. ArrayList.FixedSize (new ArrayList ());
  404. al1.Clear ();
  405. } catch (NotSupportedException) {
  406. errorThrown = true;
  407. }
  408. Assert.IsTrue (errorThrown, "add to fixed size error not thrown");
  409. }
  410. {
  411. bool errorThrown = false;
  412. try {
  413. ArrayList al1 =
  414. ArrayList.ReadOnly (new ArrayList ());
  415. al1.Clear ();
  416. } catch (NotSupportedException) {
  417. errorThrown = true;
  418. }
  419. Assert.IsTrue (errorThrown, "add to read only error not thrown");
  420. }
  421. {
  422. ArrayList al1 = new ArrayList ();
  423. al1.Add ('c');
  424. Assert.AreEqual (1, al1.Count, "should have one element");
  425. al1.Clear ();
  426. Assert.AreEqual (0, al1.Count, "should be empty");
  427. }
  428. {
  429. int [] i1 = { 1, 2, 3, 4 };
  430. ArrayList al1 = new ArrayList (i1);
  431. Assert.AreEqual (i1.Length, al1.Count, "should have elements");
  432. int capacity = al1.Capacity;
  433. al1.Clear ();
  434. Assert.AreEqual (0, al1.Count, "should be empty again");
  435. Assert.AreEqual (capacity, al1.Capacity, "capacity shouldn't have changed");
  436. }
  437. }
  438. [Test]
  439. public void TestClone ()
  440. {
  441. {
  442. char [] c1 = { 'a', 'b', 'c' };
  443. ArrayList al1 = new ArrayList (c1);
  444. ArrayList al2 = (ArrayList) al1.Clone ();
  445. Assert.AreEqual (al1 [0], al2 [0], "ArrayList match");
  446. Assert.AreEqual (al1 [1], al2 [1], "ArrayList match");
  447. Assert.AreEqual (al1 [2], al2 [2], "ArrayList match");
  448. }
  449. {
  450. char [] d10 = { 'a', 'b' };
  451. char [] d11 = { 'a', 'c' };
  452. char [] d12 = { 'b', 'c' };
  453. char [] [] d1 = { d10, d11, d12 };
  454. ArrayList al1 = new ArrayList (d1);
  455. ArrayList al2 = (ArrayList) al1.Clone ();
  456. Assert.AreEqual (al1 [0], al2 [0], "Array match");
  457. Assert.AreEqual (al1 [1], al2 [1], "Array match");
  458. Assert.AreEqual (al1 [2], al2 [2], "Array match");
  459. ((char []) al1 [0]) [0] = 'z';
  460. Assert.AreEqual (al1 [0], al2 [0], "shallow copy");
  461. }
  462. }
  463. [Test]
  464. public void TestContains ()
  465. {
  466. char [] c1 = { 'a', 'b', 'c' };
  467. ArrayList al1 = new ArrayList (c1);
  468. Assert.IsTrue (!al1.Contains (null), "never find a null");
  469. Assert.IsTrue (al1.Contains ('b'), "can't find value");
  470. Assert.IsTrue (!al1.Contains ('?'), "shouldn't find value");
  471. }
  472. [Test]
  473. public void TestCopyTo ()
  474. {
  475. {
  476. bool errorThrown = false;
  477. try {
  478. Char [] c1 = new Char [2];
  479. ArrayList al1 = new ArrayList (c1);
  480. al1.CopyTo (null, 2);
  481. } catch (ArgumentNullException) {
  482. errorThrown = true;
  483. } catch (Exception e) {
  484. Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString ());
  485. }
  486. Assert.IsTrue (errorThrown, "error not thrown 1");
  487. }
  488. {
  489. bool errorThrown = false;
  490. try {
  491. Char [] c1 = new Char [2];
  492. ArrayList al1 = new ArrayList (c1);
  493. Char [,] c2 = new Char [2, 2];
  494. al1.CopyTo (c2, 2);
  495. } catch (ArgumentException) {
  496. errorThrown = true;
  497. } catch (Exception e) {
  498. Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString ());
  499. }
  500. Assert.IsTrue (errorThrown, "error not thrown 2");
  501. }
  502. {
  503. bool errorThrown = false;
  504. try {
  505. // This appears to be a bug in the ArrayList Constructor.
  506. // It throws a RankException if a multidimensional Array
  507. // is passed. The docs imply that an IEnumerator is used
  508. // to retrieve the items from the collection, so this should
  509. // work. In anycase this test is for CopyTo, so use what
  510. // works on both platforms.
  511. //Char[,] c1 = new Char[2,2];
  512. Char [] c1 = new Char [2];
  513. ArrayList al1 = new ArrayList (c1);
  514. Char [] c2 = new Char [2];
  515. al1.CopyTo (c2, 2);
  516. } catch (ArgumentException) {
  517. errorThrown = true;
  518. } catch (Exception e) {
  519. Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString ());
  520. }
  521. Assert.IsTrue (errorThrown, "error not thrown 3");
  522. }
  523. {
  524. bool errorThrown = false;
  525. try {
  526. Char [] c1 = new Char [2];
  527. ArrayList al1 = new ArrayList (c1);
  528. Char [] c2 = new Char [2];
  529. al1.CopyTo (c2, -1);
  530. } catch (ArgumentOutOfRangeException) {
  531. errorThrown = true;
  532. } catch (Exception e) {
  533. Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString ());
  534. }
  535. Assert.IsTrue (errorThrown, "error not thrown 4");
  536. }
  537. {
  538. bool errorThrown = false;
  539. try {
  540. Char [] c1 = new Char [2];
  541. ArrayList al1 = new ArrayList (c1);
  542. Char [] c2 = new Char [2];
  543. al1.CopyTo (c2, 3);
  544. } catch (ArgumentException) {
  545. errorThrown = true;
  546. } catch (Exception e) {
  547. Assert.Fail ("Incorrect exception thrown at 5: " + e.ToString ());
  548. }
  549. Assert.IsTrue (errorThrown, "error not thrown 5");
  550. }
  551. {
  552. bool errorThrown = false;
  553. try {
  554. Char [] c1 = new Char [2];
  555. ArrayList al1 = new ArrayList (c1);
  556. Char [] c2 = new Char [2];
  557. al1.CopyTo (c2, 1);
  558. } catch (ArgumentException) {
  559. errorThrown = true;
  560. } catch (Exception e) {
  561. Assert.Fail ("Incorrect exception thrown at 6: " + e.ToString ());
  562. }
  563. Assert.IsTrue (errorThrown, "error not thrown 6");
  564. }
  565. {
  566. bool errorThrown = false;
  567. try {
  568. String [] c1 = { "String", "array" };
  569. ArrayList al1 = new ArrayList (c1);
  570. Char [] c2 = new Char [2];
  571. al1.CopyTo (c2, 0);
  572. } catch (InvalidCastException) {
  573. errorThrown = true;
  574. } catch (Exception e) {
  575. Assert.Fail ("Incorrect exception thrown at 7: " + e.ToString ());
  576. }
  577. Assert.IsTrue (errorThrown, "error not thrown 7");
  578. }
  579. Char [] orig = { 'a', 'b', 'c', 'd' };
  580. ArrayList al = new ArrayList (orig);
  581. Char [] copy = new Char [10];
  582. Array.Clear (copy, 0, copy.Length);
  583. al.CopyTo (copy, 3);
  584. Assert.AreEqual ((char) 0, copy [0], "Wrong CopyTo 0");
  585. Assert.AreEqual ((char) 0, copy [1], "Wrong CopyTo 1");
  586. Assert.AreEqual ((char) 0, copy [2], "Wrong CopyTo 2");
  587. Assert.AreEqual (orig [0], copy [3], "Wrong CopyTo 3");
  588. Assert.AreEqual (orig [1], copy [4], "Wrong CopyTo 4");
  589. Assert.AreEqual (orig [2], copy [5], "Wrong CopyTo 5");
  590. Assert.AreEqual (orig [3], copy [6], "Wrong CopyTo 6");
  591. Assert.AreEqual ((char) 0, copy [7], "Wrong CopyTo 7");
  592. Assert.AreEqual ((char) 0, copy [8], "Wrong CopyTo 8");
  593. Assert.AreEqual ((char) 0, copy [9], "Wrong CopyTo 9");
  594. }
  595. [Test]
  596. [ExpectedException (typeof (ArgumentException))]
  597. public void CopyTo_IndexOverflow ()
  598. {
  599. ArrayList al = new ArrayList ();
  600. al.Add (this);
  601. al.CopyTo (Int32.MaxValue, new byte [2], 0, 0);
  602. }
  603. [Test]
  604. [ExpectedException (typeof (ArgumentException))]
  605. public void CopyTo_ArrayIndexOverflow ()
  606. {
  607. ArrayList al = new ArrayList ();
  608. al.Add (this);
  609. al.CopyTo (0, new byte [2], Int32.MaxValue, 0);
  610. }
  611. [Test]
  612. [ExpectedException (typeof (ArgumentException))]
  613. public void CopyTo_CountOverflow ()
  614. {
  615. ArrayList al = new ArrayList ();
  616. al.Add (this);
  617. al.CopyTo (0, new byte [2], 0, Int32.MaxValue);
  618. }
  619. [Test]
  620. public void TestFixedSize ()
  621. {
  622. {
  623. bool errorThrown = false;
  624. try {
  625. ArrayList al1 = ArrayList.FixedSize (null);
  626. } catch (ArgumentNullException) {
  627. errorThrown = true;
  628. }
  629. Assert.IsTrue (errorThrown, "null arg error not thrown");
  630. }
  631. {
  632. ArrayList al1 = new ArrayList ();
  633. Assert.AreEqual (false, al1.IsFixedSize, "arrays start un-fixed.");
  634. ArrayList al2 = ArrayList.FixedSize (al1);
  635. Assert.AreEqual (true, al2.IsFixedSize, "should be fixed.");
  636. }
  637. }
  638. [Test]
  639. public void TestEnumerator ()
  640. {
  641. String [] s1 = { "this", "is", "a", "test" };
  642. ArrayList al1 = new ArrayList (s1);
  643. IEnumerator en = al1.GetEnumerator ();
  644. en.MoveNext ();
  645. al1.Add ("something");
  646. try {
  647. en.MoveNext ();
  648. Assert.Fail ("Add() didn't invalidate the enumerator");
  649. } catch (InvalidOperationException) {
  650. // do nothing...this is what we expect
  651. }
  652. en = al1.GetEnumerator ();
  653. en.MoveNext ();
  654. al1.AddRange (al1);
  655. try {
  656. en.MoveNext ();
  657. Assert.Fail ("AddRange() didn't invalidate the enumerator");
  658. } catch (InvalidOperationException) {
  659. // do nothing...this is what we expect
  660. }
  661. en = al1.GetEnumerator ();
  662. en.MoveNext ();
  663. al1.Clear ();
  664. try {
  665. en.MoveNext ();
  666. Assert.Fail ("Clear() didn't invalidate the enumerator");
  667. } catch (InvalidOperationException) {
  668. // do nothing...this is what we expect
  669. }
  670. al1 = new ArrayList (s1);
  671. en = al1.GetEnumerator ();
  672. en.MoveNext ();
  673. al1.Insert (0, "new first");
  674. try {
  675. en.MoveNext ();
  676. Assert.Fail ("Insert() didn't invalidate the enumerator");
  677. } catch (InvalidOperationException) {
  678. // do nothing...this is what we expect
  679. }
  680. en = al1.GetEnumerator ();
  681. en.MoveNext ();
  682. al1.InsertRange (0, al1);
  683. try {
  684. en.MoveNext ();
  685. Assert.Fail ("InsertRange() didn't invalidate the enumerator");
  686. } catch (InvalidOperationException) {
  687. // do nothing...this is what we expect
  688. }
  689. en = al1.GetEnumerator ();
  690. en.MoveNext ();
  691. al1.Remove ("this");
  692. try {
  693. en.MoveNext ();
  694. Assert.Fail ("Remove() didn't invalidate the enumerator");
  695. } catch (InvalidOperationException) {
  696. // do nothing...this is what we expect
  697. }
  698. en = al1.GetEnumerator ();
  699. en.MoveNext ();
  700. al1.RemoveAt (2);
  701. try {
  702. en.MoveNext ();
  703. Assert.Fail ("RemoveAt() didn't invalidate the enumerator");
  704. } catch (InvalidOperationException) {
  705. // do nothing...this is what we expect
  706. }
  707. en = al1.GetEnumerator ();
  708. en.MoveNext ();
  709. al1.RemoveRange (1, 1);
  710. try {
  711. en.MoveNext ();
  712. Assert.Fail ("RemoveRange() didn't invalidate the enumerator");
  713. } catch (InvalidOperationException) {
  714. // do nothing...this is what we expect
  715. }
  716. en = al1.GetEnumerator ();
  717. en.MoveNext ();
  718. al1.Reverse ();
  719. try {
  720. en.MoveNext ();
  721. Assert.Fail ("Reverse() didn't invalidate the enumerator");
  722. } catch (InvalidOperationException) {
  723. // do nothing...this is what we expect
  724. }
  725. en = al1.GetEnumerator ();
  726. en.MoveNext ();
  727. al1.Sort ();
  728. try {
  729. en.MoveNext ();
  730. Assert.Fail ("Sort() didn't invalidate the enumerator");
  731. } catch (InvalidOperationException) {
  732. // do nothing...this is what we expect
  733. }
  734. }
  735. [Test]
  736. public void TestGetEnumerator ()
  737. {
  738. {
  739. bool errorThrown = false;
  740. try {
  741. ArrayList a = new ArrayList ();
  742. IEnumerator en = a.GetEnumerator (-1, 1);
  743. } catch (ArgumentOutOfRangeException) {
  744. errorThrown = true;
  745. }
  746. Assert.IsTrue (errorThrown, "negative index error not thrown");
  747. }
  748. {
  749. bool errorThrown = false;
  750. try {
  751. ArrayList a = new ArrayList ();
  752. IEnumerator en = a.GetEnumerator (1, -1);
  753. } catch (ArgumentOutOfRangeException) {
  754. errorThrown = true;
  755. }
  756. Assert.IsTrue (errorThrown, "negative index error not thrown");
  757. }
  758. {
  759. bool errorThrown = false;
  760. try {
  761. ArrayList a = new ArrayList ();
  762. IEnumerator en = a.GetEnumerator (1, 1);
  763. } catch (ArgumentException) {
  764. errorThrown = true;
  765. }
  766. Assert.IsTrue (errorThrown, "out-of-range index error not thrown");
  767. }
  768. {
  769. String [] s1 = { "this", "is", "a", "test" };
  770. ArrayList al1 = new ArrayList (s1);
  771. IEnumerator en = al1.GetEnumerator ();
  772. Assert.IsNotNull (en, "No enumerator");
  773. for (int i = 0; i < s1.Length; i++) {
  774. en.MoveNext ();
  775. Assert.AreEqual (al1 [i], en.Current, "Not enumerating");
  776. }
  777. }
  778. {
  779. String [] s1 = { "this", "is", "a", "test" };
  780. ArrayList al1 = new ArrayList (s1);
  781. IEnumerator en = al1.GetEnumerator (1, 2);
  782. Assert.IsNotNull (en, "No enumerator");
  783. for (int i = 0; i < 2; i++) {
  784. en.MoveNext ();
  785. Assert.AreEqual (al1 [i + 1], en.Current, "Not enumerating");
  786. }
  787. }
  788. }
  789. [Test]
  790. [ExpectedException (typeof (ArgumentException))]
  791. public void GetEnumerator_IndexOverflow ()
  792. {
  793. ArrayList al = new ArrayList ();
  794. al.Add (this);
  795. al.GetEnumerator (Int32.MaxValue, 0);
  796. }
  797. [Test]
  798. [ExpectedException (typeof (ArgumentException))]
  799. public void GetEnumerator_CountOverflow ()
  800. {
  801. ArrayList al = new ArrayList ();
  802. al.Add (this);
  803. al.GetEnumerator (0, Int32.MaxValue);
  804. }
  805. [Test]
  806. public void TestGetRange ()
  807. {
  808. {
  809. bool errorThrown = false;
  810. try {
  811. ArrayList a = new ArrayList ();
  812. ArrayList b = a.GetRange (-1, 1);
  813. } catch (ArgumentOutOfRangeException) {
  814. errorThrown = true;
  815. }
  816. Assert.IsTrue (errorThrown, "negative index error not thrown");
  817. }
  818. {
  819. bool errorThrown = false;
  820. try {
  821. ArrayList a = new ArrayList ();
  822. ArrayList b = a.GetRange (1, -1);
  823. } catch (ArgumentOutOfRangeException) {
  824. errorThrown = true;
  825. }
  826. Assert.IsTrue (errorThrown, "negative index error not thrown");
  827. }
  828. {
  829. bool errorThrown = false;
  830. try {
  831. ArrayList a = new ArrayList ();
  832. ArrayList b = a.GetRange (1, 1);
  833. } catch (ArgumentException) {
  834. errorThrown = true;
  835. }
  836. Assert.IsTrue (errorThrown, "out-of-range index error not thrown");
  837. }
  838. {
  839. char [] chars = { 'a', 'b', 'c', 'd', 'e', 'f' };
  840. ArrayList a = new ArrayList (chars);
  841. ArrayList b = a.GetRange (1, 3);
  842. Assert.AreEqual (3, b.Count, "GetRange returned wrong size ArrayList");
  843. for (int i = 0; i < b.Count; i++) {
  844. Assert.AreEqual (chars [i + 1], b [i], "range didn't work");
  845. }
  846. a [2] = '?'; // should screw up ArrayList b.
  847. bool errorThrown = false;
  848. try {
  849. int i = b.Count;
  850. } catch (InvalidOperationException) {
  851. errorThrown = true;
  852. }
  853. Assert.AreEqual (true, errorThrown, "Munging 'a' should mess up 'b'");
  854. }
  855. {
  856. char [] chars = { 'a', 'b', 'c', 'd', 'e', 'f' };
  857. ArrayList a = new ArrayList (chars);
  858. ArrayList b = a.GetRange (3, 3);
  859. object [] obj_chars = b.ToArray ();
  860. for (int i = 0; i < 3; i++) {
  861. char c = (char) obj_chars [i];
  862. Assert.AreEqual (chars [i + 3], c, "range.ToArray didn't work");
  863. }
  864. char [] new_chars = (char []) b.ToArray (typeof (char));
  865. for (int i = 0; i < 3; i++) {
  866. Assert.AreEqual (chars [i + 3], new_chars [i], "range.ToArray with type didn't work");
  867. }
  868. }
  869. }
  870. [Test]
  871. [ExpectedException (typeof (ArgumentException))]
  872. public void GetRange_IndexOverflow ()
  873. {
  874. ArrayList al = new ArrayList ();
  875. al.Add (this);
  876. al.GetRange (Int32.MaxValue, 0);
  877. }
  878. [Test]
  879. [ExpectedException (typeof (ArgumentException))]
  880. public void GetRange_CountOverflow ()
  881. {
  882. ArrayList al = new ArrayList ();
  883. al.Add (this);
  884. al.GetRange (0, Int32.MaxValue);
  885. }
  886. [Test]
  887. public void TestIndexOf ()
  888. {
  889. {
  890. bool errorThrown = false;
  891. try {
  892. ArrayList a = new ArrayList (1);
  893. int i = a.IndexOf ('a', -1);
  894. } catch (ArgumentOutOfRangeException) {
  895. errorThrown = true;
  896. }
  897. Assert.IsTrue (errorThrown, "negative indexof error not thrown");
  898. }
  899. {
  900. bool errorThrown = false;
  901. try {
  902. ArrayList a = new ArrayList (1);
  903. int i = a.IndexOf ('a', 2);
  904. } catch (ArgumentOutOfRangeException) {
  905. errorThrown = true;
  906. }
  907. Assert.IsTrue (errorThrown, "past-end indexof error not thrown");
  908. }
  909. {
  910. bool errorThrown = false;
  911. try {
  912. ArrayList a = new ArrayList (1);
  913. int i = a.IndexOf ('a', 0, -1);
  914. } catch (ArgumentOutOfRangeException) {
  915. errorThrown = true;
  916. }
  917. Assert.IsTrue (errorThrown, "negative indexof error not thrown");
  918. }
  919. {
  920. bool errorThrown = false;
  921. try {
  922. ArrayList a = new ArrayList (1);
  923. int i = a.IndexOf ('a', 0, 2);
  924. } catch (ArgumentOutOfRangeException) {
  925. errorThrown = true;
  926. }
  927. Assert.IsTrue (errorThrown, "past-end indexof error not thrown");
  928. }
  929. {
  930. bool errorThrown = false;
  931. try {
  932. ArrayList a = new ArrayList (2);
  933. int i = a.IndexOf ('a', 1, 2);
  934. } catch (ArgumentOutOfRangeException) {
  935. errorThrown = true;
  936. }
  937. Assert.IsTrue (errorThrown, "past-end indexof error not thrown");
  938. }
  939. {
  940. char [] c = { 'a', 'b', 'c', 'd', 'e' };
  941. ArrayList a = new ArrayList (c);
  942. Assert.AreEqual (-1, a.IndexOf (null), "never find null");
  943. Assert.AreEqual (-1, a.IndexOf (null, 0), "never find null");
  944. Assert.AreEqual (-1, a.IndexOf (null, 0, 5), "never find null");
  945. Assert.AreEqual (2, a.IndexOf ('c'), "can't find elem");
  946. Assert.AreEqual (2, a.IndexOf ('c', 2), "can't find elem");
  947. Assert.AreEqual (2, a.IndexOf ('c', 2, 2), "can't find elem");
  948. Assert.AreEqual (-1, a.IndexOf ('c', 3, 2), "shouldn't find elem");
  949. Assert.AreEqual (-1, a.IndexOf ('?'), "shouldn't find");
  950. Assert.AreEqual (-1, a.IndexOf (3), "shouldn't find");
  951. }
  952. }
  953. [Test]
  954. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  955. public void IndexOf_StartIndexOverflow ()
  956. {
  957. ArrayList al = new ArrayList ();
  958. al.Add (this);
  959. al.IndexOf ('a', Int32.MaxValue, 1);
  960. }
  961. [Test]
  962. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  963. public void IndexOf_CountOverflow ()
  964. {
  965. ArrayList al = new ArrayList ();
  966. al.Add (this);
  967. al.IndexOf ('a', 1, Int32.MaxValue);
  968. }
  969. [Test]
  970. public void TestInsert ()
  971. {
  972. {
  973. bool errorThrown = false;
  974. try {
  975. ArrayList al1 =
  976. ArrayList.FixedSize (new ArrayList ());
  977. al1.Insert (0, "Hi!");
  978. } catch (NotSupportedException) {
  979. errorThrown = true;
  980. }
  981. Assert.IsTrue (errorThrown, "insert to fixed size error not thrown");
  982. }
  983. {
  984. bool errorThrown = false;
  985. try {
  986. ArrayList al1 =
  987. ArrayList.ReadOnly (new ArrayList ());
  988. al1.Insert (0, "Hi!");
  989. } catch (NotSupportedException) {
  990. errorThrown = true;
  991. }
  992. Assert.IsTrue (errorThrown, "insert to read only error not thrown");
  993. }
  994. {
  995. bool errorThrown = false;
  996. try {
  997. ArrayList al1 = new ArrayList (3);
  998. al1.Insert (-1, "Hi!");
  999. } catch (ArgumentOutOfRangeException) {
  1000. errorThrown = true;
  1001. }
  1002. Assert.IsTrue (errorThrown, "insert to read only error not thrown");
  1003. }
  1004. {
  1005. bool errorThrown = false;
  1006. try {
  1007. ArrayList al1 = new ArrayList (3);
  1008. al1.Insert (4, "Hi!");
  1009. } catch (ArgumentOutOfRangeException) {
  1010. errorThrown = true;
  1011. }
  1012. Assert.IsTrue (errorThrown, "insert to read only error not thrown");
  1013. }
  1014. {
  1015. ArrayList al1 = new ArrayList ();
  1016. Assert.AreEqual (0, al1.Count, "arraylist starts empty");
  1017. al1.Insert (0, 'a');
  1018. al1.Insert (1, 'b');
  1019. al1.Insert (0, 'c');
  1020. Assert.AreEqual (3, al1.Count, "arraylist needs stuff");
  1021. Assert.AreEqual ('c', al1 [0], "arraylist got stuff");
  1022. Assert.AreEqual ('a', al1 [1], "arraylist got stuff");
  1023. Assert.AreEqual ('b', al1 [2], "arraylist got stuff");
  1024. }
  1025. }
  1026. [Test]
  1027. public void TestInsertRange ()
  1028. {
  1029. {
  1030. bool errorThrown = false;
  1031. try {
  1032. ArrayList al1 =
  1033. ArrayList.FixedSize (new ArrayList ());
  1034. string [] s = { "Hi!" };
  1035. al1.InsertRange (0, s);
  1036. } catch (NotSupportedException) {
  1037. errorThrown = true;
  1038. }
  1039. Assert.IsTrue (errorThrown, "insert to fixed size error not thrown");
  1040. }
  1041. {
  1042. bool errorThrown = false;
  1043. try {
  1044. ArrayList al1 =
  1045. ArrayList.ReadOnly (new ArrayList ());
  1046. string [] s = { "Hi!" };
  1047. al1.InsertRange (0, s);
  1048. } catch (NotSupportedException) {
  1049. errorThrown = true;
  1050. }
  1051. Assert.IsTrue (errorThrown, "insert to read only error not thrown");
  1052. }
  1053. {
  1054. bool errorThrown = false;
  1055. try {
  1056. ArrayList al1 = new ArrayList (3);
  1057. string [] s = { "Hi!" };
  1058. al1.InsertRange (-1, s);
  1059. } catch (ArgumentOutOfRangeException) {
  1060. errorThrown = true;
  1061. }
  1062. Assert.IsTrue (errorThrown, "negative index insert error not thrown");
  1063. }
  1064. {
  1065. bool errorThrown = false;
  1066. try {
  1067. ArrayList al1 = new ArrayList (3);
  1068. string [] s = { "Hi!" };
  1069. al1.InsertRange (4, s);
  1070. } catch (ArgumentOutOfRangeException) {
  1071. errorThrown = true;
  1072. }
  1073. Assert.IsTrue (errorThrown, "out-of-range insert error not thrown");
  1074. }
  1075. {
  1076. bool errorThrown = false;
  1077. try {
  1078. ArrayList al1 = new ArrayList (3);
  1079. al1.InsertRange (0, null);
  1080. } catch (ArgumentNullException) {
  1081. errorThrown = true;
  1082. }
  1083. Assert.IsTrue (errorThrown, "null insert error not thrown");
  1084. }
  1085. {
  1086. char [] c = { 'a', 'b', 'c' };
  1087. ArrayList a = new ArrayList (c);
  1088. a.InsertRange (1, c);
  1089. Assert.AreEqual ('a', a [0], "bad insert 1");
  1090. Assert.AreEqual ('a', a [1], "bad insert 2");
  1091. Assert.AreEqual ('b', a [2], "bad insert 3");
  1092. Assert.AreEqual ('c', a [3], "bad insert 4");
  1093. Assert.AreEqual ('b', a [4], "bad insert 5");
  1094. Assert.AreEqual ('c', a [5], "bad insert 6");
  1095. }
  1096. }
  1097. [Test]
  1098. public void TestLastIndexOf ()
  1099. {
  1100. //{
  1101. //bool errorThrown = false;
  1102. //try {
  1103. //ArrayList a = new ArrayList(1);
  1104. //int i = a.LastIndexOf('a', -1);
  1105. //} catch (ArgumentOutOfRangeException) {
  1106. //errorThrown = true;
  1107. //}
  1108. //Assert.IsTrue (//errorThrown, "first negative lastindexof error not thrown");
  1109. //}
  1110. {
  1111. bool errorThrown = false;
  1112. try {
  1113. ArrayList a = new ArrayList (1);
  1114. int i = a.LastIndexOf ('a', 2);
  1115. } catch (ArgumentOutOfRangeException) {
  1116. errorThrown = true;
  1117. }
  1118. Assert.IsTrue (errorThrown, "past-end lastindexof error not thrown");
  1119. }
  1120. //{
  1121. //bool errorThrown = false;
  1122. //try {
  1123. //ArrayList a = new ArrayList(1);
  1124. //int i = a.LastIndexOf('a', 0, -1);
  1125. //} catch (ArgumentOutOfRangeException) {
  1126. //errorThrown = true;
  1127. //}
  1128. //Assert.IsTrue (//errorThrown, "second negative lastindexof error not thrown");
  1129. //}
  1130. //{
  1131. //bool errorThrown = false;
  1132. //try {
  1133. //ArrayList a = new ArrayList(1);
  1134. //int i = a.LastIndexOf('a', 0, 2);
  1135. //} catch (ArgumentOutOfRangeException) {
  1136. //errorThrown = true;
  1137. //}
  1138. //Assert.IsTrue (//errorThrown, "past-end lastindexof error not thrown");
  1139. //}
  1140. //{
  1141. //bool errorThrown = false;
  1142. //try {
  1143. //ArrayList a = new ArrayList(2);
  1144. //int i = a.LastIndexOf('a', 0, 2);
  1145. //} catch (ArgumentOutOfRangeException) {
  1146. //errorThrown = true;
  1147. //}
  1148. //Assert.IsTrue (//errorThrown, "past-end lastindexof error not thrown");
  1149. //}
  1150. int iTest = 0;
  1151. try {
  1152. char [] c = { 'a', 'b', 'c', 'd', 'e' };
  1153. ArrayList a = new ArrayList (c);
  1154. Assert.AreEqual (-1, a.LastIndexOf (null), "never find null");
  1155. iTest++;
  1156. Assert.AreEqual (-1, a.LastIndexOf (null, 4), "never find null");
  1157. iTest++;
  1158. Assert.AreEqual (-1, a.LastIndexOf (null, 4, 5), "never find null");
  1159. iTest++;
  1160. Assert.AreEqual (2, a.LastIndexOf ('c'), "can't find elem");
  1161. iTest++;
  1162. Assert.AreEqual (2, a.LastIndexOf ('c', 4), "can't find elem");
  1163. iTest++;
  1164. Assert.AreEqual (2, a.LastIndexOf ('c', 3, 2), "can't find elem");
  1165. iTest++;
  1166. Assert.AreEqual (-1, a.LastIndexOf ('c', 4, 2), "shouldn't find elem");
  1167. iTest++;
  1168. Assert.AreEqual (-1, a.LastIndexOf ('?'), "shouldn't find");
  1169. iTest++;
  1170. Assert.AreEqual (-1, a.LastIndexOf (1), "shouldn't find");
  1171. } catch (Exception e) {
  1172. Assert.Fail ("Unexpected exception caught when iTest=" + iTest + ". e=" + e);
  1173. }
  1174. }
  1175. [Test]
  1176. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  1177. public void LastIndexOf_StartIndexOverflow ()
  1178. {
  1179. ArrayList al = new ArrayList ();
  1180. al.Add (this);
  1181. al.LastIndexOf ('a', Int32.MaxValue, 1);
  1182. }
  1183. [Test]
  1184. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  1185. public void LastIndexOf_CountOverflow ()
  1186. {
  1187. ArrayList al = new ArrayList ();
  1188. al.Add (this);
  1189. al.LastIndexOf ('a', 1, Int32.MaxValue);
  1190. }
  1191. [Test]
  1192. public void TestReadOnly ()
  1193. {
  1194. {
  1195. bool errorThrown = false;
  1196. try {
  1197. ArrayList al1 = ArrayList.ReadOnly (null);
  1198. } catch (ArgumentNullException) {
  1199. errorThrown = true;
  1200. }
  1201. Assert.IsTrue (errorThrown, "null arg error not thrown");
  1202. }
  1203. {
  1204. ArrayList al1 = new ArrayList ();
  1205. Assert.AreEqual (false, al1.IsReadOnly, "arrays start writeable.");
  1206. ArrayList al2 = ArrayList.ReadOnly (al1);
  1207. Assert.AreEqual (true, al2.IsReadOnly, "should be readonly.");
  1208. }
  1209. }
  1210. [Test]
  1211. public void TestRemove ()
  1212. {
  1213. {
  1214. bool errorThrown = false;
  1215. try {
  1216. ArrayList al1 =
  1217. ArrayList.FixedSize (new ArrayList (3));
  1218. al1.Remove (1);
  1219. } catch (NotSupportedException) {
  1220. errorThrown = true;
  1221. }
  1222. Assert.IsTrue (errorThrown, "remove fixed size error not thrown");
  1223. }
  1224. {
  1225. bool errorThrown = false;
  1226. try {
  1227. ArrayList al1 =
  1228. ArrayList.ReadOnly (new ArrayList (3));
  1229. al1.Remove (1);
  1230. } catch (NotSupportedException) {
  1231. errorThrown = true;
  1232. }
  1233. Assert.IsTrue (errorThrown, "remove read only error not thrown");
  1234. }
  1235. {
  1236. char [] c = { 'a', 'b', 'c' };
  1237. ArrayList a = new ArrayList (c);
  1238. a.Remove (1);
  1239. a.Remove ('?');
  1240. Assert.AreEqual (c.Length, a.Count, "should be unchanged");
  1241. a.Remove ('a');
  1242. Assert.AreEqual (2, a.Count, "should be changed");
  1243. Assert.AreEqual ('b', a [0], "should have shifted");
  1244. Assert.AreEqual ('c', a [1], "should have shifted");
  1245. }
  1246. }
  1247. [Test]
  1248. public void TestRemoveAt ()
  1249. {
  1250. {
  1251. bool errorThrown = false;
  1252. try {
  1253. ArrayList al1 =
  1254. ArrayList.FixedSize (new ArrayList (3));
  1255. al1.RemoveAt (1);
  1256. } catch (NotSupportedException) {
  1257. errorThrown = true;
  1258. }
  1259. Assert.IsTrue (errorThrown, "remove from fixed size error not thrown");
  1260. }
  1261. {
  1262. bool errorThrown = false;
  1263. try {
  1264. ArrayList al1 =
  1265. ArrayList.ReadOnly (new ArrayList (3));
  1266. al1.RemoveAt (1);
  1267. } catch (NotSupportedException) {
  1268. errorThrown = true;
  1269. }
  1270. Assert.IsTrue (errorThrown, "remove from read only error not thrown");
  1271. }
  1272. {
  1273. bool errorThrown = false;
  1274. try {
  1275. ArrayList al1 = new ArrayList (3);
  1276. al1.RemoveAt (-1);
  1277. } catch (ArgumentOutOfRangeException) {
  1278. errorThrown = true;
  1279. }
  1280. Assert.IsTrue (errorThrown, "remove at negative index error not thrown");
  1281. }
  1282. {
  1283. bool errorThrown = false;
  1284. try {
  1285. ArrayList al1 = new ArrayList (3);
  1286. al1.RemoveAt (4);
  1287. } catch (ArgumentOutOfRangeException) {
  1288. errorThrown = true;
  1289. }
  1290. Assert.IsTrue (errorThrown, "remove at out-of-range index error not thrown");
  1291. }
  1292. {
  1293. char [] c = { 'a', 'b', 'c' };
  1294. ArrayList a = new ArrayList (c);
  1295. a.RemoveAt (0);
  1296. Assert.AreEqual (2, a.Count, "should be changed");
  1297. Assert.AreEqual ('b', a [0], "should have shifted");
  1298. Assert.AreEqual ('c', a [1], "should have shifted");
  1299. }
  1300. }
  1301. [Test]
  1302. public void TestRemoveRange ()
  1303. {
  1304. {
  1305. bool errorThrown = false;
  1306. try {
  1307. ArrayList al1 =
  1308. ArrayList.FixedSize (new ArrayList (3));
  1309. al1.RemoveRange (0, 1);
  1310. } catch (NotSupportedException) {
  1311. errorThrown = true;
  1312. }
  1313. Assert.IsTrue (errorThrown, "removerange from fixed size error not thrown");
  1314. }
  1315. {
  1316. bool errorThrown = false;
  1317. try {
  1318. ArrayList al1 =
  1319. ArrayList.ReadOnly (new ArrayList (3));
  1320. al1.RemoveRange (0, 1);
  1321. } catch (NotSupportedException) {
  1322. errorThrown = true;
  1323. }
  1324. Assert.IsTrue (errorThrown, "removerange from read only error not thrown");
  1325. }
  1326. {
  1327. bool errorThrown = false;
  1328. try {
  1329. ArrayList al1 = new ArrayList (3);
  1330. al1.RemoveRange (-1, 1);
  1331. } catch (ArgumentOutOfRangeException) {
  1332. errorThrown = true;
  1333. }
  1334. Assert.IsTrue (errorThrown, "removerange at negative index error not thrown");
  1335. }
  1336. {
  1337. bool errorThrown = false;
  1338. try {
  1339. ArrayList al1 = new ArrayList (3);
  1340. al1.RemoveRange (0, -1);
  1341. } catch (ArgumentOutOfRangeException) {
  1342. errorThrown = true;
  1343. }
  1344. Assert.IsTrue (errorThrown, "removerange at negative index error not thrown");
  1345. }
  1346. {
  1347. bool errorThrown = false;
  1348. try {
  1349. ArrayList al1 = new ArrayList (3);
  1350. al1.RemoveRange (2, 3);
  1351. } catch (ArgumentException) {
  1352. errorThrown = true;
  1353. }
  1354. Assert.IsTrue (errorThrown, "removerange at bad range error not thrown");
  1355. }
  1356. {
  1357. char [] c = { 'a', 'b', 'c' };
  1358. ArrayList a = new ArrayList (c);
  1359. a.RemoveRange (1, 2);
  1360. Assert.AreEqual (1, a.Count, "should be changed");
  1361. Assert.AreEqual ('a', a [0], "should have shifted");
  1362. }
  1363. }
  1364. [Test]
  1365. [ExpectedException (typeof (ArgumentException))]
  1366. public void RemoveRange_IndexOverflow ()
  1367. {
  1368. ArrayList al = new ArrayList ();
  1369. al.Add (this);
  1370. al.RemoveRange (Int32.MaxValue, 1);
  1371. }
  1372. [Test]
  1373. [ExpectedException (typeof (ArgumentException))]
  1374. public void RemoveRange_CountOverflow ()
  1375. {
  1376. ArrayList al = new ArrayList ();
  1377. al.Add (this);
  1378. al.RemoveRange (1, Int32.MaxValue);
  1379. }
  1380. [Test]
  1381. public void TestRepeat ()
  1382. {
  1383. {
  1384. bool errorThrown = false;
  1385. try {
  1386. ArrayList al1 = ArrayList.Repeat ('c', -1);
  1387. } catch (ArgumentOutOfRangeException) {
  1388. errorThrown = true;
  1389. }
  1390. Assert.IsTrue (errorThrown, "repeat negative copies error not thrown");
  1391. }
  1392. {
  1393. ArrayList al1 = ArrayList.Repeat ("huh?", 0);
  1394. Assert.AreEqual (0, al1.Count, "should be nothing in array");
  1395. }
  1396. {
  1397. ArrayList al1 = ArrayList.Repeat ("huh?", 3);
  1398. Assert.AreEqual (3, al1.Count, "should be something in array");
  1399. Assert.AreEqual ("huh?", al1 [0], "array elem doesn't check");
  1400. Assert.AreEqual ("huh?", al1 [1], "array elem doesn't check");
  1401. Assert.AreEqual ("huh?", al1 [2], "array elem doesn't check");
  1402. }
  1403. }
  1404. [Test]
  1405. public void TestReverse ()
  1406. {
  1407. {
  1408. bool errorThrown = false;
  1409. try {
  1410. ArrayList al1 =
  1411. ArrayList.ReadOnly (new ArrayList ());
  1412. al1.Reverse ();
  1413. } catch (NotSupportedException) {
  1414. errorThrown = true;
  1415. }
  1416. Assert.IsTrue (errorThrown, "reverse on read only error not thrown");
  1417. }
  1418. {
  1419. bool errorThrown = false;
  1420. try {
  1421. char [] c = new Char [2];
  1422. ArrayList al1 = new ArrayList (c);
  1423. al1.Reverse (0, 3);
  1424. } catch (ArgumentException) {
  1425. errorThrown = true;
  1426. }
  1427. Assert.IsTrue (errorThrown, "error not thrown");
  1428. }
  1429. {
  1430. bool errorThrown = false;
  1431. try {
  1432. char [] c = new Char [2];
  1433. ArrayList al1 = new ArrayList (c);
  1434. al1.Reverse (3, 0);
  1435. } catch (ArgumentException) {
  1436. errorThrown = true;
  1437. }
  1438. Assert.IsTrue (errorThrown, "error not thrown");
  1439. }
  1440. {
  1441. char [] c = { 'a', 'b', 'c', 'd', 'e' };
  1442. ArrayList al1 = new ArrayList (c);
  1443. al1.Reverse (2, 1);
  1444. for (int i = 0; i < al1.Count; i++) {
  1445. Assert.AreEqual (c [i], al1 [i], "Should be no change yet");
  1446. }
  1447. al1.Reverse ();
  1448. for (int i = 0; i < al1.Count; i++) {
  1449. Assert.AreEqual (c [i], al1 [4 - i], "Should be reversed");
  1450. }
  1451. al1.Reverse ();
  1452. for (int i = 0; i < al1.Count; i++) {
  1453. Assert.AreEqual (c [i], al1 [i], "Should be back to normal");
  1454. }
  1455. al1.Reverse (1, 3);
  1456. Assert.AreEqual (c [0], al1 [0], "Should be back to normal");
  1457. Assert.AreEqual (c [3], al1 [1], "Should be back to normal");
  1458. Assert.AreEqual (c [2], al1 [2], "Should be back to normal");
  1459. Assert.AreEqual (c [1], al1 [3], "Should be back to normal");
  1460. Assert.AreEqual (c [4], al1 [4], "Should be back to normal");
  1461. }
  1462. }
  1463. [Test]
  1464. [ExpectedException (typeof (ArgumentException))]
  1465. public void Reverse_IndexOverflow ()
  1466. {
  1467. ArrayList al = new ArrayList ();
  1468. al.Add (this);
  1469. al.Reverse (Int32.MaxValue, 1);
  1470. }
  1471. [Test]
  1472. [ExpectedException (typeof (ArgumentException))]
  1473. public void Reverse_CountOverflow ()
  1474. {
  1475. ArrayList al = new ArrayList ();
  1476. al.Add (this);
  1477. al.Reverse (1, Int32.MaxValue);
  1478. }
  1479. [Test]
  1480. public void TestSetRange ()
  1481. {
  1482. {
  1483. bool errorThrown = false;
  1484. try {
  1485. char [] c = { 'a', 'b', 'c' };
  1486. ArrayList al1 =
  1487. ArrayList.ReadOnly (new ArrayList (3));
  1488. al1.SetRange (0, c);
  1489. } catch (NotSupportedException) {
  1490. errorThrown = true;
  1491. } catch (Exception e) {
  1492. Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString ());
  1493. }
  1494. Assert.IsTrue (errorThrown, "setrange on read only error not thrown");
  1495. }
  1496. {
  1497. bool errorThrown = false;
  1498. try {
  1499. ArrayList al1 = new ArrayList (3);
  1500. al1.SetRange (0, null);
  1501. } catch (ArgumentNullException) {
  1502. errorThrown = true;
  1503. } catch (ArgumentOutOfRangeException) {
  1504. errorThrown = true;
  1505. } catch (Exception e) {
  1506. Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString ());
  1507. }
  1508. Assert.IsTrue (errorThrown, "setrange with null error not thrown");
  1509. }
  1510. {
  1511. bool errorThrown = false;
  1512. try {
  1513. char [] c = { 'a', 'b', 'c' };
  1514. ArrayList al1 = new ArrayList (3);
  1515. al1.SetRange (-1, c);
  1516. } catch (ArgumentOutOfRangeException) {
  1517. errorThrown = true;
  1518. } catch (Exception e) {
  1519. Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString ());
  1520. }
  1521. Assert.IsTrue (errorThrown, "setrange with negative index error not thrown");
  1522. }
  1523. {
  1524. bool errorThrown = false;
  1525. try {
  1526. char [] c = { 'a', 'b', 'c' };
  1527. ArrayList al1 = new ArrayList (3);
  1528. al1.SetRange (2, c);
  1529. } catch (ArgumentOutOfRangeException) {
  1530. errorThrown = true;
  1531. } catch (Exception e) {
  1532. Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString ());
  1533. }
  1534. Assert.IsTrue (errorThrown, "setrange with too much error not thrown");
  1535. }
  1536. {
  1537. char [] c = { 'a', 'b', 'c' };
  1538. ArrayList al1 = ArrayList.Repeat ('?', 3);
  1539. Assert.IsTrue (c [0] != (char) al1 [0], "no match yet");
  1540. Assert.IsTrue (c [1] != (char) al1 [1], "no match yet");
  1541. Assert.IsTrue (c [2] != (char) al1 [2], "no match yet");
  1542. al1.SetRange (0, c);
  1543. Assert.AreEqual (c [0], al1 [0], "should match");
  1544. Assert.AreEqual (c [1], al1 [1], "should match");
  1545. Assert.AreEqual (c [2], al1 [2], "should match");
  1546. }
  1547. }
  1548. [Test]
  1549. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  1550. public void SetRange_Overflow ()
  1551. {
  1552. ArrayList al = new ArrayList ();
  1553. al.Add (this);
  1554. al.SetRange (Int32.MaxValue, new ArrayList ());
  1555. }
  1556. [Test]
  1557. public void TestInsertRange_this ()
  1558. {
  1559. String [] s1 = { "this", "is", "a", "test" };
  1560. ArrayList al = new ArrayList (s1);
  1561. al.InsertRange (2, al);
  1562. String [] s2 = { "this", "is", "this", "is", "a", "test", "a", "test" };
  1563. for (int i = 0; i < al.Count; i++) {
  1564. Assert.AreEqual (s2 [i], al [i], "at i=" + i);
  1565. }
  1566. }
  1567. [Test]
  1568. public void TestSort ()
  1569. {
  1570. {
  1571. bool errorThrown = false;
  1572. try {
  1573. ArrayList al1 =
  1574. ArrayList.ReadOnly (new ArrayList ());
  1575. al1.Sort ();
  1576. } catch (NotSupportedException) {
  1577. errorThrown = true;
  1578. }
  1579. Assert.IsTrue (errorThrown, "sort on read only error not thrown");
  1580. }
  1581. {
  1582. char [] starter = { 'd', 'b', 'f', 'e', 'a', 'c' };
  1583. ArrayList al1 = new ArrayList (starter);
  1584. al1.Sort ();
  1585. Assert.AreEqual ('a', al1 [0], "Should be sorted");
  1586. Assert.AreEqual ('b', al1 [1], "Should be sorted");
  1587. Assert.AreEqual ('c', al1 [2], "Should be sorted");
  1588. Assert.AreEqual ('d', al1 [3], "Should be sorted");
  1589. Assert.AreEqual ('e', al1 [4], "Should be sorted");
  1590. Assert.AreEqual ('f', al1 [5], "Should be sorted");
  1591. }
  1592. {
  1593. ArrayList al1 = new ArrayList ();
  1594. al1.Add (null);
  1595. al1.Add (null);
  1596. al1.Add (32);
  1597. al1.Add (33);
  1598. al1.Add (null);
  1599. al1.Add (null);
  1600. al1.Sort ();
  1601. Assert.AreEqual (null, al1 [0], "Should be null (0)");
  1602. Assert.AreEqual (null, al1 [1], "Should be null (1)");
  1603. Assert.AreEqual (null, al1 [2], "Should be null (2)");
  1604. Assert.AreEqual (null, al1 [3], "Should be null (3)");
  1605. Assert.AreEqual (32, al1 [4], "Should be 32");
  1606. Assert.AreEqual (33, al1 [5], "Should be 33");
  1607. }
  1608. }
  1609. [Test]
  1610. [ExpectedException (typeof (ArgumentException))]
  1611. public void Sort_IndexOverflow ()
  1612. {
  1613. ArrayList al = new ArrayList ();
  1614. al.Add (this);
  1615. al.Sort (Int32.MaxValue, 1, null);
  1616. }
  1617. [Test]
  1618. [ExpectedException (typeof (ArgumentException))]
  1619. public void Sort_CountOverflow ()
  1620. {
  1621. ArrayList al = new ArrayList ();
  1622. al.Add (this);
  1623. al.Sort (1, Int32.MaxValue, null);
  1624. }
  1625. // TODO - Sort with IComparers
  1626. // TODO - Synchronize
  1627. [Test]
  1628. public void TestToArray ()
  1629. {
  1630. {
  1631. bool errorThrown = false;
  1632. try {
  1633. ArrayList al1 = new ArrayList (3);
  1634. al1.ToArray (null);
  1635. } catch (ArgumentNullException) {
  1636. errorThrown = true;
  1637. }
  1638. Assert.IsTrue (errorThrown, "toarray with null error not thrown");
  1639. }
  1640. {
  1641. bool errorThrown = false;
  1642. try {
  1643. char [] c = { 'a', 'b', 'c' };
  1644. string s = "huh?";
  1645. ArrayList al1 = new ArrayList (c);
  1646. al1.ToArray (s.GetType ());
  1647. } catch (InvalidCastException) {
  1648. errorThrown = true;
  1649. }
  1650. Assert.IsTrue (errorThrown, "toarray with bad type error not thrown");
  1651. }
  1652. {
  1653. char [] c1 = { 'a', 'b', 'c', 'd', 'e' };
  1654. ArrayList al1 = new ArrayList (c1);
  1655. object [] o2 = al1.ToArray ();
  1656. for (int i = 0; i < c1.Length; i++) {
  1657. Assert.AreEqual (c1 [i], o2 [i], "should be copy");
  1658. }
  1659. Array c2 = al1.ToArray (c1 [0].GetType ());
  1660. for (int i = 0; i < c1.Length; i++) {
  1661. Assert.AreEqual (c1 [i], c2.GetValue (i), "should be copy");
  1662. }
  1663. }
  1664. }
  1665. [Test]
  1666. [ExpectedException (typeof (NotSupportedException))]
  1667. public void TrimToSize_ReadOnly ()
  1668. {
  1669. ArrayList al1 = ArrayList.ReadOnly (new ArrayList ());
  1670. al1.TrimToSize ();
  1671. }
  1672. [Test]
  1673. public void TrimToSize ()
  1674. {
  1675. ArrayList al1 = new ArrayList ();
  1676. #if NET_2_0
  1677. // Capacity is 0 under 2.0
  1678. int capacity = 4;
  1679. #else
  1680. int capacity = al1.Capacity;
  1681. #endif
  1682. int size = capacity / 2;
  1683. for (int i = 1; i <= size; i++) {
  1684. al1.Add ('?');
  1685. }
  1686. al1.RemoveAt (0);
  1687. al1.TrimToSize ();
  1688. Assert.AreEqual (size - 1, al1.Capacity, "no capacity match");
  1689. al1.Clear ();
  1690. al1.TrimToSize ();
  1691. Assert.AreEqual (capacity, al1.Capacity, "no default capacity");
  1692. }
  1693. class Comparer : IComparer
  1694. {
  1695. private bool called = false;
  1696. public bool Called
  1697. {
  1698. get
  1699. {
  1700. bool result = called;
  1701. called = false;
  1702. return called;
  1703. }
  1704. }
  1705. public int Compare (object x, object y)
  1706. {
  1707. called = true;
  1708. return 0;
  1709. }
  1710. }
  1711. [Test]
  1712. public void BinarySearch1_EmptyList ()
  1713. {
  1714. ArrayList list = new ArrayList ();
  1715. Assert.AreEqual (-1, list.BinarySearch (0), "BinarySearch");
  1716. }
  1717. [Test]
  1718. public void BinarySearch2_EmptyList ()
  1719. {
  1720. Comparer comparer = new Comparer ();
  1721. ArrayList list = new ArrayList ();
  1722. Assert.AreEqual (-1, list.BinarySearch (0, comparer), "BinarySearch");
  1723. // bug 77030 - the comparer isn't called for an empty array/list
  1724. Assert.IsTrue (!comparer.Called, "Called");
  1725. }
  1726. [Test]
  1727. public void BinarySearch3_EmptyList ()
  1728. {
  1729. Comparer comparer = new Comparer ();
  1730. ArrayList list = new ArrayList ();
  1731. Assert.AreEqual (-1, list.BinarySearch (0, 0, 0, compare

Large files files are truncated, but you can click here to view the full file