UnitTest1.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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.Line.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.Line.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.Line.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.Line.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.Line.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.Line.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.Line.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. ToolStripStatusLabel testLabel = new ToolStripStatusLabel();
  195. private ActionHistory GetActionHistory()
  196. {
  197. return new ActionHistory(testLabel);
  198. }
  199. [DataTestMethod]
  200. [DataRow(SketchAction.ActionType.Start, 5, -1, "A new canvas was created.")]
  201. [DataRow(SketchAction.ActionType.Draw, 5, 5, "Line number 5 was drawn.")]
  202. [DataRow(SketchAction.ActionType.Delete, 10, 10, "Line number 10 was deleted.")]
  203. public void ScetchActionTest1(SketchAction.ActionType type, int id, int exit, String response)
  204. {
  205. HashSet<int> actualResult = new HashSet<int>();
  206. if (!type.Equals(SketchAction.ActionType.Start)) { actualResult.Add(id); }
  207. SketchAction testAction = new SketchAction(type, id);
  208. Assert.AreEqual(type, testAction.GetActionType());
  209. Assert.AreEqual(true, actualResult.SetEquals(testAction.GetLineIDs()));
  210. Assert.AreEqual(response, testAction.GetActionInformation());
  211. }
  212. [DataTestMethod]
  213. [DataRow(SketchAction.ActionType.Start, 1, 2, 3, "A new canvas was created.")]
  214. [DataRow(SketchAction.ActionType.Draw, 3, 3, 3, "Line number 3 was drawn.")]
  215. [DataRow(SketchAction.ActionType.Delete, 20, 30, 40, "Several Lines were deleted.")]
  216. public void ScetchActionTest2(SketchAction.ActionType type, int id1, int id2, int id3, String response)
  217. {
  218. HashSet<int> actualResult = new HashSet<int>();
  219. if (!type.Equals(SketchAction.ActionType.Start))
  220. {
  221. actualResult.Add(id1);
  222. actualResult.Add(id2);
  223. actualResult.Add(id3);
  224. }
  225. SketchAction testAction = new SketchAction(type, actualResult);
  226. Assert.AreEqual(type, testAction.GetActionType());
  227. Assert.AreEqual(true, actualResult.SetEquals(testAction.GetLineIDs()));
  228. Assert.AreEqual(response, testAction.GetActionInformation());
  229. }
  230. [DataTestMethod]
  231. [DataRow(SketchAction.ActionType.Start, SketchAction.ActionType.Start, true)]
  232. [DataRow(SketchAction.ActionType.Draw, SketchAction.ActionType.Delete, false)]
  233. public void ActionHistoryTest1(SketchAction.ActionType action1, SketchAction.ActionType action2, bool isEmpty)
  234. {
  235. ActionHistory testHistory = GetActionHistory();
  236. if (!action1.Equals(SketchAction.ActionType.Start)) { testHistory.AddNewAction(new SketchAction(action1, 5)); }
  237. if (!action2.Equals(SketchAction.ActionType.Start)) { testHistory.AddNewAction(new SketchAction(action2, 5)); }
  238. Assert.AreEqual(isEmpty, testHistory.IsEmpty());
  239. }
  240. [DataTestMethod]
  241. [DataRow(SketchAction.ActionType.Draw, "Last Action: Line number 0 was drawn.")]
  242. [DataRow(SketchAction.ActionType.Delete, "Last Action: Line number 0 was deleted.")]
  243. public void ActionHistoryUndoRedoTest(SketchAction.ActionType actionType, String message)
  244. {
  245. ActionHistory testHistory = GetActionHistory();
  246. SketchAction testAction = new SketchAction(actionType, 0);
  247. testHistory.AddNewAction(testAction);
  248. Assert.AreEqual(true, testHistory.CanUndo());
  249. testHistory.MoveAction(true);
  250. Assert.AreEqual(true, testHistory.CanRedo());
  251. testHistory.MoveAction(false);
  252. Assert.AreEqual(actionType, testHistory.GetCurrentAction().GetActionType());
  253. String currLabel = testLabel.Text;
  254. Assert.AreEqual(currLabel, message);
  255. }
  256. }
  257. [TestClass]
  258. public class FileImporterTests
  259. {
  260. [DataTestMethod]
  261. [DataRow(new int[] { 54, 43, 57, 11, 145, 34, 113, 299, 0 }, 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[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 })]
  263. [DataRow(new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, new int[] { 54, 43, 57, 11, 145, 34, 113, 199, 0 })]
  264. public void ParseISADInputSuccessfulTest(int[] xCoordinates, int[] yCoordinates)
  265. {
  266. Form1 program = new Form1();
  267. FileImporter uut = new SketchAssistant.FileImporter(program);
  268. List<String> file = new List<string>();
  269. file.Add("drawing");
  270. file.Add("300x200");
  271. for (int i = 0; i < xCoordinates.Length - 2; i += 3)
  272. {
  273. file.Add("line");
  274. file.Add(xCoordinates[i] + ";" + yCoordinates[i]);
  275. file.Add(xCoordinates[i + 1] + ";" + yCoordinates[i + 1]);
  276. file.Add(xCoordinates[i + 2] + ";" + yCoordinates[i + 2]);
  277. file.Add("endline");
  278. }
  279. file.Add("enddrawing");
  280. (int, int, List<Line>) values = uut.ParseISADInputForTesting(file.ToArray());
  281. program.CreateCanvasAndSetPictureForTesting(values.Item1, values.Item2, values.Item3);
  282. Line[] drawing = GetLeftImage(program).ToArray();
  283. Assert.AreEqual(xCoordinates.Length / 3, drawing.Length);
  284. for (int i = 0; i < xCoordinates.Length - 2; i += 3)
  285. {
  286. Point[] currentLine = drawing[i / 3].GetPoints().ToArray();
  287. Assert.AreEqual(3, currentLine.Length);
  288. for (int j = 0; j < 3; j++)
  289. {
  290. Assert.IsTrue(currentLine[j].X == xCoordinates[i + j] && currentLine[j].Y == yCoordinates[i + j]);
  291. }
  292. }
  293. }
  294. [DataTestMethod]
  295. [DataRow(new String[] {})]
  296. [DataRow(new String[] { "begindrawing", "300x300", "line", "50;50", "100;50", "endline", "enddrawing" })]
  297. [DataRow(new String[] { "drawing", "300;300", "line", "50;50", "100;50", "endline", "enddrawing" })]
  298. [DataRow(new String[] { "drawing", "30.5x300", "line", "50;50", "100;50", "endline", "enddrawing" })]
  299. [DataRow(new String[] { "drawing", "line", "50;50", "100;50", "endline", "enddrawing" })]
  300. [DataRow(new String[] { "drawing", "300x300", "beginline", "50;50", "100;50", "endline", "enddrawing" })]
  301. [DataRow(new String[] { "drawing", "300x300", "line", "500;50", "100;50", "endline", "enddrawing" })]
  302. [DataRow(new String[] { "drawing", "300x300", "line", "50x50", "100;50", "endline", "enddrawing" })]
  303. [DataRow(new String[] { "drawing", "300x300", "line", "50", "100", "endline", "enddrawing" })]
  304. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "line", "endline", "enddrawing" })]
  305. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "stopline", "enddrawing" })]
  306. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "enddrawing" })]
  307. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "endline", "endrawing" })]
  308. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "endline" })]
  309. public void ParseISADInputExceptionTest(String[] file)
  310. {
  311. bool exceptionThrown = false;
  312. Form1 program = new Form1();
  313. FileImporter uut = new SketchAssistant.FileImporter(program);
  314. //check that left image initially is uninitialized
  315. Assert.IsNull(GetLeftImage(program));
  316. //initialize left image with a valid isad drawing
  317. (int, int, List<Line>) values = uut.ParseISADInputForTesting(new string[] { "drawing", "300x205", "line", "40;40", "140;140", "endline", "enddrawing" });
  318. program.CreateCanvasAndSetPictureForTesting(values.Item1, values.Item2, values.Item3);
  319. //save left image for later comparison
  320. List<Line> oldLeftImage = GetLeftImage(program);
  321. try
  322. {
  323. //try to initialize the left image with an invalid isad drawing
  324. (int, int, List<Line>) values1 = uut.ParseISADInputForTesting(file);
  325. program.CreateCanvasAndSetPictureForTesting(values1.Item1, values1.Item2, values1.Item3);
  326. }
  327. catch(FileImporterException)
  328. {
  329. //save the occurence of an exception
  330. exceptionThrown = true;
  331. }
  332. //check that an exception has been thrown
  333. Assert.IsTrue(exceptionThrown);
  334. //check that the left image has not been changed by the failed image import
  335. Assert.AreEqual(oldLeftImage, GetLeftImage(program));
  336. }
  337. /// <summary>
  338. /// local helper method retrieving the left image from a Form1 instance
  339. /// </summary>
  340. /// <returns>the left image of the given Form1 instance</returns>
  341. private List<Line> GetLeftImage(Form1 program)
  342. {
  343. //cast is save as long as Form1#GetAllVariables() is conform to its contract
  344. return (List<Line>) program.GetAllVariables()[4];
  345. }
  346. }
  347. }