UnitTest1.cs 12 KB

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