PageRenderTime 44ms CodeModel.GetById 1ms RepoModel.GetById 0ms 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
  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(IndexOutOfRangeException))]
  1438. public void BadInsertHi()
  1439. {
  1440. lst.Add(6);
  1441. lst.Insert(2, 11);
  1442. }
  1443. [Test]
  1444. public void FIFO()
  1445. {
  1446. for (int i = 0; i < 7; i++)
  1447. lst.Add(2 * i);
  1448. Assert.IsFalse(lst.FIFO);
  1449. Assert.AreEqual(12, lst.Remove());
  1450. Assert.AreEqual(10, lst.Remove());
  1451. lst.FIFO = true;
  1452. Assert.AreEqual(0, lst.Remove());
  1453. Assert.AreEqual(2, lst.Remove());
  1454. lst.FIFO = false;
  1455. Assert.AreEqual(8, lst.Remove());
  1456. Assert.AreEqual(6, lst.Remove());
  1457. }
  1458. [Test]
  1459. public void InsertFirstLast()
  1460. {
  1461. lst.InsertFirst(4);
  1462. lst.InsertLast(5);
  1463. lst.InsertFirst(14);
  1464. lst.InsertLast(15);
  1465. lst.InsertFirst(24);
  1466. lst.InsertLast(25);
  1467. lst.InsertFirst(34);
  1468. lst.InsertLast(55);
  1469. Assert.IsTrue(IC.eq(lst, 34, 24, 14, 4, 5, 15, 25, 55));
  1470. }
  1471. [Test]
  1472. public void InsertFirst()
  1473. {
  1474. lst.Add(2);
  1475. lst.Add(3);
  1476. lst.Add(2);
  1477. lst.Add(5);
  1478. lst.ViewOf(2).InsertFirst(7);
  1479. Assert.IsTrue(lst.Check());
  1480. Assert.IsTrue(IC.eq(lst, 7, 2, 3, 2, 5));
  1481. lst.ViewOf(3).InsertFirst(8);
  1482. Assert.IsTrue(lst.Check());
  1483. Assert.IsTrue(IC.eq(lst, 7, 2, 8, 3, 2, 5));
  1484. lst.ViewOf(5).InsertFirst(9);
  1485. Assert.IsTrue(lst.Check());
  1486. Assert.IsTrue(IC.eq(lst, 7, 2, 8, 3, 2, 9, 5));
  1487. }
  1488. [Test]
  1489. public void InsertAfter()
  1490. {
  1491. lst.Add(1);
  1492. lst.Add(2);
  1493. lst.Add(3);
  1494. lst.Add(2);
  1495. lst.Add(5);
  1496. lst.LastViewOf(2).InsertLast(7);
  1497. Assert.IsTrue(lst.Check());
  1498. Assert.IsTrue(IC.eq(lst, 1, 2, 3, 2, 7, 5));
  1499. lst.LastViewOf(1).InsertLast(8);
  1500. Assert.IsTrue(lst.Check());
  1501. Assert.IsTrue(IC.eq(lst, 1, 8, 2, 3, 2, 7, 5));
  1502. lst.LastViewOf(5).InsertLast(9);
  1503. Assert.IsTrue(lst.Check());
  1504. Assert.IsTrue(IC.eq(lst, 1, 8, 2, 3, 2, 7, 5, 9));
  1505. }
  1506. [Test]
  1507. public void InsertAll()
  1508. {
  1509. lst.Add(1);
  1510. lst.Add(2);
  1511. lst.Add(3);
  1512. lst.Add(4);
  1513. IList<int> lst2 = new ArrayList<int>();
  1514. lst2.Add(7); lst2.Add(8); lst2.Add(9);
  1515. lst.InsertAll(0, lst2);
  1516. Assert.IsTrue(lst.Check());
  1517. Assert.IsTrue(IC.eq(lst, 7, 8, 9, 1, 2, 3, 4));
  1518. lst.InsertAll(7, lst2);
  1519. Assert.IsTrue(lst.Check());
  1520. Assert.IsTrue(IC.eq(lst, 7, 8, 9, 1, 2, 3, 4, 7, 8, 9));
  1521. lst.InsertAll(5, lst2);
  1522. Assert.IsTrue(lst.Check());
  1523. Assert.IsTrue(IC.eq(lst, 7, 8, 9, 1, 2, 7, 8, 9, 3, 4, 7, 8, 9));
  1524. }
  1525. [Test]
  1526. public void Map()
  1527. {
  1528. Fun<int, string> m = delegate(int i) { return "<<" + i + ">>"; };
  1529. IList<string> r = lst.Map(m);
  1530. Assert.IsTrue(r.Check());
  1531. Assert.IsTrue(r.IsEmpty);
  1532. lst.Add(1);
  1533. lst.Add(2);
  1534. lst.Add(3);
  1535. lst.Add(4);
  1536. r = lst.Map(m);
  1537. Assert.IsTrue(r.Check());
  1538. Assert.AreEqual(4, r.Count);
  1539. for (int i = 0; i < 4; i++)
  1540. Assert.AreEqual("<<" + (i + 1) + ">>", r[i]);
  1541. }
  1542. [Test]
  1543. [ExpectedException(typeof(CollectionModifiedException))]
  1544. public void BadMapper()
  1545. {
  1546. lst.Add(1);
  1547. lst.Add(2);
  1548. lst.Add(3);
  1549. Fun<int, bool> m = delegate(int i) { if (i == 2) lst.Add(7); return true; };
  1550. lst.Map(m);
  1551. }
  1552. [Test]
  1553. [ExpectedException(typeof(CollectionModifiedException))]
  1554. public void ModifyingFindAll()
  1555. {
  1556. lst.Add(1);
  1557. lst.Add(2);
  1558. lst.Add(3);
  1559. Fun<int, bool> m = delegate(int i) { if (i == 2) lst.Add(7); return true; };
  1560. lst.FindAll(m);
  1561. }
  1562. [Test]
  1563. [ExpectedException(typeof(CollectionModifiedException))]
  1564. public void BadMapperView()
  1565. {
  1566. lst = lst.View(0, 0);
  1567. lst.Add(1);
  1568. lst.Add(2);
  1569. lst.Add(3);
  1570. Fun<int, bool> m = delegate(int i) { if (i == 2) lst.Add(7); return true; };
  1571. lst.Map(m);
  1572. }
  1573. [Test]
  1574. [ExpectedException(typeof(CollectionModifiedException))]
  1575. public void ModifyingFindAllView()
  1576. {
  1577. lst = lst.View(0, 0);
  1578. lst.Add(1);
  1579. lst.Add(2);
  1580. lst.Add(3);
  1581. Fun<int, bool> m = delegate(int i) { if (i == 2) lst.Add(7); return true; };
  1582. lst.FindAll(m);
  1583. }
  1584. [Test]
  1585. [ExpectedException(typeof(NoSuchItemException))]
  1586. public void BadRemove() { lst.Remove(); }
  1587. [Test]
  1588. [ExpectedException(typeof(NoSuchItemException))]
  1589. public void BadRemoveFirst() { lst.RemoveFirst(); }
  1590. [Test]
  1591. [ExpectedException(typeof(NoSuchItemException))]
  1592. public void BadRemoveLast() { lst.RemoveLast(); }
  1593. [Test]
  1594. public void RemoveFirstLast()
  1595. {
  1596. lst.Add(1);
  1597. lst.Add(2);
  1598. lst.Add(3);
  1599. lst.Add(4);
  1600. Assert.AreEqual(1, lst.RemoveFirst());
  1601. Assert.AreEqual(4, lst.RemoveLast());
  1602. Assert.AreEqual(2, lst.RemoveFirst());
  1603. Assert.AreEqual(3, lst.RemoveLast());
  1604. Assert.IsTrue(lst.IsEmpty);
  1605. }
  1606. [Test]
  1607. public void Reverse()
  1608. {
  1609. for (int i = 0; i < 10; i++)
  1610. lst.Add(i);
  1611. lst.Reverse();
  1612. Assert.IsTrue(lst.Check());
  1613. Assert.IsTrue(IC.eq(lst, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0));
  1614. lst.View(0, 3).Reverse();
  1615. Assert.IsTrue(lst.Check());
  1616. Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 2, 1, 0));
  1617. lst.View(7, 0).Reverse();
  1618. Assert.IsTrue(lst.Check());
  1619. Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 2, 1, 0));
  1620. lst.View(7, 3).Reverse();
  1621. Assert.IsTrue(lst.Check());
  1622. Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 0, 1, 2));
  1623. lst.View(5, 1).Reverse();
  1624. Assert.IsTrue(lst.Check());
  1625. Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 0, 1, 2));
  1626. }
  1627. [Test]
  1628. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  1629. public void BadReverse()
  1630. {
  1631. for (int i = 0; i < 10; i++)
  1632. lst.Add(i);
  1633. lst.View(8, 3).Reverse();
  1634. }
  1635. }
  1636. [TestFixture]
  1637. public class Combined
  1638. {
  1639. private IList<KeyValuePair<int, int>> lst;
  1640. [SetUp]
  1641. public void Init()
  1642. {
  1643. lst = new ArrayList<KeyValuePair<int, int>>(new KeyValuePairEqualityComparer<int, int>());
  1644. for (int i = 0; i < 10; i++)
  1645. lst.Add(new KeyValuePair<int, int>(i, i + 30));
  1646. }
  1647. [TearDown]
  1648. public void Dispose() { lst = null; }
  1649. [Test]
  1650. public void Find()
  1651. {
  1652. KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
  1653. Assert.IsTrue(lst.Find(ref p));
  1654. Assert.AreEqual(3, p.Key);
  1655. Assert.AreEqual(33, p.Value);
  1656. p = new KeyValuePair<int, int>(13, 78);
  1657. Assert.IsFalse(lst.Find(ref p));
  1658. }
  1659. [Test]
  1660. public void FindOrAdd()
  1661. {
  1662. KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
  1663. Assert.IsTrue(lst.FindOrAdd(ref p));
  1664. Assert.AreEqual(3, p.Key);
  1665. Assert.AreEqual(33, p.Value);
  1666. p = new KeyValuePair<int, int>(13, 79);
  1667. Assert.IsFalse(lst.FindOrAdd(ref p));
  1668. Assert.AreEqual(13, lst[10].Key);
  1669. Assert.AreEqual(79, lst[10].Value);
  1670. }
  1671. [Test]
  1672. public void Update()
  1673. {
  1674. KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
  1675. Assert.IsTrue(lst.Update(p));
  1676. Assert.AreEqual(3, lst[3].Key);
  1677. Assert.AreEqual(78, lst[3].Value);
  1678. p = new KeyValuePair<int, int>(13, 78);
  1679. Assert.IsFalse(lst.Update(p));
  1680. }
  1681. [Test]
  1682. public void UpdateOrAdd1()
  1683. {
  1684. KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
  1685. Assert.IsTrue(lst.UpdateOrAdd(p));
  1686. Assert.AreEqual(3, lst[3].Key);
  1687. Assert.AreEqual(78, lst[3].Value);
  1688. p = new KeyValuePair<int, int>(13, 79);
  1689. Assert.IsFalse(lst.UpdateOrAdd(p));
  1690. Assert.AreEqual(13, lst[10].Key);
  1691. Assert.AreEqual(79, lst[10].Value);
  1692. }
  1693. [Test]
  1694. public void UpdateOrAdd2()
  1695. {
  1696. ICollection<String> coll = new ArrayList<String>();
  1697. // s1 and s2 are distinct objects but contain the same text:
  1698. String old, s1 = "abc", s2 = ("def" + s1).Substring(3);
  1699. Assert.IsFalse(coll.UpdateOrAdd(s1, out old));
  1700. Assert.AreEqual(null, old);
  1701. Assert.IsTrue(coll.UpdateOrAdd(s2, out old));
  1702. Assert.IsTrue(Object.ReferenceEquals(s1, old));
  1703. Assert.IsFalse(Object.ReferenceEquals(s2, old));
  1704. }
  1705. [Test]
  1706. public void RemoveWithReturn()
  1707. {
  1708. KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
  1709. Assert.IsTrue(lst.Remove(p, out p));
  1710. Assert.AreEqual(3, p.Key);
  1711. Assert.AreEqual(33, p.Value);
  1712. Assert.AreEqual(4, lst[3].Key);
  1713. Assert.AreEqual(34, lst[3].Value);
  1714. p = new KeyValuePair<int, int>(13, 78);
  1715. Assert.IsFalse(lst.Remove(p, out p));
  1716. }
  1717. }
  1718. [TestFixture]
  1719. public class SortingTests
  1720. {
  1721. private IList<int> lst;
  1722. [SetUp]
  1723. public void Init() { lst = new ArrayList<int>(); }
  1724. [TearDown]
  1725. public void Dispose() { lst = null; }
  1726. [Test]
  1727. public void Sort()
  1728. {
  1729. lst.Add(5); lst.Add(6); lst.Add(5); lst.Add(7); lst.Add(3);
  1730. Assert.IsFalse(lst.IsSorted(new IC()));
  1731. lst.Sort(new IC());
  1732. Assert.IsTrue(lst.IsSorted());
  1733. Assert.IsTrue(lst.IsSorted(new IC()));
  1734. Assert.IsTrue(IC.eq(lst, 3, 5, 5, 6, 7));
  1735. }
  1736. [Test]
  1737. public void Stability()
  1738. {
  1739. IList<KeyValuePair<int, string>> lst2 = new ArrayList<KeyValuePair<int, string>>();
  1740. SCG.IComparer<KeyValuePair<int, string>> c = new KeyValuePairComparer<int, string>(new IC());
  1741. lst2.Add(new KeyValuePair<int, string>(5, "a"));
  1742. lst2.Add(new KeyValuePair<int, string>(5, "b"));
  1743. lst2.Add(new KeyValuePair<int, string>(6, "c"));
  1744. lst2.Add(new KeyValuePair<int, string>(4, "d"));
  1745. lst2.Add(new KeyValuePair<int, string>(3, "e"));
  1746. lst2.Add(new KeyValuePair<int, string>(4, "f"));
  1747. lst2.Add(new KeyValuePair<int, string>(5, "handle"));
  1748. Assert.IsFalse(lst2.IsSorted(c));
  1749. lst2.Sort(c);
  1750. Assert.IsTrue(lst2.IsSorted(c));
  1751. KeyValuePair<int, string> p = lst2.RemoveFirst();
  1752. Assert.AreEqual(3, p.Key);
  1753. Assert.AreEqual("e", p.Value);
  1754. p = lst2.RemoveFirst();
  1755. Assert.AreEqual(4, p.Key);
  1756. Assert.AreEqual("d", p.Value);
  1757. p = lst2.RemoveFirst();
  1758. Assert.AreEqual(4, p.Key);
  1759. Assert.AreEqual("f", p.Value);
  1760. p = lst2.RemoveFirst();
  1761. Assert.AreEqual(5, p.Key);
  1762. Assert.AreEqual("a", p.Value);
  1763. p = lst2.RemoveFirst();
  1764. Assert.AreEqual(5, p.Key);
  1765. Assert.AreEqual("b", p.Value);
  1766. p = lst2.RemoveFirst();
  1767. Assert.AreEqual(5, p.Key);
  1768. Assert.AreEqual("handle", p.Value);
  1769. p = lst2.RemoveFirst();
  1770. Assert.AreEqual(6, p.Key);
  1771. Assert.AreEqual("c", p.Value);
  1772. Assert.IsTrue(lst2.IsEmpty);
  1773. }
  1774. }
  1775. [TestFixture]
  1776. public class ShuffleTests
  1777. {
  1778. private IList<int> lst;
  1779. [SetUp]
  1780. public void Init() { lst = new ArrayList<int>(); }
  1781. [TearDown]
  1782. public void Dispose() { lst = null; }
  1783. [Test]
  1784. public void Shuffle()
  1785. {
  1786. lst.Add(5); lst.Add(6); lst.Add(5); lst.Add(7); lst.Add(3);
  1787. for (int i = 0; i < 100; i++)
  1788. {
  1789. lst.Shuffle(new C5Random(i + 1));
  1790. Assert.IsTrue(lst.Check(), "Check " + i);
  1791. int[] lst2 = lst.ToArray();
  1792. Sorting.IntroSort<int>(lst2);
  1793. Assert.IsTrue(IC.eq(lst2, 3, 5, 5, 6, 7), "Contents " + i);
  1794. }
  1795. }
  1796. }
  1797. }
  1798. namespace IStackQueue
  1799. {
  1800. [TestFixture]
  1801. public class Stack
  1802. {
  1803. private IStack<int> list;
  1804. [SetUp]
  1805. public void Init() { list = new ArrayList<int>(); }
  1806. [Test]
  1807. public void Normal()
  1808. {
  1809. list.Push(7);
  1810. list.Push(5);
  1811. list.Push(7);
  1812. list.Push(8);
  1813. list.Push(9);
  1814. Assert.AreEqual(9, list.Pop());
  1815. Assert.AreEqual(8, list.Pop());
  1816. Assert.AreEqual(7, list.Pop());
  1817. Assert.AreEqual(5, list.Pop());
  1818. Assert.AreEqual(7, list.Pop());
  1819. }
  1820. [Test]
  1821. [ExpectedException(typeof(NoSuchItemException))]
  1822. public void PopEmpty()
  1823. {
  1824. list.Push(5);
  1825. Assert.AreEqual(5, list.Pop());
  1826. list.Pop();
  1827. }
  1828. [TearDown]
  1829. public void Dispose() { list = null; }
  1830. }
  1831. [TestFixture]
  1832. public class Queue
  1833. {
  1834. private IQueue<int> list;
  1835. [SetUp]
  1836. public void Init() { list = new ArrayList<int>(); }
  1837. [Test]
  1838. public void Normal()
  1839. {
  1840. list.Enqueue(7);
  1841. list.Enqueue(5);
  1842. list.Enqueue(7);
  1843. list.Enqueue(8);
  1844. list.Enqueue(9);
  1845. Assert.AreEqual(7, list.Dequeue());
  1846. Assert.AreEqual(5, list.Dequeue());
  1847. Assert.AreEqual(7, list.Dequeue());
  1848. Assert.AreEqual(8, list.Dequeue());
  1849. Assert.AreEqual(9, list.Dequeue());
  1850. }
  1851. [Test]
  1852. [ExpectedException(typeof(NoSuchItemException))]
  1853. public void DeQueueEmpty()
  1854. {
  1855. list.Enqueue(5);
  1856. Assert.AreEqual(5, list.Dequeue());
  1857. list.Dequeue();
  1858. }
  1859. [TearDown]
  1860. public void Dispose() { list = null; }
  1861. }
  1862. }
  1863. namespace Range
  1864. {
  1865. [TestFixture]
  1866. public class Range
  1867. {
  1868. private IList<int> lst;
  1869. [SetUp]
  1870. public void Init() { lst = new ArrayList<int>(); }
  1871. [TearDown]
  1872. public void Dispose() { lst = null; }
  1873. [Test]
  1874. public void GetRange()
  1875. {
  1876. //Assert.IsTrue(IC.eq(lst[0, 0)));
  1877. for (int i = 0; i < 10; i++) lst.Add(i);
  1878. Assert.IsTrue(IC.eq(lst[0, 3], 0, 1, 2));
  1879. Assert.IsTrue(IC.eq(lst[3, 4], 3, 4, 5, 6));
  1880. Assert.IsTrue(IC.eq(lst[6, 4], 6, 7, 8, 9));
  1881. }
  1882. [Test]
  1883. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  1884. public void BadGetRange()
  1885. {
  1886. object foo = lst[0, 11];
  1887. }
  1888. [Test]
  1889. public void Backwards()
  1890. {
  1891. for (int i = 0; i < 10; i++) lst.Add(i);
  1892. Assert.IsTrue(IC.eq(lst.Backwards(), 9, 8, 7, 6, 5, 4, 3, 2, 1, 0));
  1893. Assert.IsTrue(IC.eq(lst[0, 4].Backwards(), 3, 2, 1, 0));
  1894. Assert.IsTrue(IC.eq(lst[3, 4].Backwards(), 6, 5, 4, 3));
  1895. Assert.IsTrue(IC.eq(lst[6, 4].Backwards(), 9, 8, 7, 6));
  1896. }
  1897. [Test]
  1898. public void DirectionAndCount()
  1899. {
  1900. for (int i = 0; i < 10; i++) lst.Add(i);
  1901. Assert.AreEqual(EnumerationDirection.Forwards, lst.Direction);
  1902. Assert.AreEqual(EnumerationDirection.Forwards, lst[3, 4].Direction);
  1903. Assert.AreEqual(EnumerationDirection.Backwards, lst[3, 4].Backwards().Direction);
  1904. Assert.AreEqual(EnumerationDirection.Backwards, lst.Backwards().Direction);
  1905. Assert.AreEqual(4, lst[3, 4].Count);
  1906. Assert.AreEqual(4, lst[3, 4].Backwards().Count);
  1907. Assert.AreEqual(10, lst.Backwards().Count);
  1908. }
  1909. [Test]
  1910. [ExpectedException(typeof(CollectionModifiedException))]
  1911. public void MoveNextAfterUpdate()
  1912. {
  1913. for (int i = 0; i < 10; i++) lst.Add(i);
  1914. foreach (int i in lst)
  1915. {
  1916. lst.Add(45 + i);
  1917. }
  1918. }
  1919. }
  1920. }
  1921. namespace View
  1922. {
  1923. [TestFixture]
  1924. public class Simple
  1925. {
  1926. ArrayList<int> list, view;
  1927. [SetUp]
  1928. public void Init()
  1929. {
  1930. list = new ArrayList<int>();
  1931. list.Add(0); list.Add(1); list.Add(2); list.Add(3);
  1932. view = (ArrayList<int>)list.View(1, 2);
  1933. }
  1934. [TearDown]
  1935. public void Dispose()
  1936. {
  1937. list = view = null;
  1938. }
  1939. void check()
  1940. {
  1941. Assert.IsTrue(list.Check());
  1942. Assert.IsTrue(view.Check());
  1943. }
  1944. //static void pint(IEnumerable<int> l) { foreach (int cell in l) Console.WriteLine(cell); }
  1945. [Test]
  1946. public void InsertPointer()
  1947. {
  1948. IList<int> view2 = list.View(2, 0);
  1949. list.Insert(view2, 7);
  1950. check();
  1951. list.Insert(list, 8);
  1952. check();
  1953. view.Insert(view2, 9);
  1954. check();
  1955. view.Insert(list.View(3, 2), 10);
  1956. check();
  1957. view.Insert(list.ViewOf(0), 11);
  1958. check();
  1959. Assert.IsTrue(IC.eq(list, 0, 11, 1, 9, 7, 2, 10, 3, 8));
  1960. Assert.IsTrue(IC.eq(view, 11, 1, 9, 7, 2, 10));
  1961. }
  1962. [Test]
  1963. [ExpectedException(typeof(IndexOutOfRangeException))]
  1964. public void InsertPointerBad1()
  1965. {
  1966. view.Insert(list.View(0, 0), 7);
  1967. }
  1968. [Test]
  1969. [ExpectedException(typeof(IndexOutOfRangeException))]
  1970. public void InsertPointerBad2()
  1971. {
  1972. view.Insert(list, 7);
  1973. }
  1974. [Test]
  1975. [ExpectedException(typeof(IncompatibleViewException))]
  1976. public void InsertPointerBad3()
  1977. {
  1978. list.Insert(new ArrayList<int>(), 7);
  1979. }
  1980. [Test]
  1981. [ExpectedException(typeof(IncompatibleViewException))]
  1982. public void InsertPointerBad4()
  1983. {
  1984. list.Insert(new ArrayList<int>().View(0, 0), 7);
  1985. }
  1986. [Test]
  1987. public void Span()
  1988. {
  1989. IList<int> span = list.View(1, 0).Span(list.View(2, 0));
  1990. Assert.IsTrue(span.Check());
  1991. Assert.AreEqual(1, span.Offset);
  1992. Assert.AreEqual(1, span.Count);
  1993. span = list.View(0, 2).Span(list.View(2, 2));
  1994. Assert.IsTrue(span.Check());
  1995. Assert.AreEqual(0, span.Offset);
  1996. Assert.AreEqual(4, span.Count);
  1997. span = list.View(3, 1).Span(list.View(1, 1));
  1998. Assert.IsNull(span);
  1999. }
  2000. [Test]
  2001. public void ViewOf()
  2002. {
  2003. for (int i = 0; i < 4; i++)
  2004. list.Add(i);
  2005. IList<int> v = view.ViewOf(2);
  2006. Assert.IsTrue(v.Check());
  2007. Assert.IsTrue(IC.eq(v, 2));
  2008. Assert.AreEqual(2, v.Offset);
  2009. v = list.ViewOf(2);
  2010. Assert.IsTrue(v.Check());
  2011. Assert.IsTrue(IC.eq(v, 2));
  2012. Assert.AreEqual(2, v.Offset);
  2013. v = list.LastViewOf(2);
  2014. Assert.IsTrue(v.Check());
  2015. Assert.IsTrue(IC.eq(v, 2));
  2016. Assert.AreEqual(6, v.Offset);
  2017. }
  2018. [Test]
  2019. public void ArrayStuff()
  2020. {
  2021. Assert.IsTrue(IC.eq(view.ToArray(), 1, 2));
  2022. int[] extarray = new int[5];
  2023. view.CopyTo(extarray, 2);
  2024. Assert.IsTrue(IC.eq(extarray, 0, 0, 1, 2, 0));
  2025. }
  2026. [Test]
  2027. public void BadViewOf()
  2028. {
  2029. Assert.IsNull(view.ViewOf(5));
  2030. Assert.IsNull(view.LastViewOf(5));
  2031. Assert.IsNull(view.ViewOf(3));
  2032. Assert.IsNull(view.LastViewOf(3));
  2033. Assert.IsNull(view.ViewOf(0));
  2034. Assert.IsNull(view.LastViewOf(0));
  2035. }
  2036. [Test]
  2037. public void Add()
  2038. {
  2039. check();
  2040. Assert.IsTrue(IC.eq(list, 0, 1, 2, 3));
  2041. Assert.IsTrue(IC.eq(view, 1, 2));
  2042. view.InsertFirst(10);
  2043. check();
  2044. Assert.IsTrue(IC.eq(list, 0, 10, 1, 2, 3));
  2045. Assert.IsTrue(IC.eq(view, 10, 1, 2));
  2046. view.Clear();
  2047. Assert.IsFalse(view.IsReadOnly);
  2048. Assert.IsTrue(view.AllowsDuplicates);
  2049. Assert.IsTrue(view.IsEmpty);
  2050. check();
  2051. Assert.IsTrue(IC.eq(list, 0, 3));
  2052. Assert.IsTrue(IC.eq(view));
  2053. view.Add(8);
  2054. Assert.IsFalse(view.IsEmpty);
  2055. Assert.IsTrue(view.AllowsDuplicates);
  2056. Assert.IsFalse(view.IsReadOnly);
  2057. check();
  2058. Assert.IsTrue(IC.eq(list, 0, 8, 3));
  2059. Assert.IsTrue(IC.eq(view, 8));
  2060. view.Add(12);
  2061. check();
  2062. Assert.IsTrue(IC.eq(list, 0, 8, 12, 3));
  2063. Assert.IsTrue(IC.eq(view, 8, 12));
  2064. view./*ViewOf(12)*/InsertLast(15);
  2065. check();
  2066. Assert.IsTrue(IC.eq(list, 0, 8, 12, 15, 3));
  2067. Assert.IsTrue(IC.eq(view, 8, 12, 15));
  2068. view.ViewOf(12).InsertFirst(18);
  2069. check();
  2070. Assert.IsTrue(IC.eq(list, 0, 8, 18, 12, 15, 3));
  2071. Assert.IsTrue(IC.eq(view, 8, 18, 12, 15));
  2072. ArrayList<int> lst2 = new ArrayList<int>();
  2073. lst2.Add(90); lst2.Add(92);
  2074. view.AddAll(lst2);
  2075. check();
  2076. Assert.IsTrue(IC.eq(list, 0, 8, 18, 12, 15, 90, 92, 3));
  2077. Assert.IsTrue(IC.eq(view, 8, 18, 12, 15, 90, 92));
  2078. view.InsertLast(66);
  2079. check();
  2080. Assert.IsTrue(IC.eq(list, 0, 8, 18, 12, 15, 90, 92, 66, 3));
  2081. Assert.IsTrue(IC.eq(view, 8, 18, 12, 15, 90, 92, 66));
  2082. }
  2083. [Test]
  2084. public void Bxxx()
  2085. {
  2086. Assert.IsTrue(IC.eq(view.Backwards(), 2, 1));
  2087. Assert.AreSame(list, view.Underlying);
  2088. Assert.IsNull(list.Underlying);
  2089. Assert.AreEqual(EnumerationDirection.Forwards, view.Direction);
  2090. Assert.AreEqual(EnumerationDirection.Backwards, view.Backwards().Direction);
  2091. Assert.AreEqual(0, list.Offset);
  2092. Assert.AreEqual(1, view.Offset);
  2093. }
  2094. [Test]
  2095. public void Contains()
  2096. {
  2097. Assert.IsTrue(view.Contains(1));
  2098. Assert.IsFalse(view.Contains(0));
  2099. ArrayList<int> lst2 = new ArrayList<int>();
  2100. lst2.Add(2);
  2101. Assert.IsTrue(view.ContainsAll(lst2));
  2102. lst2.Add(3);
  2103. Assert.IsFalse(view.ContainsAll(lst2));
  2104. Assert.AreEqual(Speed.Linear, view.ContainsSpeed);
  2105. Assert.AreEqual(2, view.Count);
  2106. view.Add(1);
  2107. Assert.AreEqual(1, view.ContainsCount(2));
  2108. Assert.AreEqual(2, view.ContainsCount(1));
  2109. Assert.AreEqual(3, view.Count);
  2110. }
  2111. [Test]
  2112. public void CreateView()
  2113. {
  2114. ArrayList<int> view2 = (ArrayList<int>)view.View(1, 0);
  2115. Assert.AreSame(list, view2.Underlying);
  2116. }
  2117. [Test]
  2118. public void FIFO()
  2119. {
  2120. Assert.IsFalse(view.FIFO);
  2121. view.FIFO = true;
  2122. view.Add(23); view.Add(24); view.Add(25);
  2123. check();
  2124. Assert.IsTrue(IC.eq(view, 1, 2, 23, 24, 25));
  2125. Assert.AreEqual(1, view.Remove());
  2126. check();
  2127. Assert.IsTrue(IC.eq(view, 2, 23, 24, 25));
  2128. view.FIFO = false;
  2129. Assert.IsFalse(view.FIFO);
  2130. Assert.AreEqual(25, view.Remove());
  2131. check();
  2132. Assert.IsTrue(IC.eq(view, 2, 23, 24));
  2133. }
  2134. [Test]
  2135. public void MapEtc()
  2136. {
  2137. ArrayList<double> dbl = (ArrayList<double>)view.Map(new Fun<int, double>(delegate(int i) { return i / 10.0; }));
  2138. Assert.IsTrue(dbl.Check());
  2139. Assert.AreEqual(0.1, dbl[0]);
  2140. Assert.AreEqual(0.2, dbl[1]);
  2141. for (int i = 0; i < 10; i++) view.Add(i);
  2142. list = (ArrayList<int>)view.FindAll(new Fun<int, bool>(delegate(int i) { return i % 4 == 1; }));
  2143. Assert.IsTrue(list.Check());
  2144. Assert.IsTrue(IC.eq(list, 1, 1, 5, 9));
  2145. }
  2146. [Test]
  2147. public void FL()
  2148. {
  2149. Assert.AreEqual(1, view.First);
  2150. Assert.AreEqual(2, view.Last);
  2151. }
  2152. [Test]
  2153. public void Indexing()
  2154. {
  2155. list.Clear();
  2156. for (int i = 0; i < 20; i++) list.Add(i);
  2157. view = (ArrayList<int>)list.View(5, 7);
  2158. for (int i = 0; i < 7; i++) Assert.AreEqual(i + 5, view[i]);
  2159. for (int i = 0; i < 7; i++) Assert.AreEqual(i, view.IndexOf(i + 5));
  2160. for (int i = 0; i < 7; i++) Assert.AreEqual(i, view.LastIndexOf(i + 5));
  2161. }
  2162. [Test]
  2163. public void Insert()
  2164. {
  2165. view.Insert(0, 34);
  2166. view.Insert(1, 35);
  2167. view.Insert(4, 36);
  2168. Assert.IsTrue(view.Check());
  2169. Assert.IsTrue(IC.eq(view, 34, 35, 1, 2, 36));
  2170. IList<int> list2 = new ArrayList<int>();
  2171. list2.AddAll(view);
  2172. view.InsertAll(3, list2);
  2173. Assert.IsTrue(view.Check());
  2174. Assert.IsTrue(IC.eq(view, 34, 35, 1, 34, 35, 1, 2, 36, 2, 36));
  2175. }
  2176. [Test]
  2177. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  2178. public void RangeCheck1()
  2179. {
  2180. IList<int> lst = new ArrayList<int>();
  2181. lst.Add(2);
  2182. lst = lst.View(1, 1);
  2183. }
  2184. [Test]
  2185. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  2186. public void RangeCheck2()
  2187. {
  2188. IList<int> lst = new ArrayList<int>();
  2189. lst.Add(2);
  2190. lst = lst.View(1, -1);
  2191. }
  2192. [Test]
  2193. public void Sort()
  2194. {
  2195. view.Add(45); view.Add(47); view.Add(46); view.Add(48);
  2196. Assert.IsFalse(view.IsSorted(new IC()));
  2197. view.Sort(new IC());
  2198. check();
  2199. Assert.IsTrue(IC.eq(list, 0, 1, 2, 45, 46, 47, 48, 3));
  2200. Assert.IsTrue(IC.eq(view, 1, 2, 45, 46, 47, 48));
  2201. }
  2202. [Test]
  2203. public void Remove()
  2204. {
  2205. view.Add(1); view.Add(5); view.Add(3); view.Add(1); view.Add(3); view.Add(0);
  2206. Assert.IsTrue(IC.eq(view, 1, 2, 1, 5, 3, 1, 3, 0));
  2207. Assert.IsTrue(view.Remove(1));
  2208. check();
  2209. Assert.IsTrue(IC.eq(view, 1, 2, 1, 5, 3, 3, 0));
  2210. Assert.IsTrue(view.Remove(1));
  2211. check();
  2212. Assert.IsTrue(IC.eq(view, 1, 2, 5, 3, 3, 0));
  2213. Assert.IsTrue(view.Remove(0));
  2214. check();
  2215. Assert.IsTrue(IC.eq(view, 1, 2, 5, 3, 3));
  2216. view.RemoveAllCopies(3);
  2217. check();
  2218. Assert.IsTrue(IC.eq(view, 1, 2, 5));
  2219. Assert.IsTrue(IC.eq(list, 0, 1, 2, 5, 3));
  2220. view.Add(1); view.Add(5); view.Add(3); view.Add(1); view.Add(3); view.Add(0);
  2221. Assert.IsTrue(IC.eq(view, 1, 2, 5, 1, 5, 3, 1, 3, 0));
  2222. view.FIFO = true;
  2223. view.Clear(); view.Add(1); view.Add(2);
  2224. view.Add(1); view.Add(5); view.Add(3); view.Add(1); view.Add(3); view.Add(0);
  2225. Assert.IsTrue(IC.eq(view, 1, 2, 1, 5, 3, 1, 3, 0));
  2226. Assert.IsTrue(view.Remove(1));
  2227. check();
  2228. Assert.IsTrue(IC.eq(view, 2, 1, 5, 3, 1, 3, 0));
  2229. Assert.IsTrue(view.Remove(1));
  2230. check();
  2231. Assert.IsTrue(IC.eq(view, 2, 5, 3, 1, 3, 0));
  2232. Assert.IsTrue(view.Remove(0));
  2233. check();
  2234. Assert.IsTrue(IC.eq(view, 2, 5, 3, 1, 3));
  2235. view.RemoveAllCopies(3);
  2236. check();
  2237. Assert.IsTrue(IC.eq(view, 2, 5, 1));
  2238. Assert.IsTrue(IC.eq(list, 0, 2, 5, 1, 3));
  2239. view.Add(1); view.Add(5); view.Add(3); view.Add(1); view.Add(3); view.Add(0);
  2240. Assert.IsTrue(IC.eq(view, 2, 5, 1, 1, 5, 3, 1, 3, 0));
  2241. view.FIFO = false;
  2242. ArrayList<int> l2 = new ArrayList<int>();
  2243. l2.Add(1); l2.Add(2); l2.Add(2); l2.Add(3); l2.Add(1);
  2244. view.RemoveAll(l2);
  2245. check();
  2246. Assert.IsTrue(IC.eq(view, 5, 5, 1, 3, 0));
  2247. view.RetainAll(l2);
  2248. check();
  2249. Assert.IsTrue(IC.eq(view, 1, 3));
  2250. view.Add(2); view.Add(4); view.Add(5);
  2251. Assert.AreEqual(1, view.RemoveAt(0));
  2252. Assert.AreEqual(5, view.RemoveAt(3));
  2253. Assert.AreEqual(2, view.RemoveAt(1));
  2254. check();
  2255. Assert.IsTrue(IC.eq(view, 3, 4));
  2256. view.Add(8);
  2257. Assert.AreEqual(3, view.RemoveFirst());
  2258. Assert.AreEqual(8, view.RemoveLast());
  2259. view.Add(2); view.Add(5); view.Add(3); view.Add(1);
  2260. view.RemoveInterval(1, 2);
  2261. check();
  2262. Assert.IsTrue(IC.eq(view, 4, 3, 1));
  2263. }
  2264. [Test]
  2265. public void Reverse()
  2266. {
  2267. view.Clear();
  2268. for (int i = 0; i < 10; i++) view.Add(10 + i);
  2269. view.View(3, 4).Reverse();
  2270. check();
  2271. Assert.IsTrue(IC.eq(view, 10, 11, 12, 16, 15, 14, 13, 17, 18, 19));
  2272. view.Reverse();
  2273. Assert.IsTrue(IC.eq(view, 19, 18, 17, 13, 14, 15, 16, 12, 11, 10));
  2274. Assert.IsTrue(IC.eq(list, 0, 19, 18, 17, 13, 14, 15, 16, 12, 11, 10, 3));
  2275. }
  2276. [Test]
  2277. public void Slide()
  2278. {
  2279. view.Slide(1);
  2280. check();
  2281. Assert.IsTrue(IC.eq(view, 2, 3));
  2282. view.Slide(-2);
  2283. check();
  2284. Assert.IsTrue(IC.eq(view, 0, 1));
  2285. view.Slide(0, 3);
  2286. check();
  2287. Assert.IsTrue(IC.eq(view, 0, 1, 2));
  2288. view.Slide(2, 1);
  2289. check();
  2290. Assert.IsTrue(IC.eq(view, 2));
  2291. Assert.AreEqual(view, view.Slide(-1, 0));
  2292. check();
  2293. Assert.IsTrue(IC.eq(view));
  2294. view.Add(28);
  2295. Assert.IsTrue(IC.eq(list, 0, 28, 1, 2, 3));
  2296. }
  2297. [Test]
  2298. public void Iterate()
  2299. {
  2300. list.Clear();
  2301. view = null;
  2302. foreach (int i in new int[] { 2, 4, 8, 13, 6, 1, 2, 7 }) list.Add(i);
  2303. view = (ArrayList<int>)list.View(list.Count - 2, 2);
  2304. while (true)
  2305. {
  2306. if ((view.Last - view.First) % 2 == 1)
  2307. view.Insert(1, 666);
  2308. check();
  2309. if (view.Offset == 0)
  2310. break;
  2311. else
  2312. view.Slide(-1, 2);
  2313. }
  2314. Assert.IsTrue(list.Check());
  2315. Assert.IsTrue(IC.eq(list, 2, 4, 8, 666, 13, 6, 1, 666, 2, 666, 7));
  2316. }
  2317. [Test]
  2318. public void SyncRoot()
  2319. {
  2320. Assert.AreSame(((System.Collections.IList)view).SyncRoot, ((System.Collections.IList)list).SyncRoot);
  2321. }
  2322. }
  2323. [TestFixture]
  2324. public class MulipleViews
  2325. {
  2326. IList<int> list;
  2327. IList<int>[][] views;
  2328. [SetUp]
  2329. public void Init()
  2330. {
  2331. list = new ArrayList<int>();
  2332. for (int i = 0; i < 6; i++)
  2333. list.Add(i);
  2334. views = new IList<int>[7][];
  2335. for (int i = 0; i < 7; i++)
  2336. {
  2337. views[i] = new IList<int>[7 - i];
  2338. for (int j = 0; j < 7 - i; j++)
  2339. views[i][j] = list.View(i, j);
  2340. }
  2341. }
  2342. [TearDown]
  2343. public void Dispose()
  2344. {
  2345. list = null;
  2346. views = null;
  2347. }
  2348. [Test]
  2349. public void Insert()
  2350. {
  2351. Assert.IsTrue(list.Check(), "list check before insert");
  2352. list.Insert(3, 777);
  2353. Assert.IsTrue(list.Check(), "list check after insert");
  2354. for (int i = 0; i < 7; i++)
  2355. for (int j = 0; j < 7 - i; j++)
  2356. {
  2357. Assert.AreEqual(i < 3 || (i == 3 && j == 0) ? i : i + 1, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
  2358. Assert.AreEqual(i < 3 && i + j > 3 ? j + 1 : j, views[i][j].Count, "view[" + i + "][" + j + "] count");
  2359. }
  2360. }
  2361. [Test]
  2362. public void RemoveAt()
  2363. {
  2364. Assert.IsTrue(list.Check(), "list check before remove");
  2365. list.RemoveAt(3);
  2366. Assert.IsTrue(list.Check(), "list check after remove");
  2367. for (int i = 0; i < 7; i++)
  2368. for (int j = 0; j < 7 - i; j++)
  2369. {
  2370. Assert.AreEqual(i <= 3 ? i : i - 1, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
  2371. Assert.AreEqual(i <= 3 && i + j > 3 ? j - 1 : j, views[i][j].Count, "view[" + i + "][" + j + "] count");
  2372. }
  2373. }
  2374. [Test]
  2375. public void RemoveInterval()
  2376. {
  2377. Assert.IsTrue(list.Check(), "list check before remove");
  2378. list.RemoveInterval(3, 2);
  2379. Assert.IsTrue(list.Check(), "list check after remove");
  2380. for (int i = 0; i < 7; i++)
  2381. for (int j = 0; j < 7 - i; j++)
  2382. {
  2383. Assert.AreEqual(i <= 3 ? i : i <= 5 ? 3 : i - 2, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
  2384. Assert.AreEqual(j == 0 ? 0 : i <= 3 && i + j > 4 ? j - 2 : i > 4 || i + j <= 3 ? j : j - 1, views[i][j].Count, "view[" + i + "][" + j + "] count");
  2385. }
  2386. }
  2387. [Test]
  2388. public void InsertAtEnd()
  2389. {
  2390. Assert.IsTrue(list.Check(), "list check before insert");
  2391. list.InsertLast(777);
  2392. Assert.IsTrue(list.Check(), "list check after insert");
  2393. for (int i = 0; i < 7; i++)
  2394. for (int j = 0; j < 7 - i; j++)
  2395. {
  2396. Assert.AreEqual(i, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
  2397. Assert.AreEqual(j, views[i][j].Count, "view[" + i + "][" + j + "] count");
  2398. }
  2399. }
  2400. [Test]
  2401. public void RemoveAtEnd()
  2402. {
  2403. Assert.IsTrue(list.Check(), "list check before remove");
  2404. list.RemoveAt(5);
  2405. Assert.IsTrue(list.Check(), "list check after remove");
  2406. for (int i = 0; i < 7; i++)
  2407. for (int j = 0; j < 7 - i; j++)
  2408. {
  2409. Assert.AreEqual(i <= 5 ? i : i - 1, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
  2410. Assert.AreEqual(i <= 5 && i + j > 5 ? j - 1 : j, views[i][j].Count, "view[" + i + "][" + j + "] count");
  2411. }
  2412. }
  2413. [Test]
  2414. public void InsertAtStart()
  2415. {
  2416. Assert.IsTrue(list.Check(), "list check before insert");
  2417. list.Insert(0, 777);
  2418. Assert.IsTrue(list.Check(), "list check after insert");
  2419. for (int i = 0; i < 7; i++)
  2420. for (int j = 0; j < 7 - i; j++)
  2421. {
  2422. Assert.AreEqual(i == 0 && j == 0 ? 0 : i + 1, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
  2423. Assert.AreEqual(j, views[i][j].Count, "view[" + i + "][" + j + "] count");
  2424. }
  2425. }
  2426. [Test]
  2427. public void RemoveAtStart()
  2428. {
  2429. Assert.IsTrue(list.Check(), "list check before remove");
  2430. list.RemoveAt(0);
  2431. Assert.IsTrue(list.Check(), "list check after remove");
  2432. for (int i = 0; i < 7; i++)
  2433. for (int j = 0; j < 7 - i; j++)
  2434. {
  2435. Assert.AreEqual(i == 0 ? i : i - 1, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
  2436. Assert.AreEqual(i == 0 && j > 0 ? j - 1 : j, views[i][j].Count, "view[" + i + "][" + j + "] count");
  2437. }
  2438. }
  2439. [Test]
  2440. public void Clear()
  2441. {
  2442. Assert.IsTrue(list.Check(), "list check before clear");
  2443. views[2][3].Clear();
  2444. Assert.IsTrue(list.Check(), "list check after clear");
  2445. for (int i = 0; i < 7; i++)
  2446. for (int j = 0; j < 7 - i; j++)
  2447. {
  2448. Assert.AreEqual(i < 2 ? i : i < 6 ? 2 : i - 3, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
  2449. Assert.AreEqual(s(i, j), views[i][j].Count, "view[" + i + "][" + j + "] count");
  2450. }
  2451. }
  2452. private int s(int i, int j)
  2453. {
  2454. if (j == 0) return 0;
  2455. int k = i + j - 1; //end
  2456. if (i > 4 || k <= 1) return j;
  2457. if (i >= 2) return k > 4 ? k - 4 : 0;
  2458. if (i <= 2) return k >= 4 ? j - 3 : 2 - i;
  2459. return -1;
  2460. }
  2461. [Test]
  2462. public void InsertAll()
  2463. {
  2464. ArrayList<int> list2 = new ArrayList<int>();
  2465. for (int i = 0; i < 5; i++) { list2.Add(100 + i); }
  2466. Assert.IsTrue(list.Check(), "list check before insertAll");
  2467. list.InsertAll(3, list2);
  2468. Assert.IsTrue(list.Check(), "list check after insertAll");
  2469. for (int i = 0; i < 7; i++)
  2470. for (int j = 0; j < 7 - i; j++)
  2471. {
  2472. Assert.AreEqual(i < 3 || (i == 3 && j == 0) ? i : i + 5, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
  2473. Assert.AreEqual(i < 3 && i + j > 3 ? j + 5 : j, views[i][j].Count, "view[" + i + "][" + j + "] count");
  2474. }
  2475. }
  2476. [Test]
  2477. public void AddAll()
  2478. {
  2479. ArrayList<int> list2 = new ArrayList<int>();
  2480. for (int i = 0; i < 5; i++) { list2.Add(100 + i); }
  2481. Assert.IsTrue(list.Check(), "list check before AddAll");
  2482. list.View(1, 2).AddAll(list2);
  2483. Assert.IsTrue(list.Check(), "list check after AddAll");
  2484. for (int i = 0; i < 7; i++)
  2485. for (int j = 0; j < 7 - i; j++)
  2486. {
  2487. Assert.AreEqual(i < 3 || (i == 3 && j == 0) ? i : i + 5, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
  2488. Assert.AreEqual(i < 3 && i + j > 3 ? j + 5 : j, views[i][j].Count, "view[" + i + "][" + j + "] count");
  2489. }
  2490. }
  2491. [Test]
  2492. public void RemoveAll1()
  2493. {
  2494. ArrayList<int> list2 = new ArrayList<int>();
  2495. list2.Add(1); list2.Add(3); list2.Add(4);
  2496. for (int i = 0; i < 7; i++)
  2497. {
  2498. for (int j = 0; j < 7 - i; j++)
  2499. {
  2500. list = new ArrayList<int>();
  2501. for (int k = 0; k < 6; k++) list.Add(k);
  2502. ArrayList<int> v = (ArrayList<int>)list.View(i, j);
  2503. list.RemoveAll(list2);
  2504. Assert.IsTrue(list.Check(), "list check after RemoveAll, i=" + i + ", j=" + j);
  2505. }
  2506. }
  2507. }
  2508. [Test]
  2509. public void RemoveAll2()
  2510. {
  2511. ArrayList<int> list2 = new ArrayList<int>();
  2512. list2.Add(1); list2.Add(3); list2.Add(4);
  2513. Assert.IsTrue(list.Check(), "list check before RemoveAll");
  2514. list.RemoveAll(list2);
  2515. Assert.AreEqual(0, views[0][0].Offset, "view [0][0] offset");
  2516. Assert.AreEqual(0, views[0][1].Offset, "view [0][1] offset");
  2517. Assert.AreEqual(0, views[0][2].Offset, "view [0][2] offset");
  2518. Assert.AreEqual(0, views[0][3].Offset, "view [0][3] offset");
  2519. Assert.AreEqual(0, views[0][4].Offset, "view [0][4] offset");
  2520. Assert.AreEqual(0, views[0][5].Offset, "view [0][5] offset");
  2521. Assert.AreEqual(0, views[0][6].Offset, "view [0][6] offset");
  2522. Assert.AreEqual(1, views[1][0].Offset, "view [1][0] offset");
  2523. Assert.AreEqual(1, views[1][1].Offset, "view [1][1] offset");
  2524. Assert.AreEqual(1, views[1][2].Offset, "view [1][2] offset");
  2525. Assert.AreEqual(1, views[1][3].Offset, "view [1][3] offset");
  2526. Assert.AreEqual(1, views[1][4].Offset, "view [1][4] offset");
  2527. Assert.AreEqual(1, views[1][5].Offset, "view [1][5] offset");
  2528. Assert.AreEqual(1, views[2][0].Offset, "view [2][0] offset");
  2529. Assert.AreEqual(1, views[2][1].Offset, "view [2][1] offset");
  2530. Assert.AreEqual(1, views[2][2].Offset, "view [2][2] offset");
  2531. Assert.AreEqual(1, views[2][3].Offset, "view [2][3] offset");
  2532. Assert.AreEqual(1, views[2][4].Offset, "view [2][4] offset");
  2533. Assert.AreEqual(2, views[3][0].Offset, "view [3][0] offset");
  2534. Assert.AreEqual(2, views[3][1].Offset, "view [3][1] offset");
  2535. Assert.AreEqual(2, views[3][2].Offset, "view [3][2] offset");
  2536. Assert.AreEqual(2, views[3][3].Offset, "view [3][3] offset");
  2537. Assert.AreEqual(2, views[4][0].Offset, "view [4][0] offset");
  2538. Assert.AreEqual(2, views[4][1].Offset, "view [4][1] offset");
  2539. Assert.AreEqual(2, views[4][2].Offset, "view [4][2] offset");
  2540. Assert.AreEqual(2, views[5][0].Offset, "view [5][0] offset");
  2541. Assert.AreEqual(2, views[5][1].Offset, "view [5][1] offset");
  2542. Assert.AreEqual(3, views[6][0].Offset, "view [6][0] offset");
  2543. Assert.AreEqual(0, views[0][0].Count, "view [0][0] count");
  2544. Assert.AreEqual(1, views[0][1].Count, "view [0][1] count");
  2545. Assert.AreEqual(1, views[0][2].Count, "view [0][2] count");
  2546. Assert.AreEqual(2, views[0][3].Count, "view [0][3] count");
  2547. Assert.AreEqual(2, views[0][4].Count, "view [0][4] count");
  2548. Assert.AreEqual(2, views[0][5].Count, "view [0][5] count");
  2549. Assert.AreEqual(3, views[0][6].Count, "view [0][6] count");
  2550. Assert.AreEqual(0, views[1][0].Count, "view [1][0] count");
  2551. Assert.AreEqual(0, views[1][1].Count, "view [1][1] count");
  2552. Assert.AreEqual(1, views[1][2].Count, "view [1][2] count");
  2553. Assert.AreEqual(1, views[1][3].Count, "view [1][3] count");
  2554. Assert.AreEqual(1, views[1][4].Count, "view [1][4] count");
  2555. Assert.AreEqual(2, views[1][5].Count, "view [1][5] count");
  2556. Assert.AreEqual(0, views[2][0].Count, "view [2][0] count");
  2557. Assert.AreEqual(1, views[2][1].Count, "view [2][1] count");
  2558. Assert.AreEqual(1, views[2][2].Count, "view [2][2] count");
  2559. Assert.AreEqual(1, views[2][3].Count, "view [2][3] count");
  2560. Assert.AreEqual(2, views[2][4].Count, "view [2][4] count");
  2561. Assert.AreEqual(0, views[3][0].Count, "view [3][0] count");
  2562. Assert.AreEqual(0, views[3][1].Count, "view [3][1] count");
  2563. Assert.AreEqual(0, views[3][2].Count, "view [3][2] count");
  2564. Assert.AreEqual(1, views[3][3].Count, "view [3][3] count");
  2565. Assert.AreEqual(0, views[4][0].Count, "view [4][0] count");
  2566. Assert.AreEqual(0, views[4][1].Count, "view [4][1] count");
  2567. Assert.AreEqual(1, views[4][2].Count, "view [4][2] count");
  2568. Assert.AreEqual(0, views[5][0].Count, "view [5][0] count");
  2569. Assert.AreEqual(1, views[5][1].Count, "view [5][1] count");
  2570. Assert.AreEqual(0, views[6][0].Count, "view [6][0] count");
  2571. Assert.IsTrue(list.Check(), "list check after RemoveAll");
  2572. }
  2573. [Test]
  2574. public void RetainAll()
  2575. {
  2576. ArrayList<int> list2 = new ArrayList<int>();
  2577. list2.Add(2); list2.Add(4); list2.Add(5);
  2578. Assert.IsTrue(list.Check(), "list check before RetainAll");
  2579. list.RetainAll(list2);
  2580. Assert.AreEqual(0, views[0][0].Offset, "view [0][0] offset");
  2581. Assert.AreEqual(0, views[0][1].Offset, "view [0][1] offset");
  2582. Assert.AreEqual(0, views[0][2].Offset, "view [0][2] offset");
  2583. Assert.AreEqual(0, views[0][3].Offset, "view [0][3] offset");
  2584. Assert.AreEqual(0, views[0][4].Offset, "view [0][4] offset");
  2585. Assert.AreEqual(0, views[0][5].Offset, "view [0][5] offset");
  2586. Assert.AreEqual(0, views[0][6].Offset, "view [0][6] offset");
  2587. Assert.AreEqual(0, views[1][0].Offset, "view [1][0] offset");
  2588. Assert.AreEqual(0, views[1][1].Offset, "view [1][1] offset");
  2589. Assert.AreEqual(0, views[1][2].Offset, "view [1][2] offset");
  2590. Assert.AreEqual(0, views[1][3].Offset, "view [1][3] offset");
  2591. Assert.AreEqual(0, views[1][4].Offset, "view [1][4] offset");
  2592. Assert.AreEqual(0, views[1][5].Offset, "view [1][5] offset");
  2593. Assert.AreEqual(0, views[2][0].Offset, "view [2][0] offset");
  2594. Assert.AreEqual(0, views[2][1].Offset, "view [2][1] offset");
  2595. Assert.AreEqual(0, views[2][2].Offset, "view [2][2] offset");
  2596. Assert.AreEqual(0, views[2][3].Offset, "view [2][3] offset");
  2597. Assert.AreEqual(0, views[2][4].Offset, "view [2][4] offset");
  2598. Assert.AreEqual(1, views[3][0].Offset, "view [3][0] offset");
  2599. Assert.AreEqual(1, views[3][1].Offset, "view [3][1] offset");
  2600. Assert.AreEqual(1, views[3][2].Offset, "view [3][2] offset");
  2601. Assert.AreEqual(1, views[3][3].Offset, "view [3][3] offset");
  2602. Assert.AreEqual(1, views[4][0].Offset, "view [4][0] offset");
  2603. Assert.AreEqual(1, views[4][1].Offset, "view [4][1] offset");
  2604. Assert.AreEqual(1, views[4][2].Offset, "view [4][2] offset");
  2605. Assert.AreEqual(2, views[5][0].Offset, "view [5][0] offset");
  2606. Assert.AreEqual(2, views[5][1].Offset, "view [5][1] offset");
  2607. Assert.AreEqual(3, views[6][0].Offset, "view [6][0] offset");
  2608. Assert.AreEqual(0, views[0][0].Count, "view [0][0] count");
  2609. Assert.AreEqual(0, views[0][1].Count, "view [0][1] count");
  2610. Assert.AreEqual(0, views[0][2].Count, "view [0][2] count");
  2611. Assert.AreEqual(1, views[0][3].Count, "view [0][3] count");
  2612. Assert.AreEqual(1, views[0][4].Count, "view [0][4] count");
  2613. Assert.AreEqual(2, views[0][5].Count, "view [0][5] count");
  2614. Assert.AreEqual(3, views[0][6].Count, "view [0][6] count");
  2615. Assert.AreEqual(0, views[1][0].Count, "view [1][0] count");
  2616. Assert.AreEqual(0, views[1][1].Count, "view [1][1] count");
  2617. Assert.AreEqual(1, views[1][2].Count, "view [1][2] count");
  2618. Assert.AreEqual(1, views[1][3].Count, "view [1][3] count");
  2619. Assert.AreEqual(2, views[1][4].Count, "view [1][4] count");
  2620. Assert.AreEqual(3, views[1][5].Count, "view [1][5] count");
  2621. Assert.AreEqual(0, views[2][0].Count, "view [2][0] count");
  2622. Assert.AreEqual(1, views[2][1].Count, "view [2][1] count");
  2623. Assert.AreEqual(1, views[2][2].Count, "view [2][2] count");
  2624. Assert.AreEqual(2, views[2][3].Count, "view [2][3] count");
  2625. Assert.AreEqual(3, views[2][4].Count, "view [2][4] count");
  2626. Assert.AreEqual(0, views[3][0].Count, "view [3][0] count");
  2627. Assert.AreEqual(0, views[3][1].Count, "view [3][1] count");
  2628. Assert.AreEqual(1, views[3][2].Count, "view [3][2] count");
  2629. Assert.AreEqual(2, views[3][3].Count, "view [3][3] count");
  2630. Assert.AreEqual(0, views[4][0].Count, "view [4][0] count");
  2631. Assert.AreEqual(1, views[4][1].Count, "view [4][1] count");
  2632. Assert.AreEqual(2, views[4][2].Count, "view [4][2] count");
  2633. Assert.AreEqual(0, views[5][0].Count, "view [5][0] count");
  2634. Assert.AreEqual(1, views[5][1].Count, "view [5][1] count");
  2635. Assert.AreEqual(0, views[6][0].Count, "view [6][0] count");
  2636. Assert.IsTrue(list.Check(), "list check after RetainAll");
  2637. }
  2638. [Test]
  2639. public void RemoveAllCopies()
  2640. {
  2641. ArrayList<int> list2 = new ArrayList<int>();
  2642. list2.Add(0); list2.Add(2); list2.Add(2); list2.Add(2); list2.Add(5); list2.Add(2); list2.Add(1);
  2643. for (int i = 0; i < 7; i++)
  2644. {
  2645. for (int j = 0; j < 7 - i; j++)
  2646. {
  2647. list = new ArrayList<int>();
  2648. list.AddAll(list2);
  2649. ArrayList<int> v = (ArrayList<int>)list.View(i, j);
  2650. list.RemoveAllCopies(2);
  2651. Assert.AreEqual((i == 0 && j > 0 ? 1 : 0) + (i <= 4 && i + j > 4 ? 1 : 0) + (i <= 6 && i + j > 6 ? 1 : 0), v.Count, "v.Count, i=" + i + ", j=" + j);
  2652. Assert.AreEqual(i == 0 ? 0 : i <= 4 ? 1 : i <= 6 ? 2 : 3, v.Offset, "v.Offset, i=" + i + ", j=" + j);
  2653. Assert.IsTrue(list.Check(), "list check after RemoveAllCopies, i=" + i + ", j=" + j);
  2654. }
  2655. }
  2656. }
  2657. private void checkDisposed(bool reverse, int start, int count)
  2658. {
  2659. int k = 0;
  2660. for (int i = 0; i < 7; i++)
  2661. for (int j = 0; j < 7 - i; j++)
  2662. {
  2663. if (i + j <= start || i >= start + count || (i <= start && i + j >= start + count) || (reverse && start <= i && start + count >= i + j))
  2664. {
  2665. try
  2666. {
  2667. k = views[i][j].Count;
  2668. }
  2669. catch (ViewDisposedException)
  2670. {
  2671. Assert.Fail("view[" + i + "][" + j + "] threw");
  2672. }
  2673. Assert.AreEqual(j, views[i][j].Count, "view[" + i + "][" + j + "] size");
  2674. if (reverse && ((j > 0 && start <= i && start + count >= i + j) || (j == 0 && start < i && start + count > i)))
  2675. Assert.AreEqual(start + (start + count - i - j), views[i][j].Offset, "view[" + i + "][" + j + "] offset (mirrored)");
  2676. else
  2677. Assert.AreEqual(i, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
  2678. }
  2679. else
  2680. {
  2681. try
  2682. {
  2683. k = views[i][j].Count;
  2684. Assert.Fail("view[" + i + "][" + j + "] no throw");
  2685. }
  2686. catch (ViewDisposedException) { }
  2687. }
  2688. }
  2689. }
  2690. [Test]
  2691. public void Reverse()
  2692. {
  2693. int start = 2, count = 3;
  2694. IList<int> list2 = list.View(start, count);
  2695. Assert.IsTrue(list.Check(), "list check before Reverse");
  2696. list2.Reverse();
  2697. Assert.IsTrue(list.Check(), "list check after Reverse");
  2698. checkDisposed(true, start, count);
  2699. }
  2700. [Test]
  2701. public void Sort()
  2702. {
  2703. int start = 2, count = 3;
  2704. IList<int> list2 = list.View(start, count);
  2705. Assert.IsTrue(list.Check(), "list check before Sort");
  2706. list2.Sort();
  2707. Assert.IsTrue(list.Check(), "list check after Sort");
  2708. checkDisposed(false, start, count);
  2709. }
  2710. [Test]
  2711. public void Shuffle()
  2712. {
  2713. int start = 2, count = 3;
  2714. IList<int> list2 = list.View(start, count);
  2715. Assert.IsTrue(list.Check(), "list check before Shuffle");
  2716. list2.Shuffle();
  2717. Assert.IsTrue(list.Check(), "list check after Shuffle");
  2718. checkDisposed(false, start, count);
  2719. }
  2720. }
  2721. }
  2722. namespace ArrayListOfTreesORLists
  2723. {
  2724. [TestFixture]
  2725. public class MultiLevelUnorderedOfUnOrdered
  2726. {
  2727. private ICollection<int> dit, dat, dut;
  2728. private ICollection<ICollection<int>> Dit, Dat, Dut;
  2729. [SetUp]
  2730. public void Init()
  2731. {
  2732. dit = new ArrayList<int>();
  2733. dat = new TreeSet<int>(Comparer<int>.Default, EqualityComparer<int>.Default);
  2734. dut = new ArrayList<int>();
  2735. dit.Add(2); dit.Add(1);
  2736. dat.Add(1); dat.Add(2);
  2737. dut.Add(3);
  2738. Dit = new ArrayList<ICollection<int>>();
  2739. Dat = new ArrayList<ICollection<int>>();
  2740. Dut = new ArrayList<ICollection<int>>();
  2741. }
  2742. [Test]
  2743. public void Check()
  2744. {
  2745. Assert.IsTrue(dit.UnsequencedEquals(dat));
  2746. Assert.IsFalse(dit.UnsequencedEquals(dut));
  2747. }
  2748. [Test]
  2749. public void Multi()
  2750. {
  2751. Dit.Add(dit); Dit.Add(dut); Dit.Add(dit);
  2752. Dat.Add(dut); Dat.Add(dit); Dat.Add(dat);
  2753. Assert.IsTrue(Dit.UnsequencedEquals(Dat));
  2754. Assert.IsFalse(Dit.UnsequencedEquals(Dut));
  2755. }
  2756. [TearDown]
  2757. public void Dispose()
  2758. {
  2759. dit = dat = dut = null;
  2760. Dit = Dat = Dut = null;
  2761. }
  2762. }
  2763. [TestFixture]
  2764. public class MultiLevelOrderedOfUnOrdered
  2765. {
  2766. private ICollection<int> dit, dat, dut;
  2767. private ISequenced<ICollection<int>> Dit, Dat, Dut;
  2768. [SetUp]
  2769. public void Init()
  2770. {
  2771. dit = new ArrayList<int>();
  2772. dat = new TreeSet<int>(Comparer<int>.Default, EqualityComparer<int>.Default);
  2773. dut = new ArrayList<int>();
  2774. dit.Add(2); dit.Add(1);
  2775. dat.Add(1); dat.Add(2);
  2776. dut.Add(3);
  2777. Dit = new ArrayList<ICollection<int>>();
  2778. Dat = new ArrayList<ICollection<int>>();
  2779. Dut = new ArrayList<ICollection<int>>();
  2780. }
  2781. [Test]
  2782. public void Check()
  2783. {
  2784. Assert.IsTrue(dit.UnsequencedEquals(dat));
  2785. Assert.IsFalse(dit.UnsequencedEquals(dut));
  2786. }
  2787. [Test]
  2788. public void Multi()
  2789. {
  2790. Dit.Add(dit); Dit.Add(dut); Dit.Add(dit);
  2791. Dat.Add(dut); Dat.Add(dit); Dat.Add(dat);
  2792. Dut.Add(dit); Dut.Add(dut); Dut.Add(dat);
  2793. Assert.IsFalse(Dit.SequencedEquals(Dat));
  2794. Assert.IsTrue(Dit.SequencedEquals(Dut));
  2795. }
  2796. [TearDown]
  2797. public void Dispose()
  2798. {
  2799. dit = dat = dut = null;
  2800. Dit = Dat = Dut = null;
  2801. }
  2802. }
  2803. [TestFixture]
  2804. public class MultiLevelUnOrderedOfOrdered
  2805. {
  2806. private ISequenced<int> dit, dat, dut, dot;
  2807. private ICollection<ISequenced<int>> Dit, Dat, Dut, Dot;
  2808. [SetUp]
  2809. public void Init()
  2810. {
  2811. dit = new TreeSet<int>(Comparer<int>.Default, EqualityComparer<int>.Default);
  2812. dat = new ArrayList<int>();
  2813. dut = new ArrayList<int>();
  2814. dot = new ArrayList<int>();
  2815. dit.Add(2); dit.Add(1);
  2816. dat.Add(2); dat.Add(1);
  2817. dut.Add(3);
  2818. dot.Add(1); dot.Add(2);
  2819. Dit = new ArrayList<ISequenced<int>>();
  2820. Dat = new ArrayList<ISequenced<int>>();
  2821. Dut = new ArrayList<ISequenced<int>>();
  2822. Dot = new ArrayList<ISequenced<int>>();
  2823. }
  2824. [Test]
  2825. public void Check()
  2826. {
  2827. Assert.IsFalse(dit.SequencedEquals(dat));
  2828. Assert.IsTrue(dit.SequencedEquals(dot));
  2829. Assert.IsFalse(dit.SequencedEquals(dut));
  2830. }
  2831. [Test]
  2832. public void Multi()
  2833. {
  2834. Dit.Add(dit); Dit.Add(dut); Dit.Add(dit);
  2835. Dat.Add(dut); Dat.Add(dit); Dat.Add(dat);
  2836. Dut.Add(dot); Dut.Add(dut); Dut.Add(dit);
  2837. Dot.Add(dit); Dot.Add(dit); Dot.Add(dut);
  2838. Assert.IsTrue(Dit.UnsequencedEquals(Dut));
  2839. Assert.IsFalse(Dit.UnsequencedEquals(Dat));
  2840. Assert.IsTrue(Dit.UnsequencedEquals(Dot));
  2841. }
  2842. [TearDown]
  2843. public void Dispose()
  2844. {
  2845. dit = dat = dut = dot = null;
  2846. Dit = Dat = Dut = Dot = null;
  2847. }
  2848. }
  2849. [TestFixture]
  2850. public class MultiLevelOrderedOfOrdered
  2851. {
  2852. private ISequenced<int> dit, dat, dut, dot;
  2853. private ISequenced<ISequenced<int>> Dit, Dat, Dut, Dot;
  2854. [SetUp]
  2855. public void Init()
  2856. {
  2857. dit = new TreeSet<int>(Comparer<int>.Default, EqualityComparer<int>.Default);
  2858. dat = new ArrayList<int>();
  2859. dut = new ArrayList<int>();
  2860. dot = new ArrayList<int>();
  2861. dit.Add(2); dit.Add(1);
  2862. dat.Add(2); dat.Add(1);
  2863. dut.Add(3);
  2864. dot.Add(1); dot.Add(2);
  2865. Dit = new ArrayList<ISequenced<int>>();
  2866. Dat = new ArrayList<ISequenced<int>>();
  2867. Dut = new ArrayList<ISequenced<int>>();
  2868. Dot = new ArrayList<ISequenced<int>>();
  2869. }
  2870. [Test]
  2871. public void Check()
  2872. {
  2873. Assert.IsFalse(dit.SequencedEquals(dat));
  2874. Assert.IsTrue(dit.SequencedEquals(dot));
  2875. Assert.IsFalse(dit.SequencedEquals(dut));
  2876. }
  2877. [Test]
  2878. public void Multi()
  2879. {
  2880. Dit.Add(dit); Dit.Add(dut); Dit.Add(dit);
  2881. Dat.Add(dut); Dat.Add(dit); Dat.Add(dat);
  2882. Dut.Add(dot); Dut.Add(dut); Dut.Add(dit);
  2883. Dot.Add(dit); Dot.Add(dit); Dot.Add(dut);
  2884. Assert.IsTrue(Dit.SequencedEquals(Dut));
  2885. Assert.IsFalse(Dit.SequencedEquals(Dat));
  2886. Assert.IsFalse(Dit.SequencedEquals(Dot));
  2887. }
  2888. [TearDown]
  2889. public void Dispose()
  2890. {
  2891. dit = dat = dut = dot = null;
  2892. Dit = Dat = Dut = Dot = null;
  2893. }
  2894. }
  2895. }
  2896. namespace HashingAndEquals
  2897. {
  2898. [TestFixture]
  2899. public class ISequenced
  2900. {
  2901. private ISequenced<int> dit, dat, dut;
  2902. [SetUp]
  2903. public void Init()
  2904. {
  2905. dit = new ArrayList<int>();
  2906. dat = new ArrayList<int>();
  2907. dut = new ArrayList<int>();
  2908. }
  2909. [Test]
  2910. public void EmptyEmpty()
  2911. {
  2912. Assert.IsTrue(dit.SequencedEquals(dat));
  2913. }
  2914. [Test]
  2915. public void EmptyNonEmpty()
  2916. {
  2917. dit.Add(3);
  2918. Assert.IsFalse(dit.SequencedEquals(dat));
  2919. Assert.IsFalse(dat.SequencedEquals(dit));
  2920. }
  2921. [Test]
  2922. public void HashVal()
  2923. {
  2924. Assert.AreEqual(CHC.sequencedhashcode(), dit.GetSequencedHashCode());
  2925. dit.Add(3);
  2926. Assert.AreEqual(CHC.sequencedhashcode(3), dit.GetSequencedHashCode());
  2927. dit.Add(7);
  2928. Assert.AreEqual(CHC.sequencedhashcode(3, 7), dit.GetSequencedHashCode());
  2929. Assert.AreEqual(CHC.sequencedhashcode(), dut.GetSequencedHashCode());
  2930. dut.Add(7);
  2931. Assert.AreEqual(CHC.sequencedhashcode(7), dut.GetSequencedHashCode());
  2932. dut.Add(3);
  2933. Assert.AreEqual(CHC.sequencedhashcode(7, 3), dut.GetSequencedHashCode());
  2934. }
  2935. [Test]
  2936. public void EqualHashButDifferent()
  2937. {
  2938. dit.Add(0); dit.Add(31);
  2939. dat.Add(1); dat.Add(0);
  2940. Assert.AreEqual(dit.GetSequencedHashCode(), dat.GetSequencedHashCode());
  2941. Assert.IsFalse(dit.SequencedEquals(dat));
  2942. }
  2943. [Test]
  2944. public void Normal()
  2945. {
  2946. dit.Add(3);
  2947. dit.Add(7);
  2948. dat.Add(3);
  2949. Assert.IsFalse(dit.SequencedEquals(dat));
  2950. Assert.IsFalse(dat.SequencedEquals(dit));
  2951. dat.Add(7);
  2952. Assert.IsTrue(dit.SequencedEquals(dat));
  2953. Assert.IsTrue(dat.SequencedEquals(dit));
  2954. }
  2955. [Test]
  2956. public void WrongOrder()
  2957. {
  2958. dit.Add(3);
  2959. dut.Add(3);
  2960. Assert.IsTrue(dit.SequencedEquals(dut));
  2961. Assert.IsTrue(dut.SequencedEquals(dit));
  2962. dit.Add(7);
  2963. ((ArrayList<int>)dut).InsertFirst(7);
  2964. Assert.IsFalse(dit.SequencedEquals(dut));
  2965. Assert.IsFalse(dut.SequencedEquals(dit));
  2966. }
  2967. [Test]
  2968. public void Reflexive()
  2969. {
  2970. Assert.IsTrue(dit.SequencedEquals(dit));
  2971. dit.Add(3);
  2972. Assert.IsTrue(dit.SequencedEquals(dit));
  2973. dit.Add(7);
  2974. Assert.IsTrue(dit.SequencedEquals(dit));
  2975. }
  2976. [TearDown]
  2977. public void Dispose()
  2978. {
  2979. dit = null;
  2980. dat = null;
  2981. dut = null;
  2982. }
  2983. }
  2984. [TestFixture]
  2985. public class IEditableCollection
  2986. {
  2987. private ICollection<int> dit, dat, dut;
  2988. [SetUp]
  2989. public void Init()
  2990. {
  2991. dit = new ArrayList<int>();
  2992. dat = new ArrayList<int>();
  2993. dut = new ArrayList<int>();
  2994. }
  2995. [Test]
  2996. public void EmptyEmpty()
  2997. {
  2998. Assert.IsTrue(dit.UnsequencedEquals(dat));
  2999. }
  3000. [Test]
  3001. public void EmptyNonEmpty()
  3002. {
  3003. dit.Add(3);
  3004. Assert.IsFalse(dit.UnsequencedEquals(dat));
  3005. Assert.IsFalse(dat.UnsequencedEquals(dit));
  3006. }
  3007. [Test]
  3008. public void HashVal()
  3009. {
  3010. Assert.AreEqual(CHC.unsequencedhashcode(), dit.GetUnsequencedHashCode());
  3011. dit.Add(3);
  3012. Assert.AreEqual(CHC.unsequencedhashcode(3), dit.GetUnsequencedHashCode());
  3013. dit.Add(7);
  3014. Assert.AreEqual(CHC.unsequencedhashcode(3, 7), dit.GetUnsequencedHashCode());
  3015. Assert.AreEqual(CHC.unsequencedhashcode(), dut.GetUnsequencedHashCode());
  3016. dut.Add(3);
  3017. Assert.AreEqual(CHC.unsequencedhashcode(3), dut.GetUnsequencedHashCode());
  3018. dut.Add(7);
  3019. Assert.AreEqual(CHC.unsequencedhashcode(7, 3), dut.GetUnsequencedHashCode());
  3020. }
  3021. [Test]
  3022. public void EqualHashButDifferent()
  3023. {
  3024. dit.Add(-1657792980); dit.Add(-1570288808);
  3025. dat.Add(1862883298); dat.Add(-272461342);
  3026. Assert.AreEqual(dit.GetUnsequencedHashCode(), dat.GetUnsequencedHashCode());
  3027. Assert.IsFalse(dit.UnsequencedEquals(dat));
  3028. }
  3029. [Test]
  3030. public void Normal()
  3031. {
  3032. dit.Add(3);
  3033. dit.Add(7);
  3034. dat.Add(3);
  3035. Assert.IsFalse(dit.UnsequencedEquals(dat));
  3036. Assert.IsFalse(dat.UnsequencedEquals(dit));
  3037. dat.Add(7);
  3038. Assert.IsTrue(dit.UnsequencedEquals(dat));
  3039. Assert.IsTrue(dat.UnsequencedEquals(dit));
  3040. }
  3041. [Test]
  3042. public void WrongOrder()
  3043. {
  3044. dit.Add(3);
  3045. dut.Add(3);
  3046. Assert.IsTrue(dit.UnsequencedEquals(dut));
  3047. Assert.IsTrue(dut.UnsequencedEquals(dit));
  3048. dit.Add(7);
  3049. dut.Add(7);
  3050. Assert.IsTrue(dit.UnsequencedEquals(dut));
  3051. Assert.IsTrue(dut.UnsequencedEquals(dit));
  3052. }
  3053. [Test]
  3054. public void Reflexive()
  3055. {
  3056. Assert.IsTrue(dit.UnsequencedEquals(dit));
  3057. dit.Add(3);
  3058. Assert.IsTrue(dit.UnsequencedEquals(dit));
  3059. dit.Add(7);
  3060. Assert.IsTrue(dit.UnsequencedEquals(dit));
  3061. }
  3062. [TearDown]
  3063. public void Dispose()
  3064. {
  3065. dit = null;
  3066. dat = null;
  3067. dut = null;
  3068. }
  3069. }
  3070. [TestFixture]
  3071. public class MultiLevelUnorderedOfUnOrdered
  3072. {
  3073. private ICollection<int> dit, dat, dut;
  3074. private ICollection<ICollection<int>> Dit, Dat, Dut;
  3075. [SetUp]
  3076. public void Init()
  3077. {
  3078. dit = new ArrayList<int>();
  3079. dat = new ArrayList<int>();
  3080. dut = new ArrayList<int>();
  3081. dit.Add(2); dit.Add(1);
  3082. dat.Add(1); dat.Add(2);
  3083. dut.Add(3);
  3084. Dit = new ArrayList<ICollection<int>>();
  3085. Dat = new ArrayList<ICollection<int>>();
  3086. Dut = new ArrayList<ICollection<int>>();
  3087. }
  3088. [Test]
  3089. public void Check()
  3090. {
  3091. Assert.IsTrue(dit.UnsequencedEquals(dat));
  3092. Assert.IsFalse(dit.UnsequencedEquals(dut));
  3093. }
  3094. [Test]
  3095. public void Multi()
  3096. {
  3097. Dit.Add(dit); Dit.Add(dut); Dit.Add(dit);
  3098. Dat.Add(dut); Dat.Add(dit); Dat.Add(dat);
  3099. Assert.IsTrue(Dit.UnsequencedEquals(Dat));
  3100. Assert.IsFalse(Dit.UnsequencedEquals(Dut));
  3101. }
  3102. [TearDown]
  3103. public void Dispose()
  3104. {
  3105. dit = dat = dut = null;
  3106. Dit = Dat = Dut = null;
  3107. }
  3108. }
  3109. [TestFixture]
  3110. public class MultiLevelOrderedOfUnOrdered
  3111. {
  3112. private ICollection<int> dit, dat, dut;
  3113. private ISequenced<ICollection<int>> Dit, Dat, Dut;
  3114. [SetUp]
  3115. public void Init()
  3116. {
  3117. dit = new ArrayList<int>();
  3118. dat = new ArrayList<int>();
  3119. dut = new ArrayList<int>();
  3120. dit.Add(2); dit.Add(1);
  3121. dat.Add(1); dat.Add(2);
  3122. dut.Add(3);
  3123. Dit = new ArrayList<ICollection<int>>();
  3124. Dat = new ArrayList<ICollection<int>>();
  3125. Dut = new ArrayList<ICollection<int>>();
  3126. }
  3127. [Test]
  3128. public void Check()
  3129. {
  3130. Assert.IsTrue(dit.UnsequencedEquals(dat));
  3131. Assert.IsFalse(dit.UnsequencedEquals(dut));
  3132. }
  3133. [Test]
  3134. public void Multi()
  3135. {
  3136. Dit.Add(dit); Dit.Add(dut); Dit.Add(dit);
  3137. Dat.Add(dut); Dat.Add(dit); Dat.Add(dat);
  3138. Dut.Add(dit); Dut.Add(dut); Dut.Add(dat);
  3139. Assert.IsFalse(Dit.SequencedEquals(Dat));
  3140. Assert.IsTrue(Dit.SequencedEquals(Dut));
  3141. }
  3142. [TearDown]
  3143. public void Dispose()
  3144. {
  3145. dit = dat = dut = null;
  3146. Dit = Dat = Dut = null;
  3147. }
  3148. }
  3149. [TestFixture]
  3150. public class MultiLevelUnOrderedOfOrdered
  3151. {
  3152. private ISequenced<int> dit, dat, dut, dot;
  3153. private ICollection<ISequenced<int>> Dit, Dat, Dut, Dot;
  3154. [SetUp]
  3155. public void Init()
  3156. {
  3157. dit = new ArrayList<int>();
  3158. dat = new ArrayList<int>();
  3159. dut = new ArrayList<int>();
  3160. dot = new ArrayList<int>();
  3161. dit.Add(2); dit.Add(1);
  3162. dat.Add(1); dat.Add(2);
  3163. dut.Add(3);
  3164. dot.Add(2); dot.Add(1);
  3165. Dit = new ArrayList<ISequenced<int>>();
  3166. Dat = new ArrayList<ISequenced<int>>();
  3167. Dut = new ArrayList<ISequenced<int>>();
  3168. Dot = new ArrayList<ISequenced<int>>();
  3169. }
  3170. [Test]
  3171. public void Check()
  3172. {
  3173. Assert.IsFalse(dit.SequencedEquals(dat));
  3174. Assert.IsTrue(dit.SequencedEquals(dot));
  3175. Assert.IsFalse(dit.SequencedEquals(dut));
  3176. }
  3177. [Test]
  3178. public void Multi()
  3179. {
  3180. Dit.Add(dit); Dit.Add(dut); Dit.Add(dit);
  3181. Dat.Add(dut); Dat.Add(dit); Dat.Add(dat);
  3182. Dut.Add(dot); Dut.Add(dut); Dut.Add(dit);
  3183. Dot.Add(dit); Dot.Add(dit); Dot.Add(dut);
  3184. Assert.IsTrue(Dit.UnsequencedEquals(Dut));
  3185. Assert.IsFalse(Dit.UnsequencedEquals(Dat));
  3186. Assert.IsTrue(Dit.UnsequencedEquals(Dot));
  3187. }
  3188. [TearDown]
  3189. public void Dispose()
  3190. {
  3191. dit = dat = dut = dot = null;
  3192. Dit = Dat = Dut = Dot = null;
  3193. }
  3194. }
  3195. [TestFixture]
  3196. public class MultiLevelOrderedOfOrdered
  3197. {
  3198. private ISequenced<int> dit, dat, dut, dot;
  3199. private ISequenced<ISequenced<int>> Dit, Dat, Dut, Dot;
  3200. [SetUp]
  3201. public void Init()
  3202. {
  3203. dit = new ArrayList<int>();
  3204. dat = new ArrayList<int>();
  3205. dut = new ArrayList<int>();
  3206. dot = new ArrayList<int>();
  3207. dit.Add(2); dit.Add(1);
  3208. dat.Add(1); dat.Add(2);
  3209. dut.Add(3);
  3210. dot.Add(2); dot.Add(1);
  3211. Dit = new ArrayList<ISequenced<int>>();
  3212. Dat = new ArrayList<ISequenced<int>>();
  3213. Dut = new ArrayList<ISequenced<int>>();
  3214. Dot = new ArrayList<ISequenced<int>>();
  3215. }
  3216. [Test]
  3217. public void Check()
  3218. {
  3219. Assert.IsFalse(dit.SequencedEquals(dat));
  3220. Assert.IsTrue(dit.SequencedEquals(dot));
  3221. Assert.IsFalse(dit.SequencedEquals(dut));
  3222. }
  3223. [Test]
  3224. public void Multi()
  3225. {
  3226. Dit.Add(dit); Dit.Add(dut); Dit.Add(dit);
  3227. Dat.Add(dut); Dat.Add(dit); Dat.Add(dat);
  3228. Dut.Add(dot); Dut.Add(dut); Dut.Add(dit);
  3229. Dot.Add(dit); Dot.Add(dit); Dot.Add(dut);
  3230. Assert.IsTrue(Dit.SequencedEquals(Dut));
  3231. Assert.IsFalse(Dit.SequencedEquals(Dat));
  3232. Assert.IsFalse(Dit.SequencedEquals(Dot));
  3233. }
  3234. [TearDown]
  3235. public void Dispose()
  3236. {
  3237. dit = dat = dut = dot = null;
  3238. Dit = Dat = Dut = Dot = null;
  3239. }
  3240. }
  3241. }
  3242. }