UnitTest1.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. /*
  256. [TestClass]
  257. public class FileImporterTests
  258. {
  259. [DataTestMethod]
  260. [DataRow(new int[] { 54, 43, 57, 11, 145, 34, 113, 299, 0 }, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 })]
  261. [DataRow(new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 })]
  262. [DataRow(new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, new int[] { 54, 43, 57, 11, 145, 34, 113, 199, 0 })]
  263. public void ParseISADInputSuccessfulTest(int[] xCoordinates, int[] yCoordinates)
  264. {
  265. Form1 program = new Form1();
  266. FileImporter uut = new SketchAssistant.FileImporter();
  267. List<String> file = new List<string>();
  268. file.Add("drawing");
  269. file.Add("300x200");
  270. for (int i = 0; i < xCoordinates.Length - 2; i += 3)
  271. {
  272. file.Add("line");
  273. file.Add(xCoordinates[i] + ";" + yCoordinates[i]);
  274. file.Add(xCoordinates[i + 1] + ";" + yCoordinates[i + 1]);
  275. file.Add(xCoordinates[i + 2] + ";" + yCoordinates[i + 2]);
  276. file.Add("endline");
  277. }
  278. file.Add("enddrawing");
  279. (int, int, List<Line>) values = uut.ParseISADInputForTesting(file.ToArray());
  280. program.CreateCanvasAndSetPictureForTesting(values.Item1, values.Item2, values.Item3);
  281. Line[] drawing = GetLeftImage(program).ToArray();
  282. Assert.AreEqual(xCoordinates.Length / 3, drawing.Length);
  283. for (int i = 0; i < xCoordinates.Length - 2; i += 3)
  284. {
  285. Point[] currentLine = drawing[i / 3].GetPoints().ToArray();
  286. Assert.AreEqual(3, currentLine.Length);
  287. for (int j = 0; j < 3; j++)
  288. {
  289. Assert.IsTrue(currentLine[j].X == xCoordinates[i + j] && currentLine[j].Y == yCoordinates[i + j]);
  290. }
  291. }
  292. }
  293. [DataTestMethod]
  294. [DataRow(new String[] {})]
  295. [DataRow(new String[] { "begindrawing", "300x300", "line", "50;50", "100;50", "endline", "enddrawing" })]
  296. [DataRow(new String[] { "drawing", "300;300", "line", "50;50", "100;50", "endline", "enddrawing" })]
  297. [DataRow(new String[] { "drawing", "30.5x300", "line", "50;50", "100;50", "endline", "enddrawing" })]
  298. [DataRow(new String[] { "drawing", "line", "50;50", "100;50", "endline", "enddrawing" })]
  299. [DataRow(new String[] { "drawing", "300x300", "beginline", "50;50", "100;50", "endline", "enddrawing" })]
  300. [DataRow(new String[] { "drawing", "300x300", "line", "500;50", "100;50", "endline", "enddrawing" })]
  301. [DataRow(new String[] { "drawing", "300x300", "line", "50x50", "100;50", "endline", "enddrawing" })]
  302. [DataRow(new String[] { "drawing", "300x300", "line", "50", "100", "endline", "enddrawing" })]
  303. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "line", "endline", "enddrawing" })]
  304. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "stopline", "enddrawing" })]
  305. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "enddrawing" })]
  306. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "endline", "endrawing" })]
  307. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "endline" })]
  308. public void ParseISADInputExceptionTest(String[] file)
  309. {
  310. bool exceptionThrown = false;
  311. Form1 program = new Form1();
  312. FileImporter uut = new SketchAssistant.FileImporter();
  313. //check that left image initially is uninitialized
  314. Assert.IsNull(GetLeftImage(program));
  315. //initialize left image with a valid isad drawing
  316. (int, int, List<Line>) values = uut.ParseISADInputForTesting(new string[] { "drawing", "300x205", "line", "40;40", "140;140", "endline", "enddrawing" });
  317. program.CreateCanvasAndSetPictureForTesting(values.Item1, values.Item2, values.Item3);
  318. //save left image for later comparison
  319. List<Line> oldLeftImage = GetLeftImage(program);
  320. try
  321. {
  322. //try to initialize the left image with an invalid isad drawing
  323. (int, int, List<Line>) values1 = uut.ParseISADInputForTesting(file);
  324. program.CreateCanvasAndSetPictureForTesting(values1.Item1, values1.Item2, values1.Item3);
  325. }
  326. catch(FileImporterException)
  327. {
  328. //save the occurence of an exception
  329. exceptionThrown = true;
  330. }
  331. //check that an exception has been thrown
  332. Assert.IsTrue(exceptionThrown);
  333. //check that the left image has not been changed by the failed image import
  334. Assert.AreEqual(oldLeftImage, GetLeftImage(program));
  335. }
  336. /// <summary>
  337. /// local helper method retrieving the left image from a Form1 instance
  338. /// </summary>
  339. /// <returns>the left image of the given Form1 instance</returns>
  340. private List<Line> GetLeftImage(Form1 program)
  341. {
  342. //cast is save as long as Form1#GetAllVariables() is conform to its contract
  343. return (List<Line>) program.GetAllVariables()[4];
  344. }
  345. }
  346. */
  347. }