UnitTest1.cs 8.4 KB

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