Browse Source

Added Testsuite and some unittests for Line.cs

Martin Edlund 5 years ago
parent
commit
c136e41eb5

+ 6 - 0
SketchAssistant/SketchAssistant.sln

@@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.28010.2050
 MinimumVisualStudioVersion = 10.0.40219.1
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SketchAssistant", "SketchAssistant\SketchAssistant.csproj", "{0336F628-A2F7-4170-8B2E-9277C23118D4}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SketchAssistantTestSuite", "SketchAssistantTestSuite\SketchAssistantTestSuite.csproj", "{B6EAD3FB-F14C-4409-9D36-E35DE318EA6E}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
 		{0336F628-A2F7-4170-8B2E-9277C23118D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{0336F628-A2F7-4170-8B2E-9277C23118D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{0336F628-A2F7-4170-8B2E-9277C23118D4}.Release|Any CPU.Build.0 = Release|Any CPU
+		{B6EAD3FB-F14C-4409-9D36-E35DE318EA6E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{B6EAD3FB-F14C-4409-9D36-E35DE318EA6E}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{B6EAD3FB-F14C-4409-9D36-E35DE318EA6E}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{B6EAD3FB-F14C-4409-9D36-E35DE318EA6E}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 32 - 13
SketchAssistant/SketchAssistant/Line.cs

@@ -7,21 +7,35 @@ using System.Drawing;
 
 namespace SketchAssistant
 {
-    class Line
+    public class Line
     {
         private List<Point> linePoints;
         private int identifier;
+        private bool isTemporary;
 
+        /// <summary>
+        /// The constructor for lines which are only temporary.
+        /// If you want nice lines use the other constructor.
+        /// </summary>
+        /// <param name="points">The points of the line</param>
         public Line(List<Point> points)
         {
             linePoints = new List<Point>(points);
+            isTemporary = true;
         }
 
+        /// <summary>
+        /// The constructor for lines, which will be more resource efficient 
+        /// and have the ability to populate deletion matrixes.
+        /// </summary>
+        /// <param name="points">The points of the line</param>
+        /// <param name="id">The identifier of the line</param>
         public Line(List<Point> points, int id)
         {
             linePoints = new List<Point>(points);
             identifier = id;
             CleanPoints();
+            isTemporary = false;
         }
 
         public Point GetStartPoint()
@@ -34,6 +48,11 @@ namespace SketchAssistant
             return linePoints.Last();
         }
 
+        public List<Point> GetPoints()
+        {
+            return linePoints;
+        }
+
         /// <summary>
         /// A function that takes a Graphics element and returns it with
         /// the line drawn on it.
@@ -52,28 +71,28 @@ namespace SketchAssistant
 
         /// <summary>
         /// A function that will take to matrixes and populate the with the line data of this line object
-        /// Exceptions:
-        ///     Will throw IndexOutOfRangeException if any of the points of this line are 
-        ///     outside of the given matrixes.
         /// </summary>
         /// <param name="boolMatrix">The Matrix of booleans, in which is saved wether there is a line at this position.</param>
         /// <param name="listMatrix">The Matrix of Lists of integers, in which is saved which lines are at this position</param>
         public void PopulateMatrixes(bool[,] boolMatrix, List<int>[,] listMatrix)
         {
-            foreach(Point currPoint in linePoints)
+            if(!isTemporary)
             {
-                try
+                foreach (Point currPoint in linePoints)
                 {
-                    boolMatrix[currPoint.X, currPoint.Y] = true;
-                    if (listMatrix[currPoint.X, currPoint.Y] == null)
+                    try
                     {
-                        listMatrix[currPoint.X, currPoint.Y] = new List<int>();
+                        boolMatrix[currPoint.X, currPoint.Y] = true;
+                        if (listMatrix[currPoint.X, currPoint.Y] == null)
+                        {
+                            listMatrix[currPoint.X, currPoint.Y] = new List<int>();
+                        }
+                        listMatrix[currPoint.X, currPoint.Y].Add(identifier);
                     }
-                    listMatrix[currPoint.X, currPoint.Y].Add(identifier);
-                }
-                catch(IndexOutOfRangeException e)
-                {
+                    catch (IndexOutOfRangeException e)
+                    {
 
+                    }
                 }
             }
         }

+ 19 - 0
SketchAssistant/SketchAssistantTestSuite/SketchAssistantTestSuite.csproj

@@ -0,0 +1,19 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFramework>netcoreapp2.1</TargetFramework>
+
+    <IsPackable>false</IsPackable>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
+    <PackageReference Include="MSTest.TestAdapter" Version="1.3.2" />
+    <PackageReference Include="MSTest.TestFramework" Version="1.3.2" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\SketchAssistant\SketchAssistant.csproj" />
+  </ItemGroup>
+
+</Project>

+ 177 - 0
SketchAssistant/SketchAssistantTestSuite/UnitTest1.cs

@@ -0,0 +1,177 @@
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using SketchAssistant;
+
+namespace SketchAssistantTestSuite
+{
+    [TestClass]
+    public class LineTests
+    {
+        //========================//
+        //= Bresenham Line Tests =//
+        //========================//
+
+        [TestMethod]
+        public void BresenhamLineTest1()
+        {
+            //Test point
+            List<Point> expectedResult = new List<Point>();
+            expectedResult.Add(new Point(1, 2));
+            List<Point> actualResult = SketchAssistant.Line.BresenhamLineAlgorithm(new Point(1, 2), new Point(1, 2));
+            Assert.AreEqual(1, actualResult.Count);
+            for(int i = 0; i < actualResult.Count; i++)
+            {
+                Assert.AreEqual(expectedResult[i], actualResult[i]);
+            }
+        }
+
+        [TestMethod]
+        public void BresenhamLineTest2()
+        {
+            //Test line going from left to right
+            List<Point> expectedResult = new List<Point>();
+            for (int i = 1; i <= 6; i++) { expectedResult.Add(new Point(i, 2)); }
+            List<Point> actualResult = SketchAssistant.Line.BresenhamLineAlgorithm(new Point(1, 2), new Point(6, 2));
+            Assert.AreEqual(expectedResult.Count, actualResult.Count);
+            for (int i = 0; i < actualResult.Count; i++)
+            {
+                Assert.AreEqual(expectedResult[i], actualResult[i]);
+            }
+        }
+
+        [TestMethod]
+        public void BresenhamLineTest3()
+        {
+            //Test line going from right to left
+            List<Point> expectedResult = new List<Point>();
+            for (int i = 6; i >= 1; i--) { expectedResult.Add(new Point(i, 2)); }
+            List<Point> actualResult = SketchAssistant.Line.BresenhamLineAlgorithm(new Point(6, 2), new Point(1, 2));
+            Assert.AreEqual(expectedResult.Count, actualResult.Count);
+            for (int i = 0; i < actualResult.Count; i++)
+            {
+                Assert.AreEqual(expectedResult[i], actualResult[i]);
+            }
+        }
+
+        [TestMethod]
+        public void BresenhamLineTest4()
+        {
+            //Test line going from top to bottom
+            List<Point> expectedResult = new List<Point>();
+            for (int i = 5; i <= 25; i++) { expectedResult.Add(new Point(7, i)); }
+            List<Point> actualResult = SketchAssistant.Line.BresenhamLineAlgorithm(new Point(7, 5), new Point(7, 25));
+            Assert.AreEqual(expectedResult.Count, actualResult.Count);
+            for (int i = 0; i < actualResult.Count; i++)
+            {
+                Assert.AreEqual(expectedResult[i], actualResult[i]);
+            }
+        }
+
+        [TestMethod]
+        public void BresenhamLineTest5()
+        {
+            //Test line going from bottom to top
+            List<Point> expectedResult = new List<Point>();
+            for (int i = 25; i >= 5; i--) { expectedResult.Add(new Point(7, i)); }
+            List<Point> actualResult = SketchAssistant.Line.BresenhamLineAlgorithm(new Point(7, 25), new Point(7, 5));
+            Assert.AreEqual(expectedResult.Count, actualResult.Count);
+            for (int i = 0; i < actualResult.Count; i++)
+            {
+                Assert.AreEqual(expectedResult[i], actualResult[i]);
+            }
+        }
+
+        [TestMethod]
+        public void BresenhamLineTest6()
+        {
+            //Test exactly diagonal line from top left to bottom right
+            List<Point> expectedResult = new List<Point>();
+            for (int i = 5; i <= 25; i++) { expectedResult.Add(new Point(i + 2, i)); }
+            List<Point> actualResult = SketchAssistant.Line.BresenhamLineAlgorithm(new Point(7, 5), new Point(27, 25));
+            Assert.AreEqual(expectedResult.Count, actualResult.Count);
+            for (int i = 0; i < actualResult.Count; i++)
+            {
+                Assert.AreEqual(expectedResult[i], actualResult[i]);
+            }
+        }
+
+        [TestMethod]
+        public void BresenhamLineTest7()
+        {
+            //Test exactly diagonal line from bottom right to top left
+            List<Point> expectedResult = new List<Point>();
+            for (int i = 25; i >= 5; i--) { expectedResult.Add(new Point(i + 2, i)); }
+            List<Point> actualResult = SketchAssistant.Line.BresenhamLineAlgorithm(new Point(27, 25), new Point(7, 5));
+            Assert.AreEqual(expectedResult.Count, actualResult.Count);
+            for (int i = 0; i < actualResult.Count; i++)
+            {
+                Assert.AreEqual(expectedResult[i], actualResult[i]);
+            }
+        }
+
+        //===========================//
+        //= Matrix Population Tests =//
+        //===========================//
+
+        [TestMethod]
+        public void MatrixTest1()
+        {
+            //Populate Matrix for temporary Line
+            List<Point> thePoints = new List<Point>();
+            thePoints.Add(new Point(1, 1));
+            thePoints.Add(new Point(1, 2));
+            bool[,] testBoolMatrix = new bool[5, 5];
+            List<int>[,] testLineMatrix = new List<int>[5, 5];
+            bool[,] resultBoolMatrix = new bool[5, 5];
+            List<int>[,] resultLineMatrix = new List<int>[5, 5];
+            Line testLine = new Line(thePoints);
+            testLine.PopulateMatrixes(resultBoolMatrix, resultLineMatrix);
+            for(int i = 0; i < 5; i++)
+            {
+                for (int j = 0; j < 5; j++)
+                {
+                    Assert.AreEqual(testBoolMatrix[i, j], resultBoolMatrix[i, j]);
+                    Assert.AreEqual(testLineMatrix[i, j], resultLineMatrix[i, j]);
+                }
+            }
+        }
+
+        [TestMethod]
+        public void MatrixTest2()
+        {
+            //Populate Matrix for non-temporary Line
+            List<Point> thePoints = new List<Point>();
+            thePoints.Add(new Point(1, 1));
+            thePoints.Add(new Point(3, 3));
+            bool[,] testBoolMatrix = new bool[5, 5];
+            List<int>[,] testLineMatrix = new List<int>[5, 5];
+            for (int i = 1; i <= 3; i++)
+            {
+                testBoolMatrix[i, i] = true;
+                List<int> temp = new List<int>();
+                temp.Add(5);
+                testLineMatrix[i, i] = temp;
+            }
+            bool[,] resultBoolMatrix = new bool[5, 5];
+            List<int>[,] resultLineMatrix = new List<int>[5, 5];
+            Line testLine = new Line(thePoints, 5);
+            testLine.PopulateMatrixes(resultBoolMatrix, resultLineMatrix);
+            for (int i = 0; i < 5; i++)
+            {
+                for (int j = 0; j < 5; j++)
+                {
+                    Assert.AreEqual(testBoolMatrix[i, j], resultBoolMatrix[i, j]);
+                    if(testLineMatrix[i, j] != null && resultLineMatrix[i, j] != null)
+                    {
+                        for (int k = 0; k < resultLineMatrix[i, j].Count; k++)
+                        {
+                            Assert.AreEqual(testLineMatrix[i, j][k], resultLineMatrix[i, j][k]);
+                        }
+                    }
+                }
+            }
+        }
+    }
+}