PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/danipen/mono
C# | 2492 lines | 1890 code | 545 blank | 57 comment | 119 complexity | 888b20b2ab7e318b307254f3ee19719a MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0

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

  1. /*
  2. Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.
  18. */
  19. using System;
  20. using C5;
  21. using NUnit.Framework;
  22. using SCG = System.Collections.Generic;
  23. namespace C5UnitTests.arrays.sorted
  24. {
  25. using CollectionOfInt = SortedArray<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.SortedIndexedTester<CollectionOfInt>().Test(factory);
  34. }
  35. [Test]
  36. public void Extensible()
  37. {
  38. C5UnitTests.Templates.Extensible.Clone.Tester<CollectionOfInt>();
  39. C5UnitTests.Templates.Extensible.Serialization.Tester<CollectionOfInt>();
  40. }
  41. }
  42. static class Factory
  43. {
  44. public static ICollection<T> New<T>() { return new SortedArray<T>(); }
  45. }
  46. [TestFixture]
  47. public class Formatting
  48. {
  49. ICollection<int> coll;
  50. IFormatProvider rad16;
  51. [SetUp]
  52. public void Init() { coll = Factory.New<int>(); rad16 = new RadixFormatProvider(16); }
  53. [TearDown]
  54. public void Dispose() { coll = null; rad16 = null; }
  55. [Test]
  56. public void Format()
  57. {
  58. Assert.AreEqual("{ }", coll.ToString());
  59. coll.AddAll<int>(new int[] { -4, 28, 129, 65530 });
  60. Assert.AreEqual("{ -4, 28, 129, 65530 }", coll.ToString());
  61. Assert.AreEqual("{ -4, 1C, 81, FFFA }", coll.ToString(null, rad16));
  62. Assert.AreEqual("{ -4, 28, 129... }", coll.ToString("L14", null));
  63. Assert.AreEqual("{ -4, 1C, 81... }", coll.ToString("L14", rad16));
  64. }
  65. }
  66. [TestFixture]
  67. public class Ranges
  68. {
  69. private SortedArray<int> array;
  70. private SCG.IComparer<int> c;
  71. [SetUp]
  72. public void Init()
  73. {
  74. c = new IC();
  75. array = new SortedArray<int>(c);
  76. for (int i = 1; i <= 10; i++)
  77. {
  78. array.Add(i * 2);
  79. }
  80. }
  81. [Test]
  82. public void Enumerator()
  83. {
  84. SCG.IEnumerator<int> e = array.RangeFromTo(5, 17).GetEnumerator();
  85. int i = 3;
  86. while (e.MoveNext())
  87. {
  88. Assert.AreEqual(2 * i++, e.Current);
  89. }
  90. Assert.AreEqual(9, i);
  91. }
  92. [Test]
  93. [ExpectedException(typeof(CollectionModifiedException))]
  94. public void Enumerator3()
  95. {
  96. SCG.IEnumerator<int> e = array.RangeFromTo(5, 17).GetEnumerator();
  97. e.MoveNext();
  98. array.Add(67);
  99. e.MoveNext();
  100. }
  101. [Test]
  102. public void Remove()
  103. {
  104. int[] all = new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
  105. array.RemoveRangeFrom(18);
  106. Assert.IsTrue(IC.eq(array, new int[] { 2, 4, 6, 8, 10, 12, 14, 16 }));
  107. array.RemoveRangeFrom(28);
  108. Assert.IsTrue(IC.eq(array, new int[] { 2, 4, 6, 8, 10, 12, 14, 16 }));
  109. array.RemoveRangeFrom(13);
  110. Assert.IsTrue(IC.eq(array, new int[] { 2, 4, 6, 8, 10, 12 }));
  111. array.RemoveRangeFrom(2);
  112. Assert.IsTrue(IC.eq(array));
  113. foreach (int i in all) array.Add(i);
  114. array.RemoveRangeTo(10);
  115. Assert.IsTrue(IC.eq(array, new int[] { 10, 12, 14, 16, 18, 20 }));
  116. array.RemoveRangeTo(2);
  117. Assert.IsTrue(IC.eq(array, new int[] { 10, 12, 14, 16, 18, 20 }));
  118. array.RemoveRangeTo(21);
  119. Assert.IsTrue(IC.eq(array));
  120. foreach (int i in all) array.Add(i);
  121. array.RemoveRangeFromTo(4, 8);
  122. Assert.IsTrue(IC.eq(array, 2, 8, 10, 12, 14, 16, 18, 20));
  123. array.RemoveRangeFromTo(14, 28);
  124. Assert.IsTrue(IC.eq(array, 2, 8, 10, 12));
  125. array.RemoveRangeFromTo(0, 9);
  126. Assert.IsTrue(IC.eq(array, 10, 12));
  127. array.RemoveRangeFromTo(0, 81);
  128. Assert.IsTrue(IC.eq(array));
  129. }
  130. [Test]
  131. public void Normal()
  132. {
  133. int[] all = new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
  134. Assert.IsTrue(IC.eq(array, all));
  135. Assert.IsTrue(IC.eq(array.RangeAll(), all));
  136. Assert.AreEqual(10, array.RangeAll().Count);
  137. Assert.IsTrue(IC.eq(array.RangeFrom(11), new int[] { 12, 14, 16, 18, 20 }));
  138. Assert.AreEqual(5, array.RangeFrom(11).Count);
  139. Assert.IsTrue(IC.eq(array.RangeFrom(12), new int[] { 12, 14, 16, 18, 20 }));
  140. Assert.IsTrue(IC.eq(array.RangeFrom(2), all));
  141. Assert.IsTrue(IC.eq(array.RangeFrom(1), all));
  142. Assert.IsTrue(IC.eq(array.RangeFrom(21), new int[] { }));
  143. Assert.IsTrue(IC.eq(array.RangeFrom(20), new int[] { 20 }));
  144. Assert.IsTrue(IC.eq(array.RangeTo(8), new int[] { 2, 4, 6 }));
  145. Assert.IsTrue(IC.eq(array.RangeTo(7), new int[] { 2, 4, 6 }));
  146. Assert.AreEqual(3, array.RangeTo(7).Count);
  147. Assert.IsTrue(IC.eq(array.RangeTo(2), new int[] { }));
  148. Assert.IsTrue(IC.eq(array.RangeTo(1), new int[] { }));
  149. Assert.IsTrue(IC.eq(array.RangeTo(3), new int[] { 2 }));
  150. Assert.IsTrue(IC.eq(array.RangeTo(20), new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18 }));
  151. Assert.IsTrue(IC.eq(array.RangeTo(21), all));
  152. Assert.IsTrue(IC.eq(array.RangeFromTo(7, 12), new int[] { 8, 10 }));
  153. Assert.IsTrue(IC.eq(array.RangeFromTo(6, 11), new int[] { 6, 8, 10 }));
  154. Assert.IsTrue(IC.eq(array.RangeFromTo(1, 12), new int[] { 2, 4, 6, 8, 10 }));
  155. Assert.AreEqual(5, array.RangeFromTo(1, 12).Count);
  156. Assert.IsTrue(IC.eq(array.RangeFromTo(2, 12), new int[] { 2, 4, 6, 8, 10 }));
  157. Assert.IsTrue(IC.eq(array.RangeFromTo(6, 21), new int[] { 6, 8, 10, 12, 14, 16, 18, 20 }));
  158. Assert.IsTrue(IC.eq(array.RangeFromTo(6, 20), new int[] { 6, 8, 10, 12, 14, 16, 18 }));
  159. }
  160. [Test]
  161. public void Backwards()
  162. {
  163. int[] all = new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
  164. int[] lla = new int[] { 20, 18, 16, 14, 12, 10, 8, 6, 4, 2 };
  165. Assert.IsTrue(IC.eq(array, all));
  166. Assert.IsTrue(IC.eq(array.RangeAll().Backwards(), lla));
  167. Assert.IsTrue(IC.eq(array.RangeFrom(11).Backwards(), new int[] { 20, 18, 16, 14, 12 }));
  168. Assert.IsTrue(IC.eq(array.RangeFrom(12).Backwards(), new int[] { 20, 18, 16, 14, 12 }));
  169. Assert.IsTrue(IC.eq(array.RangeFrom(2).Backwards(), lla));
  170. Assert.IsTrue(IC.eq(array.RangeFrom(1).Backwards(), lla));
  171. Assert.IsTrue(IC.eq(array.RangeFrom(21).Backwards(), new int[] { }));
  172. Assert.IsTrue(IC.eq(array.RangeFrom(20).Backwards(), new int[] { 20 }));
  173. Assert.IsTrue(IC.eq(array.RangeTo(8).Backwards(), new int[] { 6, 4, 2 }));
  174. Assert.IsTrue(IC.eq(array.RangeTo(7).Backwards(), new int[] { 6, 4, 2 }));
  175. Assert.IsTrue(IC.eq(array.RangeTo(2).Backwards(), new int[] { }));
  176. Assert.IsTrue(IC.eq(array.RangeTo(1).Backwards(), new int[] { }));
  177. Assert.IsTrue(IC.eq(array.RangeTo(3).Backwards(), new int[] { 2 }));
  178. Assert.IsTrue(IC.eq(array.RangeTo(20).Backwards(), new int[] { 18, 16, 14, 12, 10, 8, 6, 4, 2}));
  179. Assert.IsTrue(IC.eq(array.RangeTo(21).Backwards(), lla));
  180. Assert.IsTrue(IC.eq(array.RangeFromTo(7, 12).Backwards(), new int[] { 10, 8 }));
  181. Assert.IsTrue(IC.eq(array.RangeFromTo(6, 11).Backwards(), new int[] { 10, 8, 6 }));
  182. Assert.IsTrue(IC.eq(array.RangeFromTo(1, 12).Backwards(), new int[] { 10, 8, 6, 4, 2 }));
  183. Assert.IsTrue(IC.eq(array.RangeFromTo(2, 12).Backwards(), new int[] { 10, 8, 6, 4, 2 }));
  184. Assert.IsTrue(IC.eq(array.RangeFromTo(6, 21).Backwards(), new int[] { 20, 18, 16, 14, 12, 10, 8, 6 }));
  185. Assert.IsTrue(IC.eq(array.RangeFromTo(6, 20).Backwards(), new int[] { 18, 16, 14, 12, 10, 8, 6 }));
  186. }
  187. [Test]
  188. public void Direction()
  189. {
  190. Assert.AreEqual(EnumerationDirection.Forwards, array.Direction);
  191. Assert.AreEqual(EnumerationDirection.Forwards, array.RangeFrom(20).Direction);
  192. Assert.AreEqual(EnumerationDirection.Forwards, array.RangeTo(7).Direction);
  193. Assert.AreEqual(EnumerationDirection.Forwards, array.RangeFromTo(1, 12).Direction);
  194. Assert.AreEqual(EnumerationDirection.Forwards, array.RangeAll().Direction);
  195. Assert.AreEqual(EnumerationDirection.Backwards, array.Backwards().Direction);
  196. Assert.AreEqual(EnumerationDirection.Backwards, array.RangeFrom(20).Backwards().Direction);
  197. Assert.AreEqual(EnumerationDirection.Backwards, array.RangeTo(7).Backwards().Direction);
  198. Assert.AreEqual(EnumerationDirection.Backwards, array.RangeFromTo(1, 12).Backwards().Direction);
  199. Assert.AreEqual(EnumerationDirection.Backwards, array.RangeAll().Backwards().Direction);
  200. }
  201. [TearDown]
  202. public void Dispose()
  203. {
  204. array = null;
  205. c = null;
  206. }
  207. }
  208. [TestFixture]
  209. public class BagItf
  210. {
  211. private SortedArray<int> array;
  212. [SetUp]
  213. public void Init()
  214. {
  215. array = new SortedArray<int>(new IC());
  216. for (int i = 10; i < 20; i++)
  217. {
  218. array.Add(i);
  219. array.Add(i + 10);
  220. }
  221. }
  222. [Test]
  223. public void Both()
  224. {
  225. Assert.AreEqual(0, array.ContainsCount(7));
  226. Assert.AreEqual(1, array.ContainsCount(10));
  227. array.RemoveAllCopies(10);
  228. Assert.AreEqual(0, array.ContainsCount(10));
  229. array.RemoveAllCopies(7);
  230. }
  231. [TearDown]
  232. public void Dispose()
  233. {
  234. array = null;
  235. }
  236. }
  237. [TestFixture]
  238. public class Div
  239. {
  240. private SortedArray<int> array;
  241. [SetUp]
  242. public void Init()
  243. {
  244. array = new SortedArray<int>(new IC());
  245. }
  246. [Test]
  247. [ExpectedException(typeof(NullReferenceException))]
  248. public void NullEqualityComparerinConstructor1()
  249. {
  250. new SortedArray<int>(null);
  251. }
  252. [Test]
  253. [ExpectedException(typeof(NullReferenceException))]
  254. public void NullEqualityComparerinConstructor2()
  255. {
  256. new SortedArray<int>(5, null);
  257. }
  258. [Test]
  259. [ExpectedException(typeof(NullReferenceException))]
  260. public void NullEqualityComparerinConstructor3()
  261. {
  262. new SortedArray<int>(5, null, EqualityComparer<int>.Default);
  263. }
  264. [Test]
  265. [ExpectedException(typeof(NullReferenceException))]
  266. public void NullEqualityComparerinConstructor4()
  267. {
  268. new SortedArray<int>(5, Comparer<int>.Default, null);
  269. }
  270. [Test]
  271. [ExpectedException(typeof(NullReferenceException))]
  272. public void NullEqualityComparerinConstructor5()
  273. {
  274. new SortedArray<int>(5, null, null);
  275. }
  276. [Test]
  277. public void Choose()
  278. {
  279. array.Add(7);
  280. Assert.AreEqual(7, array.Choose());
  281. }
  282. [Test]
  283. [ExpectedException(typeof(NoSuchItemException))]
  284. public void BadChoose()
  285. {
  286. array.Choose();
  287. }
  288. private void loadup()
  289. {
  290. for (int i = 10; i < 20; i++)
  291. {
  292. array.Add(i);
  293. array.Add(i + 10);
  294. }
  295. }
  296. [Test]
  297. public void NoDuplicatesEtc()
  298. {
  299. Assert.IsFalse(array.AllowsDuplicates);
  300. loadup();
  301. Assert.IsFalse(array.AllowsDuplicates);
  302. Assert.AreEqual(Speed.Log, array.ContainsSpeed);
  303. Assert.IsTrue(array.Comparer.Compare(2, 3) < 0);
  304. Assert.IsTrue(array.Comparer.Compare(4, 3) > 0);
  305. Assert.IsTrue(array.Comparer.Compare(3, 3) == 0);
  306. }
  307. [Test]
  308. public void Add()
  309. {
  310. Assert.IsTrue(array.Add(17));
  311. Assert.IsFalse(array.Add(17));
  312. Assert.IsTrue(array.Add(18));
  313. Assert.IsFalse(array.Add(18));
  314. Assert.AreEqual(2, array.Count);
  315. }
  316. [TearDown]
  317. public void Dispose()
  318. {
  319. array = null;
  320. }
  321. }
  322. [TestFixture]
  323. public class FindOrAdd
  324. {
  325. private SortedArray<KeyValuePair<int,string>> bag;
  326. [SetUp]
  327. public void Init()
  328. {
  329. bag = new SortedArray<KeyValuePair<int,string>>(new KeyValuePairComparer<int,string>(new IC()));
  330. }
  331. [TearDown]
  332. public void Dispose()
  333. {
  334. bag = null;
  335. }
  336. [Test]
  337. public void Test()
  338. {
  339. KeyValuePair<int,string> p = new KeyValuePair<int,string>(3, "tre");
  340. Assert.IsFalse(bag.FindOrAdd(ref p));
  341. p.Value = "drei";
  342. Assert.IsTrue(bag.FindOrAdd(ref p));
  343. Assert.AreEqual("tre", p.Value);
  344. p.Value = "three";
  345. Assert.AreEqual(1, bag.ContainsCount(p));
  346. Assert.AreEqual("tre", bag[0].Value);
  347. }
  348. }
  349. [TestFixture]
  350. public class FindPredicate
  351. {
  352. private SortedArray<int> list;
  353. Fun<int, bool> pred;
  354. [SetUp]
  355. public void Init()
  356. {
  357. list = new SortedArray<int>(TenEqualityComparer.Default);
  358. pred = delegate(int i) { return i % 5 == 0; };
  359. }
  360. [TearDown]
  361. public void Dispose() { list = null; }
  362. [Test]
  363. public void Find()
  364. {
  365. int i;
  366. Assert.IsFalse(list.Find(pred, out i));
  367. list.AddAll<int>(new int[] { 4, 22, 67, 37 });
  368. Assert.IsFalse(list.Find(pred, out i));
  369. list.AddAll<int>(new int[] { 45, 122, 675, 137 });
  370. Assert.IsTrue(list.Find(pred, out i));
  371. Assert.AreEqual(45, i);
  372. }
  373. [Test]
  374. public void FindLast()
  375. {
  376. int i;
  377. Assert.IsFalse(list.FindLast(pred, out i));
  378. list.AddAll<int>(new int[] { 4, 22, 67, 37 });
  379. Assert.IsFalse(list.FindLast(pred, out i));
  380. list.AddAll<int>(new int[] { 45, 122, 675, 137 });
  381. Assert.IsTrue(list.FindLast(pred, out i));
  382. Assert.AreEqual(675, i);
  383. }
  384. [Test]
  385. public void FindIndex()
  386. {
  387. Assert.IsFalse(0 <= list.FindIndex(pred));
  388. list.AddAll<int>(new int[] { 4, 22, 67, 37 });
  389. Assert.IsFalse(0 <= list.FindIndex(pred));
  390. list.AddAll<int>(new int[] { 45, 122, 675, 137 });
  391. Assert.AreEqual(3, list.FindIndex(pred));
  392. }
  393. [Test]
  394. public void FindLastIndex()
  395. {
  396. Assert.IsFalse(0 <= list.FindLastIndex(pred));
  397. list.AddAll<int>(new int[] { 4, 22, 67, 37 });
  398. Assert.IsFalse(0 <= list.FindLastIndex(pred));
  399. list.AddAll<int>(new int[] { 45, 122, 675, 137 });
  400. Assert.AreEqual(7, list.FindLastIndex(pred));
  401. }
  402. }
  403. [TestFixture]
  404. public class UniqueItems
  405. {
  406. private SortedArray<int> list;
  407. [SetUp]
  408. public void Init() { list = new SortedArray<int>(); }
  409. [TearDown]
  410. public void Dispose() { list = null; }
  411. [Test]
  412. public void Test()
  413. {
  414. Assert.IsTrue(IC.seteq(list.UniqueItems()));
  415. Assert.IsTrue(IC.seteq(list.ItemMultiplicities()));
  416. list.AddAll<int>(new int[] { 7, 9, 7 });
  417. Assert.IsTrue(IC.seteq(list.UniqueItems(), 7, 9));
  418. Assert.IsTrue(IC.seteq(list.ItemMultiplicities(), 7, 1, 9, 1));
  419. }
  420. }
  421. [TestFixture]
  422. public class ArrayTest
  423. {
  424. private SortedArray<int> tree;
  425. int[] a;
  426. [SetUp]
  427. public void Init()
  428. {
  429. tree = new SortedArray<int>(new IC());
  430. a = new int[10];
  431. for (int i = 0; i < 10; i++)
  432. a[i] = 1000 + i;
  433. }
  434. [TearDown]
  435. public void Dispose() { tree = null; }
  436. private string aeq(int[] a, params int[] b)
  437. {
  438. if (a.Length != b.Length)
  439. return "Lengths differ: " + a.Length + " != " + b.Length;
  440. for (int i = 0; i < a.Length; i++)
  441. if (a[i] != b[i])
  442. return String.Format("{0}'th elements differ: {1} != {2}", i, a[i], b[i]);
  443. return "Alles klar";
  444. }
  445. [Test]
  446. public void ToArray()
  447. {
  448. Assert.AreEqual("Alles klar", aeq(tree.ToArray()));
  449. tree.Add(7);
  450. tree.Add(4);
  451. Assert.AreEqual("Alles klar", aeq(tree.ToArray(), 4, 7));
  452. }
  453. [Test]
  454. public void CopyTo()
  455. {
  456. tree.CopyTo(a, 1);
  457. Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
  458. tree.Add(6);
  459. tree.CopyTo(a, 2);
  460. Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
  461. tree.Add(4);
  462. tree.Add(9);
  463. tree.CopyTo(a, 4);
  464. Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 4, 6, 9, 1007, 1008, 1009));
  465. tree.Clear();
  466. tree.Add(7);
  467. tree.CopyTo(a, 9);
  468. Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 4, 6, 9, 1007, 1008, 7));
  469. }
  470. [Test]
  471. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  472. public void CopyToBad()
  473. {
  474. tree.CopyTo(a, 11);
  475. }
  476. [Test]
  477. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  478. public void CopyToBad2()
  479. {
  480. tree.CopyTo(a, -1);
  481. }
  482. [Test]
  483. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  484. public void CopyToTooFar()
  485. {
  486. tree.Add(3);
  487. tree.Add(4);
  488. tree.CopyTo(a, 9);
  489. }
  490. }
  491. [TestFixture]
  492. public class Combined
  493. {
  494. private IIndexedSorted<KeyValuePair<int,int>> lst;
  495. [SetUp]
  496. public void Init()
  497. {
  498. lst = new SortedArray<KeyValuePair<int,int>>(new KeyValuePairComparer<int,int>(new IC()));
  499. for (int i = 0; i < 10; i++)
  500. lst.Add(new KeyValuePair<int,int>(i, i + 30));
  501. }
  502. [TearDown]
  503. public void Dispose() { lst = null; }
  504. [Test]
  505. public void Find()
  506. {
  507. KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
  508. Assert.IsTrue(lst.Find(ref p));
  509. Assert.AreEqual(3, p.Key);
  510. Assert.AreEqual(33, p.Value);
  511. p = new KeyValuePair<int,int>(13, 78);
  512. Assert.IsFalse(lst.Find(ref p));
  513. }
  514. [Test]
  515. public void FindOrAdd()
  516. {
  517. KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
  518. Assert.IsTrue(lst.FindOrAdd(ref p));
  519. Assert.AreEqual(3, p.Key);
  520. Assert.AreEqual(33, p.Value);
  521. p = new KeyValuePair<int,int>(13, 79);
  522. Assert.IsFalse(lst.FindOrAdd(ref p));
  523. Assert.AreEqual(13, lst[10].Key);
  524. Assert.AreEqual(79, lst[10].Value);
  525. }
  526. [Test]
  527. public void Update()
  528. {
  529. KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
  530. Assert.IsTrue(lst.Update(p));
  531. Assert.AreEqual(3, lst[3].Key);
  532. Assert.AreEqual(78, lst[3].Value);
  533. p = new KeyValuePair<int,int>(13, 78);
  534. Assert.IsFalse(lst.Update(p));
  535. }
  536. [Test]
  537. public void UpdateOrAdd1()
  538. {
  539. KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
  540. Assert.IsTrue(lst.UpdateOrAdd(p));
  541. Assert.AreEqual(3, lst[3].Key);
  542. Assert.AreEqual(78, lst[3].Value);
  543. p = new KeyValuePair<int,int>(13, 79);
  544. Assert.IsFalse(lst.UpdateOrAdd(p));
  545. Assert.AreEqual(13, lst[10].Key);
  546. Assert.AreEqual(79, lst[10].Value);
  547. }
  548. [Test]
  549. public void UpdateOrAdd2()
  550. {
  551. ICollection<String> coll = new SortedArray<String>();
  552. // s1 and s2 are distinct objects but contain the same text:
  553. String old, s1 = "abc", s2 = ("def" + s1).Substring(3);
  554. Assert.IsFalse(coll.UpdateOrAdd(s1, out old));
  555. Assert.AreEqual(null, old);
  556. Assert.IsTrue(coll.UpdateOrAdd(s2, out old));
  557. Assert.IsTrue(Object.ReferenceEquals(s1, old));
  558. Assert.IsFalse(Object.ReferenceEquals(s2, old));
  559. }
  560. [Test]
  561. public void UpdateOrAddWithExpand()
  562. {
  563. // bug20071217
  564. SortedArray<double> arr = new SortedArray<double>();
  565. for (int i = 0; i < 50; i++)
  566. {
  567. arr.UpdateOrAdd(i + 0.1);
  568. arr.Add(i + 0.2);
  569. }
  570. Assert.IsTrue(arr.Count == 100);
  571. }
  572. [Test]
  573. public void FindOrAddWithExpand()
  574. {
  575. // bug20071217
  576. SortedArray<double> arr = new SortedArray<double>();
  577. for (int i = 0; i < 50; i++)
  578. {
  579. double iVar = i + 0.1;
  580. arr.FindOrAdd(ref iVar);
  581. arr.Add(i * 0.2);
  582. }
  583. Assert.IsTrue(arr.Count == 100);
  584. }
  585. [Test]
  586. public void RemoveWithReturn()
  587. {
  588. KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
  589. Assert.IsTrue(lst.Remove(p, out p));
  590. Assert.AreEqual(3, p.Key);
  591. Assert.AreEqual(33, p.Value);
  592. Assert.AreEqual(4, lst[3].Key);
  593. Assert.AreEqual(34, lst[3].Value);
  594. p = new KeyValuePair<int,int>(13, 78);
  595. Assert.IsFalse(lst.Remove(p, out p));
  596. }
  597. }
  598. [TestFixture]
  599. public class Remove
  600. {
  601. private SortedArray<int> array;
  602. [SetUp]
  603. public void Init()
  604. {
  605. array = new SortedArray<int>(new IC());
  606. for (int i = 10; i < 20; i++)
  607. {
  608. array.Add(i);
  609. array.Add(i + 10);
  610. }
  611. }
  612. [Test]
  613. public void SmallTrees()
  614. {
  615. array.Clear();
  616. array.Add(7);
  617. array.Add(9);
  618. Assert.IsTrue(array.Remove(7));
  619. Assert.IsTrue(array.Check());
  620. }
  621. [Test]
  622. public void ByIndex()
  623. {
  624. //Remove root!
  625. int n = array.Count;
  626. int i = array[10];
  627. array.RemoveAt(10);
  628. Assert.IsTrue(array.Check());
  629. Assert.IsFalse(array.Contains(i));
  630. Assert.AreEqual(n - 1, array.Count);
  631. //Low end
  632. i = array.FindMin();
  633. array.RemoveAt(0);
  634. Assert.IsTrue(array.Check());
  635. Assert.IsFalse(array.Contains(i));
  636. Assert.AreEqual(n - 2, array.Count);
  637. //high end
  638. i = array.FindMax();
  639. array.RemoveAt(array.Count - 1);
  640. Assert.IsTrue(array.Check());
  641. Assert.IsFalse(array.Contains(i));
  642. Assert.AreEqual(n - 3, array.Count);
  643. //Some leaf
  644. i = 18;
  645. array.RemoveAt(7);
  646. Assert.IsTrue(array.Check());
  647. Assert.IsFalse(array.Contains(i));
  648. Assert.AreEqual(n - 4, array.Count);
  649. }
  650. [Test]
  651. public void AlmostEmpty()
  652. {
  653. //Almost empty
  654. array.Clear();
  655. array.Add(3);
  656. array.RemoveAt(0);
  657. Assert.IsTrue(array.Check());
  658. Assert.IsFalse(array.Contains(3));
  659. Assert.AreEqual(0, array.Count);
  660. }
  661. [Test]
  662. [ExpectedException(typeof(IndexOutOfRangeException), ExpectedMessage="Index out of range for sequenced collectionvalue")]
  663. public void Empty()
  664. {
  665. array.Clear();
  666. array.RemoveAt(0);
  667. }
  668. [Test]
  669. [ExpectedException(typeof(IndexOutOfRangeException), ExpectedMessage="Index out of range for sequenced collectionvalue")]
  670. public void HighIndex()
  671. {
  672. array.RemoveAt(array.Count);
  673. }
  674. [Test]
  675. [ExpectedException(typeof(IndexOutOfRangeException), ExpectedMessage="Index out of range for sequenced collectionvalue")]
  676. public void LowIndex()
  677. {
  678. array.RemoveAt(-1);
  679. }
  680. [Test]
  681. public void Normal()
  682. {
  683. Assert.IsFalse(array.Remove(-20));
  684. //No demote case, with move_item
  685. Assert.IsTrue(array.Remove(20));
  686. Assert.IsTrue(array.Check());
  687. Assert.IsFalse(array.Remove(20));
  688. //plain case 2
  689. Assert.IsTrue(array.Remove(14));
  690. Assert.IsTrue(array.Check(), "Bad tree");
  691. //case 1b
  692. Assert.IsTrue(array.Remove(25));
  693. Assert.IsTrue(array.Check(), "Bad tree");
  694. //case 1c
  695. Assert.IsTrue(array.Remove(29));
  696. Assert.IsTrue(array.Check(), "Bad tree");
  697. //1a (terminating)
  698. Assert.IsTrue(array.Remove(10));
  699. Assert.IsTrue(array.Check(), "Bad tree");
  700. //2+1b
  701. Assert.IsTrue(array.Remove(12));
  702. Assert.IsTrue(array.Remove(11));
  703. //1a+1b
  704. Assert.IsTrue(array.Remove(18));
  705. Assert.IsTrue(array.Remove(13));
  706. Assert.IsTrue(array.Remove(15));
  707. //2+1c
  708. for (int i = 0; i < 10; i++)
  709. array.Add(50 - 2 * i);
  710. Assert.IsTrue(array.Remove(42));
  711. Assert.IsTrue(array.Remove(38));
  712. Assert.IsTrue(array.Remove(28));
  713. Assert.IsTrue(array.Remove(40));
  714. //
  715. Assert.IsTrue(array.Remove(16));
  716. Assert.IsTrue(array.Remove(23));
  717. Assert.IsTrue(array.Remove(17));
  718. Assert.IsTrue(array.Remove(19));
  719. Assert.IsTrue(array.Remove(50));
  720. Assert.IsTrue(array.Remove(26));
  721. Assert.IsTrue(array.Remove(21));
  722. Assert.IsTrue(array.Remove(22));
  723. Assert.IsTrue(array.Remove(24));
  724. for (int i = 0; i < 48; i++)
  725. array.Remove(i);
  726. //Almost empty tree:
  727. Assert.IsFalse(array.Remove(26));
  728. Assert.IsTrue(array.Remove(48));
  729. Assert.IsTrue(array.Check(), "Bad tree");
  730. //Empty tree:
  731. Assert.IsFalse(array.Remove(26));
  732. Assert.IsTrue(array.Check(), "Bad tree");
  733. }
  734. [TearDown]
  735. public void Dispose()
  736. {
  737. array = null;
  738. }
  739. }
  740. [TestFixture]
  741. public class PredecessorStructure
  742. {
  743. private SortedArray<int> tree;
  744. [SetUp]
  745. public void Init()
  746. {
  747. tree = new SortedArray<int>(new IC());
  748. }
  749. private void loadup()
  750. {
  751. for (int i = 0; i < 20; i++)
  752. tree.Add(2 * i);
  753. }
  754. [Test]
  755. public void FindPredecessor()
  756. {
  757. loadup();
  758. int res;
  759. Assert.IsTrue(tree.TryPredecessor(7, out res) && res == 6);
  760. Assert.IsTrue(tree.TryPredecessor(8, out res) && res == 6);
  761. //The bottom
  762. Assert.IsTrue(tree.TryPredecessor(1, out res) && res == 0);
  763. //The top
  764. Assert.IsTrue(tree.TryPredecessor(39, out res) && res == 38);
  765. }
  766. [Test]
  767. public void FindPredecessorTooLow1()
  768. {
  769. int res;
  770. Assert.IsFalse(tree.TryPredecessor(-2, out res));
  771. Assert.AreEqual(0, res);
  772. Assert.IsFalse(tree.TryPredecessor(0, out res));
  773. Assert.AreEqual(0, res);
  774. }
  775. [Test]
  776. public void FindWeakPredecessor()
  777. {
  778. loadup();
  779. int res;
  780. Assert.IsTrue(tree.TryWeakPredecessor(7, out res) && res == 6);
  781. Assert.IsTrue(tree.TryWeakPredecessor(8, out res) && res == 8);
  782. //The bottom
  783. Assert.IsTrue(tree.TryWeakPredecessor(1, out res) && res == 0);
  784. Assert.IsTrue(tree.TryWeakPredecessor(0, out res) && res == 0);
  785. //The top
  786. Assert.IsTrue(tree.TryWeakPredecessor(39, out res) && res == 38);
  787. Assert.IsTrue(tree.TryWeakPredecessor(38, out res) && res == 38);
  788. }
  789. [Test]
  790. public void FindWeakPredecessorTooLow1()
  791. {
  792. int res;
  793. Assert.IsFalse(tree.TryWeakPredecessor(-1, out res));
  794. Assert.AreEqual(0, res);
  795. }
  796. [Test]
  797. public void FindSuccessor()
  798. {
  799. loadup();
  800. int res;
  801. Assert.IsTrue(tree.TrySuccessor(7, out res) && res == 8);
  802. Assert.IsTrue(tree.TrySuccessor(8, out res) && res == 10);
  803. //The bottom
  804. Assert.IsTrue(tree.TrySuccessor(0, out res) && res == 2);
  805. Assert.IsTrue(tree.TrySuccessor(-1, out res) && res == 0);
  806. //The top
  807. Assert.IsTrue(tree.TrySuccessor(37, out res) && res == 38);
  808. }
  809. [Test]
  810. public void FindSuccessorTooHigh()
  811. {
  812. int res;
  813. Assert.IsFalse(tree.TrySuccessor(38, out res));
  814. Assert.AreEqual(0, res);
  815. Assert.IsFalse(tree.TrySuccessor(39, out res));
  816. Assert.AreEqual(0, res);
  817. }
  818. [Test]
  819. public void FindWeakSuccessor()
  820. {
  821. loadup();
  822. int res;
  823. Assert.IsTrue(tree.TryWeakSuccessor(6, out res) && res == 6);
  824. Assert.IsTrue(tree.TryWeakSuccessor(7, out res) && res == 8);
  825. //The bottom
  826. Assert.IsTrue(tree.TryWeakSuccessor(-1, out res) && res == 0);
  827. Assert.IsTrue(tree.TryWeakSuccessor(0, out res) && res == 0);
  828. //The top
  829. Assert.IsTrue(tree.TryWeakSuccessor(37, out res) && res == 38);
  830. Assert.IsTrue(tree.TryWeakSuccessor(38, out res) && res == 38);
  831. }
  832. [Test]
  833. public void FindWeakSuccessorTooHigh1()
  834. {
  835. int res;
  836. Assert.IsFalse(tree.TryWeakSuccessor(39, out res));
  837. Assert.AreEqual(0, res);
  838. }
  839. [Test]
  840. public void Predecessor()
  841. {
  842. loadup();
  843. Assert.AreEqual(6, tree.Predecessor(7));
  844. Assert.AreEqual(6, tree.Predecessor(8));
  845. //The bottom
  846. Assert.AreEqual(0, tree.Predecessor(1));
  847. //The top
  848. Assert.AreEqual(38, tree.Predecessor(39));
  849. }
  850. [Test]
  851. [ExpectedException(typeof(NoSuchItemException))]
  852. public void PredecessorTooLow1()
  853. {
  854. tree.Predecessor(-2);
  855. }
  856. [Test]
  857. [ExpectedException(typeof(NoSuchItemException))]
  858. public void PredecessorTooLow2()
  859. {
  860. tree.Predecessor(0);
  861. }
  862. [Test]
  863. public void WeakPredecessor()
  864. {
  865. loadup();
  866. Assert.AreEqual(6, tree.WeakPredecessor(7));
  867. Assert.AreEqual(8, tree.WeakPredecessor(8));
  868. //The bottom
  869. Assert.AreEqual(0, tree.WeakPredecessor(1));
  870. Assert.AreEqual(0, tree.WeakPredecessor(0));
  871. //The top
  872. Assert.AreEqual(38, tree.WeakPredecessor(39));
  873. Assert.AreEqual(38, tree.WeakPredecessor(38));
  874. }
  875. [Test]
  876. [ExpectedException(typeof(NoSuchItemException))]
  877. public void WeakPredecessorTooLow1()
  878. {
  879. tree.WeakPredecessor(-1);
  880. }
  881. [Test]
  882. public void Successor()
  883. {
  884. loadup();
  885. Assert.AreEqual(8, tree.Successor(7));
  886. Assert.AreEqual(10, tree.Successor(8));
  887. //The bottom
  888. Assert.AreEqual(2, tree.Successor(0));
  889. Assert.AreEqual(0, tree.Successor(-1));
  890. //The top
  891. Assert.AreEqual(38, tree.Successor(37));
  892. }
  893. [Test]
  894. [ExpectedException(typeof(NoSuchItemException))]
  895. public void SuccessorTooHigh1()
  896. {
  897. tree.Successor(38);
  898. }
  899. [Test]
  900. [ExpectedException(typeof(NoSuchItemException))]
  901. public void SuccessorTooHigh2()
  902. {
  903. tree.Successor(39);
  904. }
  905. [Test]
  906. public void WeakSuccessor()
  907. {
  908. loadup();
  909. Assert.AreEqual(6, tree.WeakSuccessor(6));
  910. Assert.AreEqual(8, tree.WeakSuccessor(7));
  911. //The bottom
  912. Assert.AreEqual(0, tree.WeakSuccessor(-1));
  913. Assert.AreEqual(0, tree.WeakSuccessor(0));
  914. //The top
  915. Assert.AreEqual(38, tree.WeakSuccessor(37));
  916. Assert.AreEqual(38, tree.WeakSuccessor(38));
  917. }
  918. [Test]
  919. [ExpectedException(typeof(NoSuchItemException))]
  920. public void WeakSuccessorTooHigh1()
  921. {
  922. tree.WeakSuccessor(39);
  923. }
  924. [TearDown]
  925. public void Dispose()
  926. {
  927. tree = null;
  928. }
  929. }
  930. [TestFixture]
  931. public class PriorityQueue
  932. {
  933. private SortedArray<int> tree;
  934. [SetUp]
  935. public void Init()
  936. {
  937. tree = new SortedArray<int>(new IC());
  938. }
  939. private void loadup()
  940. {
  941. foreach (int i in new int[] { 1, 2, 3, 4 })
  942. tree.Add(i);
  943. }
  944. [Test]
  945. public void Normal()
  946. {
  947. loadup();
  948. Assert.AreEqual(1, tree.FindMin());
  949. Assert.AreEqual(4, tree.FindMax());
  950. Assert.AreEqual(1, tree.DeleteMin());
  951. Assert.AreEqual(4, tree.DeleteMax());
  952. Assert.IsTrue(tree.Check(), "Bad tree");
  953. Assert.AreEqual(2, tree.FindMin());
  954. Assert.AreEqual(3, tree.FindMax());
  955. Assert.AreEqual(2, tree.DeleteMin());
  956. Assert.AreEqual(3, tree.DeleteMax());
  957. Assert.IsTrue(tree.Check(), "Bad tree");
  958. }
  959. [Test]
  960. [ExpectedException(typeof(NoSuchItemException))]
  961. public void Empty1()
  962. {
  963. tree.FindMin();
  964. }
  965. [Test]
  966. [ExpectedException(typeof(NoSuchItemException))]
  967. public void Empty2()
  968. {
  969. tree.FindMax();
  970. }
  971. [Test]
  972. [ExpectedException(typeof(NoSuchItemException))]
  973. public void Empty3()
  974. {
  975. tree.DeleteMin();
  976. }
  977. [Test]
  978. [ExpectedException(typeof(NoSuchItemException))]
  979. public void Empty4()
  980. {
  981. tree.DeleteMax();
  982. }
  983. [TearDown]
  984. public void Dispose()
  985. {
  986. tree = null;
  987. }
  988. }
  989. [TestFixture]
  990. public class IndexingAndCounting
  991. {
  992. private SortedArray<int> array;
  993. [SetUp]
  994. public void Init()
  995. {
  996. array = new SortedArray<int>(new IC());
  997. }
  998. private void populate()
  999. {
  1000. array.Add(30);
  1001. array.Add(50);
  1002. array.Add(10);
  1003. array.Add(70);
  1004. }
  1005. [Test]
  1006. public void ToArray()
  1007. {
  1008. populate();
  1009. int[] a = array.ToArray();
  1010. Assert.AreEqual(4, a.Length);
  1011. Assert.AreEqual(10, a[0]);
  1012. Assert.AreEqual(30, a[1]);
  1013. Assert.AreEqual(50, a[2]);
  1014. Assert.AreEqual(70, a[3]);
  1015. }
  1016. [Test]
  1017. public void GoodIndex()
  1018. {
  1019. Assert.AreEqual(~0, array.IndexOf(20));
  1020. Assert.AreEqual(~0, array.LastIndexOf(20));
  1021. populate();
  1022. Assert.AreEqual(10, array[0]);
  1023. Assert.AreEqual(30, array[1]);
  1024. Assert.AreEqual(50, array[2]);
  1025. Assert.AreEqual(70, array[3]);
  1026. Assert.AreEqual(0, array.IndexOf(10));
  1027. Assert.AreEqual(1, array.IndexOf(30));
  1028. Assert.AreEqual(2, array.IndexOf(50));
  1029. Assert.AreEqual(3, array.IndexOf(70));
  1030. Assert.AreEqual(~1, array.IndexOf(20));
  1031. Assert.AreEqual(~0, array.IndexOf(0));
  1032. Assert.AreEqual(~4, array.IndexOf(90));
  1033. Assert.AreEqual(0, array.LastIndexOf(10));
  1034. Assert.AreEqual(1, array.LastIndexOf(30));
  1035. Assert.AreEqual(2, array.LastIndexOf(50));
  1036. Assert.AreEqual(3, array.LastIndexOf(70));
  1037. Assert.AreEqual(~1, array.LastIndexOf(20));
  1038. Assert.AreEqual(~0, array.LastIndexOf(0));
  1039. Assert.AreEqual(~4, array.LastIndexOf(90));
  1040. }
  1041. [Test]
  1042. [ExpectedException(typeof(IndexOutOfRangeException))]
  1043. public void IndexTooLarge()
  1044. {
  1045. populate();
  1046. Console.WriteLine(array[4]);
  1047. }
  1048. [Test]
  1049. [ExpectedException(typeof(IndexOutOfRangeException))]
  1050. public void IndexTooSmall()
  1051. {
  1052. populate();
  1053. Console.WriteLine(array[-1]);
  1054. }
  1055. [Test]
  1056. public void FilledTreeOutsideInput()
  1057. {
  1058. populate();
  1059. Assert.AreEqual(0, array.CountFrom(90));
  1060. Assert.AreEqual(0, array.CountFromTo(-20, 0));
  1061. Assert.AreEqual(0, array.CountFromTo(80, 100));
  1062. Assert.AreEqual(0, array.CountTo(0));
  1063. Assert.AreEqual(4, array.CountTo(90));
  1064. Assert.AreEqual(4, array.CountFromTo(-20, 90));
  1065. Assert.AreEqual(4, array.CountFrom(0));
  1066. }
  1067. [Test]
  1068. public void FilledTreeIntermediateInput()
  1069. {
  1070. populate();
  1071. Assert.AreEqual(3, array.CountFrom(20));
  1072. Assert.AreEqual(1, array.CountFromTo(20, 40));
  1073. Assert.AreEqual(2, array.CountTo(40));
  1074. }
  1075. [Test]
  1076. public void FilledTreeMatchingInput()
  1077. {
  1078. populate();
  1079. Assert.AreEqual(3, array.CountFrom(30));
  1080. Assert.AreEqual(2, array.CountFromTo(30, 70));
  1081. Assert.AreEqual(0, array.CountFromTo(50, 30));
  1082. Assert.AreEqual(0, array.CountFromTo(50, 50));
  1083. Assert.AreEqual(0, array.CountTo(10));
  1084. Assert.AreEqual(2, array.CountTo(50));
  1085. }
  1086. [Test]
  1087. public void CountEmptyTree()
  1088. {
  1089. Assert.AreEqual(0, array.CountFrom(20));
  1090. Assert.AreEqual(0, array.CountFromTo(20, 40));
  1091. Assert.AreEqual(0, array.CountTo(40));
  1092. }
  1093. [TearDown]
  1094. public void Dispose()
  1095. {
  1096. array = null;
  1097. }
  1098. }
  1099. namespace ModificationCheck
  1100. {
  1101. [TestFixture]
  1102. public class Enumerator
  1103. {
  1104. private SortedArray<int> tree;
  1105. private SCG.IEnumerator<int> e;
  1106. [SetUp]
  1107. public void Init()
  1108. {
  1109. tree = new SortedArray<int>(new IC());
  1110. for (int i = 0; i < 10; i++)
  1111. tree.Add(i);
  1112. e = tree.GetEnumerator();
  1113. }
  1114. [Test]
  1115. public void CurrentAfterModification()
  1116. {
  1117. e.MoveNext();
  1118. tree.Add(34);
  1119. Assert.AreEqual(0, e.Current);
  1120. }
  1121. [Test]
  1122. [ExpectedException(typeof(CollectionModifiedException))]
  1123. public void MoveNextAfterAdd()
  1124. {
  1125. e.MoveNext();
  1126. tree.Add(34);
  1127. e.MoveNext();
  1128. }
  1129. [Test]
  1130. [ExpectedException(typeof(CollectionModifiedException))]
  1131. public void MoveNextAfterRemove()
  1132. {
  1133. e.MoveNext();
  1134. tree.Remove(34);
  1135. e.MoveNext();
  1136. }
  1137. [Test]
  1138. [ExpectedException(typeof(CollectionModifiedException))]
  1139. public void MoveNextAfterClear()
  1140. {
  1141. e.MoveNext();
  1142. tree.Clear();
  1143. e.MoveNext();
  1144. }
  1145. [TearDown]
  1146. public void Dispose()
  1147. {
  1148. tree = null;
  1149. e = null;
  1150. }
  1151. }
  1152. [TestFixture]
  1153. public class RangeEnumerator
  1154. {
  1155. private SortedArray<int> tree;
  1156. private SCG.IEnumerator<int> e;
  1157. [SetUp]
  1158. public void Init()
  1159. {
  1160. tree = new SortedArray<int>(new IC());
  1161. for (int i = 0; i < 10; i++)
  1162. tree.Add(i);
  1163. e = tree.RangeFromTo(3, 7).GetEnumerator();
  1164. }
  1165. [Test]
  1166. public void CurrentAfterModification()
  1167. {
  1168. e.MoveNext();
  1169. tree.Add(34);
  1170. Assert.AreEqual(3, e.Current);
  1171. }
  1172. [Test]
  1173. [ExpectedException(typeof(CollectionModifiedException))]
  1174. public void MoveNextAfterAdd()
  1175. {
  1176. tree.Add(34);
  1177. e.MoveNext();
  1178. }
  1179. [Test]
  1180. [ExpectedException(typeof(CollectionModifiedException))]
  1181. public void MoveNextAfterRemove()
  1182. {
  1183. tree.Remove(34);
  1184. e.MoveNext();
  1185. }
  1186. [Test]
  1187. [ExpectedException(typeof(CollectionModifiedException))]
  1188. public void MoveNextAfterClear()
  1189. {
  1190. tree.Clear();
  1191. e.MoveNext();
  1192. }
  1193. [TearDown]
  1194. public void Dispose()
  1195. {
  1196. tree = null;
  1197. e = null;
  1198. }
  1199. }
  1200. }
  1201. namespace HigherOrder
  1202. {
  1203. internal class CubeRoot: IComparable<int>
  1204. {
  1205. private int c;
  1206. internal CubeRoot(int c) { this.c = c; }
  1207. public int CompareTo(int that) { return c - that * that * that; }
  1208. public bool Equals(int that) { return c == that * that * that; }
  1209. }
  1210. class Interval: IComparable<int>
  1211. {
  1212. private int b, t;
  1213. internal Interval(int b, int t) { this.b = b; this.t = t; }
  1214. public int CompareTo(int that) { return that < b ? 1 : that > t ? -1 : 0; }
  1215. public bool Equals(int that) { return that >= b && that <= t; }
  1216. }
  1217. [TestFixture]
  1218. public class Simple
  1219. {
  1220. private SortedArray<int> array;
  1221. private SCG.IComparer<int> ic;
  1222. [SetUp]
  1223. public void Init()
  1224. {
  1225. ic = new IC();
  1226. array = new SortedArray<int>(ic);
  1227. }
  1228. private bool never(int i) { return false; }
  1229. private bool always(int i) { return true; }
  1230. private bool even(int i) { return i % 2 == 0; }
  1231. private string themap(int i) { return String.Format("AA {0,4} BB", i); }
  1232. private string badmap(int i) { return String.Format("AA {0} BB", i); }
  1233. private int appfield1;
  1234. private int appfield2;
  1235. private void apply(int i) { appfield1++; appfield2 += i * i; }
  1236. [Test]
  1237. public void Apply()
  1238. {
  1239. Simple simple1 = new Simple();
  1240. array.Apply(new Act<int>(simple1.apply));
  1241. Assert.AreEqual(0, simple1.appfield1);
  1242. Assert.AreEqual(0, simple1.appfield2);
  1243. Simple simple2 = new Simple();
  1244. for (int i = 0; i < 10; i++) array.Add(i);
  1245. array.Apply(new Act<int>(simple2.apply));
  1246. Assert.AreEqual(10, simple2.appfield1);
  1247. Assert.AreEqual(285, simple2.appfield2);
  1248. }
  1249. [Test]
  1250. public void All()
  1251. {
  1252. Assert.IsTrue(array.All(new Fun<int, bool>(never)));
  1253. Assert.IsTrue(array.All(new Fun<int, bool>(even)));
  1254. Assert.IsTrue(array.All(new Fun<int, bool>(always)));
  1255. for (int i = 0; i < 10; i++) array.Add(i);
  1256. Assert.IsFalse(array.All(new Fun<int, bool>(never)));
  1257. Assert.IsFalse(array.All(new Fun<int, bool>(even)));
  1258. Assert.IsTrue(array.All(new Fun<int, bool>(always)));
  1259. array.Clear();
  1260. for (int i = 0; i < 10; i++) array.Add(i * 2);
  1261. Assert.IsFalse(array.All(new Fun<int, bool>(never)));
  1262. Assert.IsTrue(array.All(new Fun<int, bool>(even)));
  1263. Assert.IsTrue(array.All(new Fun<int, bool>(always)));
  1264. array.Clear();
  1265. for (int i = 0; i < 10; i++) array.Add(i * 2 + 1);
  1266. Assert.IsFalse(array.All(new Fun<int, bool>(never)));
  1267. Assert.IsFalse(array.All(new Fun<int, bool>(even)));
  1268. Assert.IsTrue(array.All(new Fun<int, bool>(always)));
  1269. }
  1270. [Test]
  1271. public void Exists()
  1272. {
  1273. Assert.IsFalse(array.Exists(new Fun<int, bool>(never)));
  1274. Assert.IsFalse(array.Exists(new Fun<int, bool>(even)));
  1275. Assert.IsFalse(array.Exists(new Fun<int, bool>(always)));
  1276. for (int i = 0; i < 10; i++) array.Add(i);
  1277. Assert.IsFalse(array.Exists(new Fun<int, bool>(never)));
  1278. Assert.IsTrue(array.Exists(new Fun<int, bool>(even)));
  1279. Assert.IsTrue(array.Exists(new Fun<int, bool>(always)));
  1280. array.Clear();
  1281. for (int i = 0; i < 10; i++) array.Add(i * 2);
  1282. Assert.IsFalse(array.Exists(new Fun<int, bool>(never)));
  1283. Assert.IsTrue(array.Exists(new Fun<int, bool>(even)));
  1284. Assert.IsTrue(array.Exists(new Fun<int, bool>(always)));
  1285. array.Clear();
  1286. for (int i = 0; i < 10; i++) array.Add(i * 2 + 1);
  1287. Assert.IsFalse(array.Exists(new Fun<int, bool>(never)));
  1288. Assert.IsFalse(array.Exists(new Fun<int, bool>(even)));
  1289. Assert.IsTrue(array.Exists(new Fun<int, bool>(always)));
  1290. }
  1291. [Test]
  1292. public void FindAll()
  1293. {
  1294. Assert.AreEqual(0, array.FindAll(new Fun<int, bool>(never)).Count);
  1295. for (int i = 0; i < 10; i++)
  1296. array.Add(i);
  1297. Assert.AreEqual(0, array.FindAll(new Fun<int, bool>(never)).Count);
  1298. Assert.AreEqual(10, array.FindAll(new Fun<int, bool>(always)).Count);
  1299. Assert.AreEqual(5, array.FindAll(new Fun<int, bool>(even)).Count);
  1300. Assert.IsTrue(((SortedArray<int>)array.FindAll(new Fun<int, bool>(even))).Check());
  1301. }
  1302. [Test]
  1303. public void Map()
  1304. {
  1305. Assert.AreEqual(0, array.Map(new Fun<int,string>(themap), new SC()).Count);
  1306. for (int i = 0; i < 11; i++)
  1307. array.Add(i * i * i);
  1308. IIndexedSorted<string> res = array.Map(new Fun<int,string>(themap), new SC());
  1309. Assert.IsTrue(((SortedArray<string>)res).Check());
  1310. Assert.AreEqual(11, res.Count);
  1311. Assert.AreEqual("AA 0 BB", res[0]);
  1312. Assert.AreEqual("AA 27 BB", res[3]);
  1313. Assert.AreEqual("AA 125 BB", res[5]);
  1314. Assert.AreEqual("AA 1000 BB", res[10]);
  1315. }
  1316. [Test]
  1317. [ExpectedException(typeof(ArgumentException), ExpectedMessage="mapper not monotonic")]
  1318. public void BadMap()
  1319. {
  1320. for (int i = 0; i < 11; i++)
  1321. array.Add(i * i * i);
  1322. ISorted<string> res = array.Map(new Fun<int,string>(badmap), new SC());
  1323. }
  1324. [Test]
  1325. public void Cut()
  1326. {
  1327. for (int i = 0; i < 10; i++)
  1328. array.Add(i);
  1329. int low, high;
  1330. bool lval, hval;
  1331. Assert.IsTrue(array.Cut(new CubeRoot(27), out low, out lval, out high, out hval));
  1332. Assert.IsTrue(lval && hval);
  1333. Assert.AreEqual(4, high);
  1334. Assert.AreEqual(2, low);
  1335. Assert.IsFalse(array.Cut(new CubeRoot(30), out low, out lval, out high, out hval));
  1336. Assert.IsTrue(lval && hval);
  1337. Assert.AreEqual(4, high);
  1338. Assert.AreEqual(3, low);
  1339. }
  1340. [Test]
  1341. public void CutInt()
  1342. {
  1343. for (int i = 0; i < 10; i++)
  1344. array.Add(2 * i);
  1345. int low, high;
  1346. bool lval, hval;
  1347. Assert.IsFalse(array.Cut(new IC(3), out low, out lval, out high, out hval));
  1348. Assert.IsTrue(lval && hval);
  1349. Assert.AreEqual(4, high);
  1350. Assert.AreEqual(2, low);
  1351. Assert.IsTrue(array.Cut(new IC(6), out low, out lval, out high, out hval));
  1352. Assert.IsTrue(lval && hval);
  1353. Assert.AreEqual(8, high);
  1354. Assert.AreEqual(4, low);
  1355. }
  1356. [Test]
  1357. public void CutInterval()
  1358. {
  1359. for (int i = 0; i < 10; i++)
  1360. array.Add(2 * i);
  1361. int lo, hi;
  1362. bool lv, hv;
  1363. Assert.IsTrue(array.Cut(new Interval(5, 9), out lo, out lv, out hi, out hv));
  1364. Assert.IsTrue(lv && hv);
  1365. Assert.AreEqual(10, hi);
  1366. Assert.AreEqual(4, lo);
  1367. Assert.IsTrue(array.Cut(new Interval(6, 10), out lo, out lv, out hi, out hv));
  1368. Assert.IsTrue(lv && hv);
  1369. Assert.AreEqual(12, hi);
  1370. Assert.AreEqual(4, lo);
  1371. for (int i = 0; i < 100; i++)
  1372. array.Add(2 * i);
  1373. array.Cut(new Interval(77, 105), out lo, out lv, out hi, out hv);
  1374. Assert.IsTrue(lv && hv);
  1375. Assert.AreEqual(106, hi);
  1376. Assert.AreEqual(76, lo);
  1377. array.Cut(new Interval(5, 7), out lo, out lv, out hi, out hv);
  1378. Assert.IsTrue(lv && hv);
  1379. Assert.AreEqual(8, hi);
  1380. Assert.AreEqual(4, lo);
  1381. array.Cut(new Interval(80, 110), out lo, out lv, out hi, out hv);
  1382. Assert.IsTrue(lv && hv);
  1383. Assert.AreEqual(112, hi);
  1384. Assert.AreEqual(78, lo);
  1385. }
  1386. [Test]
  1387. public void UpperCut()
  1388. {
  1389. for (int i = 0; i < 10; i++)
  1390. array.Add(i);
  1391. int l, h;
  1392. bool lv, hv;
  1393. Assert.IsFalse(array.Cut(new CubeRoot(1000), out l, out lv, out h, out hv));
  1394. Assert.IsTrue(lv && !hv);
  1395. Assert.AreEqual(9, l);
  1396. Assert.IsFalse(array.Cut(new CubeRoot(-50), out l, out lv, out h, out hv));
  1397. Assert.IsTrue(!lv && hv);
  1398. Assert.AreEqual(0, h);
  1399. }
  1400. [TearDown]
  1401. public void Dispose() { ic = null; array = null; }
  1402. }
  1403. }
  1404. namespace MultiOps
  1405. {
  1406. [TestFixture]
  1407. public class AddAll
  1408. {
  1409. private int sqr(int i) { return i * i; }
  1410. SortedArray<int> array;
  1411. [SetUp]
  1412. public void Init() { array = new SortedArray<int>(new IC()); }
  1413. [Test]
  1414. public void EmptyEmpty()
  1415. {
  1416. array.AddAll(new FunEnumerable(0, new Fun<int,int>(sqr)));
  1417. Assert.AreEqual(0, array.Count);
  1418. Assert.IsTrue(array.Check());
  1419. }
  1420. [Test]
  1421. public void SomeEmpty()
  1422. {
  1423. for (int i = 4; i < 9; i++) array.Add(i);
  1424. array.AddAll(new FunEnumerable(0, new Fun<int,int>(sqr)));
  1425. Assert.AreEqual(5, array.Count);
  1426. Assert.IsTrue(array.Check());
  1427. }
  1428. [Test]
  1429. public void EmptySome()
  1430. {
  1431. array.AddAll(new FunEnumerable(4, new Fun<int,int>(sqr)));
  1432. Assert.AreEqual(4, array.Count);
  1433. Assert.IsTrue(array.Check());
  1434. Assert.AreEqual(0, array[0]);
  1435. Assert.AreEqual(1, array[1]);
  1436. Assert.AreEqual(4, array[2]);
  1437. Assert.AreEqual(9, array[3]);
  1438. }
  1439. [Test]
  1440. public void SomeSome()
  1441. {
  1442. for (int i = 3; i < 9; i++) array.Add(i);
  1443. array.AddAll(new FunEnumerable(4, new Fun<int,int>(sqr)));
  1444. Assert.AreEqual(9, array.Count);
  1445. Assert.IsTrue(array.Check());
  1446. Assert.IsTrue(IC.eq(array, 0, 1, 3,4, 5, 6, 7, 8, 9));
  1447. }
  1448. [TearDown]
  1449. public void Dispose() { array = null; }
  1450. }
  1451. [TestFixture]
  1452. public class AddSorted
  1453. {
  1454. private int sqr(int i) { return i * i; }
  1455. private int bad(int i) { return i * (5 - i); }
  1456. SortedArray<int> array;
  1457. [SetUp]
  1458. public void Init() { array = new SortedArray<int>(new IC()); }
  1459. [Test]
  1460. public void EmptyEmpty()
  1461. {
  1462. array.AddSorted(new FunEnumerable(0, new Fun<int,int>(sqr)));
  1463. Assert.AreEqual(0, array.Count);
  1464. Assert.IsTrue(array.Check());
  1465. }
  1466. [Test]
  1467. public void SomeEmpty()
  1468. {
  1469. for (int i = 4; i < 9; i++) array.Add(i);
  1470. array.AddSorted(new FunEnumerable(0, new Fun<int,int>(sqr)));
  1471. Assert.AreEqual(5, array.Count);
  1472. Assert.IsTrue(array.Check());
  1473. }
  1474. [Test]
  1475. public void EmptySome()
  1476. {
  1477. array.AddSorted(new FunEnumerable(4, new Fun<int,int>(sqr)));
  1478. Assert.AreEqual(4, array.Count);
  1479. Assert.IsTrue(array.Check());
  1480. Assert.AreEqual(0, array[0]);
  1481. Assert.AreEqual(1, array[1]);
  1482. Assert.AreEqual(4, array[2]);
  1483. Assert.AreEqual(9, array[3]);
  1484. }
  1485. [Test]
  1486. public void SomeSome()
  1487. {
  1488. for (int i = 3; i < 9; i++) array.Add(i);
  1489. array.AddSorted(new FunEnumerable(4, new Fun<int,int>(sqr)));
  1490. Assert.AreEqual(9, array.Count);
  1491. Assert.IsTrue(array.Check());
  1492. Assert.IsTrue(IC.eq(array, 0, 1, 3, 4, 5, 6, 7, 8, 9));
  1493. }
  1494. [Test]
  1495. [ExpectedException(typeof(ArgumentException), ExpectedMessage="Argument not sorted")]
  1496. public void EmptyBad()
  1497. {
  1498. array.AddSorted(new FunEnumerable(9, new Fun<int,int>(bad)));
  1499. }
  1500. [TearDown]
  1501. public void Dispose() { array = null; }
  1502. }
  1503. [TestFixture]
  1504. public class Rest
  1505. {
  1506. SortedArray<int> array, array2;
  1507. [SetUp]
  1508. public void Init()
  1509. {
  1510. array = new SortedArray<int>(new IC());
  1511. array2 = new SortedArray<int>(new IC());
  1512. for (int i = 0; i < 10; i++)
  1513. array.Add(i);
  1514. for (int i = 0; i < 10; i++)
  1515. array2.Add(2 * i);
  1516. }
  1517. [Test]
  1518. public void RemoveAll()
  1519. {
  1520. array.RemoveAll(array2.RangeFromTo(3, 7));
  1521. Assert.AreEqual(8, array.Count);
  1522. Assert.IsTrue(array.Check());
  1523. Assert.IsTrue(IC.eq(array, 0, 1, 2, 3, 5, 7, 8, 9));
  1524. array.RemoveAll(array2.RangeFromTo(3, 7));
  1525. Assert.AreEqual(8, array.Count);
  1526. Assert.IsTrue(array.Check());
  1527. Assert.IsTrue(IC.eq(array, 0, 1, 2, 3, 5, 7, 8, 9));
  1528. array.RemoveAll(array2.RangeFromTo(13, 17));
  1529. Assert.AreEqual(8, array.Count);
  1530. Assert.IsTrue(array.Check());
  1531. Assert.IsTrue(IC.eq(array, 0, 1, 2, 3, 5, 7, 8, 9));
  1532. array.RemoveAll(array2.RangeFromTo(3, 17));
  1533. Assert.AreEqual(7, array.Count);
  1534. Assert.IsTrue(array.Check());
  1535. Assert.IsTrue(IC.eq(array, 0, 1, 2, 3, 5, 7, 9));
  1536. for (int i = 0; i < 10; i++) array2.Add(i);
  1537. array.RemoveAll(array2.RangeFromTo(-1, 10));
  1538. Assert.AreEqual(0, array.Count);
  1539. Assert.IsTrue(array.Check());
  1540. Assert.IsTrue(IC.eq(array));
  1541. }
  1542. [Test]
  1543. public void RetainAll()
  1544. {
  1545. array.RetainAll(array2.RangeFromTo(3, 17));
  1546. Assert.AreEqual(3, array.Count);
  1547. Assert.IsTrue(array.Check());
  1548. Assert.IsTrue(IC.eq(array, 4, 6, 8));
  1549. array.RetainAll(array2.RangeFromTo(1, 17));
  1550. Assert.AreEqual(3, array.Count);
  1551. Assert.IsTrue(array.Check());
  1552. Assert.IsTrue(IC.eq(array, 4, 6, 8));
  1553. array.RetainAll(array2.RangeFromTo(3, 5));
  1554. Assert.AreEqual(1, array.Count);
  1555. Assert.IsTrue(array.Check());
  1556. Assert.IsTrue(IC.eq(array, 4));
  1557. array.RetainAll(array2.RangeFromTo(7, 17));
  1558. Assert.AreEqual(0, array.Count);
  1559. Assert.IsTrue(array.Check());
  1560. Assert.IsTrue(IC.eq(array));
  1561. for (int i = 0; i < 10; i++) array.Add(i);
  1562. array.RetainAll(array2.RangeFromTo(5, 5));
  1563. Assert.AreEqual(0, array.Count);
  1564. Assert.IsTrue(array.Check());
  1565. Assert.IsTrue(IC.eq(array));
  1566. for (int i = 0; i < 10; i++) array.Add(i);
  1567. array.RetainAll(array2.RangeFromTo(15, 25));
  1568. Assert.AreEqual(0, array.Count);
  1569. Assert.IsTrue(array.Check());
  1570. Assert.IsTrue(IC.eq(array));
  1571. }
  1572. [Test]
  1573. public void ContainsAll()
  1574. {
  1575. Assert.IsFalse(array.ContainsAll(array2));
  1576. Assert.IsTrue(array.ContainsAll(array));
  1577. array2.Clear();
  1578. Assert.IsTrue(array.ContainsAll(array2));
  1579. array.Clear();
  1580. Assert.IsTrue(array.ContainsAll(array2));
  1581. array2.Add(8);
  1582. Assert.IsFalse(array.ContainsAll(array2));
  1583. }
  1584. [Test]
  1585. public void RemoveInterval()
  1586. {
  1587. array.RemoveInterval(3, 4);
  1588. Assert.IsTrue(array.Check());
  1589. Assert.AreEqual(6, array.Count);
  1590. Assert.IsTrue(IC.eq(array, 0, 1, 2, 7, 8, 9));
  1591. array.RemoveInterval(2, 3);
  1592. Assert.IsTrue(array.Check());
  1593. Assert.AreEqual(3, array.Count);
  1594. Assert.IsTrue(IC.eq(array, 0, 1, 9));
  1595. array.RemoveInterval(0, 3);
  1596. Assert.IsTrue(array.Check());
  1597. Assert.AreEqual(0, array.Count);
  1598. Assert.IsTrue(IC.eq(array));
  1599. }
  1600. [Test]
  1601. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  1602. public void RemoveRangeBad1()
  1603. {
  1604. array.RemoveInterval(-3, 8);
  1605. }
  1606. [Test]
  1607. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  1608. public void RemoveRangeBad2()
  1609. {
  1610. array.RemoveInterval(3, -8);
  1611. }
  1612. [Test]
  1613. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  1614. public void RemoveRangeBad3()
  1615. {
  1616. array.RemoveInterval(3, 8);
  1617. }
  1618. [Test]
  1619. public void GetRange()
  1620. {
  1621. SCG.IEnumerable<int> e = array[3, 3];
  1622. Assert.IsTrue(IC.eq(e, 3, 4, 5));
  1623. e = array[3, 0];
  1624. Assert.IsTrue(IC.eq(e));
  1625. }
  1626. [Test]
  1627. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  1628. public void GetRangeBad1()
  1629. {
  1630. object foo = array[-3, 0];
  1631. }
  1632. [Test]
  1633. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  1634. public void GetRangeBad2()
  1635. {
  1636. object foo = array[3, -1];
  1637. }
  1638. [Test]
  1639. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  1640. public void GetRangeBad3()
  1641. {
  1642. object foo = array[3, 8];
  1643. }
  1644. [TearDown]
  1645. public void Dispose() { array = null; array2 = null; }
  1646. }

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