UnitTest1.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. using System.IO;
  8. namespace Tests
  9. {
  10. [TestClass]
  11. public class LineTests
  12. {
  13. //========================//
  14. //= Bresenham Line Tests =//
  15. //========================//
  16. [TestMethod]
  17. public void BresenhamLineTest1()
  18. {
  19. //Test point
  20. List<Point> expectedResult = new List<Point>();
  21. expectedResult.Add(new Point(1, 2));
  22. List<Point> actualResult = SketchAssistant.GeometryCalculator.BresenhamLineAlgorithm(new Point(1, 2), new Point(1, 2));
  23. Assert.AreEqual(1, actualResult.Count);
  24. for (int i = 0; i < actualResult.Count; i++)
  25. {
  26. Assert.AreEqual(expectedResult[i], actualResult[i]);
  27. }
  28. }
  29. [TestMethod]
  30. public void BresenhamLineTest2()
  31. {
  32. //Test line going from left to right
  33. List<Point> expectedResult = new List<Point>();
  34. for (int i = 1; i <= 6; i++) { expectedResult.Add(new Point(i, 2)); }
  35. List<Point> actualResult = SketchAssistant.GeometryCalculator.BresenhamLineAlgorithm(new Point(1, 2), new Point(6, 2));
  36. Assert.AreEqual(expectedResult.Count, actualResult.Count);
  37. for (int i = 0; i < actualResult.Count; i++)
  38. {
  39. Assert.AreEqual(expectedResult[i], actualResult[i]);
  40. }
  41. }
  42. [TestMethod]
  43. public void BresenhamLineTest3()
  44. {
  45. //Test line going from right to left
  46. List<Point> expectedResult = new List<Point>();
  47. for (int i = 6; i >= 1; i--) { expectedResult.Add(new Point(i, 2)); }
  48. List<Point> actualResult = SketchAssistant.GeometryCalculator.BresenhamLineAlgorithm(new Point(6, 2), new Point(1, 2));
  49. Assert.AreEqual(expectedResult.Count, actualResult.Count);
  50. for (int i = 0; i < actualResult.Count; i++)
  51. {
  52. Assert.AreEqual(expectedResult[i], actualResult[i]);
  53. }
  54. }
  55. [TestMethod]
  56. public void BresenhamLineTest4()
  57. {
  58. //Test line going from top to bottom
  59. List<Point> expectedResult = new List<Point>();
  60. for (int i = 5; i <= 25; i++) { expectedResult.Add(new Point(7, i)); }
  61. List<Point> actualResult = SketchAssistant.GeometryCalculator.BresenhamLineAlgorithm(new Point(7, 5), new Point(7, 25));
  62. Assert.AreEqual(expectedResult.Count, actualResult.Count);
  63. for (int i = 0; i < actualResult.Count; i++)
  64. {
  65. Assert.AreEqual(expectedResult[i], actualResult[i]);
  66. }
  67. }
  68. [TestMethod]
  69. public void BresenhamLineTest5()
  70. {
  71. //Test line going from bottom to top
  72. List<Point> expectedResult = new List<Point>();
  73. for (int i = 25; i >= 5; i--) { expectedResult.Add(new Point(7, i)); }
  74. List<Point> actualResult = SketchAssistant.GeometryCalculator.BresenhamLineAlgorithm(new Point(7, 25), new Point(7, 5));
  75. Assert.AreEqual(expectedResult.Count, actualResult.Count);
  76. for (int i = 0; i < actualResult.Count; i++)
  77. {
  78. Assert.AreEqual(expectedResult[i], actualResult[i]);
  79. }
  80. }
  81. [TestMethod]
  82. public void BresenhamLineTest6()
  83. {
  84. //Test exactly diagonal line from top left to bottom right
  85. List<Point> expectedResult = new List<Point>();
  86. for (int i = 5; i <= 25; i++) { expectedResult.Add(new Point(i + 2, i)); }
  87. List<Point> actualResult = SketchAssistant.GeometryCalculator.BresenhamLineAlgorithm(new Point(7, 5), new Point(27, 25));
  88. Assert.AreEqual(expectedResult.Count, actualResult.Count);
  89. for (int i = 0; i < actualResult.Count; i++)
  90. {
  91. Assert.AreEqual(expectedResult[i], actualResult[i]);
  92. }
  93. }
  94. [TestMethod]
  95. public void BresenhamLineTest7()
  96. {
  97. //Test exactly diagonal line from bottom right to top left
  98. List<Point> expectedResult = new List<Point>();
  99. for (int i = 25; i >= 5; i--) { expectedResult.Add(new Point(i + 2, i)); }
  100. List<Point> actualResult = SketchAssistant.GeometryCalculator.BresenhamLineAlgorithm(new Point(27, 25), new Point(7, 5));
  101. Assert.AreEqual(expectedResult.Count, actualResult.Count);
  102. for (int i = 0; i < actualResult.Count; i++)
  103. {
  104. Assert.AreEqual(expectedResult[i], actualResult[i]);
  105. }
  106. }
  107. //===========================//
  108. //= Matrix Population Tests =//
  109. //===========================//
  110. [TestMethod]
  111. public void MatrixTest1()
  112. {
  113. //Populate Matrix for temporary Line
  114. List<Point> thePoints = new List<Point>();
  115. thePoints.Add(new Point(1, 1));
  116. thePoints.Add(new Point(1, 2));
  117. bool[,] testBoolMatrix = new bool[5, 5];
  118. List<int>[,] testLineMatrix = new List<int>[5, 5];
  119. bool[,] resultBoolMatrix = new bool[5, 5];
  120. HashSet<int>[,] resultLineMatrix = new HashSet<int>[5, 5];
  121. Line testLine = new Line(thePoints);
  122. testLine.PopulateMatrixes(resultBoolMatrix, resultLineMatrix);
  123. for (int i = 0; i < 5; i++)
  124. {
  125. for (int j = 0; j < 5; j++)
  126. {
  127. Assert.AreEqual(testBoolMatrix[i, j], resultBoolMatrix[i, j]);
  128. Assert.AreEqual(testLineMatrix[i, j], resultLineMatrix[i, j]);
  129. }
  130. }
  131. }
  132. [TestMethod]
  133. public void MatrixTest2()
  134. {
  135. //Populate Matrix for non-temporary Line
  136. List<Point> thePoints = new List<Point>();
  137. thePoints.Add(new Point(1, 1));
  138. thePoints.Add(new Point(3, 3));
  139. bool[,] testBoolMatrix = new bool[5, 5];
  140. HashSet<int>[,] testLineMatrix = new HashSet<int>[5, 5];
  141. for (int i = 1; i <= 3; i++)
  142. {
  143. testBoolMatrix[i, i] = true;
  144. HashSet<int> temp = new HashSet<int>();
  145. temp.Add(5);
  146. testLineMatrix[i, i] = temp;
  147. }
  148. bool[,] resultBoolMatrix = new bool[5, 5];
  149. HashSet<int>[,] resultLineMatrix = new HashSet<int>[5, 5];
  150. Line testLine = new Line(thePoints, 5);
  151. testLine.PopulateMatrixes(resultBoolMatrix, resultLineMatrix);
  152. for (int i = 0; i < 5; i++)
  153. {
  154. for (int j = 0; j < 5; j++)
  155. {
  156. Assert.AreEqual(testBoolMatrix[i, j], resultBoolMatrix[i, j]);
  157. if (testLineMatrix[i, j] != null && resultLineMatrix[i, j] != null)
  158. {
  159. for (int k = 0; k < resultLineMatrix[i, j].Count; k++)
  160. {
  161. Assert.AreEqual(true, testLineMatrix[i, j].SetEquals(resultLineMatrix[i, j]));
  162. }
  163. }
  164. }
  165. }
  166. }
  167. //=========================//
  168. //= Line Constructor Test =//
  169. //=========================//
  170. [TestMethod]
  171. public void ConstructorTest()
  172. {
  173. //Create non-temporary Line and check points
  174. //reference Points
  175. 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),
  176. 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),
  177. 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),
  178. new Point(3, 5), new Point(2, 5), new Point(1, 4)};
  179. //test Points, with intermediate points missing & duplicate points
  180. 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),
  181. 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),
  182. new Point(5, 5), new Point(2, 5), new Point(2, 5), new Point(1, 4) };
  183. Line testLine = new Line(testPoints, 0);
  184. List<Point> returnedPoints = testLine.GetPoints();
  185. Assert.AreEqual(comparisonPoints.Count, returnedPoints.Count);
  186. for (int i = 0; i < returnedPoints.Count; i++)
  187. {
  188. Assert.AreEqual(comparisonPoints[i], returnedPoints[i]);
  189. }
  190. }
  191. }
  192. [TestClass]
  193. public class ActionHistoryTests
  194. {
  195. private ActionHistory GetActionHistory()
  196. {
  197. return new ActionHistory();
  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. var lastActionLabel = testHistory.MoveAction(false);
  252. Assert.AreEqual(actionType, testHistory.GetCurrentAction().GetActionType());
  253. Assert.AreEqual(message, lastActionLabel);
  254. }
  255. }
  256. [TestClass]
  257. [DeploymentItem(@"SketchAssistant.Tests\test_input_files\")]
  258. public class FileImporterTests
  259. {
  260. /// <summary>
  261. /// instance of TestContext to be able to access deployed files
  262. /// </summary>
  263. private TestContext testContextInstance;
  264. /// <summary>
  265. ///Gets or sets the test context which provides
  266. ///information about and functionality for the current test run.
  267. ///</summary>
  268. public TestContext TestContext
  269. {
  270. get
  271. {
  272. return testContextInstance;
  273. }
  274. set
  275. {
  276. testContextInstance = value;
  277. }
  278. }
  279. /*
  280. [DataTestMethod]
  281. [DataRow(new int[] { 54, 43, 57, 11, 145, 34, 113, 299, 0 }, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 })]
  282. [DataRow(new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 })]
  283. [DataRow(new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, new int[] { 54, 43, 57, 11, 145, 34, 113, 199, 0 })]
  284. public void ParseISADInputSuccessfulTest(int[] xCoordinates, int[] yCoordinates)
  285. {
  286. Form1 program = new Form1();
  287. FileImporter uut = new SketchAssistant.FileImporter();
  288. List<String> file = new List<string>();
  289. file.Add("drawing");
  290. file.Add("300x200");
  291. for (int i = 0; i < xCoordinates.Length - 2; i += 3)
  292. {
  293. file.Add("line");
  294. file.Add(xCoordinates[i] + ";" + yCoordinates[i]);
  295. file.Add(xCoordinates[i + 1] + ";" + yCoordinates[i + 1]);
  296. file.Add(xCoordinates[i + 2] + ";" + yCoordinates[i + 2]);
  297. file.Add("endline");
  298. }
  299. file.Add("enddrawing");
  300. (int, int, List<Line>) values = uut.ParseISADInputForTesting(file.ToArray());
  301. program.CreateCanvasAndSetPictureForTesting(values.Item1, values.Item2, values.Item3);
  302. Line[] drawing = GetLeftImage(program).ToArray();
  303. Assert.AreEqual(xCoordinates.Length / 3, drawing.Length);
  304. for (int i = 0; i < xCoordinates.Length - 2; i += 3)
  305. {
  306. Point[] currentLine = drawing[i / 3].GetPoints().ToArray();
  307. Assert.AreEqual(3, currentLine.Length);
  308. for (int j = 0; j < 3; j++)
  309. {
  310. Assert.IsTrue(currentLine[j].X == xCoordinates[i + j] && currentLine[j].Y == yCoordinates[i + j]);
  311. }
  312. }
  313. }
  314. [DataTestMethod]
  315. [DataRow(new String[] {})]
  316. [DataRow(new String[] { "begindrawing", "300x300", "line", "50;50", "100;50", "endline", "enddrawing" })]
  317. [DataRow(new String[] { "drawing", "300;300", "line", "50;50", "100;50", "endline", "enddrawing" })]
  318. [DataRow(new String[] { "drawing", "30.5x300", "line", "50;50", "100;50", "endline", "enddrawing" })]
  319. [DataRow(new String[] { "drawing", "line", "50;50", "100;50", "endline", "enddrawing" })]
  320. [DataRow(new String[] { "drawing", "300x300", "beginline", "50;50", "100;50", "endline", "enddrawing" })]
  321. [DataRow(new String[] { "drawing", "300x300", "line", "500;50", "100;50", "endline", "enddrawing" })]
  322. [DataRow(new String[] { "drawing", "300x300", "line", "50x50", "100;50", "endline", "enddrawing" })]
  323. [DataRow(new String[] { "drawing", "300x300", "line", "50", "100", "endline", "enddrawing" })]
  324. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "line", "endline", "enddrawing" })]
  325. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "stopline", "enddrawing" })]
  326. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "enddrawing" })]
  327. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "endline", "endrawing" })]
  328. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "endline" })]
  329. public void ParseISADInputExceptionTest(String[] file)
  330. {
  331. bool exceptionThrown = false;
  332. Form1 program = new Form1();
  333. FileImporter uut = new SketchAssistant.FileImporter();
  334. //check that left image initially is uninitialized
  335. Assert.IsNull(GetLeftImage(program));
  336. //initialize left image with a valid isad drawing
  337. (int, int, List<Line>) values = uut.ParseISADInputForTesting(new string[] { "drawing", "300x205", "line", "40;40", "140;140", "endline", "enddrawing" });
  338. program.CreateCanvasAndSetPictureForTesting(values.Item1, values.Item2, values.Item3);
  339. //save left image for later comparison
  340. List<Line> oldLeftImage = GetLeftImage(program);
  341. try
  342. {
  343. //try to initialize the left image with an invalid isad drawing
  344. (int, int, List<Line>) values1 = uut.ParseISADInputForTesting(file);
  345. program.CreateCanvasAndSetPictureForTesting(values1.Item1, values1.Item2, values1.Item3);
  346. }
  347. catch(FileImporterException)
  348. {
  349. //save the occurence of an exception
  350. exceptionThrown = true;
  351. }
  352. //check that an exception has been thrown
  353. Assert.IsTrue(exceptionThrown);
  354. //check that the left image has not been changed by the failed image import
  355. Assert.AreEqual(oldLeftImage, GetLeftImage(program));
  356. }
  357. /// <summary>
  358. /// local helper method retrieving the left image from a Form1 instance
  359. /// </summary>
  360. /// <returns>the left image of the given Form1 instance</returns>
  361. private List<Line> GetLeftImage(Form1 program)
  362. {
  363. //cast is save as long as Form1#GetAllVariables() is conform to its contract
  364. return (List<Line>) program.GetAllVariables()[4];
  365. }
  366. */
  367. /// <summary>
  368. /// parses all whitelisted files and ensures no exceptions are thrown (parsing abortion, e.g. due to corrupted input files, are realized by throwing a FileImporterException)
  369. /// </summary>
  370. [TestMethod]
  371. public void parseSVGInputNoErrorForWhitelistedFilesTest()
  372. {
  373. FileImporter uut = new FileImporter();
  374. string[] files = Directory.GetFiles(TestContext.DeploymentDirectory + @"\test_input_files\whitelisted", "*.svg", SearchOption.AllDirectories);
  375. Assert.IsTrue(files.Length > 0);
  376. foreach (string s in files) //parse each of the whitelisted files
  377. {
  378. bool noExceptionThrown = true;
  379. try
  380. {
  381. uut.ParseSVGInputFile(s, 10000, 10000);
  382. }
  383. catch (Exception e)
  384. {
  385. noExceptionThrown = false;
  386. }
  387. Assert.IsTrue(noExceptionThrown);
  388. }
  389. }
  390. /// <summary>
  391. /// parses all blacklisted files and ensures an instance of FileIporterException is thrown for each file, but no other exceptions occur
  392. /// </summary>
  393. [TestMethod]
  394. public void parseSVGInputNoErrorForBlacklistedFilesTest()
  395. {
  396. FileImporter uut = new FileImporter();
  397. string[] files = Directory.GetFiles(TestContext.DeploymentDirectory + @"\test_input_files\blacklisted", "*.svg", SearchOption.AllDirectories);
  398. Assert.IsTrue(files.Length > 0);
  399. foreach (string s in files) //parse each of the blacklisted files
  400. {
  401. bool correctExceptionThrown = false;
  402. try
  403. {
  404. uut.ParseSVGInputFile(s, 10000, 10000);
  405. }
  406. catch(FileImporterException e)
  407. {
  408. correctExceptionThrown = true;
  409. }
  410. catch(Exception e)
  411. {
  412. }
  413. Assert.IsTrue(correctExceptionThrown);
  414. }
  415. }
  416. }
  417. }