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

/branches/StyleInterfaces/src/GeoAPI.Tests/DataStructures/Collections/Generic/TreeListTests.cs

http://GeoAPI.codeplex.com
C# | 424 lines | 312 code | 82 blank | 30 comment | 20 complexity | 281a852d84d4bfaeabe22bfac52c7fd8 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using GeoAPI.DataStructures.Collections.Generic;
  6. using NPack;
  7. using NUnit.Framework;
  8. namespace GeoAPI.Tests.DataStructures.Collections.Generic
  9. {
  10. [TestFixture]
  11. public class TreeListTests
  12. {
  13. private Random _rnd = new MersenneTwister();
  14. [Test]
  15. public void CreateDefaultTreeListSucceeds()
  16. {
  17. TreeList<Double> tree = new TreeList<Double>();
  18. Assert.IsNotNull(tree);
  19. }
  20. public class ReverseTestComparer : IComparer<Double>
  21. {
  22. #region IComparer<Double> Members
  23. public Int32 Compare(Double x, Double y)
  24. {
  25. return x.CompareTo(y) * -1;
  26. }
  27. #endregion
  28. }
  29. [Test]
  30. public void CreateTreeListWithCustomComparerSucceeds()
  31. {
  32. TreeList<Double> tree = new TreeList<Double>(new ReverseTestComparer());
  33. tree.Add(0);
  34. tree.Add(1);
  35. tree.Add(2);
  36. Assert.AreEqual(0, tree.IndexOf(2));
  37. }
  38. [Test]
  39. public void AddSucceeds()
  40. {
  41. TreeList<Double> tree = new TreeList<Double>();
  42. tree.Add(0);
  43. Assert.AreEqual(1, tree.Count);
  44. tree.Add(1);
  45. Assert.AreEqual(2, tree.Count);
  46. tree.Add(2);
  47. Assert.AreEqual(3, tree.Count);
  48. }
  49. [Test]
  50. [ExpectedException(typeof(ArgumentException))]
  51. public void AddWithExistingFails()
  52. {
  53. TreeList<Double> tree = new TreeList<Double>();
  54. tree.Add(0);
  55. tree.Add(0);
  56. }
  57. [Test]
  58. public void AddWithoutThrowSucceeds()
  59. {
  60. TreeList<Double> tree = new TreeList<Double>();
  61. tree.Add(0);
  62. tree.Add(0, false);
  63. Assert.AreEqual(1, tree.Count);
  64. }
  65. [Test]
  66. public void ClearSucceeds()
  67. {
  68. TreeList<Double> tree = new TreeList<Double>();
  69. for (Int32 i = 0; i < 10000; i++)
  70. {
  71. tree.Add(_rnd.NextDouble(), false);
  72. }
  73. Assert.Greater(tree.Count, 0);
  74. tree.Clear();
  75. Assert.AreEqual(0, tree.Count);
  76. }
  77. [Test]
  78. public void ContainsSucceeds()
  79. {
  80. TreeList<Double> tree = new TreeList<Double>();
  81. tree.Add(0.1);
  82. Assert.IsTrue(tree.Contains(0.1));
  83. for (Int32 i = 0; i < 10000; i++)
  84. {
  85. tree.Add(_rnd.NextDouble(), false);
  86. }
  87. Assert.IsTrue(tree.Contains(0.1));
  88. }
  89. [Test]
  90. public void CountBeforeSucceeds()
  91. {
  92. TreeList<Double> tree = new TreeList<Double>();
  93. // 10001 elements
  94. for (Int32 i = 0; i <= 10000; i++)
  95. {
  96. tree.Add(i);
  97. }
  98. // 5000 should be the middle element
  99. Assert.AreEqual(5000, tree.CountBefore(5000.0));
  100. Assert.AreEqual(0, tree.CountBefore(0));
  101. Assert.AreEqual(10000, tree.CountBefore(10000));
  102. }
  103. [Test]
  104. public void CountAfterSucceeds()
  105. {
  106. TreeList<Double> tree = new TreeList<Double>();
  107. // 10001 elements
  108. for (Int32 i = 0; i <= 10000; i++)
  109. {
  110. tree.Add(i);
  111. }
  112. // 5000 should be the middle element
  113. Assert.AreEqual(5000, tree.CountAfter(5000.0));
  114. Assert.AreEqual(0, tree.CountAfter(10000));
  115. Assert.AreEqual(10000, tree.CountAfter(0));
  116. }
  117. [Test]
  118. public void RemoveSucceeds()
  119. {
  120. TreeList<Double> tree = new TreeList<Double>();
  121. // 10001 elements
  122. for (Int32 i = 0; i <= 10000; i++)
  123. {
  124. tree.Add(i);
  125. }
  126. Assert.AreEqual(10001, tree.Count);
  127. for (Int32 i = 0; i <= 10000; i++)
  128. {
  129. tree.Remove(i);
  130. Assert.IsFalse(tree.Contains(i));
  131. }
  132. Assert.AreEqual(0, tree.Count);
  133. }
  134. [Test]
  135. public void RemoveAtSucceeds()
  136. {
  137. TreeList<Double> tree = new TreeList<Double>();
  138. for (Int32 i = 0; i < 3; i++)
  139. {
  140. tree.Add(i);
  141. }
  142. tree.RemoveAt(0);
  143. Assert.AreEqual(2, tree.Count);
  144. Assert.AreEqual(0, tree.IndexOf(1));
  145. tree.RemoveAt(0);
  146. Assert.AreEqual(1, tree.Count);
  147. Assert.AreEqual(0, tree.IndexOf(2));
  148. tree = new TreeList<Double>();
  149. for (Int32 i = 0; i < 15; i++)
  150. {
  151. tree.Add(i);
  152. }
  153. //tree.Print(Console.Out);
  154. tree.RemoveAt(0);
  155. Assert.AreEqual(14, tree.Count);
  156. Assert.AreEqual(0, tree.IndexOf(1));
  157. //tree.Print(Console.Out);
  158. tree.RemoveAt(7);
  159. Assert.AreEqual(13, tree.Count);
  160. Assert.AreEqual(7, tree.IndexOf(9));
  161. //tree.Print(Console.Out);
  162. tree.RemoveAt(9);
  163. Assert.AreEqual(12, tree.Count);
  164. Assert.AreEqual(9, tree.IndexOf(12));
  165. return;
  166. // TODO: the following test still finds an error in the removal.
  167. // On line 934 of TreeList.cs, parent.Right is null, and shouldn't be.
  168. tree.Clear();
  169. // 10001 elements
  170. for (Int32 i = 0; i <= 10000; i++)
  171. {
  172. tree.Add(i);
  173. }
  174. while (tree.Count > 0)
  175. {
  176. Int32 initialCount = tree.Count;
  177. Int32 index = _rnd.Next(initialCount);
  178. Debug.WriteLine(index);
  179. tree.RemoveAt(index);
  180. Assert.AreEqual(initialCount - 1, tree.Count);
  181. }
  182. }
  183. [Test]
  184. public void CopyToSucceeds()
  185. {
  186. TreeList<Double> tree = new TreeList<Double>();
  187. // 10001 elements
  188. for (Int32 i = 0; i <= 10000; i++)
  189. {
  190. tree.Add(i);
  191. }
  192. Double[] values = new Double[10001];
  193. tree.CopyTo(values, 0);
  194. for (Int32 i = 1; i < values.Length; i++)
  195. {
  196. Assert.Less(values[i - 1], values[i]);
  197. }
  198. }
  199. [Test]
  200. public void IsFixedSizeIsFalse()
  201. {
  202. TreeList<Double> tree = new TreeList<Double>();
  203. Assert.IsFalse(tree.IsFixedSize);
  204. }
  205. [Test]
  206. public void IsReadOnlyIsFalse()
  207. {
  208. TreeList<Double> tree = new TreeList<Double>();
  209. Assert.IsFalse(tree.IsReadOnly);
  210. }
  211. [Test]
  212. public void IsSynchronizedIsFalse()
  213. {
  214. TreeList<Double> tree = new TreeList<Double>();
  215. Assert.IsFalse(tree.IsSynchronized);
  216. }
  217. [Test]
  218. public void SyncRootIsThisObjectReference()
  219. {
  220. TreeList<Double> tree = new TreeList<Double>();
  221. Assert.AreSame(tree, tree.SyncRoot);
  222. }
  223. [Test]
  224. public void GetEnumeratorSucceeds()
  225. {
  226. TreeList<Double> tree = new TreeList<Double>();
  227. IEnumerator<Double> enumerator = tree.GetEnumerator();
  228. Assert.IsNotNull(enumerator);
  229. }
  230. [Test]
  231. public void GetEnumeratorIteratesInSortedOrder()
  232. {
  233. TreeList<Double> tree = new TreeList<Double>();
  234. // 10001 elements
  235. for (Int32 i = 0; i <= 10000; i++)
  236. {
  237. tree.Add(i);
  238. }
  239. Double previous = -1;
  240. foreach (Double current in tree)
  241. {
  242. Assert.Less(previous, current);
  243. previous = current;
  244. }
  245. }
  246. [Test]
  247. public void IndexerSucceeds()
  248. {
  249. TreeList<Int32> tree = new TreeList<Int32>();
  250. // 10001 elements
  251. for (Int32 i = 0; i <= 10000; i++)
  252. {
  253. tree.Add(i);
  254. }
  255. for (Int32 i = 0; i <= 10000; i++)
  256. {
  257. Assert.AreEqual(i, tree[i], "Indexer failed at index: " + i);
  258. }
  259. }
  260. [Test]
  261. public void IndexingIntoTreeWithNodeHavingNullRightChildSucceeds()
  262. {
  263. TreeList<Int32> tree = new TreeList<Int32>();
  264. // create a tree with the following shape:
  265. //
  266. // 1
  267. // / \
  268. // 0 3
  269. // /
  270. // 2
  271. //
  272. // There was a defect in the indexer where a NullReferenceException
  273. // as being thrown when the node '3' had a null right child. This was
  274. // fixed by coalescing with the left child on the right turn on line 126
  275. // of the indexer of TreeList`1 in rev. 17477. This test should pass
  276. // on this and later revisions.
  277. tree.Add(1);
  278. tree.Add(0);
  279. tree.Add(3);
  280. tree.Add(2);
  281. Assert.AreEqual(2, tree[2]);
  282. }
  283. [Test]
  284. public void CloneCreatesAShallowCopy()
  285. {
  286. TreeList<Object> tree = new TreeList<Object>();
  287. // 10001 elements
  288. for (Int32 i = 0; i <= 10000; i++)
  289. {
  290. tree.Add(i);
  291. }
  292. TreeList<Object> clone = tree.Clone() as TreeList<Object>;
  293. Assert.AreEqual(tree.Count, clone.Count);
  294. for (Int32 i = 0; i <= 10000; i++)
  295. {
  296. Assert.AreSame(tree[i], clone[i]);
  297. }
  298. }
  299. [Test]
  300. [ExpectedException(typeof(NotSupportedException))]
  301. public void InsertFails()
  302. {
  303. TreeList<Object> tree = new TreeList<Object>();
  304. tree.Insert(0, new Object());
  305. }
  306. [Test]
  307. [ExpectedException(typeof(NotSupportedException))]
  308. public void IndexerSetFails()
  309. {
  310. TreeList<Double> tree = new TreeList<Double>();
  311. tree.Add(0);
  312. tree[0] = 1;
  313. }
  314. [Test]
  315. [Ignore]
  316. public void PerformanceTest()
  317. {
  318. TreeList<Double> tree = new TreeList<Double>();
  319. // Ten million-ish elements (could be some dupes)
  320. Int32 count = 10000000;
  321. for (Int32 i = 0; i < count; i++)
  322. {
  323. tree.Add(_rnd.NextDouble(), false);
  324. }
  325. for (Int32 i = 0; i < count; i++)
  326. {
  327. Int32 index = tree.IndexOf(tree[i]);
  328. Assert.AreEqual(i, index);
  329. }
  330. // very probably lots of misses
  331. for (Int32 i = 0; i < count; i++)
  332. {
  333. tree.Remove(_rnd.NextDouble());
  334. }
  335. while(tree.Count > 0)
  336. {
  337. Int32 removeIndex = _rnd.Next(tree.Count);
  338. tree.RemoveAt(removeIndex);
  339. }
  340. }
  341. }
  342. }