PageRenderTime 55ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/Mono.C5/Test/arrays/ArrayListTest.cs

https://bitbucket.org/danipen/mono
C# | 3840 lines | 3213 code | 596 blank | 31 comment | 128 complexity | 52f9b691a04199e37b226030aa67e1b4 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. /*
  2. Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.
  18. */
  19. using System;
  20. using C5;
  21. using NUnit.Framework;
  22. using SCG = System.Collections.Generic;
  23. namespace C5UnitTests.arrays.list
  24. {
  25. using CollectionOfInt = ArrayList<int>;
  26. [TestFixture]
  27. public class GenericTesters
  28. {
  29. [Test]
  30. public void TestEvents()
  31. {
  32. Fun<CollectionOfInt> factory = delegate() { return new CollectionOfInt(TenEqualityComparer.Default); };
  33. new C5UnitTests.Templates.Events.ListTester<CollectionOfInt>().Test(factory);
  34. new C5UnitTests.Templates.Events.QueueTester<CollectionOfInt>().Test(factory);
  35. new C5UnitTests.Templates.Events.StackTester<CollectionOfInt>().Test(factory);
  36. }
  37. [Test]
  38. public void Extensible()
  39. {
  40. C5UnitTests.Templates.Extensible.Clone.Tester<CollectionOfInt>();
  41. C5UnitTests.Templates.Extensible.Clone.ViewTester<CollectionOfInt>();
  42. C5UnitTests.Templates.Extensible.Serialization.Tester<CollectionOfInt>();
  43. C5UnitTests.Templates.Extensible.Serialization.ViewTester<CollectionOfInt>();
  44. }
  45. [Test]
  46. public void List()
  47. {
  48. C5UnitTests.Templates.List.Dispose.Tester<CollectionOfInt>();
  49. C5UnitTests.Templates.List.SCG_IList.Tester<CollectionOfInt>();
  50. }
  51. }
  52. static class Factory
  53. {
  54. public static ICollection<T> New<T>() { return new ArrayList<T>(); }
  55. }
  56. namespace Events
  57. {
  58. [TestFixture]
  59. public class IList_
  60. {
  61. private ArrayList<int> list;
  62. CollectionEventList<int> seen;
  63. [SetUp]
  64. public void Init()
  65. {
  66. list = new ArrayList<int>(TenEqualityComparer.Default);
  67. seen = new CollectionEventList<int>(IntEqualityComparer.Default);
  68. }
  69. private void listen() { seen.Listen(list, EventTypeEnum.Added); }
  70. [Test]
  71. public void Listenable()
  72. {
  73. Assert.AreEqual(EventTypeEnum.All, list.ListenableEvents);
  74. Assert.AreEqual(EventTypeEnum.None, list.ActiveEvents);
  75. listen();
  76. Assert.AreEqual(EventTypeEnum.Added, list.ActiveEvents);
  77. }
  78. [Test]
  79. public void SetThis()
  80. {
  81. list.Add(4); list.Add(56); list.Add(8);
  82. listen();
  83. list[1] = 45;
  84. seen.Check(new CollectionEvent<int>[] {
  85. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), list),
  86. new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(56,1), list),
  87. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), list),
  88. new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(45,1), list),
  89. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
  90. });
  91. }
  92. [Test]
  93. public void Insert()
  94. {
  95. list.Add(4); list.Add(56); list.Add(8);
  96. listen();
  97. list.Insert(1, 45);
  98. seen.Check(new CollectionEvent<int>[] {
  99. new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(45,1), list),
  100. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), list),
  101. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
  102. });
  103. }
  104. [Test]
  105. public void InsertAll()
  106. {
  107. list.Add(4); list.Add(56); list.Add(8);
  108. listen();
  109. list.InsertAll<int>(1, new int[] { 666, 777, 888 });
  110. //seen.Print(Console.Error);
  111. seen.Check(new CollectionEvent<int>[] {
  112. new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(666,1), list),
  113. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(666, 1), list),
  114. new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(777,2), list),
  115. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(777, 1), list),
  116. new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(888,3), list),
  117. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(888, 1), list),
  118. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
  119. });
  120. list.InsertAll<int>(1, new int[] {});
  121. seen.Check(new CollectionEvent<int>[] {});
  122. }
  123. [Test]
  124. public void InsertFirstLast()
  125. {
  126. list.Add(4); list.Add(56); list.Add(8);
  127. listen();
  128. list.InsertFirst(45);
  129. seen.Check(new CollectionEvent<int>[] {
  130. new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(45,0), list),
  131. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), list),
  132. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
  133. });
  134. list.InsertLast(88);
  135. seen.Check(new CollectionEvent<int>[] {
  136. new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(88,4), list),
  137. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(88, 1), list),
  138. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
  139. });
  140. }
  141. [Test]
  142. public void Remove()
  143. {
  144. list.Add(4); list.Add(56); list.Add(8);
  145. listen();
  146. list.Remove();
  147. seen.Check(new CollectionEvent<int>[] {
  148. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(8, 1), list),
  149. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
  150. }
  151. [Test]
  152. public void RemoveFirst()
  153. {
  154. list.Add(4); list.Add(56); list.Add(8);
  155. listen();
  156. list.RemoveFirst();
  157. seen.Check(new CollectionEvent<int>[] {
  158. new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(4,0), list),
  159. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(4, 1), list),
  160. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
  161. }
  162. [Test]
  163. public void RemoveLast()
  164. {
  165. list.Add(4); list.Add(56); list.Add(8);
  166. listen();
  167. list.RemoveLast();
  168. seen.Check(new CollectionEvent<int>[] {
  169. new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(8,2), list),
  170. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(8, 1), list),
  171. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
  172. }
  173. [Test]
  174. public void Reverse()
  175. {
  176. list.Add(4); list.Add(56); list.Add(8);
  177. listen();
  178. list.Reverse();
  179. seen.Check(new CollectionEvent<int>[] {
  180. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
  181. });
  182. list.View(1, 0).Reverse();
  183. seen.Check(new CollectionEvent<int>[] {});
  184. }
  185. [Test]
  186. public void Sort()
  187. {
  188. list.Add(4); list.Add(56); list.Add(8);
  189. listen();
  190. list.Sort();
  191. seen.Check(new CollectionEvent<int>[] {
  192. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
  193. });
  194. list.View(1, 0).Sort();
  195. seen.Check(new CollectionEvent<int>[] {});
  196. }
  197. [Test]
  198. public void Shuffle()
  199. {
  200. list.Add(4); list.Add(56); list.Add(8);
  201. listen();
  202. list.Shuffle();
  203. seen.Check(new CollectionEvent<int>[] {
  204. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
  205. });
  206. list.View(1, 0).Shuffle();
  207. seen.Check(new CollectionEvent<int>[] {});
  208. }
  209. [Test]
  210. public void RemoveAt()
  211. {
  212. list.Add(4); list.Add(56); list.Add(8);
  213. listen();
  214. list.RemoveAt(1);
  215. seen.Check(new CollectionEvent<int>[] {
  216. new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(56,1), list),
  217. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), list),
  218. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
  219. }
  220. [Test]
  221. public void RemoveInterval()
  222. {
  223. list.Add(4); list.Add(56); list.Add(8);
  224. listen();
  225. list.RemoveInterval(1, 2);
  226. seen.Check(new CollectionEvent<int>[] {
  227. new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(false,2,1), list),
  228. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
  229. });
  230. list.RemoveInterval(1, 0);
  231. seen.Check(new CollectionEvent<int>[] {});
  232. }
  233. [Test]
  234. public void Update()
  235. {
  236. list.Add(4); list.Add(56); list.Add(8);
  237. listen();
  238. list.Update(53);
  239. seen.Check(new CollectionEvent<int>[] {
  240. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), list),
  241. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(53, 1), list),
  242. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
  243. });
  244. list.Update(67);
  245. seen.Check(new CollectionEvent<int>[] {});
  246. }
  247. [Test]
  248. public void FindOrAdd()
  249. {
  250. list.Add(4); list.Add(56); list.Add(8);
  251. listen();
  252. int val = 53;
  253. list.FindOrAdd(ref val);
  254. seen.Check(new CollectionEvent<int>[] {});
  255. val = 67;
  256. list.FindOrAdd(ref val);
  257. seen.Check(new CollectionEvent<int>[] {
  258. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(67, 1), list),
  259. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
  260. });
  261. }
  262. [Test]
  263. public void UpdateOrAdd()
  264. {
  265. list.Add(4); list.Add(56); list.Add(8);
  266. listen();
  267. int val = 53;
  268. list.UpdateOrAdd(val);
  269. seen.Check(new CollectionEvent<int>[] {
  270. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), list),
  271. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(53, 1), list),
  272. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
  273. });
  274. val = 67;
  275. list.UpdateOrAdd(val);
  276. seen.Check(new CollectionEvent<int>[] {
  277. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(67, 1), list),
  278. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
  279. });
  280. list.UpdateOrAdd(51, out val);
  281. seen.Check(new CollectionEvent<int>[] {
  282. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(53, 1), list),
  283. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(51, 1), list),
  284. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
  285. });
  286. val = 67;
  287. list.UpdateOrAdd(81, out val);
  288. seen.Check(new CollectionEvent<int>[] {
  289. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(81, 1), list),
  290. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
  291. });
  292. }
  293. [Test]
  294. public void RemoveItem()
  295. {
  296. list.Add(4); list.Add(56); list.Add(18);
  297. listen();
  298. list.Remove(53);
  299. seen.Check(new CollectionEvent<int>[] {
  300. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), list),
  301. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
  302. list.Remove(11);
  303. seen.Check(new CollectionEvent<int>[] {
  304. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(18, 1), list),
  305. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
  306. }
  307. [Test]
  308. public void RemoveAll()
  309. {
  310. for (int i = 0; i < 10; i++)
  311. {
  312. list.Add(10 * i + 5);
  313. }
  314. listen();
  315. list.RemoveAll<int>(new int[] { 32, 187, 45 });
  316. //TODO: the order depends on internals of the HashSet
  317. seen.Check(new CollectionEvent<int>[] {
  318. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(35, 1), list),
  319. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(45, 1), list),
  320. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
  321. list.RemoveAll<int>(new int[] { 200, 300 });
  322. seen.Check(new CollectionEvent<int>[] {});
  323. }
  324. [Test]
  325. public void Clear()
  326. {
  327. list.Add(4); list.Add(56); list.Add(8);
  328. listen();
  329. list.View(1, 1).Clear();
  330. seen.Check(new CollectionEvent<int>[] {
  331. new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(false,1,1), list),
  332. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
  333. });
  334. list.Clear();
  335. seen.Check(new CollectionEvent<int>[] {
  336. new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(true,2,0), list),
  337. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
  338. });
  339. list.Clear();
  340. seen.Check(new CollectionEvent<int>[] {});
  341. }
  342. [Test]
  343. public void ListDispose()
  344. {
  345. list.Add(4); list.Add(56); list.Add(8);
  346. listen();
  347. list.View(1, 1).Dispose();
  348. seen.Check(new CollectionEvent<int>[] {});
  349. list.Dispose();
  350. seen.Check(new CollectionEvent<int>[] {
  351. new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(true,3,0), list),
  352. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
  353. });
  354. list.Dispose();
  355. seen.Check(new CollectionEvent<int>[] {});
  356. }
  357. [Test]
  358. public void RetainAll()
  359. {
  360. for (int i = 0; i < 10; i++)
  361. {
  362. list.Add(10 * i + 5);
  363. }
  364. listen();
  365. list.RetainAll<int>(new int[] { 32, 187, 45, 62, 82, 95, 2 });
  366. seen.Check(new CollectionEvent<int>[] {
  367. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(15, 1), list),
  368. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(25, 1), list),
  369. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(55, 1), list),
  370. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(75, 1), list),
  371. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
  372. list.RetainAll<int>(new int[] { 32, 187, 45, 62, 82, 95, 2 });
  373. seen.Check(new CollectionEvent<int>[] {});
  374. }
  375. [Test]
  376. public void RemoveAllCopies()
  377. {
  378. for (int i = 0; i < 10; i++)
  379. {
  380. list.Add(3 * i + 5);
  381. }
  382. listen();
  383. list.RemoveAllCopies(14);
  384. seen.Check(new CollectionEvent<int>[] {
  385. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(11, 1), list),
  386. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(14, 1), list),
  387. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(17, 1), list),
  388. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
  389. list.RemoveAllCopies(14);
  390. seen.Check(new CollectionEvent<int>[] {});
  391. }
  392. [Test]
  393. public void Add()
  394. {
  395. listen();
  396. seen.Check(new CollectionEvent<int>[0]);
  397. list.Add(23);
  398. seen.Check(new CollectionEvent<int>[] {
  399. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(23, 1), list),
  400. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
  401. }
  402. [Test]
  403. public void AddAll()
  404. {
  405. for (int i = 0; i < 10; i++)
  406. {
  407. list.Add(10 * i + 5);
  408. }
  409. listen();
  410. list.AddAll<int>(new int[] { 45, 56, 67 });
  411. seen.Check(new CollectionEvent<int>[] {
  412. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), list),
  413. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(56, 1), list),
  414. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(67, 1), list),
  415. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
  416. list.AddAll<int>(new int[] { });
  417. seen.Check(new CollectionEvent<int>[] {});
  418. }
  419. [TearDown]
  420. public void Dispose() { list = null; seen = null; }
  421. [Test]
  422. [ExpectedException(typeof(UnlistenableEventException))]
  423. public void ViewChanged()
  424. {
  425. IList<int> w = list.View(0, 0);
  426. w.CollectionChanged += new CollectionChangedHandler<int>(w_CollectionChanged);
  427. }
  428. [Test]
  429. [ExpectedException(typeof(UnlistenableEventException))]
  430. public void ViewCleared()
  431. {
  432. IList<int> w = list.View(0, 0);
  433. w.CollectionCleared += new CollectionClearedHandler<int>(w_CollectionCleared);
  434. }
  435. [Test]
  436. [ExpectedException(typeof(UnlistenableEventException))]
  437. public void ViewAdded()
  438. {
  439. IList<int> w = list.View(0, 0);
  440. w.ItemsAdded += new ItemsAddedHandler<int>(w_ItemAdded);
  441. }
  442. [Test]
  443. [ExpectedException(typeof(UnlistenableEventException))]
  444. public void ViewInserted()
  445. {
  446. IList<int> w = list.View(0, 0);
  447. w.ItemInserted += new ItemInsertedHandler<int>(w_ItemInserted);
  448. }
  449. [Test]
  450. [ExpectedException(typeof(UnlistenableEventException))]
  451. public void ViewRemoved()
  452. {
  453. IList<int> w = list.View(0, 0);
  454. w.ItemsRemoved += new ItemsRemovedHandler<int>(w_ItemRemoved);
  455. }
  456. [Test]
  457. [ExpectedException(typeof(UnlistenableEventException))]
  458. public void ViewRemovedAt()
  459. {
  460. IList<int> w = list.View(0, 0);
  461. w.ItemRemovedAt += new ItemRemovedAtHandler<int>(w_ItemRemovedAt);
  462. }
  463. void w_CollectionChanged(object sender)
  464. {
  465. throw new NotImplementedException();
  466. }
  467. void w_CollectionCleared(object sender, ClearedEventArgs eventArgs)
  468. {
  469. throw new NotImplementedException();
  470. }
  471. void w_ItemAdded(object sender, ItemCountEventArgs<int> eventArgs)
  472. {
  473. throw new NotImplementedException();
  474. }
  475. void w_ItemInserted(object sender, ItemAtEventArgs<int> eventArgs)
  476. {
  477. throw new NotImplementedException();
  478. }
  479. void w_ItemRemoved(object sender, ItemCountEventArgs<int> eventArgs)
  480. {
  481. throw new NotImplementedException();
  482. }
  483. void w_ItemRemovedAt(object sender, ItemAtEventArgs<int> eventArgs)
  484. {
  485. throw new NotImplementedException();
  486. }
  487. }
  488. [TestFixture]
  489. public class StackQueue
  490. {
  491. private ArrayList<int> list;
  492. CollectionEventList<int> seen;
  493. [SetUp]
  494. public void Init()
  495. {
  496. list = new ArrayList<int>(TenEqualityComparer.Default);
  497. seen = new CollectionEventList<int>(IntEqualityComparer.Default);
  498. }
  499. private void listen() { seen.Listen(list, EventTypeEnum.All); }
  500. [Test]
  501. public void EnqueueDequeue()
  502. {
  503. listen();
  504. list.Enqueue(67);
  505. seen.Check(new CollectionEvent<int>[] {
  506. new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(67,0), list),
  507. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(67, 1), list),
  508. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
  509. list.Enqueue(2);
  510. seen.Check(new CollectionEvent<int>[] {
  511. new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(2,1), list),
  512. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(2, 1), list),
  513. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
  514. list.Dequeue();
  515. seen.Check(new CollectionEvent<int>[] {
  516. new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(67,0), list),
  517. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(67, 1), list),
  518. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
  519. list.Dequeue();
  520. seen.Check(new CollectionEvent<int>[] {
  521. new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(2,0), list),
  522. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(2, 1), list),
  523. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
  524. }
  525. [Test]
  526. public void PushPop()
  527. {
  528. listen();
  529. seen.Check(new CollectionEvent<int>[0]);
  530. list.Push(23);
  531. seen.Check(new CollectionEvent<int>[] {
  532. new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(23,0), list),
  533. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(23, 1), list),
  534. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
  535. list.Push(-12);
  536. seen.Check(new CollectionEvent<int>[] {
  537. new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(-12,1), list),
  538. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(-12, 1), list),
  539. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
  540. list.Pop();
  541. seen.Check(new CollectionEvent<int>[] {
  542. new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(-12,1), list),
  543. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(-12, 1), list),
  544. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
  545. list.Pop();
  546. seen.Check(new CollectionEvent<int>[] {
  547. new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(23,0), list),
  548. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(23, 1), list),
  549. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
  550. }
  551. [TearDown]
  552. public void Dispose() { list = null; seen = null; }
  553. }
  554. }
  555. namespace Safety
  556. {
  557. /// <summary>
  558. /// Tests to see if the collection classes are robust for enumerable arguments that throw exceptions.
  559. /// </summary>
  560. [TestFixture]
  561. public class BadEnumerable
  562. {
  563. private ArrayList<int> list;
  564. [SetUp]
  565. public void Init()
  566. {
  567. list = new ArrayList<int>();
  568. }
  569. [Test]
  570. public void InsertAll()
  571. {
  572. list.Add(4); list.Add(56); list.Add(8);
  573. try
  574. {
  575. list.InsertAll<int>(1, new BadEnumerable<int>(new BadEnumerableException(), 9, 8, 7));
  576. Assert.Fail("Should not get here");
  577. }
  578. catch (BadEnumerableException) { }
  579. Assert.IsTrue(list.Check());
  580. Assert.IsTrue(IC.eq(list, 4, 9, 8, 7, 56, 8));
  581. }
  582. [Test]
  583. public void AddAll()
  584. {
  585. list.Add(4); list.Add(56); list.Add(8);
  586. try
  587. {
  588. list.View(0, 1).AddAll<int>(new BadEnumerable<int>(new BadEnumerableException(), 9, 8, 7));
  589. Assert.Fail("Should not get here");
  590. }
  591. catch (BadEnumerableException) { }
  592. Assert.IsTrue(list.Check());
  593. Assert.IsTrue(IC.eq(list, 4, 9, 8, 7, 56, 8));
  594. }
  595. [Test]
  596. public void RemoveAll()
  597. {
  598. list.Add(4); list.Add(56); list.Add(8);
  599. try
  600. {
  601. list.RemoveAll(new BadEnumerable<int>(new BadEnumerableException(), 9, 8, 7));
  602. Assert.Fail("Should not get here");
  603. }
  604. catch (BadEnumerableException) { }
  605. Assert.IsTrue(list.Check());
  606. Assert.IsTrue(IC.eq(list, 4, 56, 8));
  607. }
  608. [Test]
  609. public void RetainAll()
  610. {
  611. list.Add(4); list.Add(56); list.Add(8);
  612. try
  613. {
  614. list.RetainAll(new BadEnumerable<int>(new BadEnumerableException(), 9, 8, 7));
  615. Assert.Fail("Should not get here");
  616. }
  617. catch (BadEnumerableException) { }
  618. Assert.IsTrue(list.Check());
  619. Assert.IsTrue(IC.eq(list, 4, 56, 8));
  620. }
  621. [Test]
  622. public void ContainsAll()
  623. {
  624. list.Add(4); list.Add(56); list.Add(8);
  625. try
  626. {
  627. list.ContainsAll(new BadEnumerable<int>(new BadEnumerableException(), 4, 18));
  628. Assert.Fail("Should not get here");
  629. }
  630. catch (BadEnumerableException) { }
  631. Assert.IsTrue(list.Check());
  632. Assert.IsTrue(IC.eq(list, 4, 56, 8));
  633. }
  634. [TearDown]
  635. public void Dispose() { list = null; }
  636. }
  637. /// <summary>
  638. /// Tests to see if the collection classes are robust for delegate arguments that throw exceptions.
  639. /// </summary>
  640. [TestFixture]
  641. public class BadFun
  642. {
  643. private ArrayList<int> list;
  644. [SetUp]
  645. public void Init()
  646. {
  647. list = new ArrayList<int>();
  648. }
  649. [Test]
  650. public void NoTests() { }
  651. [TearDown]
  652. public void Dispose() { list = null; }
  653. }
  654. }
  655. namespace Enumerable
  656. {
  657. [TestFixture]
  658. public class Multiops
  659. {
  660. private ArrayList<int> list;
  661. private Fun<int, bool> always, never, even;
  662. [SetUp]
  663. public void Init()
  664. {
  665. list = new ArrayList<int>();
  666. always = delegate { return true; };
  667. never = delegate { return false; };
  668. even = delegate(int i) { return i % 2 == 0; };
  669. }
  670. [Test]
  671. public void All()
  672. {
  673. Assert.IsTrue(list.All(always));
  674. Assert.IsTrue(list.All(never));
  675. Assert.IsTrue(list.All(even));
  676. list.Add(8);
  677. Assert.IsTrue(list.All(always));
  678. Assert.IsFalse(list.All(never));
  679. Assert.IsTrue(list.All(even));
  680. list.Add(5);
  681. Assert.IsTrue(list.All(always));
  682. Assert.IsFalse(list.All(never));
  683. Assert.IsFalse(list.All(even));
  684. }
  685. [Test]
  686. public void Exists()
  687. {
  688. Assert.IsFalse(list.Exists(always));
  689. Assert.IsFalse(list.Exists(never));
  690. Assert.IsFalse(list.Exists(even));
  691. list.Add(5);
  692. Assert.IsTrue(list.Exists(always));
  693. Assert.IsFalse(list.Exists(never));
  694. Assert.IsFalse(list.Exists(even));
  695. list.Add(8);
  696. Assert.IsTrue(list.Exists(always));
  697. Assert.IsFalse(list.Exists(never));
  698. Assert.IsTrue(list.Exists(even));
  699. }
  700. [Test]
  701. public void Apply()
  702. {
  703. int sum = 0;
  704. Act<int> a = delegate(int i) { sum = i + 10 * sum; };
  705. list.Apply(a);
  706. Assert.AreEqual(0, sum);
  707. sum = 0;
  708. list.Add(5); list.Add(8); list.Add(7); list.Add(5);
  709. list.Apply(a);
  710. Assert.AreEqual(5875, sum);
  711. }
  712. [TearDown]
  713. public void Dispose() { list = null; }
  714. }
  715. [TestFixture]
  716. public class GetEnumerator
  717. {
  718. private ArrayList<int> list;
  719. [SetUp]
  720. public void Init() { list = new ArrayList<int>(); }
  721. [Test]
  722. public void Empty()
  723. {
  724. SCG.IEnumerator<int> e = list.GetEnumerator();
  725. Assert.IsFalse(e.MoveNext());
  726. }
  727. [Test]
  728. public void Normal()
  729. {
  730. list.Add(5);
  731. list.Add(8);
  732. list.Add(5);
  733. list.Add(5);
  734. list.Add(10);
  735. list.Add(1);
  736. SCG.IEnumerator<int> e = list.GetEnumerator();
  737. Assert.IsTrue(e.MoveNext());
  738. Assert.AreEqual(5, e.Current);
  739. Assert.IsTrue(e.MoveNext());
  740. Assert.AreEqual(8, e.Current);
  741. Assert.IsTrue(e.MoveNext());
  742. Assert.AreEqual(5, e.Current);
  743. Assert.IsTrue(e.MoveNext());
  744. Assert.AreEqual(5, e.Current);
  745. Assert.IsTrue(e.MoveNext());
  746. Assert.AreEqual(10, e.Current);
  747. Assert.IsTrue(e.MoveNext());
  748. Assert.AreEqual(1, e.Current);
  749. Assert.IsFalse(e.MoveNext());
  750. }
  751. [Test]
  752. public void DoDispose()
  753. {
  754. list.Add(5);
  755. list.Add(8);
  756. list.Add(5);
  757. SCG.IEnumerator<int> e = list.GetEnumerator();
  758. e.MoveNext();
  759. e.MoveNext();
  760. e.Dispose();
  761. }
  762. [Test]
  763. [ExpectedException(typeof(CollectionModifiedException))]
  764. public void MoveNextAfterUpdate()
  765. {
  766. list.Add(5);
  767. list.Add(8);
  768. list.Add(5);
  769. SCG.IEnumerator<int> e = list.GetEnumerator();
  770. e.MoveNext();
  771. list.Add(99);
  772. e.MoveNext();
  773. }
  774. [TearDown]
  775. public void Dispose() { list = null; }
  776. }
  777. }
  778. namespace CollectionOrSink
  779. {
  780. [TestFixture]
  781. public class Formatting
  782. {
  783. ICollection<int> coll;
  784. IFormatProvider rad16;
  785. [SetUp]
  786. public void Init() { coll = Factory.New<int>(); rad16 = new RadixFormatProvider(16); }
  787. [TearDown]
  788. public void Dispose() { coll = null; rad16 = null; }
  789. [Test]
  790. public void Format()
  791. {
  792. Assert.AreEqual("[ ]", coll.ToString());
  793. coll.AddAll<int>(new int[] { -4, 28, 129, 65530 });
  794. Assert.AreEqual("[ 0:-4, 1:28, 2:129, 3:65530 ]", coll.ToString());
  795. Assert.AreEqual("[ 0:-4, 1:1C, 2:81, 3:FFFA ]", coll.ToString(null, rad16));
  796. Assert.AreEqual("[ 0:-4, 1:28... ]", coll.ToString("L14", null));
  797. Assert.AreEqual("[ 0:-4, 1:1C... ]", coll.ToString("L14", rad16));
  798. }
  799. }
  800. [TestFixture]
  801. public class CollectionOrSink
  802. {
  803. private ArrayList<int> list;
  804. [SetUp]
  805. public void Init() { list = new ArrayList<int>(); }
  806. [Test]
  807. public void Choose()
  808. {
  809. list.Add(7);
  810. Assert.AreEqual(7, list.Choose());
  811. }
  812. [Test]
  813. [ExpectedException(typeof(NoSuchItemException))]
  814. public void BadChoose()
  815. {
  816. list.Choose();
  817. }
  818. [Test]
  819. public void CountEtAl()
  820. {
  821. Assert.AreEqual(0, list.Count);
  822. Assert.IsTrue(list.IsEmpty);
  823. Assert.IsTrue(list.AllowsDuplicates);
  824. list.Add(5);
  825. Assert.AreEqual(1, list.Count);
  826. Assert.IsFalse(list.IsEmpty);
  827. list.Add(5);
  828. Assert.AreEqual(2, list.Count);
  829. Assert.IsFalse(list.IsEmpty);
  830. list.Add(8);
  831. Assert.AreEqual(3, list.Count);
  832. }
  833. [Test]
  834. public void AddAll()
  835. {
  836. list.Add(3); list.Add(4); list.Add(5);
  837. ArrayList<int> list2 = new ArrayList<int>();
  838. list2.AddAll(list);
  839. Assert.IsTrue(IC.eq(list2, 3, 4, 5));
  840. list.AddAll(list2);
  841. Assert.IsTrue(IC.eq(list2, 3, 4, 5));
  842. Assert.IsTrue(IC.eq(list, 3, 4, 5, 3, 4, 5));
  843. }
  844. [TearDown]
  845. public void Dispose() { list = null; }
  846. }
  847. [TestFixture]
  848. public class FindPredicate
  849. {
  850. private ArrayList<int> list;
  851. Fun<int, bool> pred;
  852. [SetUp]
  853. public void Init()
  854. {
  855. list = new ArrayList<int>(TenEqualityComparer.Default);
  856. pred = delegate(int i) { return i % 5 == 0; };
  857. }
  858. [TearDown]
  859. public void Dispose() { list = null; }
  860. [Test]
  861. public void Find()
  862. {
  863. int i;
  864. Assert.IsFalse(list.Find(pred, out i));
  865. list.AddAll<int>(new int[] { 4, 22, 67, 37 });
  866. Assert.IsFalse(list.Find(pred, out i));
  867. list.AddAll<int>(new int[] { 45, 122, 675, 137 });
  868. Assert.IsTrue(list.Find(pred, out i));
  869. Assert.AreEqual(45, i);
  870. }
  871. [Test]
  872. public void FindLast()
  873. {
  874. int i;
  875. Assert.IsFalse(list.FindLast(pred, out i));
  876. list.AddAll<int>(new int[] { 4, 22, 67, 37 });
  877. Assert.IsFalse(list.FindLast(pred, out i));
  878. list.AddAll<int>(new int[] { 45, 122, 675, 137 });
  879. Assert.IsTrue(list.FindLast(pred, out i));
  880. Assert.AreEqual(675, i);
  881. }
  882. [Test]
  883. public void FindIndex()
  884. {
  885. Assert.IsFalse(0 <= list.FindIndex(pred));
  886. list.AddAll<int>(new int[] { 4, 22, 67, 37 });
  887. Assert.IsFalse(0 <= list.FindIndex(pred));
  888. list.AddAll<int>(new int[] { 45, 122, 675, 137 });
  889. Assert.AreEqual(4, list.FindIndex(pred));
  890. }
  891. [Test]
  892. public void FindLastIndex()
  893. {
  894. Assert.IsFalse(0 <= list.FindLastIndex(pred));
  895. list.AddAll<int>(new int[] { 4, 22, 67, 37 });
  896. Assert.IsFalse(0 <= list.FindLastIndex(pred));
  897. list.AddAll<int>(new int[] { 45, 122, 675, 137 });
  898. Assert.AreEqual(6, list.FindLastIndex(pred));
  899. }
  900. }
  901. [TestFixture]
  902. public class UniqueItems
  903. {
  904. private ArrayList<int> list;
  905. [SetUp]
  906. public void Init() { list = new ArrayList<int>(); }
  907. [TearDown]
  908. public void Dispose() { list = null; }
  909. [Test]
  910. public void Test()
  911. {
  912. Assert.IsTrue(IC.seteq(list.UniqueItems()));
  913. Assert.IsTrue(IC.seteq(list.ItemMultiplicities()));
  914. list.AddAll<int>(new int[] { 7, 9, 7 });
  915. Assert.IsTrue(IC.seteq(list.UniqueItems(), 7, 9));
  916. Assert.IsTrue(IC.seteq(list.ItemMultiplicities(), 7, 2, 9, 1));
  917. }
  918. }
  919. [TestFixture]
  920. public class ArrayTest
  921. {
  922. private ArrayList<int> list;
  923. int[] a;
  924. [SetUp]
  925. public void Init()
  926. {
  927. list = new ArrayList<int>();
  928. a = new int[10];
  929. for (int i = 0; i < 10; i++)
  930. a[i] = 1000 + i;
  931. }
  932. [TearDown]
  933. public void Dispose() { list = null; }
  934. private string aeq(int[] a, params int[] b)
  935. {
  936. if (a.Length != b.Length)
  937. return "Lengths differ: " + a.Length + " != " + b.Length;
  938. for (int i = 0; i < a.Length; i++)
  939. if (a[i] != b[i])
  940. return String.Format("{0}'th elements differ: {1} != {2}", i, a[i], b[i]);
  941. return "Alles klar";
  942. }
  943. [Test]
  944. public void ToArray()
  945. {
  946. Assert.AreEqual("Alles klar", aeq(list.ToArray()));
  947. list.Add(7);
  948. list.Add(7);
  949. Assert.AreEqual("Alles klar", aeq(list.ToArray(), 7, 7));
  950. }
  951. [Test]
  952. public void CopyTo()
  953. {
  954. list.CopyTo(a, 1);
  955. Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
  956. list.Add(6);
  957. list.CopyTo(a, 2);
  958. Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
  959. list.Add(4);
  960. list.Add(4);
  961. list.Add(9);
  962. list.CopyTo(a, 4);
  963. Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 6, 4, 4, 9, 1008, 1009));
  964. list.Clear();
  965. list.Add(7);
  966. list.CopyTo(a, 9);
  967. Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 6, 4, 4, 9, 1008, 7));
  968. }
  969. [Test]
  970. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  971. public void CopyToBad()
  972. {
  973. list.CopyTo(a, 11);
  974. }
  975. [Test]
  976. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  977. public void CopyToBad2()
  978. {
  979. list.CopyTo(a, -1);
  980. }
  981. [Test]
  982. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  983. public void CopyToTooFar()
  984. {
  985. list.Add(3);
  986. list.Add(3);
  987. list.CopyTo(a, 9);
  988. }
  989. }
  990. [TestFixture]
  991. public class Sync
  992. {
  993. private ArrayList<int> list;
  994. [SetUp]
  995. public void Init()
  996. {
  997. list = new ArrayList<int>();
  998. }
  999. [TearDown]
  1000. public void Dispose() { list = null; }
  1001. [Test]
  1002. public void Get()
  1003. {
  1004. Assert.IsNotNull(((System.Collections.IList)list).SyncRoot);
  1005. }
  1006. }
  1007. }
  1008. namespace EditableCollection
  1009. {
  1010. [TestFixture]
  1011. public class Searching
  1012. {
  1013. private ArrayList<int> list;
  1014. [SetUp]
  1015. public void Init() { list = new ArrayList<int>(); }
  1016. [Test]
  1017. [ExpectedException(typeof(NullReferenceException))]
  1018. public void NullEqualityComparerinConstructor1()
  1019. {
  1020. new ArrayList<int>(null);
  1021. }
  1022. [Test]
  1023. [ExpectedException(typeof(NullReferenceException))]
  1024. public void NullEqualityComparerinConstructor2()
  1025. {
  1026. new ArrayList<int>(5, null);
  1027. }
  1028. [Test]
  1029. public void Contains()
  1030. {
  1031. Assert.IsFalse(list.Contains(5));
  1032. list.Add(5);
  1033. Assert.IsTrue(list.Contains(5));
  1034. Assert.IsFalse(list.Contains(7));
  1035. list.Add(8);
  1036. list.Add(10);
  1037. Assert.IsTrue(list.Contains(5));
  1038. Assert.IsFalse(list.Contains(7));
  1039. Assert.IsTrue(list.Contains(8));
  1040. Assert.IsTrue(list.Contains(10));
  1041. list.Remove(8);
  1042. Assert.IsTrue(list.Contains(5));
  1043. Assert.IsFalse(list.Contains(7));
  1044. Assert.IsFalse(list.Contains(8));
  1045. Assert.IsTrue(list.Contains(10));
  1046. }
  1047. [Test]
  1048. public void ContainsCount()
  1049. {
  1050. Assert.AreEqual(0, list.ContainsCount(5));
  1051. list.Add(5);
  1052. Assert.AreEqual(1, list.ContainsCount(5));
  1053. Assert.AreEqual(0, list.ContainsCount(7));
  1054. list.Add(8);
  1055. Assert.AreEqual(1, list.ContainsCount(5));
  1056. Assert.AreEqual(0, list.ContainsCount(7));
  1057. Assert.AreEqual(1, list.ContainsCount(8));
  1058. list.Add(5);
  1059. Assert.AreEqual(2, list.ContainsCount(5));
  1060. Assert.AreEqual(0, list.ContainsCount(7));
  1061. Assert.AreEqual(1, list.ContainsCount(8));
  1062. }
  1063. [Test]
  1064. public void RemoveAllCopies()
  1065. {
  1066. list.Add(5); list.Add(7); list.Add(5);
  1067. Assert.AreEqual(2, list.ContainsCount(5));
  1068. Assert.AreEqual(1, list.ContainsCount(7));
  1069. list.RemoveAllCopies(5);
  1070. Assert.AreEqual(0, list.ContainsCount(5));
  1071. Assert.AreEqual(1, list.ContainsCount(7));
  1072. list.Add(5); list.Add(8); list.Add(5);
  1073. list.RemoveAllCopies(8);
  1074. Assert.IsTrue(IC.eq(list, 7, 5, 5));
  1075. }
  1076. [Test]
  1077. public void FindAll()
  1078. {
  1079. Fun<int, bool> f = delegate(int i) { return i % 2 == 0; };
  1080. Assert.IsTrue(list.FindAll(f).IsEmpty);
  1081. list.Add(5); list.Add(8); list.Add(5); list.Add(10); list.Add(8);
  1082. Assert.IsTrue(((ArrayList<int>)list.FindAll(f)).Check());
  1083. Assert.IsTrue(IC.eq(list.FindAll(f), 8, 10, 8));
  1084. }
  1085. [Test]
  1086. public void ContainsAll()
  1087. {
  1088. ArrayList<int> list2 = new ArrayList<int>();
  1089. Assert.IsTrue(list.ContainsAll(list2));
  1090. list2.Add(4);
  1091. Assert.IsFalse(list.ContainsAll(list2));
  1092. list.Add(4);
  1093. Assert.IsTrue(list.ContainsAll(list2));
  1094. list.Add(5);
  1095. Assert.IsTrue(list.ContainsAll(list2));
  1096. list2.Add(4);
  1097. Assert.IsFalse(list.ContainsAll(list2));
  1098. list.Add(4);
  1099. Assert.IsTrue(list.ContainsAll(list2));
  1100. }
  1101. [Test]
  1102. public void RetainAll()
  1103. {
  1104. ArrayList<int> list2 = new ArrayList<int>();
  1105. list.Add(4); list.Add(4); list.Add(5); list.Add(4); list.Add(6);
  1106. list2.Add(5); list2.Add(4); list2.Add(7); list2.Add(7); list2.Add(4);
  1107. list.RetainAll(list2);
  1108. Assert.IsTrue(list.Check());
  1109. Assert.IsTrue(IC.eq(list, 4, 4, 5));
  1110. list.Add(5); list.Add(4); list.Add(6);
  1111. list2.Clear();
  1112. list2.Add(5); list2.Add(5); list2.Add(6);
  1113. list.RetainAll(list2);
  1114. Assert.IsTrue(list.Check());
  1115. Assert.IsTrue(IC.eq(list, 5, 5, 6));
  1116. list2.Clear();
  1117. list2.Add(7); list2.Add(8); list2.Add(9);
  1118. list.RetainAll(list2);
  1119. Assert.IsTrue(list.Check());
  1120. Assert.IsTrue(IC.eq(list));
  1121. }
  1122. [Test]
  1123. public void RemoveAll()
  1124. {
  1125. ArrayList<int> list2 = new ArrayList<int>();
  1126. list.Add(4); list.Add(4); list.Add(5); list.Add(4); list.Add(6);
  1127. list2.Add(5); list2.Add(4); list2.Add(7); list2.Add(7); list2.Add(4);
  1128. list.RemoveAll(list2);
  1129. Assert.IsTrue(list.Check());
  1130. Assert.IsTrue(IC.eq(list, 4, 6));
  1131. list.Add(5); list.Add(4); list.Add(6);
  1132. list2.Clear();
  1133. list2.Add(6); list2.Add(5); list2.Add(5); list2.Add(6);
  1134. list.RemoveAll(list2);
  1135. Assert.IsTrue(list.Check());
  1136. Assert.IsTrue(IC.eq(list, 4, 4));
  1137. list2.Clear();
  1138. list2.Add(7); list2.Add(8); list2.Add(9);
  1139. list.RemoveAll(list2);
  1140. Assert.IsTrue(list.Check());
  1141. Assert.IsTrue(IC.eq(list, 4, 4));
  1142. }
  1143. [Test]
  1144. public void Remove()
  1145. {
  1146. Assert.IsFalse(list.FIFO);
  1147. list.Add(4); list.Add(4); list.Add(5); list.Add(4); list.Add(6);
  1148. Assert.IsFalse(list.Remove(2));
  1149. Assert.IsTrue(list.Check());
  1150. Assert.IsTrue(list.Remove(4));
  1151. Assert.IsTrue(list.Check());
  1152. Assert.IsTrue(IC.eq(list, 4, 4, 5, 6));
  1153. Assert.AreEqual(6, list.RemoveLast());
  1154. Assert.IsTrue(list.Check());
  1155. Assert.IsTrue(IC.eq(list, 4, 4, 5));
  1156. list.Add(7);
  1157. Assert.AreEqual(4, list.RemoveFirst());
  1158. Assert.IsTrue(list.Check());
  1159. Assert.IsTrue(IC.eq(list, 4, 5, 7));
  1160. list.FIFO = true;
  1161. list.Clear();
  1162. list.Add(4); list.Add(4); list.Add(5); list.Add(4); list.Add(6);
  1163. Assert.IsFalse(list.Remove(2));
  1164. Assert.IsTrue(list.Check());
  1165. Assert.IsTrue(list.Remove(4));
  1166. Assert.IsTrue(list.Check());
  1167. Assert.IsTrue(IC.eq(list, 4, 5, 4, 6));
  1168. Assert.AreEqual(6, list.RemoveLast());
  1169. Assert.IsTrue(list.Check());
  1170. Assert.IsTrue(IC.eq(list, 4, 5, 4));
  1171. list.Add(7);
  1172. Assert.AreEqual(4, list.RemoveFirst());
  1173. Assert.IsTrue(list.Check());
  1174. Assert.IsTrue(IC.eq(list, 5, 4, 7));
  1175. }
  1176. [Test]
  1177. public void Clear()
  1178. {
  1179. list.Add(7); list.Add(7);
  1180. list.Clear();
  1181. Assert.IsTrue(list.IsEmpty);
  1182. }
  1183. [TearDown]
  1184. public void Dispose() { list = null; }
  1185. }
  1186. }
  1187. namespace Indexed
  1188. {
  1189. [TestFixture]
  1190. public class Searching
  1191. {
  1192. private IIndexed<int> dit;
  1193. [SetUp]
  1194. public void Init()
  1195. {
  1196. dit = new ArrayList<int>();
  1197. }
  1198. [Test]
  1199. public void IndexOf()
  1200. {
  1201. Assert.AreEqual(~0, dit.IndexOf(6));
  1202. dit.Add(7);
  1203. Assert.AreEqual(~1, dit.IndexOf(6));
  1204. Assert.AreEqual(~1, dit.LastIndexOf(6));
  1205. Assert.AreEqual(0, dit.IndexOf(7));
  1206. dit.Add(5); dit.Add(7); dit.Add(8); dit.Add(7);
  1207. Assert.AreEqual(~5, dit.IndexOf(6));
  1208. Assert.AreEqual(0, dit.IndexOf(7));
  1209. Assert.AreEqual(4, dit.LastIndexOf(7));
  1210. Assert.AreEqual(3, dit.IndexOf(8));
  1211. Assert.AreEqual(1, dit.LastIndexOf(5));
  1212. }
  1213. [TearDown]
  1214. public void Dispose()
  1215. {
  1216. dit = null;
  1217. }
  1218. }
  1219. [TestFixture]
  1220. public class Removing
  1221. {
  1222. private IIndexed<int> dit;
  1223. [SetUp]
  1224. public void Init()
  1225. {
  1226. dit = new ArrayList<int>();
  1227. }
  1228. [Test]
  1229. public void RemoveAt()
  1230. {
  1231. dit.Add(5); dit.Add(7); dit.Add(9); dit.Add(1); dit.Add(2);
  1232. Assert.AreEqual(7, dit.RemoveAt(1));
  1233. Assert.IsTrue(((ArrayList<int>)dit).Check());
  1234. Assert.IsTrue(IC.eq(dit, 5, 9, 1, 2));
  1235. Assert.AreEqual(5, dit.RemoveAt(0));
  1236. Assert.IsTrue(((ArrayList<int>)dit).Check());
  1237. Assert.IsTrue(IC.eq(dit, 9, 1, 2));
  1238. Assert.AreEqual(2, dit.RemoveAt(2));
  1239. Assert.IsTrue(((ArrayList<int>)dit).Check());
  1240. Assert.IsTrue(IC.eq(dit, 9, 1));
  1241. }
  1242. [Test]
  1243. [ExpectedException(typeof(IndexOutOfRangeException))]
  1244. public void RemoveAtBad0()
  1245. {
  1246. dit.RemoveAt(0);
  1247. }
  1248. [Test]
  1249. [ExpectedException(typeof(IndexOutOfRangeException))]
  1250. public void RemoveAtBadM1()
  1251. {
  1252. dit.RemoveAt(-1);
  1253. }
  1254. [Test]
  1255. [ExpectedException(typeof(IndexOutOfRangeException))]
  1256. public void RemoveAtBad1()
  1257. {
  1258. dit.Add(8);
  1259. dit.RemoveAt(1);
  1260. }
  1261. [Test]
  1262. public void RemoveInterval()
  1263. {
  1264. dit.RemoveInterval(0, 0);
  1265. dit.Add(10); dit.Add(20); dit.Add(30); dit.Add(40); dit.Add(50); dit.Add(60);
  1266. dit.RemoveInterval(3, 0);
  1267. Assert.IsTrue(((ArrayList<int>)dit).Check());
  1268. Assert.IsTrue(IC.eq(dit, 10, 20, 30, 40, 50, 60));
  1269. dit.RemoveInterval(3, 1);
  1270. Assert.IsTrue(((ArrayList<int>)dit).Check());
  1271. Assert.IsTrue(IC.eq(dit, 10, 20, 30, 50, 60));
  1272. dit.RemoveInterval(1, 3);
  1273. Assert.IsTrue(((ArrayList<int>)dit).Check());
  1274. Assert.IsTrue(IC.eq(dit, 10, 60));
  1275. dit.RemoveInterval(0, 2);
  1276. Assert.IsTrue(((ArrayList<int>)dit).Check());
  1277. Assert.IsTrue(IC.eq(dit));
  1278. dit.Add(10); dit.Add(20); dit.Add(30); dit.Add(40); dit.Add(50); dit.Add(60);
  1279. dit.RemoveInterval(0, 2);
  1280. Assert.IsTrue(((ArrayList<int>)dit).Check());
  1281. Assert.IsTrue(IC.eq(dit, 30, 40, 50, 60));
  1282. dit.RemoveInterval(2, 2);
  1283. Assert.IsTrue(((ArrayList<int>)dit).Check());
  1284. Assert.IsTrue(IC.eq(dit, 30, 40));
  1285. }
  1286. [TearDown]
  1287. public void Dispose()
  1288. {
  1289. dit = null;
  1290. }
  1291. }
  1292. }
  1293. namespace List
  1294. {
  1295. [TestFixture]
  1296. public class Searching
  1297. {
  1298. private IList<int> lst;
  1299. [SetUp]
  1300. public void Init() { lst = new ArrayList<int>(); }
  1301. [TearDown]
  1302. public void Dispose() { lst = null; }
  1303. [Test]
  1304. [ExpectedException(typeof(NoSuchItemException))]
  1305. public void FirstBad()
  1306. {
  1307. int f = lst.First;
  1308. }
  1309. [Test]
  1310. [ExpectedException(typeof(NoSuchItemException))]
  1311. public void LastBad()
  1312. {
  1313. int f = lst.Last;
  1314. }
  1315. [Test]
  1316. public void FirstLast()
  1317. {
  1318. lst.Add(19);
  1319. Assert.AreEqual(19, lst.First);
  1320. Assert.AreEqual(19, lst.Last);
  1321. lst.Add(34); lst.InsertFirst(12);
  1322. Assert.AreEqual(12, lst.First);
  1323. Assert.AreEqual(34, lst.Last);
  1324. }
  1325. [Test]
  1326. public void This()
  1327. {
  1328. lst.Add(34);
  1329. Assert.AreEqual(34, lst[0]);
  1330. lst[0] = 56;
  1331. Assert.AreEqual(56, lst.First);
  1332. lst.Add(7); lst.Add(7); lst.Add(7); lst.Add(7);
  1333. lst[0] = 45; lst[2] = 78; lst[4] = 101;
  1334. Assert.IsTrue(IC.eq(lst, 45, 7, 78, 7, 101));
  1335. }
  1336. [Test]
  1337. [ExpectedException(typeof(IndexOutOfRangeException))]
  1338. public void ThisBadEmptyGet()
  1339. {
  1340. int f = lst[0];
  1341. }
  1342. [Test]
  1343. [ExpectedException(typeof(IndexOutOfRangeException))]
  1344. public void ThisBadLowGet()
  1345. {
  1346. lst.Add(7);
  1347. int f = lst[-1];
  1348. }
  1349. [Test]
  1350. [ExpectedException(typeof(IndexOutOfRangeException))]
  1351. public void ThisBadHiGet()
  1352. {
  1353. lst.Add(6);
  1354. int f = lst[1];
  1355. }
  1356. [Test]
  1357. [ExpectedException(typeof(IndexOutOfRangeException))]
  1358. public void ThisBadEmptySet()
  1359. {
  1360. lst[0] = 4;
  1361. }
  1362. [Test]
  1363. [ExpectedException(typeof(IndexOutOfRangeException))]
  1364. public void ThisBadLowSet()
  1365. {
  1366. lst.Add(7);
  1367. lst[-1] = 9;
  1368. }
  1369. [Test]
  1370. [ExpectedException(typeof(IndexOutOfRangeException))]
  1371. public void ThisBadHiSet()
  1372. {
  1373. lst.Add(6);
  1374. lst[1] = 11;
  1375. }
  1376. }
  1377. [TestFixture]
  1378. public class Inserting
  1379. {
  1380. private IList<int> lst;
  1381. [SetUp]
  1382. public void Init() { lst = new ArrayList<int>(); }
  1383. [TearDown]
  1384. public void Dispose() { lst = null; }
  1385. [Test]
  1386. public void Insert()
  1387. {
  1388. lst.Insert(0, 5);
  1389. Assert.IsTrue(IC.eq(lst, 5));
  1390. lst.Insert(0, 7);
  1391. Assert.IsTrue(IC.eq(lst, 7, 5));
  1392. lst.Insert(1, 4);
  1393. Assert.IsTrue(IC.eq(lst, 7, 4, 5));
  1394. lst.Insert(3, 2);
  1395. Assert.IsTrue(IC.eq(lst, 7, 4, 5, 2));
  1396. }
  1397. [Test]
  1398. public void InsertDuplicate()
  1399. {
  1400. lst.Insert(0, 5);
  1401. Assert.IsTrue(IC.eq(lst, 5));
  1402. lst.Insert(0, 7);
  1403. Assert.IsTrue(IC.eq(lst, 7, 5));
  1404. lst.Insert(1, 5);
  1405. Assert.IsTrue(IC.eq(lst, 7, 5, 5));
  1406. }
  1407. [Test]
  1408. public void InsertAllDuplicate1()
  1409. {
  1410. lst.Insert(0, 3);
  1411. Assert.IsTrue(IC.eq(lst, 3));
  1412. lst.Insert(0, 7);
  1413. Assert.IsTrue(IC.eq(lst, 7, 3));
  1414. lst.InsertAll<int>(1, new int[] { 1, 2, 3, 4 });
  1415. Assert.IsTrue(IC.eq(lst, 7, 1, 2, 3, 4, 3));
  1416. Assert.IsTrue(lst.Check());
  1417. }
  1418. [Test]
  1419. public void InsertAllDuplicate2()
  1420. {
  1421. lst.Insert(0, 3);
  1422. Assert.IsTrue(IC.eq(lst, 3));
  1423. lst.Insert(0, 7);
  1424. Assert.IsTrue(IC.eq(lst, 7, 3));
  1425. lst.InsertAll<int>(1, new int[] { 5, 6, 5, 8 });
  1426. Assert.IsTrue(lst.Check());
  1427. Assert.IsTrue(IC.eq(lst, 7, 5, 6, 5, 8, 3));
  1428. }
  1429. [Test]
  1430. [ExpectedException(typeof(IndexOutOfRangeException))]
  1431. public void BadInsertLow()
  1432. {
  1433. lst.Add(7);
  1434. lst.Insert(-1, 9);
  1435. }
  1436. [Test]
  1437. [ExpectedException(typeof(IndexOutO

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