UnitTest1.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. public class FileImporterTests
  258. {
  259. /// <summary>
  260. /// instance of TestContext to be able to access deployed files
  261. /// </summary>
  262. private TestContext testContextInstance;
  263. /// <summary>
  264. ///Gets or sets the test context which provides
  265. ///information about and functionality for the current test run.
  266. ///</summary>
  267. public TestContext TestContext
  268. {
  269. get
  270. {
  271. return testContextInstance;
  272. }
  273. set
  274. {
  275. testContextInstance = value;
  276. }
  277. }
  278. /*
  279. [DataTestMethod]
  280. [DataRow(new int[] { 54, 43, 57, 11, 145, 34, 113, 299, 0 }, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 })]
  281. [DataRow(new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, 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[] { 54, 43, 57, 11, 145, 34, 113, 199, 0 })]
  283. public void ParseISADInputSuccessfulTest(int[] xCoordinates, int[] yCoordinates)
  284. {
  285. Form1 program = new Form1();
  286. FileImporter uut = new SketchAssistant.FileImporter();
  287. List<String> file = new List<string>();
  288. file.Add("drawing");
  289. file.Add("300x200");
  290. for (int i = 0; i < xCoordinates.Length - 2; i += 3)
  291. {
  292. file.Add("line");
  293. file.Add(xCoordinates[i] + ";" + yCoordinates[i]);
  294. file.Add(xCoordinates[i + 1] + ";" + yCoordinates[i + 1]);
  295. file.Add(xCoordinates[i + 2] + ";" + yCoordinates[i + 2]);
  296. file.Add("endline");
  297. }
  298. file.Add("enddrawing");
  299. (int, int, List<Line>) values = uut.ParseISADInputForTesting(file.ToArray());
  300. program.CreateCanvasAndSetPictureForTesting(values.Item1, values.Item2, values.Item3);
  301. Line[] drawing = GetLeftImage(program).ToArray();
  302. Assert.AreEqual(xCoordinates.Length / 3, drawing.Length);
  303. for (int i = 0; i < xCoordinates.Length - 2; i += 3)
  304. {
  305. Point[] currentLine = drawing[i / 3].GetPoints().ToArray();
  306. Assert.AreEqual(3, currentLine.Length);
  307. for (int j = 0; j < 3; j++)
  308. {
  309. Assert.IsTrue(currentLine[j].X == xCoordinates[i + j] && currentLine[j].Y == yCoordinates[i + j]);
  310. }
  311. }
  312. }
  313. [DataTestMethod]
  314. [DataRow(new String[] {})]
  315. [DataRow(new String[] { "begindrawing", "300x300", "line", "50;50", "100;50", "endline", "enddrawing" })]
  316. [DataRow(new String[] { "drawing", "300;300", "line", "50;50", "100;50", "endline", "enddrawing" })]
  317. [DataRow(new String[] { "drawing", "30.5x300", "line", "50;50", "100;50", "endline", "enddrawing" })]
  318. [DataRow(new String[] { "drawing", "line", "50;50", "100;50", "endline", "enddrawing" })]
  319. [DataRow(new String[] { "drawing", "300x300", "beginline", "50;50", "100;50", "endline", "enddrawing" })]
  320. [DataRow(new String[] { "drawing", "300x300", "line", "500;50", "100;50", "endline", "enddrawing" })]
  321. [DataRow(new String[] { "drawing", "300x300", "line", "50x50", "100;50", "endline", "enddrawing" })]
  322. [DataRow(new String[] { "drawing", "300x300", "line", "50", "100", "endline", "enddrawing" })]
  323. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "line", "endline", "enddrawing" })]
  324. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "stopline", "enddrawing" })]
  325. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "enddrawing" })]
  326. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "endline", "endrawing" })]
  327. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "endline" })]
  328. public void ParseISADInputExceptionTest(String[] file)
  329. {
  330. bool exceptionThrown = false;
  331. Form1 program = new Form1();
  332. FileImporter uut = new SketchAssistant.FileImporter();
  333. //check that left image initially is uninitialized
  334. Assert.IsNull(GetLeftImage(program));
  335. //initialize left image with a valid isad drawing
  336. (int, int, List<Line>) values = uut.ParseISADInputForTesting(new string[] { "drawing", "300x205", "line", "40;40", "140;140", "endline", "enddrawing" });
  337. program.CreateCanvasAndSetPictureForTesting(values.Item1, values.Item2, values.Item3);
  338. //save left image for later comparison
  339. List<Line> oldLeftImage = GetLeftImage(program);
  340. try
  341. {
  342. //try to initialize the left image with an invalid isad drawing
  343. (int, int, List<Line>) values1 = uut.ParseISADInputForTesting(file);
  344. program.CreateCanvasAndSetPictureForTesting(values1.Item1, values1.Item2, values1.Item3);
  345. }
  346. catch(FileImporterException)
  347. {
  348. //save the occurence of an exception
  349. exceptionThrown = true;
  350. }
  351. //check that an exception has been thrown
  352. Assert.IsTrue(exceptionThrown);
  353. //check that the left image has not been changed by the failed image import
  354. Assert.AreEqual(oldLeftImage, GetLeftImage(program));
  355. }
  356. /// <summary>
  357. /// local helper method retrieving the left image from a Form1 instance
  358. /// </summary>
  359. /// <returns>the left image of the given Form1 instance</returns>
  360. private List<Line> GetLeftImage(Form1 program)
  361. {
  362. //cast is save as long as Form1#GetAllVariables() is conform to its contract
  363. return (List<Line>) program.GetAllVariables()[4];
  364. }
  365. */
  366. [TestMethod]
  367. [DeploymentItem(@"test_input_files")]
  368. public void parseSVGInputNoErrorForWhitelistedFilesTest()
  369. {
  370. FileImporter uut = new FileImporter();
  371. string[] files = Directory.GetFiles(TestContext.DeploymentDirectory, " *.svg", SearchOption.AllDirectories);
  372. foreach(string s in files) //parse each of the whitelisted files
  373. {
  374. bool noExceptionThrown = true;
  375. try
  376. {
  377. uut.ParseSVGInputFile(s, 10000, 10000);
  378. }
  379. catch (FileImporterException e)
  380. {
  381. noExceptionThrown = false;
  382. }
  383. catch (Exception e)
  384. {
  385. noExceptionThrown = false;
  386. }
  387. Assert.IsTrue(noExceptionThrown);
  388. }
  389. Assert.IsTrue(files.Length > 0);
  390. }
  391. [TestMethod]
  392. [DeploymentItem(@"test_input_files")]
  393. public void parseSVGInputNoErrorForBlacklistedFilesTest()
  394. {
  395. FileImporter uut = new FileImporter();
  396. string[] files = Directory.GetFiles(TestContext.DeploymentDirectory, " *.svg", SearchOption.AllDirectories);
  397. foreach (string s in files) //parse each of the whitelisted files
  398. {
  399. bool correctExceptionThrown = false;
  400. try
  401. {
  402. uut.ParseSVGInputFile(s, 10000, 10000);
  403. }
  404. catch(FileImporterException e)
  405. {
  406. correctExceptionThrown = true;
  407. }
  408. catch(Exception e)
  409. {
  410. }
  411. Assert.IsTrue(correctExceptionThrown);
  412. }
  413. Assert.IsTrue(files.Length > 0);
  414. }
  415. }
  416. }