UnitTest1.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. using NUnit.Framework;
  2. using System.Collections.Generic;
  3. using SketchAssistantWPF;
  4. using System;
  5. using System.Windows;
  6. namespace Tests
  7. {
  8. public class Tests
  9. {
  10. [SetUp]
  11. public void Setup()
  12. {
  13. }
  14. [Test]
  15. public void Test1()
  16. {
  17. Assert.AreEqual(true, true);
  18. }
  19. }
  20. /*
  21. public class LineTests
  22. {
  23. //========================//
  24. //= Bresenham Line Tests =//
  25. //========================//
  26. [Test]
  27. public void BresenhamLineTest1()
  28. {
  29. AvalonTestRunner.RunInSTA(
  30. delegate
  31. {
  32. //Test point
  33. List<System.Windows.Point> expectedResult = new List<System.Windows.Point>();
  34. expectedResult.Add(new System.Windows.Point(1, 2));
  35. List<System.Windows.Point> actualResult = GeometryCalculator.BresenhamLineAlgorithm(new System.Windows.Point(1, 2), new System.Windows.Point(1, 2));
  36. Assert.AreEqual(1, actualResult.Count);
  37. for (int i = 0; i < actualResult.Count; i++)
  38. {
  39. Assert.AreEqual(expectedResult[i], actualResult[i]);
  40. }
  41. });
  42. }
  43. [Test]
  44. public void BresenhamLineTest2()
  45. {
  46. //Test line going from left to right
  47. List<System.Windows.Point> expectedResult = new List<System.Windows.Point>();
  48. for (int i = 1; i <= 6; i++) { expectedResult.Add(new System.Windows.Point(i, 2)); }
  49. List<System.Windows.Point> actualResult = SketchAssistantWPF.GeometryCalculator.BresenhamLineAlgorithm(new System.Windows.Point(1, 2), new System.Windows.Point(6, 2));
  50. Assert.AreEqual(expectedResult.Count, actualResult.Count);
  51. for (int i = 0; i < actualResult.Count; i++)
  52. {
  53. Assert.AreEqual(expectedResult[i], actualResult[i]);
  54. }
  55. }
  56. [Test]
  57. public void BresenhamLineTest3()
  58. {
  59. //Test line going from right to left
  60. List<System.Windows.Point> expectedResult = new List<System.Windows.Point>();
  61. for (int i = 6; i >= 1; i--) { expectedResult.Add(new System.Windows.Point(i, 2)); }
  62. List<System.Windows.Point> actualResult = SketchAssistantWPF.GeometryCalculator.BresenhamLineAlgorithm(new System.Windows.Point(6, 2), new System.Windows.Point(1, 2));
  63. Assert.AreEqual(expectedResult.Count, actualResult.Count);
  64. for (int i = 0; i < actualResult.Count; i++)
  65. {
  66. Assert.AreEqual(expectedResult[i], actualResult[i]);
  67. }
  68. }
  69. [Test]
  70. public void BresenhamLineTest4()
  71. {
  72. //Test line going from top to bottom
  73. List<System.Windows.Point> expectedResult = new List<System.Windows.Point>();
  74. for (int i = 5; i <= 25; i++) { expectedResult.Add(new System.Windows.Point(7, i)); }
  75. List<System.Windows.Point> actualResult = SketchAssistantWPF.GeometryCalculator.BresenhamLineAlgorithm(new System.Windows.Point(7, 5), new System.Windows.Point(7, 25));
  76. Assert.AreEqual(expectedResult.Count, actualResult.Count);
  77. for (int i = 0; i < actualResult.Count; i++)
  78. {
  79. Assert.AreEqual(expectedResult[i], actualResult[i]);
  80. }
  81. }
  82. [Test]
  83. public void BresenhamLineTest5()
  84. {
  85. //Test line going from bottom to top
  86. List<System.Windows.Point> expectedResult = new List<System.Windows.Point>();
  87. for (int i = 25; i >= 5; i--) { expectedResult.Add(new System.Windows.Point(7, i)); }
  88. List<System.Windows.Point> actualResult = SketchAssistantWPF.GeometryCalculator.BresenhamLineAlgorithm(new System.Windows.Point(7, 25), new System.Windows.Point(7, 5));
  89. Assert.AreEqual(expectedResult.Count, actualResult.Count);
  90. for (int i = 0; i < actualResult.Count; i++)
  91. {
  92. Assert.AreEqual(expectedResult[i], actualResult[i]);
  93. }
  94. }
  95. [Test]
  96. public void BresenhamLineTest6()
  97. {
  98. //Test exactly diagonal line from top left to bottom right
  99. List<System.Windows.Point> expectedResult = new List<System.Windows.Point>();
  100. for (int i = 5; i <= 25; i++) { expectedResult.Add(new System.Windows.Point(i + 2, i)); }
  101. List<System.Windows.Point> actualResult = SketchAssistantWPF.GeometryCalculator.BresenhamLineAlgorithm(new System.Windows.Point(7, 5), new System.Windows.Point(27, 25));
  102. Assert.AreEqual(expectedResult.Count, actualResult.Count);
  103. for (int i = 0; i < actualResult.Count; i++)
  104. {
  105. Assert.AreEqual(expectedResult[i], actualResult[i]);
  106. }
  107. }
  108. [Test]
  109. public void BresenhamLineTest7()
  110. {
  111. //Test exactly diagonal line from bottom right to top left
  112. List<System.Windows.Point> expectedResult = new List<System.Windows.Point>();
  113. for (int i = 25; i >= 5; i--) { expectedResult.Add(new System.Windows.Point(i + 2, i)); }
  114. List<System.Windows.Point> actualResult = SketchAssistantWPF.GeometryCalculator.BresenhamLineAlgorithm(new System.Windows.Point(27, 25), new System.Windows.Point(7, 5));
  115. Assert.AreEqual(expectedResult.Count, actualResult.Count);
  116. for (int i = 0; i < actualResult.Count; i++)
  117. {
  118. Assert.AreEqual(expectedResult[i], actualResult[i]);
  119. }
  120. }
  121. //===========================//
  122. //= Matrix Population Tests =//
  123. //===========================//
  124. [Test]
  125. public void MatrixTest1()
  126. {
  127. //Populate Matrix for temporary Line
  128. List<System.Windows.Point> thePoints = new List<System.Windows.Point>();
  129. thePoints.Add(new System.Windows.Point(1, 1));
  130. thePoints.Add(new System.Windows.Point(1, 2));
  131. bool[,] testBoolMatrix = new bool[5, 5];
  132. List<int>[,] testLineMatrix = new List<int>[5, 5];
  133. bool[,] resultBoolMatrix = new bool[5, 5];
  134. HashSet<int>[,] resultLineMatrix = new HashSet<int>[5, 5];
  135. InternalLine testLine = new InternalLine(thePoints);
  136. testLine.PopulateMatrixes(resultBoolMatrix, resultLineMatrix);
  137. for (int i = 0; i < 5; i++)
  138. {
  139. for (int j = 0; j < 5; j++)
  140. {
  141. Assert.AreEqual(testBoolMatrix[i, j], resultBoolMatrix[i, j]);
  142. Assert.AreEqual(testLineMatrix[i, j], resultLineMatrix[i, j]);
  143. }
  144. }
  145. }
  146. [Test]
  147. public void MatrixTest2()
  148. {
  149. //Populate Matrix for non-temporary InternalLine
  150. List<System.Windows.Point> thePoints = new List<System.Windows.Point>();
  151. thePoints.Add(new System.Windows.Point(1, 1));
  152. thePoints.Add(new System.Windows.Point(3, 3));
  153. bool[,] testBoolMatrix = new bool[5, 5];
  154. HashSet<int>[,] testLineMatrix = new HashSet<int>[5, 5];
  155. for (int i = 1; i <= 3; i++)
  156. {
  157. testBoolMatrix[i, i] = true;
  158. HashSet<int> temp = new HashSet<int>();
  159. temp.Add(5);
  160. testLineMatrix[i, i] = temp;
  161. }
  162. bool[,] resultBoolMatrix = new bool[5, 5];
  163. HashSet<int>[,] resultLineMatrix = new HashSet<int>[5, 5];
  164. InternalLine testLine = new InternalLine(thePoints, 5);
  165. testLine.PopulateMatrixes(resultBoolMatrix, resultLineMatrix);
  166. for (int i = 0; i < 5; i++)
  167. {
  168. for (int j = 0; j < 5; j++)
  169. {
  170. Assert.AreEqual(testBoolMatrix[i, j], resultBoolMatrix[i, j]);
  171. if (testLineMatrix[i, j] != null && resultLineMatrix[i, j] != null)
  172. {
  173. for (int k = 0; k < resultLineMatrix[i, j].Count; k++)
  174. {
  175. Assert.AreEqual(true, testLineMatrix[i, j].SetEquals(resultLineMatrix[i, j]));
  176. }
  177. }
  178. }
  179. }
  180. }
  181. //=========================//
  182. //= Line Constructor Test =//
  183. //=========================//
  184. [Test]
  185. public void ConstructorTest()
  186. {
  187. //Create non-temporary Line and check points
  188. //reference Points
  189. List<System.Windows.Point> comparisonPoints = new List<System.Windows.Point> {new System.Windows.Point(2,2), new System.Windows.Point(3, 1), new System.Windows.Point(4, 1), new System.Windows.Point(5, 1), new System.Windows.Point(6, 2),
  190. new System.Windows.Point(7, 3), new System.Windows.Point(8, 4), new System.Windows.Point(9, 5), new System.Windows.Point(10, 6), new System.Windows.Point(11, 5), new System.Windows.Point(11, 4), new System.Windows.Point(11, 3),
  191. new System.Windows.Point(10, 2), new System.Windows.Point(9, 1), new System.Windows.Point(8, 2), new System.Windows.Point(7, 3), new System.Windows.Point(6, 4), new System.Windows.Point(5, 5), new System.Windows.Point(4, 5),
  192. new System.Windows.Point(3, 5), new System.Windows.Point(2, 5), new System.Windows.Point(1, 4)};
  193. //test Points, with intermediate points missing & duplicate points
  194. List<System.Windows.Point> testPoints = new List<System.Windows.Point> {new System.Windows.Point(2,2), new System.Windows.Point(3, 1), new System.Windows.Point(5, 1), new System.Windows.Point(5, 1), new System.Windows.Point(5, 1),
  195. new System.Windows.Point(8, 4), new System.Windows.Point(10, 6), new System.Windows.Point(11, 5), new System.Windows.Point(11, 3), new System.Windows.Point(9, 1), new System.Windows.Point(9, 1), new System.Windows.Point(9, 1),
  196. new System.Windows.Point(5, 5), new System.Windows.Point(2, 5), new System.Windows.Point(2, 5), new System.Windows.Point(1, 4) };
  197. InternalLine testLine = new InternalLine(testPoints, 0);
  198. List<System.Windows.Point> returnedPoints = testLine.GetPoints();
  199. Assert.AreEqual(comparisonPoints.Count, returnedPoints.Count);
  200. for (int i = 0; i < returnedPoints.Count; i++)
  201. {
  202. Assert.AreEqual(comparisonPoints[i], returnedPoints[i]);
  203. }
  204. }
  205. }
  206. */
  207. public class ActionHistoryTests
  208. {
  209. private ActionHistory GetActionHistory()
  210. {
  211. return new ActionHistory();
  212. }
  213. [TestCase(SketchAction.ActionType.Start, 5, -1, "A new canvas was created.")]
  214. [TestCase(SketchAction.ActionType.Draw, 5, 5, "Line number 5 was drawn.")]
  215. [TestCase(SketchAction.ActionType.Delete, 10, 10, "Line number 10 was deleted.")]
  216. public void ScetchActionTest1(SketchAction.ActionType type, int id, int exit, String response)
  217. {
  218. HashSet<int> actualResult = new HashSet<int>();
  219. if (!type.Equals(SketchAction.ActionType.Start)) { actualResult.Add(id); }
  220. SketchAction testAction = new SketchAction(type, id);
  221. Assert.AreEqual(type, testAction.GetActionType());
  222. Assert.AreEqual(true, actualResult.SetEquals(testAction.GetLineIDs()));
  223. Assert.AreEqual(response, testAction.GetActionInformation());
  224. }
  225. [TestCase(SketchAction.ActionType.Start, 1, 2, 3, "A new canvas was created.")]
  226. [TestCase(SketchAction.ActionType.Draw, 3, 3, 3, "Line number 3 was drawn.")]
  227. [TestCase(SketchAction.ActionType.Delete, 20, 30, 40, "Several Lines were deleted.")]
  228. public void ScetchActionTest2(SketchAction.ActionType type, int id1, int id2, int id3, String response)
  229. {
  230. HashSet<int> actualResult = new HashSet<int>();
  231. if (!type.Equals(SketchAction.ActionType.Start))
  232. {
  233. actualResult.Add(id1);
  234. actualResult.Add(id2);
  235. actualResult.Add(id3);
  236. }
  237. SketchAction testAction = new SketchAction(type, actualResult);
  238. Assert.AreEqual(type, testAction.GetActionType());
  239. Assert.AreEqual(true, actualResult.SetEquals(testAction.GetLineIDs()));
  240. Assert.AreEqual(response, testAction.GetActionInformation());
  241. }
  242. [TestCase(SketchAction.ActionType.Start, SketchAction.ActionType.Start, true)]
  243. [TestCase(SketchAction.ActionType.Draw, SketchAction.ActionType.Delete, false)]
  244. public void ActionHistoryTest1(SketchAction.ActionType action1, SketchAction.ActionType action2, bool isEmpty)
  245. {
  246. ActionHistory testHistory = GetActionHistory();
  247. if (!action1.Equals(SketchAction.ActionType.Start)) { testHistory.AddNewAction(new SketchAction(action1, 5)); }
  248. if (!action2.Equals(SketchAction.ActionType.Start)) { testHistory.AddNewAction(new SketchAction(action2, 5)); }
  249. Assert.AreEqual(isEmpty, testHistory.IsEmpty());
  250. }
  251. [TestCase(SketchAction.ActionType.Draw, "Last Action: Line number 0 was drawn.")]
  252. [TestCase(SketchAction.ActionType.Delete, "Last Action: Line number 0 was deleted.")]
  253. public void ActionHistoryUndoRedoTest(SketchAction.ActionType actionType, String message)
  254. {
  255. ActionHistory testHistory = GetActionHistory();
  256. SketchAction testAction = new SketchAction(actionType, 0);
  257. testHistory.AddNewAction(testAction);
  258. Assert.AreEqual(true, testHistory.CanUndo());
  259. testHistory.MoveAction(true);
  260. Assert.AreEqual(true, testHistory.CanRedo());
  261. var lastActionLabel = testHistory.MoveAction(false);
  262. Assert.AreEqual(actionType, testHistory.GetCurrentAction().GetActionType());
  263. Assert.AreEqual(message, lastActionLabel);
  264. }
  265. }
  266. }