PageRenderTime 46ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/Mono.C5/Test/WrappersTest.cs

https://bitbucket.org/danipen/mono
C# | 858 lines | 765 code | 61 blank | 32 comment | 6 complexity | 43248e794c404f68478f65c17e57e8bc 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.wrappers
  24. {
  25. namespace events
  26. {
  27. [TestFixture]
  28. public class IList_
  29. {
  30. private ArrayList<int> list;
  31. ICollectionValue<int> guarded;
  32. CollectionEventList<int> seen;
  33. [SetUp]
  34. public void Init()
  35. {
  36. list = new ArrayList<int>(TenEqualityComparer.Default);
  37. guarded = new GuardedList<int>(list);
  38. seen = new CollectionEventList<int>(IntEqualityComparer.Default);
  39. }
  40. private void listen() { seen.Listen(guarded, EventTypeEnum.All); }
  41. [Test]
  42. public void Listenable()
  43. {
  44. Assert.AreEqual(EventTypeEnum.All, guarded.ListenableEvents);
  45. Assert.AreEqual(EventTypeEnum.None, guarded.ActiveEvents);
  46. listen();
  47. Assert.AreEqual(EventTypeEnum.All, guarded.ActiveEvents);
  48. }
  49. [Test]
  50. public void SetThis()
  51. {
  52. list.Add(4); list.Add(56); list.Add(8);
  53. listen();
  54. list[1] = 45;
  55. seen.Check(new CollectionEvent<int>[] {
  56. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), guarded),
  57. new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(56,1), guarded),
  58. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), guarded),
  59. new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(45,1), guarded),
  60. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
  61. });
  62. }
  63. [Test]
  64. public void Insert()
  65. {
  66. list.Add(4); list.Add(56); list.Add(8);
  67. listen();
  68. list.Insert(1, 45);
  69. seen.Check(new CollectionEvent<int>[] {
  70. new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(45,1), guarded),
  71. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), guarded),
  72. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
  73. });
  74. }
  75. [Test]
  76. public void InsertAll()
  77. {
  78. list.Add(4); list.Add(56); list.Add(8);
  79. listen();
  80. list.InsertAll<int>(1, new int[] { 666, 777, 888 });
  81. //seen.Print(Console.Error);
  82. seen.Check(new CollectionEvent<int>[] {
  83. new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(666,1), guarded),
  84. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(666, 1), guarded),
  85. new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(777,2), guarded),
  86. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(777, 1), guarded),
  87. new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(888,3), guarded),
  88. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(888, 1), guarded),
  89. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
  90. });
  91. list.InsertAll<int>(1, new int[] {});
  92. seen.Check(new CollectionEvent<int>[] {});
  93. }
  94. [Test]
  95. public void InsertFirstLast()
  96. {
  97. list.Add(4); list.Add(56); list.Add(8);
  98. listen();
  99. list.InsertFirst(45);
  100. seen.Check(new CollectionEvent<int>[] {
  101. new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(45,0), guarded),
  102. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), guarded),
  103. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
  104. });
  105. list.InsertLast(88);
  106. seen.Check(new CollectionEvent<int>[] {
  107. new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(88,4), guarded),
  108. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(88, 1), guarded),
  109. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
  110. });
  111. }
  112. [Test]
  113. public void Remove()
  114. {
  115. list.Add(4); list.Add(56); list.Add(8);
  116. listen();
  117. list.Remove();
  118. seen.Check(new CollectionEvent<int>[] {
  119. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(8, 1), guarded),
  120. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
  121. }
  122. [Test]
  123. public void RemoveFirst()
  124. {
  125. list.Add(4); list.Add(56); list.Add(8);
  126. listen();
  127. list.RemoveFirst();
  128. seen.Check(new CollectionEvent<int>[] {
  129. new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(4,0), guarded),
  130. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(4, 1), guarded),
  131. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
  132. }
  133. [Test]
  134. public void RemoveLast()
  135. {
  136. list.Add(4); list.Add(56); list.Add(8);
  137. listen();
  138. list.RemoveLast();
  139. seen.Check(new CollectionEvent<int>[] {
  140. new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(8,2), guarded),
  141. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(8, 1), guarded),
  142. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
  143. }
  144. [Test]
  145. public void Reverse()
  146. {
  147. list.Add(4); list.Add(56); list.Add(8);
  148. listen();
  149. list.Reverse();
  150. seen.Check(new CollectionEvent<int>[] {
  151. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
  152. });
  153. list.View(1, 0).Reverse();
  154. seen.Check(new CollectionEvent<int>[] {});
  155. }
  156. [Test]
  157. public void Sort()
  158. {
  159. list.Add(4); list.Add(56); list.Add(8);
  160. listen();
  161. list.Sort();
  162. seen.Check(new CollectionEvent<int>[] {
  163. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
  164. });
  165. list.View(1, 0).Sort();
  166. seen.Check(new CollectionEvent<int>[] {});
  167. }
  168. [Test]
  169. public void Shuffle()
  170. {
  171. list.Add(4); list.Add(56); list.Add(8);
  172. listen();
  173. list.Shuffle();
  174. seen.Check(new CollectionEvent<int>[] {
  175. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
  176. });
  177. list.View(1, 0).Shuffle();
  178. seen.Check(new CollectionEvent<int>[] {});
  179. }
  180. [Test]
  181. public void RemoveAt()
  182. {
  183. list.Add(4); list.Add(56); list.Add(8);
  184. listen();
  185. list.RemoveAt(1);
  186. seen.Check(new CollectionEvent<int>[] {
  187. new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(56,1), guarded),
  188. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), guarded),
  189. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
  190. }
  191. [Test]
  192. public void RemoveInterval()
  193. {
  194. list.Add(4); list.Add(56); list.Add(8);
  195. listen();
  196. list.RemoveInterval(1, 2);
  197. seen.Check(new CollectionEvent<int>[] {
  198. new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(false,2,1), guarded),
  199. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
  200. });
  201. list.RemoveInterval(1, 0);
  202. seen.Check(new CollectionEvent<int>[] {});
  203. }
  204. [Test]
  205. public void Update()
  206. {
  207. list.Add(4); list.Add(56); list.Add(8);
  208. listen();
  209. list.Update(53);
  210. seen.Check(new CollectionEvent<int>[] {
  211. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), guarded),
  212. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(53, 1), guarded),
  213. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
  214. });
  215. list.Update(67);
  216. seen.Check(new CollectionEvent<int>[] {});
  217. }
  218. [Test]
  219. public void FindOrAdd()
  220. {
  221. list.Add(4); list.Add(56); list.Add(8);
  222. listen();
  223. int val = 53;
  224. list.FindOrAdd(ref val);
  225. seen.Check(new CollectionEvent<int>[] {});
  226. val = 67;
  227. list.FindOrAdd(ref val);
  228. seen.Check(new CollectionEvent<int>[] {
  229. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(67, 1), guarded),
  230. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
  231. });
  232. }
  233. [Test]
  234. public void UpdateOrAdd()
  235. {
  236. list.Add(4); list.Add(56); list.Add(8);
  237. listen();
  238. int val = 53;
  239. list.UpdateOrAdd(val);
  240. seen.Check(new CollectionEvent<int>[] {
  241. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), guarded),
  242. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(53, 1), guarded),
  243. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
  244. });
  245. val = 67;
  246. list.UpdateOrAdd(val);
  247. seen.Check(new CollectionEvent<int>[] {
  248. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(67, 1), guarded),
  249. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
  250. });
  251. list.UpdateOrAdd(51, out val);
  252. seen.Check(new CollectionEvent<int>[] {
  253. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(53, 1), guarded),
  254. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(51, 1), guarded),
  255. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
  256. });
  257. val = 67;
  258. list.UpdateOrAdd(81, out val);
  259. seen.Check(new CollectionEvent<int>[] {
  260. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(81, 1), guarded),
  261. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
  262. });
  263. }
  264. [Test]
  265. public void RemoveItem()
  266. {
  267. list.Add(4); list.Add(56); list.Add(18);
  268. listen();
  269. list.Remove(53);
  270. seen.Check(new CollectionEvent<int>[] {
  271. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), guarded),
  272. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
  273. list.Remove(11);
  274. seen.Check(new CollectionEvent<int>[] {
  275. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(18, 1), guarded),
  276. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
  277. }
  278. [Test]
  279. public void RemoveAll()
  280. {
  281. for (int i = 0; i < 10; i++)
  282. {
  283. list.Add(10 * i + 5);
  284. }
  285. listen();
  286. list.RemoveAll<int>(new int[] { 32, 187, 45 });
  287. //TODO: the order depends on internals of the HashSet
  288. seen.Check(new CollectionEvent<int>[] {
  289. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(35, 1), guarded),
  290. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(45, 1), guarded),
  291. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
  292. list.RemoveAll<int>(new int[] { 200, 300 });
  293. seen.Check(new CollectionEvent<int>[] {});
  294. }
  295. [Test]
  296. public void Clear()
  297. {
  298. list.Add(4); list.Add(56); list.Add(8);
  299. listen();
  300. list.View(1, 1).Clear();
  301. seen.Check(new CollectionEvent<int>[] {
  302. new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(false,1,1), guarded),
  303. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
  304. });
  305. list.Clear();
  306. seen.Check(new CollectionEvent<int>[] {
  307. new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(true,2,0), guarded),
  308. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
  309. });
  310. list.Clear();
  311. seen.Check(new CollectionEvent<int>[] {});
  312. }
  313. [Test]
  314. public void ListDispose()
  315. {
  316. list.Add(4); list.Add(56); list.Add(8);
  317. listen();
  318. list.View(1, 1).Dispose();
  319. seen.Check(new CollectionEvent<int>[] {});
  320. list.Dispose();
  321. seen.Check(new CollectionEvent<int>[] {
  322. new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(true,3,0), guarded),
  323. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
  324. });
  325. list.Dispose();
  326. seen.Check(new CollectionEvent<int>[] {});
  327. }
  328. [Test]
  329. public void RetainAll()
  330. {
  331. for (int i = 0; i < 10; i++)
  332. {
  333. list.Add(10 * i + 5);
  334. }
  335. listen();
  336. list.RetainAll<int>(new int[] { 32, 187, 45, 62, 82, 95, 2 });
  337. seen.Check(new CollectionEvent<int>[] {
  338. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(15, 1), guarded),
  339. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(25, 1), guarded),
  340. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(55, 1), guarded),
  341. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(75, 1), guarded),
  342. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
  343. list.RetainAll<int>(new int[] { 32, 187, 45, 62, 82, 95, 2 });
  344. seen.Check(new CollectionEvent<int>[] {});
  345. }
  346. [Test]
  347. public void RemoveAllCopies()
  348. {
  349. for (int i = 0; i < 10; i++)
  350. {
  351. list.Add(3 * i + 5);
  352. }
  353. listen();
  354. list.RemoveAllCopies(14);
  355. seen.Check(new CollectionEvent<int>[] {
  356. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(11, 1), guarded),
  357. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(14, 1), guarded),
  358. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(17, 1), guarded),
  359. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
  360. list.RemoveAllCopies(14);
  361. seen.Check(new CollectionEvent<int>[] {});
  362. }
  363. [Test]
  364. public void Add()
  365. {
  366. listen();
  367. seen.Check(new CollectionEvent<int>[0]);
  368. list.Add(23);
  369. seen.Check(new CollectionEvent<int>[] {
  370. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(23, 1), guarded),
  371. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
  372. }
  373. [Test]
  374. public void AddAll()
  375. {
  376. for (int i = 0; i < 10; i++)
  377. {
  378. list.Add(10 * i + 5);
  379. }
  380. listen();
  381. list.AddAll<int>(new int[] { 45, 56, 67 });
  382. seen.Check(new CollectionEvent<int>[] {
  383. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), guarded),
  384. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(56, 1), guarded),
  385. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(67, 1), guarded),
  386. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
  387. list.AddAll<int>(new int[] { });
  388. seen.Check(new CollectionEvent<int>[] {});
  389. }
  390. [TearDown]
  391. public void Dispose() { list = null; seen = null; }
  392. [Test]
  393. [ExpectedException(typeof(UnlistenableEventException))]
  394. public void ViewChanged()
  395. {
  396. IList<int> w = list.View(0, 0);
  397. w.CollectionChanged += new CollectionChangedHandler<int>(w_CollectionChanged);
  398. }
  399. [Test]
  400. [ExpectedException(typeof(UnlistenableEventException))]
  401. public void ViewCleared()
  402. {
  403. IList<int> w = list.View(0, 0);
  404. w.CollectionCleared += new CollectionClearedHandler<int>(w_CollectionCleared);
  405. }
  406. [Test]
  407. [ExpectedException(typeof(UnlistenableEventException))]
  408. public void ViewAdded()
  409. {
  410. IList<int> w = list.View(0, 0);
  411. w.ItemsAdded += new ItemsAddedHandler<int>(w_ItemAdded);
  412. }
  413. [Test]
  414. [ExpectedException(typeof(UnlistenableEventException))]
  415. public void ViewInserted()
  416. {
  417. IList<int> w = list.View(0, 0);
  418. w.ItemInserted += new ItemInsertedHandler<int>(w_ItemInserted);
  419. }
  420. [Test]
  421. [ExpectedException(typeof(UnlistenableEventException))]
  422. public void ViewRemoved()
  423. {
  424. IList<int> w = list.View(0, 0);
  425. w.ItemsRemoved += new ItemsRemovedHandler<int>(w_ItemRemoved);
  426. }
  427. [Test]
  428. [ExpectedException(typeof(UnlistenableEventException))]
  429. public void ViewRemovedAt()
  430. {
  431. IList<int> w = list.View(0, 0);
  432. w.ItemRemovedAt += new ItemRemovedAtHandler<int>(w_ItemRemovedAt);
  433. }
  434. void w_CollectionChanged(object sender)
  435. {
  436. throw new NotImplementedException();
  437. }
  438. void w_CollectionCleared(object sender, ClearedEventArgs eventArgs)
  439. {
  440. throw new NotImplementedException();
  441. }
  442. void w_ItemAdded(object sender, ItemCountEventArgs<int> eventArgs)
  443. {
  444. throw new NotImplementedException();
  445. }
  446. void w_ItemInserted(object sender, ItemAtEventArgs<int> eventArgs)
  447. {
  448. throw new NotImplementedException();
  449. }
  450. void w_ItemRemoved(object sender, ItemCountEventArgs<int> eventArgs)
  451. {
  452. throw new NotImplementedException();
  453. }
  454. void w_ItemRemovedAt(object sender, ItemAtEventArgs<int> eventArgs)
  455. {
  456. throw new NotImplementedException();
  457. }
  458. }
  459. [TestFixture]
  460. public class StackQueue
  461. {
  462. private ArrayList<int> list;
  463. ICollectionValue<int> guarded;
  464. CollectionEventList<int> seen;
  465. [SetUp]
  466. public void Init()
  467. {
  468. list = new ArrayList<int>(TenEqualityComparer.Default);
  469. guarded = new GuardedList<int>(list);
  470. seen = new CollectionEventList<int>(IntEqualityComparer.Default);
  471. }
  472. private void listen() { seen.Listen(guarded, EventTypeEnum.All); }
  473. [Test]
  474. public void EnqueueDequeue()
  475. {
  476. listen();
  477. list.Enqueue(67);
  478. seen.Check(new CollectionEvent<int>[] {
  479. new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(67,0), guarded),
  480. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(67, 1), guarded),
  481. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
  482. list.Enqueue(2);
  483. seen.Check(new CollectionEvent<int>[] {
  484. new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(2,1), guarded),
  485. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(2, 1), guarded),
  486. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
  487. list.Dequeue();
  488. seen.Check(new CollectionEvent<int>[] {
  489. new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(67,0), guarded),
  490. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(67, 1), guarded),
  491. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
  492. list.Dequeue();
  493. seen.Check(new CollectionEvent<int>[] {
  494. new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(2,0), guarded),
  495. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(2, 1), guarded),
  496. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
  497. }
  498. [Test]
  499. public void PushPop()
  500. {
  501. listen();
  502. seen.Check(new CollectionEvent<int>[0]);
  503. list.Push(23);
  504. seen.Check(new CollectionEvent<int>[] {
  505. new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(23,0), guarded),
  506. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(23, 1), guarded),
  507. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
  508. list.Push(-12);
  509. seen.Check(new CollectionEvent<int>[] {
  510. new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(-12,1), guarded),
  511. new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(-12, 1), guarded),
  512. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
  513. list.Pop();
  514. seen.Check(new CollectionEvent<int>[] {
  515. new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(-12,1), guarded),
  516. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(-12, 1), guarded),
  517. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
  518. list.Pop();
  519. seen.Check(new CollectionEvent<int>[] {
  520. new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(23,0), guarded),
  521. new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(23, 1), guarded),
  522. new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
  523. }
  524. [TearDown]
  525. public void Dispose() { list = null; seen = null; }
  526. }
  527. }
  528. namespace wrappedarray
  529. {
  530. [TestFixture]
  531. public class Basic
  532. {
  533. [SetUp]
  534. public void Init()
  535. {
  536. }
  537. [TearDown]
  538. public void Dispose()
  539. {
  540. }
  541. [Test]
  542. public void NoExc()
  543. {
  544. WrappedArray<int> wrapped = new WrappedArray<int>(new int[] { 4, 6, 5 });
  545. Assert.AreEqual(6, wrapped[1]);
  546. Assert.IsTrue(IC.eq(wrapped[1, 2], 6, 5));
  547. //
  548. Fun<int, bool> is4 = delegate(int i) { return i == 4; };
  549. Assert.AreEqual(EventTypeEnum.None, wrapped.ActiveEvents);
  550. Assert.AreEqual(false, wrapped.All(is4));
  551. Assert.AreEqual(true, wrapped.AllowsDuplicates);
  552. wrapped.Apply(delegate(int i) { });
  553. Assert.AreEqual("{ 5, 6, 4 }", wrapped.Backwards().ToString());
  554. Assert.AreEqual(true, wrapped.Check());
  555. wrapped.Choose();
  556. Assert.AreEqual(true, wrapped.Contains(4));
  557. Assert.AreEqual(true, wrapped.ContainsAll(new ArrayList<int>()));
  558. Assert.AreEqual(1, wrapped.ContainsCount(4));
  559. Assert.AreEqual(Speed.Linear, wrapped.ContainsSpeed);
  560. int[] extarray = new int[5];
  561. wrapped.CopyTo(extarray, 1);
  562. Assert.IsTrue(IC.eq(extarray, 0, 4, 6, 5, 0));
  563. Assert.AreEqual(3, wrapped.Count);
  564. Assert.AreEqual(Speed.Constant, wrapped.CountSpeed);
  565. Assert.AreEqual(EnumerationDirection.Forwards, wrapped.Direction);
  566. Assert.AreEqual(false, wrapped.DuplicatesByCounting);
  567. Assert.AreEqual(IntEqualityComparer.Default, wrapped.EqualityComparer);
  568. Assert.AreEqual(true, wrapped.Exists(is4));
  569. Assert.IsTrue(IC.eq(wrapped.Filter(is4), 4));
  570. int j = 5;
  571. Assert.AreEqual(true, wrapped.Find(ref j));
  572. Assert.AreEqual(true, wrapped.Find(is4, out j));
  573. Assert.AreEqual("[ 0:4 ]", wrapped.FindAll(is4).ToString());
  574. Assert.AreEqual(0, wrapped.FindIndex(is4));
  575. Assert.AreEqual(true, wrapped.FindLast(is4, out j));
  576. Assert.AreEqual(0, wrapped.FindLastIndex(is4));
  577. Assert.AreEqual(4, wrapped.First);
  578. wrapped.GetEnumerator();
  579. Assert.AreEqual(CHC.sequencedhashcode(4, 6, 5), wrapped.GetSequencedHashCode());
  580. Assert.AreEqual(CHC.unsequencedhashcode(4, 6, 5), wrapped.GetUnsequencedHashCode());
  581. Assert.AreEqual(Speed.Constant, wrapped.IndexingSpeed);
  582. Assert.AreEqual(2, wrapped.IndexOf(5));
  583. Assert.AreEqual(false, wrapped.IsEmpty);
  584. Assert.AreEqual(true, wrapped.IsReadOnly);
  585. Assert.AreEqual(false, wrapped.IsSorted());
  586. Assert.AreEqual(true, wrapped.IsValid);
  587. Assert.AreEqual(5, wrapped.Last);
  588. Assert.AreEqual(2, wrapped.LastIndexOf(5));
  589. Assert.AreEqual(EventTypeEnum.None, wrapped.ListenableEvents);
  590. Fun<int, string> i2s = delegate(int i) { return string.Format("T{0}", i); };
  591. Assert.AreEqual("[ 0:T4, 1:T6, 2:T5 ]", wrapped.Map<string>(i2s).ToString());
  592. Assert.AreEqual(0, wrapped.Offset);
  593. wrapped.Reverse();
  594. Assert.AreEqual("[ 0:5, 1:6, 2:4 ]", wrapped.ToString());
  595. IList<int> other = new ArrayList<int>(); other.AddAll<int>(new int[] { 4, 5, 6 });
  596. Assert.IsFalse(wrapped.SequencedEquals(other));
  597. j = 30;
  598. Assert.AreEqual(true, wrapped.Show(new System.Text.StringBuilder(), ref j, null));
  599. wrapped.Sort();
  600. Assert.AreEqual("[ 0:4, 1:5, 2:6 ]", wrapped.ToString());
  601. Assert.IsTrue(IC.eq(wrapped.ToArray(), 4, 5, 6));
  602. Assert.AreEqual("[ ... ]", wrapped.ToString("L4", null));
  603. Assert.AreEqual(null, wrapped.Underlying);
  604. Assert.IsTrue(IC.seteq(wrapped.UniqueItems(), 4, 5, 6));
  605. Assert.IsTrue(wrapped.UnsequencedEquals(other));
  606. wrapped.Shuffle();
  607. Assert.IsTrue(IC.seteq(wrapped.UniqueItems(), 4, 5, 6));
  608. }
  609. [Test]
  610. public void WithExc()
  611. {
  612. WrappedArray<int> wrapped = new WrappedArray<int>(new int[] { 3, 4, 6, 5, 7 });
  613. //
  614. try { wrapped.Add(1); Assert.Fail("No throw"); }
  615. catch (FixedSizeCollectionException) { }
  616. try { wrapped.AddAll<int>(null); Assert.Fail("No throw"); }
  617. catch (FixedSizeCollectionException) { }
  618. try { wrapped.Clear(); Assert.Fail("No throw"); }
  619. catch (FixedSizeCollectionException) { }
  620. try { wrapped.Dispose(); Assert.Fail("No throw"); }
  621. catch (FixedSizeCollectionException) { }
  622. int j = 1;
  623. try { wrapped.FindOrAdd(ref j); Assert.Fail("No throw"); }
  624. catch (FixedSizeCollectionException) { }
  625. try { wrapped.Insert(1, 1); Assert.Fail("No throw"); }
  626. catch (FixedSizeCollectionException) { }
  627. try { wrapped.Insert(wrapped.View(0, 0), 1); Assert.Fail("No throw"); }
  628. catch (FixedSizeCollectionException) { }
  629. try { wrapped.InsertAll<int>(1, null); Assert.Fail("No throw"); }
  630. catch (FixedSizeCollectionException) { }
  631. try { wrapped.InsertFirst(1); Assert.Fail("No throw"); }
  632. catch (FixedSizeCollectionException) { }
  633. try { wrapped.InsertLast(1); Assert.Fail("No throw"); }
  634. catch (FixedSizeCollectionException) { }
  635. try { wrapped.Remove(); Assert.Fail("No throw"); }
  636. catch (FixedSizeCollectionException) { }
  637. try { wrapped.Remove(1); Assert.Fail("No throw"); }
  638. catch (FixedSizeCollectionException) { }
  639. try { wrapped.RemoveAll<int>(null); Assert.Fail("No throw"); }
  640. catch (FixedSizeCollectionException) { }
  641. try { wrapped.RemoveAllCopies(1); Assert.Fail("No throw"); }
  642. catch (FixedSizeCollectionException) { }
  643. try { wrapped.RemoveAt(1); Assert.Fail("No throw"); }
  644. catch (FixedSizeCollectionException) { }
  645. try { wrapped.RemoveFirst(); Assert.Fail("No throw"); }
  646. catch (FixedSizeCollectionException) { }
  647. try { wrapped.RemoveInterval(0, 0); Assert.Fail("No throw"); }
  648. catch (FixedSizeCollectionException) { }
  649. try { wrapped.RemoveLast(); Assert.Fail("No throw"); }
  650. catch (FixedSizeCollectionException) { }
  651. try { wrapped.RetainAll<int>(null); Assert.Fail("No throw"); }
  652. catch (FixedSizeCollectionException) { }
  653. try { wrapped.Update(1, out j); Assert.Fail("No throw"); }
  654. catch (FixedSizeCollectionException) { }
  655. try { wrapped.UpdateOrAdd(1); Assert.Fail("No throw"); }
  656. catch (FixedSizeCollectionException) { }
  657. }
  658. [Test]
  659. [Category("NotWorking")]
  660. public void View()
  661. {
  662. int[] inner = new int[] { 3, 4, 6, 5, 7 };
  663. WrappedArray<int> outerwrapped = new WrappedArray<int>(inner);
  664. WrappedArray<int> wrapped = (WrappedArray<int>)outerwrapped.View(1, 3);
  665. //
  666. Assert.AreEqual(6, wrapped[1]);
  667. Assert.IsTrue(IC.eq(wrapped[1, 2], 6, 5));
  668. //
  669. Fun<int, bool> is4 = delegate(int i) { return i == 4; };
  670. Assert.AreEqual(EventTypeEnum.None, wrapped.ActiveEvents);
  671. Assert.AreEqual(false, wrapped.All(is4));
  672. Assert.AreEqual(true, wrapped.AllowsDuplicates);
  673. wrapped.Apply(delegate(int i) { });
  674. Assert.AreEqual("{ 5, 6, 4 }", wrapped.Backwards().ToString());
  675. Assert.AreEqual(true, wrapped.Check());
  676. wrapped.Choose();
  677. Assert.AreEqual(true, wrapped.Contains(4));
  678. Assert.AreEqual(true, wrapped.ContainsAll(new ArrayList<int>()));
  679. Assert.AreEqual(1, wrapped.ContainsCount(4));
  680. Assert.AreEqual(Speed.Linear, wrapped.ContainsSpeed);
  681. int[] extarray = new int[5];
  682. wrapped.CopyTo(extarray, 1);
  683. Assert.IsTrue(IC.eq(extarray, 0, 4, 6, 5, 0));
  684. Assert.AreEqual(3, wrapped.Count);
  685. Assert.AreEqual(Speed.Constant, wrapped.CountSpeed);
  686. Assert.AreEqual(EnumerationDirection.Forwards, wrapped.Direction);
  687. Assert.AreEqual(false, wrapped.DuplicatesByCounting);
  688. Assert.AreEqual(IntEqualityComparer.Default, wrapped.EqualityComparer);
  689. Assert.AreEqual(true, wrapped.Exists(is4));
  690. Assert.IsTrue(IC.eq(wrapped.Filter(is4), 4));
  691. int j = 5;
  692. Assert.AreEqual(true, wrapped.Find(ref j));
  693. Assert.AreEqual(true, wrapped.Find(is4, out j));
  694. Assert.AreEqual("[ 0:4 ]", wrapped.FindAll(is4).ToString());
  695. Assert.AreEqual(0, wrapped.FindIndex(is4));
  696. Assert.AreEqual(true, wrapped.FindLast(is4, out j));
  697. Assert.AreEqual(0, wrapped.FindLastIndex(is4));
  698. Assert.AreEqual(4, wrapped.First);
  699. wrapped.GetEnumerator();
  700. Assert.AreEqual(CHC.sequencedhashcode(4, 6, 5), wrapped.GetSequencedHashCode());
  701. Assert.AreEqual(CHC.unsequencedhashcode(4, 6, 5), wrapped.GetUnsequencedHashCode());
  702. Assert.AreEqual(Speed.Constant, wrapped.IndexingSpeed);
  703. Assert.AreEqual(2, wrapped.IndexOf(5));
  704. Assert.AreEqual(false, wrapped.IsEmpty);
  705. Assert.AreEqual(true, wrapped.IsReadOnly);
  706. Assert.AreEqual(false, wrapped.IsSorted());
  707. Assert.AreEqual(true, wrapped.IsValid);
  708. Assert.AreEqual(5, wrapped.Last);
  709. Assert.AreEqual(2, wrapped.LastIndexOf(5));
  710. Assert.AreEqual(EventTypeEnum.None, wrapped.ListenableEvents);
  711. Fun<int, string> i2s = delegate(int i) { return string.Format("T{0}", i); };
  712. Assert.AreEqual("[ 0:T4, 1:T6, 2:T5 ]", wrapped.Map<string>(i2s).ToString());
  713. Assert.AreEqual(1, wrapped.Offset);
  714. wrapped.Reverse();
  715. Assert.AreEqual("[ 0:5, 1:6, 2:4 ]", wrapped.ToString());
  716. IList<int> other = new ArrayList<int>(); other.AddAll<int>(new int[] { 4, 5, 6 });
  717. Assert.IsFalse(wrapped.SequencedEquals(other));
  718. j = 30;
  719. Assert.AreEqual(true, wrapped.Show(new System.Text.StringBuilder(), ref j, null));
  720. wrapped.Sort();
  721. Assert.AreEqual("[ 0:4, 1:5, 2:6 ]", wrapped.ToString());
  722. Assert.IsTrue(IC.eq(wrapped.ToArray(), 4, 5, 6));
  723. Assert.AreEqual("[ ... ]", wrapped.ToString("L4", null));
  724. Assert.AreEqual(outerwrapped, wrapped.Underlying);
  725. Assert.IsTrue(IC.seteq(wrapped.UniqueItems(), 4, 5, 6));
  726. Assert.IsTrue(wrapped.UnsequencedEquals(other));
  727. //
  728. Assert.IsTrue(wrapped.TrySlide(1));
  729. Assert.IsTrue(IC.eq(wrapped, 5, 6, 7));
  730. Assert.IsTrue(wrapped.TrySlide(-1, 2));
  731. Assert.IsTrue(IC.eq(wrapped, 4, 5));
  732. Assert.IsFalse(wrapped.TrySlide(-2));
  733. Assert.IsTrue(IC.eq(wrapped.Span(outerwrapped.ViewOf(7)), 4, 5, 6, 7));
  734. //
  735. wrapped.Shuffle();
  736. Assert.IsTrue(IC.seteq(wrapped.UniqueItems(), 4, 5));
  737. Assert.IsTrue(wrapped.IsValid);
  738. wrapped.Dispose();
  739. Assert.IsFalse(wrapped.IsValid);
  740. }
  741. [Test]
  742. public void ViewWithExc()
  743. {
  744. int[] inner = new int[] { 3, 4, 6, 5, 7 };
  745. WrappedArray<int> outerwrapped = new WrappedArray<int>(inner);
  746. WrappedArray<int> wrapped = (WrappedArray<int>)outerwrapped.View(1, 3);
  747. //
  748. try { wrapped.Add(1); Assert.Fail("No throw"); }
  749. catch (FixedSizeCollectionException) { }
  750. try { wrapped.AddAll<int>(null); Assert.Fail("No throw"); }
  751. catch (FixedSizeCollectionException) { }
  752. try { wrapped.Clear(); Assert.Fail("No throw"); }
  753. catch (FixedSizeCollectionException) { }
  754. //Should not throw
  755. //try { wrapped.Dispose(); Assert.Fail("No throw"); }
  756. //catch (FixedSizeCollectionException) { }
  757. int j = 1;
  758. try { wrapped.FindOrAdd(ref j); Assert.Fail("No throw"); }
  759. catch (FixedSizeCollectionException) { }
  760. try { wrapped.Insert(1, 1); Assert.Fail("No throw"); }
  761. catch (FixedSizeCollectionException) { }
  762. try { wrapped.Insert(wrapped.View(0, 0), 1); Assert.Fail("No throw"); }
  763. catch (FixedSizeCollectionException) { }
  764. try { wrapped.InsertAll<int>(1, null); Assert.Fail("No throw"); }
  765. catch (FixedSizeCollectionException) { }
  766. try { wrapped.InsertFirst(1); Assert.Fail("No throw"); }
  767. catch (FixedSizeCollectionException) { }
  768. try { wrapped.InsertLast(1); Assert.Fail("No throw"); }
  769. catch (FixedSizeCollectionException) { }
  770. try { wrapped.Remove(); Assert.Fail("No throw"); }
  771. catch (FixedSizeCollectionException) { }
  772. try { wrapped.Remove(1); Assert.Fail("No throw"); }
  773. catch (FixedSizeCollectionException) { }
  774. try { wrapped.RemoveAll<int>(null); Assert.Fail("No throw"); }
  775. catch (FixedSizeCollectionException) { }
  776. try { wrapped.RemoveAllCopies(1); Assert.Fail("No throw"); }
  777. catch (FixedSizeCollectionException) { }
  778. try { wrapped.RemoveAt(1); Assert.Fail("No throw"); }
  779. catch (FixedSizeCollectionException) { }
  780. try { wrapped.RemoveFirst(); Assert.Fail("No throw"); }
  781. catch (FixedSizeCollectionException) { }
  782. try { wrapped.RemoveInterval(0, 0); Assert.Fail("No throw"); }
  783. catch (FixedSizeCollectionException) { }
  784. try { wrapped.RemoveLast(); Assert.Fail("No throw"); }
  785. catch (FixedSizeCollectionException) { }
  786. try { wrapped.RetainAll<int>(null); Assert.Fail("No throw"); }
  787. catch (FixedSizeCollectionException) { }
  788. try { wrapped.Update(1, out j); Assert.Fail("No throw"); }
  789. catch (FixedSizeCollectionException) { }
  790. try { wrapped.UpdateOrAdd(1); Assert.Fail("No throw"); }
  791. catch (FixedSizeCollectionException) { }
  792. }
  793. }
  794. }
  795. }