UnitTest1.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using SketchAssistant;
  6. namespace SketchAssistantTestSuite
  7. {
  8. [TestClass]
  9. public class LineTests
  10. {
  11. //========================//
  12. //= Bresenham Line Tests =//
  13. //========================//
  14. [TestMethod]
  15. public void BresenhamLineTest1()
  16. {
  17. //Test point
  18. List<Point> expectedResult = new List<Point>();
  19. expectedResult.Add(new Point(1, 2));
  20. List<Point> actualResult = SketchAssistant.Line.BresenhamLineAlgorithm(new Point(1, 2), new Point(1, 2));
  21. Assert.AreEqual(1, actualResult.Count);
  22. for (int i = 0; i < actualResult.Count; i++)
  23. {
  24. Assert.AreEqual(expectedResult[i], actualResult[i]);
  25. }
  26. }
  27. [TestMethod]
  28. public void BresenhamLineTest2()
  29. {
  30. //Test line going from left to right
  31. List<Point> expectedResult = new List<Point>();
  32. for (int i = 1; i <= 6; i++) { expectedResult.Add(new Point(i, 2)); }
  33. List<Point> actualResult = SketchAssistant.Line.BresenhamLineAlgorithm(new Point(1, 2), new Point(6, 2));
  34. Assert.AreEqual(expectedResult.Count, actualResult.Count);
  35. for (int i = 0; i < actualResult.Count; i++)
  36. {
  37. Assert.AreEqual(expectedResult[i], actualResult[i]);
  38. }
  39. }
  40. [TestMethod]
  41. public void BresenhamLineTest3()
  42. {
  43. //Test line going from right to left
  44. List<Point> expectedResult = new List<Point>();
  45. for (int i = 6; i >= 1; i--) { expectedResult.Add(new Point(i, 2)); }
  46. List<Point> actualResult = SketchAssistant.Line.BresenhamLineAlgorithm(new Point(6, 2), new Point(1, 2));
  47. Assert.AreEqual(expectedResult.Count, actualResult.Count);
  48. for (int i = 0; i < actualResult.Count; i++)
  49. {
  50. Assert.AreEqual(expectedResult[i], actualResult[i]);
  51. }
  52. }
  53. [TestMethod]
  54. public void BresenhamLineTest4()
  55. {
  56. //Test line going from top to bottom
  57. List<Point> expectedResult = new List<Point>();
  58. for (int i = 5; i <= 25; i++) { expectedResult.Add(new Point(7, i)); }
  59. List<Point> actualResult = SketchAssistant.Line.BresenhamLineAlgorithm(new Point(7, 5), new Point(7, 25));
  60. Assert.AreEqual(expectedResult.Count, actualResult.Count);
  61. for (int i = 0; i < actualResult.Count; i++)
  62. {
  63. Assert.AreEqual(expectedResult[i], actualResult[i]);
  64. }
  65. }
  66. [TestMethod]
  67. public void BresenhamLineTest5()
  68. {
  69. //Test line going from bottom to top
  70. List<Point> expectedResult = new List<Point>();
  71. for (int i = 25; i >= 5; i--) { expectedResult.Add(new Point(7, i)); }
  72. List<Point> actualResult = SketchAssistant.Line.BresenhamLineAlgorithm(new Point(7, 25), new Point(7, 5));
  73. Assert.AreEqual(expectedResult.Count, actualResult.Count);
  74. for (int i = 0; i < actualResult.Count; i++)
  75. {
  76. Assert.AreEqual(expectedResult[i], actualResult[i]);
  77. }
  78. }
  79. [TestMethod]
  80. public void BresenhamLineTest6()
  81. {
  82. //Test exactly diagonal line from top left to bottom right
  83. List<Point> expectedResult = new List<Point>();
  84. for (int i = 5; i <= 25; i++) { expectedResult.Add(new Point(i + 2, i)); }
  85. List<Point> actualResult = SketchAssistant.Line.BresenhamLineAlgorithm(new Point(7, 5), new Point(27, 25));
  86. Assert.AreEqual(expectedResult.Count, actualResult.Count);
  87. for (int i = 0; i < actualResult.Count; i++)
  88. {
  89. Assert.AreEqual(expectedResult[i], actualResult[i]);
  90. }
  91. }
  92. [TestMethod]
  93. public void BresenhamLineTest7()
  94. {
  95. //Test exactly diagonal line from bottom right to top left
  96. List<Point> expectedResult = new List<Point>();
  97. for (int i = 25; i >= 5; i--) { expectedResult.Add(new Point(i + 2, i)); }
  98. List<Point> actualResult = SketchAssistant.Line.BresenhamLineAlgorithm(new Point(27, 25), new Point(7, 5));
  99. Assert.AreEqual(expectedResult.Count, actualResult.Count);
  100. for (int i = 0; i < actualResult.Count; i++)
  101. {
  102. Assert.AreEqual(expectedResult[i], actualResult[i]);
  103. }
  104. }
  105. //===========================//
  106. //= Matrix Population Tests =//
  107. //===========================//
  108. [TestMethod]
  109. public void MatrixTest1()
  110. {
  111. //Populate Matrix for temporary Line
  112. List<Point> thePoints = new List<Point>();
  113. thePoints.Add(new Point(1, 1));
  114. thePoints.Add(new Point(1, 2));
  115. bool[,] testBoolMatrix = new bool[5, 5];
  116. List<int>[,] testLineMatrix = new List<int>[5, 5];
  117. bool[,] resultBoolMatrix = new bool[5, 5];
  118. HashSet<int>[,] resultLineMatrix = new HashSet<int>[5, 5];
  119. Line testLine = new Line(thePoints);
  120. testLine.PopulateMatrixes(resultBoolMatrix, resultLineMatrix);
  121. for (int i = 0; i < 5; i++)
  122. {
  123. for (int j = 0; j < 5; j++)
  124. {
  125. Assert.AreEqual(testBoolMatrix[i, j], resultBoolMatrix[i, j]);
  126. Assert.AreEqual(testLineMatrix[i, j], resultLineMatrix[i, j]);
  127. }
  128. }
  129. }
  130. [TestMethod]
  131. public void MatrixTest2()
  132. {
  133. //Populate Matrix for non-temporary Line
  134. List<Point> thePoints = new List<Point>();
  135. thePoints.Add(new Point(1, 1));
  136. thePoints.Add(new Point(3, 3));
  137. bool[,] testBoolMatrix = new bool[5, 5];
  138. HashSet<int>[,] testLineMatrix = new HashSet<int>[5, 5];
  139. for (int i = 1; i <= 3; i++)
  140. {
  141. testBoolMatrix[i, i] = true;
  142. HashSet<int> temp = new HashSet<int>();
  143. temp.Add(5);
  144. testLineMatrix[i, i] = temp;
  145. }
  146. bool[,] resultBoolMatrix = new bool[5, 5];
  147. HashSet<int>[,] resultLineMatrix = new HashSet<int>[5, 5];
  148. Line testLine = new Line(thePoints, 5);
  149. testLine.PopulateMatrixes(resultBoolMatrix, resultLineMatrix);
  150. for (int i = 0; i < 5; i++)
  151. {
  152. for (int j = 0; j < 5; j++)
  153. {
  154. Assert.AreEqual(testBoolMatrix[i, j], resultBoolMatrix[i, j]);
  155. if (testLineMatrix[i, j] != null && resultLineMatrix[i, j] != null)
  156. {
  157. for (int k = 0; k < resultLineMatrix[i, j].Count; k++)
  158. {
  159. Assert.AreEqual(true, testLineMatrix[i, j].SetEquals(resultLineMatrix[i, j]));
  160. }
  161. }
  162. }
  163. }
  164. }
  165. //=========================//
  166. //= Line Constructor Test =//
  167. //=========================//
  168. [TestMethod]
  169. public void ConstructorTest()
  170. {
  171. //Create non-temporary Line and check points
  172. //reference Points
  173. 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),
  174. 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),
  175. 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),
  176. new Point(3, 5), new Point(2, 5), new Point(1, 4)};
  177. //test Points, with intermediate points missing & duplicate points
  178. 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),
  179. 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),
  180. new Point(5, 5), new Point(2, 5), new Point(2, 5), new Point(1, 4) };
  181. Line testLine = new Line(testPoints, 0);
  182. List<Point> returnedPoints = testLine.GetPoints();
  183. Assert.AreEqual(comparisonPoints.Count, returnedPoints.Count);
  184. for (int i = 0; i < returnedPoints.Count; i++)
  185. {
  186. Assert.AreEqual(comparisonPoints[i],returnedPoints[i]);
  187. }
  188. }
  189. }
  190. }