PageRenderTime 69ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/danipen/mono
C# | 453 lines | 391 code | 34 blank | 28 comment | 0 complexity | f1af8331482da443ccc9684f6e66008c 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.interfaces
  24. {
  25. [TestFixture]
  26. public class ICollectionsTests
  27. {
  28. public void TryC5Coll(ICollection<double> coll)
  29. {
  30. Assert.AreEqual(0, coll.Count);
  31. double[] arr = { };
  32. coll.CopyTo(arr, 0);
  33. Assert.IsFalse(coll.IsReadOnly);
  34. coll.Add(2.3);
  35. coll.Add(3.2);
  36. Assert.AreEqual(2, coll.Count);
  37. Assert.IsTrue(coll.Contains(2.3));
  38. Assert.IsFalse(coll.Contains(3.1));
  39. Assert.IsFalse(coll.Remove(3.1));
  40. Assert.IsTrue(coll.Remove(3.2));
  41. Assert.IsFalse(coll.Contains(3.1));
  42. Assert.AreEqual(1, coll.Count);
  43. coll.Clear();
  44. Assert.AreEqual(0, coll.Count);
  45. Assert.IsFalse(coll.Remove(3.1));
  46. }
  47. public void TrySCGColl(SCG.ICollection<double> coll)
  48. {
  49. // All members of SCG.ICollection<T>
  50. Assert.AreEqual(0, coll.Count);
  51. double[] arr = { };
  52. coll.CopyTo(arr, 0);
  53. Assert.IsFalse(coll.IsReadOnly);
  54. coll.Add(2.3);
  55. coll.Add(3.2);
  56. Assert.AreEqual(2, coll.Count);
  57. Assert.IsTrue(coll.Contains(2.3));
  58. Assert.IsFalse(coll.Contains(3.1));
  59. Assert.IsFalse(coll.Remove(3.1));
  60. Assert.IsTrue(coll.Remove(3.2));
  61. Assert.IsFalse(coll.Contains(3.1));
  62. Assert.AreEqual(1, coll.Count);
  63. coll.Clear();
  64. Assert.AreEqual(0, coll.Count);
  65. Assert.IsFalse(coll.Remove(3.1));
  66. }
  67. public void TryBothColl(ICollection<double> coll)
  68. {
  69. TryC5Coll(coll);
  70. TrySCGColl(coll);
  71. }
  72. [Test]
  73. public void Test1()
  74. {
  75. TryBothColl(new HashSet<double>());
  76. TryBothColl(new HashBag<double>());
  77. TryBothColl(new TreeSet<double>());
  78. TryBothColl(new TreeBag<double>());
  79. TryBothColl(new ArrayList<double>());
  80. TryBothColl(new LinkedList<double>());
  81. TryBothColl(new HashedArrayList<double>());
  82. TryBothColl(new HashedLinkedList<double>());
  83. TryBothColl(new SortedArray<double>());
  84. }
  85. }
  86. [TestFixture]
  87. public class SCIListTests
  88. {
  89. class A { }
  90. class B : A { }
  91. class C : B { }
  92. public void TrySCIList(System.Collections.IList list)
  93. {
  94. // Should be called with a C5.IList<B> which is not a WrappedArray
  95. Assert.AreEqual(0, list.Count);
  96. list.CopyTo(new A[0], 0);
  97. list.CopyTo(new B[0], 0);
  98. list.CopyTo(new C[0], 0);
  99. Assert.IsTrue(!list.IsFixedSize);
  100. Assert.IsFalse(list.IsReadOnly);
  101. Assert.IsFalse(list.IsSynchronized);
  102. Assert.AreNotEqual(null, list.SyncRoot);
  103. Object b1 = new B(), b2 = new B(), c1 = new C(), c2 = new C();
  104. Assert.AreEqual(0, list.Add(b1));
  105. Assert.AreEqual(1, list.Add(c1));
  106. Assert.AreEqual(2, list.Count);
  107. Assert.IsTrue(list.Contains(c1));
  108. Assert.IsFalse(list.Contains(b2));
  109. list[0] = b2;
  110. Assert.AreEqual(b2, list[0]);
  111. list[1] = c2;
  112. Assert.AreEqual(c2, list[1]);
  113. Assert.IsTrue(list.Contains(b2));
  114. Assert.IsTrue(list.Contains(c2));
  115. Array arrA = new A[2], arrB = new B[2];
  116. list.CopyTo(arrA, 0);
  117. list.CopyTo(arrB, 0);
  118. Assert.AreEqual(b2, arrA.GetValue(0));
  119. Assert.AreEqual(b2, arrB.GetValue(0));
  120. Assert.AreEqual(c2, arrA.GetValue(1));
  121. Assert.AreEqual(c2, arrB.GetValue(1));
  122. Assert.AreEqual(0, list.IndexOf(b2));
  123. Assert.AreEqual(-1, list.IndexOf(b1));
  124. list.Remove(b1);
  125. list.Remove(b2);
  126. Assert.IsFalse(list.Contains(b2));
  127. Assert.AreEqual(1, list.Count); // Contains c2 only
  128. list.Insert(0, b2);
  129. list.Insert(2, b1);
  130. Assert.AreEqual(b2, list[0]);
  131. Assert.AreEqual(c2, list[1]);
  132. Assert.AreEqual(b1, list[2]);
  133. list.Remove(c2);
  134. Assert.AreEqual(b2, list[0]);
  135. Assert.AreEqual(b1, list[1]);
  136. list.RemoveAt(1);
  137. Assert.AreEqual(b2, list[0]);
  138. list.Clear();
  139. Assert.AreEqual(0, list.Count);
  140. list.Remove(b1);
  141. }
  142. [Test]
  143. public void Test1()
  144. {
  145. TrySCIList(new ArrayList<B>());
  146. TrySCIList(new HashedArrayList<B>());
  147. TrySCIList(new LinkedList<B>());
  148. TrySCIList(new HashedLinkedList<B>());
  149. }
  150. [Test]
  151. public void TryWrappedArrayAsSCIList1()
  152. {
  153. B[] myarray = new B[] { new B(), new B(), new C() };
  154. System.Collections.IList list = new WrappedArray<B>(myarray);
  155. // Should be called with a three-element WrappedArray<B>
  156. Assert.AreEqual(3, list.Count);
  157. Assert.IsTrue(list.IsFixedSize);
  158. Assert.IsFalse(list.IsSynchronized);
  159. Assert.AreNotEqual(null, list.SyncRoot);
  160. Assert.AreEqual(myarray.SyncRoot, list.SyncRoot);
  161. Object b1 = new B(), b2 = new B(), c1 = new C(), c2 = new C();
  162. list[0] = b2;
  163. Assert.AreEqual(b2, list[0]);
  164. list[1] = c2;
  165. Assert.AreEqual(c2, list[1]);
  166. Assert.IsTrue(list.Contains(b2));
  167. Assert.IsTrue(list.Contains(c2));
  168. Array arrA = new A[3], arrB = new B[3];
  169. list.CopyTo(arrA, 0);
  170. list.CopyTo(arrB, 0);
  171. Assert.AreEqual(b2, arrA.GetValue(0));
  172. Assert.AreEqual(b2, arrB.GetValue(0));
  173. Assert.AreEqual(c2, arrA.GetValue(1));
  174. Assert.AreEqual(c2, arrB.GetValue(1));
  175. Assert.AreEqual(0, list.IndexOf(b2));
  176. Assert.AreEqual(-1, list.IndexOf(b1));
  177. Assert.AreEqual(-1, list.IndexOf(c1));
  178. Assert.IsFalse(list.Contains(b1));
  179. Assert.IsFalse(list.Contains(c1));
  180. }
  181. [Test]
  182. public void TryWrappedArrayAsSCIList2()
  183. {
  184. B[] myarray = new B[] { };
  185. System.Collections.IList list = new WrappedArray<B>(myarray);
  186. // Should be called with an empty WrappedArray<B>
  187. Assert.AreEqual(0, list.Count);
  188. list.CopyTo(new A[0], 0);
  189. list.CopyTo(new B[0], 0);
  190. list.CopyTo(new C[0], 0);
  191. Assert.IsFalse(list.IsSynchronized);
  192. Assert.AreNotEqual(null, list.SyncRoot);
  193. Object b1 = new B(), b2 = new B(), c1 = new C(), c2 = new C();
  194. Assert.IsFalse(list.Contains(b2));
  195. Assert.IsFalse(list.Contains(c2));
  196. Assert.AreEqual(-1, list.IndexOf(b1));
  197. Assert.AreEqual(-1, list.IndexOf(c1));
  198. }
  199. [Test]
  200. public void TryGuardedListAsSCIList1()
  201. {
  202. B b1_ = new B(), b2_ = new B();
  203. C c1_ = new C(), c2_ = new C();
  204. ArrayList<B> mylist = new ArrayList<B>();
  205. mylist.AddAll(new B[] { b1_, b2_, c1_ });
  206. System.Collections.IList list = new GuardedList<B>(mylist);
  207. Object b1 = b1_, b2 = b2_, c1 = c1_, c2 = c2_;
  208. // Should be called with a three-element GuardedList<B>
  209. Assert.AreEqual(3, list.Count);
  210. Assert.IsTrue(list.IsFixedSize);
  211. Assert.IsTrue(list.IsReadOnly);
  212. Assert.IsFalse(list.IsSynchronized);
  213. Assert.AreNotEqual(null, list.SyncRoot);
  214. Assert.AreEqual(list.SyncRoot, ((System.Collections.IList)mylist).SyncRoot);
  215. Assert.IsTrue(list.Contains(b1));
  216. Assert.IsTrue(list.Contains(b2));
  217. Assert.IsTrue(list.Contains(c1));
  218. Assert.IsFalse(list.Contains(c2));
  219. Array arrA = new A[3], arrB = new B[3];
  220. list.CopyTo(arrA, 0);
  221. list.CopyTo(arrB, 0);
  222. Assert.AreEqual(b1, arrA.GetValue(0));
  223. Assert.AreEqual(b1, arrB.GetValue(0));
  224. Assert.AreEqual(b2, arrA.GetValue(1));
  225. Assert.AreEqual(b2, arrB.GetValue(1));
  226. Assert.AreEqual(0, list.IndexOf(b1));
  227. Assert.AreEqual(-1, list.IndexOf(c2));
  228. }
  229. [Test]
  230. public void TryGuardedListAsSCIList2()
  231. {
  232. System.Collections.IList list = new GuardedList<B>(new ArrayList<B>());
  233. // Should be called with an empty GuardedList<B>
  234. Assert.AreEqual(0, list.Count);
  235. list.CopyTo(new A[0], 0);
  236. list.CopyTo(new B[0], 0);
  237. list.CopyTo(new C[0], 0);
  238. Assert.IsFalse(list.IsSynchronized);
  239. Assert.AreNotEqual(null, list.SyncRoot);
  240. Object b1 = new B(), b2 = new B(), c1 = new C(), c2 = new C();
  241. Assert.IsFalse(list.Contains(b2));
  242. Assert.IsFalse(list.Contains(c2));
  243. Assert.AreEqual(-1, list.IndexOf(b1));
  244. Assert.AreEqual(-1, list.IndexOf(c1));
  245. }
  246. [Test]
  247. public void TryViewOfGuardedListAsSCIList1()
  248. {
  249. B b1_ = new B(), b2_ = new B();
  250. C c1_ = new C(), c2_ = new C();
  251. ArrayList<B> mylist = new ArrayList<B>();
  252. mylist.AddAll(new B[] { new B(), b1_, b2_, c1_, new B()});
  253. System.Collections.IList list = new GuardedList<B>(mylist).View(1, 3);
  254. Object b1 = b1_, b2 = b2_, c1 = c1_, c2 = c2_;
  255. // Should be called with a three-element view of a GuardedList<B>
  256. Assert.AreEqual(3, list.Count);
  257. Assert.IsTrue(list.IsFixedSize);
  258. Assert.IsTrue(list.IsReadOnly);
  259. Assert.IsFalse(list.IsSynchronized);
  260. Assert.AreNotEqual(null, list.SyncRoot);
  261. Assert.AreEqual(list.SyncRoot, ((System.Collections.IList)mylist).SyncRoot);
  262. Assert.IsTrue(list.Contains(b1));
  263. Assert.IsTrue(list.Contains(b2));
  264. Assert.IsTrue(list.Contains(c1));
  265. Assert.IsFalse(list.Contains(c2));
  266. Array arrA = new A[3], arrB = new B[3];
  267. list.CopyTo(arrA, 0);
  268. list.CopyTo(arrB, 0);
  269. Assert.AreEqual(b1, arrA.GetValue(0));
  270. Assert.AreEqual(b1, arrB.GetValue(0));
  271. Assert.AreEqual(b2, arrA.GetValue(1));
  272. Assert.AreEqual(b2, arrB.GetValue(1));
  273. Assert.AreEqual(0, list.IndexOf(b1));
  274. Assert.AreEqual(-1, list.IndexOf(c2));
  275. }
  276. [Test]
  277. public void TryViewOfGuardedListAsSCIList2()
  278. {
  279. System.Collections.IList list = new GuardedList<B>(new ArrayList<B>()).View(0, 0);
  280. Assert.AreEqual(0, list.Count);
  281. list.CopyTo(new A[0], 0);
  282. list.CopyTo(new B[0], 0);
  283. list.CopyTo(new C[0], 0);
  284. Assert.IsFalse(list.IsSynchronized);
  285. Assert.AreNotEqual(null, list.SyncRoot);
  286. Object b1 = new B(), b2 = new B(), c1 = new C(), c2 = new C();
  287. Assert.IsFalse(list.Contains(b2));
  288. Assert.IsFalse(list.Contains(c2));
  289. Assert.AreEqual(-1, list.IndexOf(b1));
  290. Assert.AreEqual(-1, list.IndexOf(c1));
  291. }
  292. void TryListViewAsSCIList1(IList<B> mylist)
  293. {
  294. B b1_ = new B(), b2_ = new B();
  295. C c1_ = new C(), c2_ = new C();
  296. mylist.AddAll(new B[] { new B(), b1_, b2_, c1_, new B() });
  297. System.Collections.IList list = mylist.View(1, 3);
  298. Object b1 = b1_, b2 = b2_, c1 = c1_, c2 = c2_;
  299. // Should be called with a three-element view on ArrayList<B>
  300. Assert.AreEqual(3, list.Count);
  301. Assert.IsFalse(list.IsSynchronized);
  302. Assert.AreNotEqual(null, list.SyncRoot);
  303. Assert.AreEqual(list.SyncRoot, mylist.SyncRoot);
  304. Assert.IsTrue(list.Contains(b1));
  305. Assert.IsTrue(list.Contains(b2));
  306. Assert.IsTrue(list.Contains(c1));
  307. Assert.IsFalse(list.Contains(c2));
  308. Array arrA = new A[3], arrB = new B[3];
  309. list.CopyTo(arrA, 0);
  310. list.CopyTo(arrB, 0);
  311. Assert.AreEqual(b1, arrA.GetValue(0));
  312. Assert.AreEqual(b1, arrB.GetValue(0));
  313. Assert.AreEqual(b2, arrA.GetValue(1));
  314. Assert.AreEqual(b2, arrB.GetValue(1));
  315. Assert.AreEqual(0, list.IndexOf(b1));
  316. Assert.AreEqual(-1, list.IndexOf(c2));
  317. }
  318. void TryListViewAsSCIList2(IList<B> mylist)
  319. {
  320. System.Collections.IList list = mylist.View(0, 0);
  321. Assert.AreEqual(0, list.Count);
  322. list.CopyTo(new A[0], 0);
  323. list.CopyTo(new B[0], 0);
  324. list.CopyTo(new C[0], 0);
  325. Assert.IsFalse(list.IsSynchronized);
  326. Assert.AreNotEqual(null, list.SyncRoot);
  327. Assert.AreEqual(list.SyncRoot, mylist.SyncRoot);
  328. Object b1 = new B(), b2 = new B(), c1 = new C(), c2 = new C();
  329. Assert.IsFalse(list.Contains(b2));
  330. Assert.IsFalse(list.Contains(c2));
  331. Assert.AreEqual(-1, list.IndexOf(b1));
  332. Assert.AreEqual(-1, list.IndexOf(c1));
  333. }
  334. [Test]
  335. public void TryArrayListViewAsSCIList()
  336. {
  337. TryListViewAsSCIList1(new ArrayList<B>());
  338. TryListViewAsSCIList2(new ArrayList<B>());
  339. }
  340. [Test]
  341. public void TryLinkedListViewAsSCIList()
  342. {
  343. TryListViewAsSCIList1(new LinkedList<B>());
  344. TryListViewAsSCIList2(new LinkedList<B>());
  345. }
  346. [Test]
  347. public void TryHashedArrayListViewAsSCIList()
  348. {
  349. TryListViewAsSCIList1(new HashedArrayList<B>());
  350. TryListViewAsSCIList2(new HashedArrayList<B>());
  351. }
  352. [Test]
  353. public void TryHashedLinkedListViewAsSCIList()
  354. {
  355. TryListViewAsSCIList1(new HashedLinkedList<B>());
  356. TryListViewAsSCIList2(new HashedLinkedList<B>());
  357. }
  358. [Test]
  359. public void TryGuardedViewAsSCIList()
  360. {
  361. ArrayList<B> mylist = new ArrayList<B>();
  362. TryListViewAsSCIList2(new GuardedList<B>(mylist));
  363. }
  364. }
  365. [TestFixture]
  366. public class IDictionaryTests
  367. {
  368. public void TryDictionary(IDictionary<string,string> dict)
  369. {
  370. Assert.AreEqual(0, dict.Count);
  371. Assert.IsTrue(dict.IsEmpty);
  372. Assert.IsFalse(dict.IsReadOnly);
  373. KeyValuePair<string,string>[] arr = { };
  374. dict.CopyTo(arr, 0);
  375. dict["R"] = "A";
  376. dict["S"] = "B";
  377. dict["T"] = "C";
  378. String old;
  379. Assert.IsTrue(dict.Update("R", "A1"));
  380. Assert.AreEqual("A1", dict["R"]);
  381. Assert.IsFalse(dict.Update("U", "D1"));
  382. Assert.IsFalse(dict.Contains("U"));
  383. Assert.IsTrue(dict.Update("R", "A2", out old));
  384. Assert.AreEqual("A2", dict["R"]);
  385. Assert.AreEqual("A1", old);
  386. Assert.IsFalse(dict.Update("U", "D2", out old));
  387. Assert.AreEqual(null, old);
  388. Assert.IsFalse(dict.Contains("U"));
  389. Assert.IsTrue(dict.UpdateOrAdd("R", "A3"));
  390. Assert.AreEqual("A3", dict["R"]);
  391. Assert.IsFalse(dict.UpdateOrAdd("U", "D3"));
  392. Assert.IsTrue(dict.Contains("U"));
  393. Assert.AreEqual("D3", dict["U"]);
  394. Assert.IsTrue(dict.UpdateOrAdd("R", "A4", out old));
  395. Assert.AreEqual("A4", dict["R"]);
  396. Assert.AreEqual("A3", old);
  397. Assert.IsTrue(dict.UpdateOrAdd("U", "D4", out old));
  398. Assert.IsTrue(dict.Contains("U"));
  399. Assert.AreEqual("D4", dict["U"]);
  400. Assert.AreEqual("D3", old);
  401. Assert.IsFalse(dict.UpdateOrAdd("V", "E1", out old));
  402. Assert.IsTrue(dict.Contains("V"));
  403. Assert.AreEqual("E1", dict["V"]);
  404. Assert.AreEqual(null, old);
  405. }
  406. [Test]
  407. public void TestHashDictionary()
  408. {
  409. TryDictionary(new HashDictionary<string,string>());
  410. }
  411. [Test]
  412. public void TestTreeDictionary()
  413. {
  414. TryDictionary(new TreeDictionary<string, string>());
  415. }
  416. }
  417. }