PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/mcs/class/Mono.C5/Test/trees/RedBlackTreeSetTests.cs

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

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