TreeDictionaryTests.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using NUnit.Framework;
  2. using Ryujinx.Common.Collections;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Ryujinx.Tests.Collections
  6. {
  7. class TreeDictionaryTests
  8. {
  9. [Test]
  10. public void EnsureAddIntegrity()
  11. {
  12. TreeDictionary<int, int> dictionary = new TreeDictionary<int, int>();
  13. Assert.AreEqual(dictionary.Count, 0);
  14. dictionary.Add(2, 7);
  15. dictionary.Add(1, 4);
  16. dictionary.Add(10, 2);
  17. dictionary.Add(4, 1);
  18. dictionary.Add(3, 2);
  19. dictionary.Add(11, 2);
  20. dictionary.Add(5, 2);
  21. Assert.AreEqual(dictionary.Count, 7);
  22. List<KeyValuePair<int, int>> list = dictionary.AsLevelOrderList();
  23. /*
  24. * Tree Should Look as Follows After Rotations
  25. *
  26. * 2
  27. * 1 4
  28. * 3 10
  29. * 5 11
  30. *
  31. */
  32. Assert.AreEqual(list.Count, dictionary.Count);
  33. Assert.AreEqual(list[0].Key, 2);
  34. Assert.AreEqual(list[1].Key, 1);
  35. Assert.AreEqual(list[2].Key, 4);
  36. Assert.AreEqual(list[3].Key, 3);
  37. Assert.AreEqual(list[4].Key, 10);
  38. Assert.AreEqual(list[5].Key, 5);
  39. Assert.AreEqual(list[6].Key, 11);
  40. }
  41. [Test]
  42. public void EnsureRemoveIntegrity()
  43. {
  44. TreeDictionary<int, int> dictionary = new TreeDictionary<int, int>();
  45. Assert.AreEqual(dictionary.Count, 0);
  46. dictionary.Add(2, 7);
  47. dictionary.Add(1, 4);
  48. dictionary.Add(10, 2);
  49. dictionary.Add(4, 1);
  50. dictionary.Add(3, 2);
  51. dictionary.Add(11, 2);
  52. dictionary.Add(5, 2);
  53. dictionary.Add(7, 2);
  54. dictionary.Add(9, 2);
  55. dictionary.Add(8, 2);
  56. dictionary.Add(13, 2);
  57. dictionary.Add(24, 2);
  58. dictionary.Add(6, 2);
  59. Assert.AreEqual(dictionary.Count, 13);
  60. List<KeyValuePair<int, int>> list = dictionary.AsLevelOrderList();
  61. /*
  62. * Tree Should Look as Follows After Rotations
  63. *
  64. * 4
  65. * 2 10
  66. * 1 3 7 13
  67. * 5 9 11 24
  68. * 6 8
  69. */
  70. foreach (KeyValuePair<int, int> node in list)
  71. {
  72. Console.WriteLine($"{node.Key} -> {node.Value}");
  73. }
  74. Assert.AreEqual(list.Count, dictionary.Count);
  75. Assert.AreEqual(list[0].Key, 4);
  76. Assert.AreEqual(list[1].Key, 2);
  77. Assert.AreEqual(list[2].Key, 10);
  78. Assert.AreEqual(list[3].Key, 1);
  79. Assert.AreEqual(list[4].Key, 3);
  80. Assert.AreEqual(list[5].Key, 7);
  81. Assert.AreEqual(list[6].Key, 13);
  82. Assert.AreEqual(list[7].Key, 5);
  83. Assert.AreEqual(list[8].Key, 9);
  84. Assert.AreEqual(list[9].Key, 11);
  85. Assert.AreEqual(list[10].Key, 24);
  86. Assert.AreEqual(list[11].Key, 6);
  87. Assert.AreEqual(list[12].Key, 8);
  88. list.Clear();
  89. dictionary.Remove(7);
  90. /*
  91. * Tree Should Look as Follows After Removal
  92. *
  93. * 4
  94. * 2 10
  95. * 1 3 6 13
  96. * 5 9 11 24
  97. * 8
  98. */
  99. list = dictionary.AsLevelOrderList();
  100. foreach (KeyValuePair<int, int> node in list)
  101. {
  102. Console.WriteLine($"{node.Key} -> {node.Value}");
  103. }
  104. Assert.AreEqual(list[0].Key, 4);
  105. Assert.AreEqual(list[1].Key, 2);
  106. Assert.AreEqual(list[2].Key, 10);
  107. Assert.AreEqual(list[3].Key, 1);
  108. Assert.AreEqual(list[4].Key, 3);
  109. Assert.AreEqual(list[5].Key, 6);
  110. Assert.AreEqual(list[6].Key, 13);
  111. Assert.AreEqual(list[7].Key, 5);
  112. Assert.AreEqual(list[8].Key, 9);
  113. Assert.AreEqual(list[9].Key, 11);
  114. Assert.AreEqual(list[10].Key, 24);
  115. Assert.AreEqual(list[11].Key, 8);
  116. list.Clear();
  117. dictionary.Remove(10);
  118. list = dictionary.AsLevelOrderList();
  119. /*
  120. * Tree Should Look as Follows After Removal
  121. *
  122. * 4
  123. * 2 9
  124. * 1 3 6 13
  125. * 5 8 11 24
  126. *
  127. */
  128. foreach (KeyValuePair<int, int> node in list)
  129. {
  130. Console.WriteLine($"{node.Key} -> {node.Value}");
  131. }
  132. Assert.AreEqual(list[0].Key, 4);
  133. Assert.AreEqual(list[1].Key, 2);
  134. Assert.AreEqual(list[2].Key, 9);
  135. Assert.AreEqual(list[3].Key, 1);
  136. Assert.AreEqual(list[4].Key, 3);
  137. Assert.AreEqual(list[5].Key, 6);
  138. Assert.AreEqual(list[6].Key, 13);
  139. Assert.AreEqual(list[7].Key, 5);
  140. Assert.AreEqual(list[8].Key, 8);
  141. Assert.AreEqual(list[9].Key, 11);
  142. Assert.AreEqual(list[10].Key, 24);
  143. }
  144. [Test]
  145. public void EnsureOverwriteIntegrity()
  146. {
  147. TreeDictionary<int, int> dictionary = new TreeDictionary<int, int>();
  148. Assert.AreEqual(dictionary.Count, 0);
  149. dictionary.Add(2, 7);
  150. dictionary.Add(1, 4);
  151. dictionary.Add(10, 2);
  152. dictionary.Add(4, 1);
  153. dictionary.Add(3, 2);
  154. dictionary.Add(11, 2);
  155. dictionary.Add(5, 2);
  156. dictionary.Add(7, 2);
  157. dictionary.Add(9, 2);
  158. dictionary.Add(8, 2);
  159. dictionary.Add(13, 2);
  160. dictionary.Add(24, 2);
  161. dictionary.Add(6, 2);
  162. Assert.AreEqual(dictionary.Count, 13);
  163. List<KeyValuePair<int, int>> list = dictionary.AsLevelOrderList();
  164. foreach (KeyValuePair<int, int> node in list)
  165. {
  166. Console.WriteLine($"{node.Key} -> {node.Value}");
  167. }
  168. /*
  169. * Tree Should Look as Follows After Rotations
  170. *
  171. * 4
  172. * 2 10
  173. * 1 3 7 13
  174. * 5 9 11 24
  175. * 6 8
  176. */
  177. Assert.AreEqual(list.Count, dictionary.Count);
  178. Assert.AreEqual(list[0].Key, 4);
  179. Assert.AreEqual(list[1].Key, 2);
  180. Assert.AreEqual(list[2].Key, 10);
  181. Assert.AreEqual(list[3].Key, 1);
  182. Assert.AreEqual(list[4].Key, 3);
  183. Assert.AreEqual(list[5].Key, 7);
  184. Assert.AreEqual(list[6].Key, 13);
  185. Assert.AreEqual(list[7].Key, 5);
  186. Assert.AreEqual(list[8].Key, 9);
  187. Assert.AreEqual(list[9].Key, 11);
  188. Assert.AreEqual(list[10].Key, 24);
  189. Assert.AreEqual(list[11].Key, 6);
  190. Assert.AreEqual(list[12].Key, 8);
  191. Assert.AreEqual(list[4].Value, 2);
  192. dictionary.Add(3, 4);
  193. list = dictionary.AsLevelOrderList();
  194. Assert.AreEqual(list[4].Value, 4);
  195. // Assure that none of the nodes locations have been modified.
  196. Assert.AreEqual(list[0].Key, 4);
  197. Assert.AreEqual(list[1].Key, 2);
  198. Assert.AreEqual(list[2].Key, 10);
  199. Assert.AreEqual(list[3].Key, 1);
  200. Assert.AreEqual(list[4].Key, 3);
  201. Assert.AreEqual(list[5].Key, 7);
  202. Assert.AreEqual(list[6].Key, 13);
  203. Assert.AreEqual(list[7].Key, 5);
  204. Assert.AreEqual(list[8].Key, 9);
  205. Assert.AreEqual(list[9].Key, 11);
  206. Assert.AreEqual(list[10].Key, 24);
  207. Assert.AreEqual(list[11].Key, 6);
  208. Assert.AreEqual(list[12].Key, 8);
  209. }
  210. }
  211. }