Quellcode durchsuchen

Added tests for the RedrawAssistant

Martin Edlund vor 6 Jahren
Ursprung
Commit
ecfb77a81b

+ 222 - 0
SketchAssistant/SketchAssistant.Tests/UnitTest1.cs

@@ -370,4 +370,226 @@ namespace Tests
             return (List<Line>)program.GetAllVariables().Find(x => x.Item1.Equals("leftLineList")).Item2;
         }
     }
+
+    [TestClass]
+    public class RedrawAssistantTests
+    {
+        private RedrawAssistant GetAssistant(List<Line> input)
+        {
+            if(input.Count == 0)
+            {
+                return new RedrawAssistant();
+            }
+            else
+            {
+                return new RedrawAssistant(input);
+            }
+        }
+
+        [DataTestMethod]
+        [DataRow(17, 20, new int[] { 54, 43, 57, 11, 145, 34, 113, 299, 0 }, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, true)]
+        [DataRow(-50, 30, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, false)]
+        [DataRow(-70, -20, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, new int[] { 54, 43, 57, 11, 145, 34, 113, 199, 0 }, true)]
+        public void InactiveRedrawAssistantTest(int x, int y, int[] xCoords, int[] yCoords, bool lineActive)
+        {
+            RedrawAssistant testAssistant = GetAssistant(new List<Line>());
+            List<Point> testPoints = new List<Point>();
+            for (int i = 0; i < xCoords.Length; i++)
+            {
+                testPoints.Add(new Point(xCoords[i], yCoords[i]));
+            }
+            List<Tuple<bool, Line>> testLines = new List<Tuple<bool, Line>> { new Tuple<bool, Line>(lineActive, new Line(testPoints, 0)) };
+            List<HashSet<Point>> result = testAssistant.Tick(new Point(1, 1), testLines, -1 );
+            Assert.AreEqual(0, result.Count);
+        }
+
+        [DataTestMethod]
+        [DataRow(17, 20, new int[] { 54, 43, 57, 11, 145, 34, 113, 299, 0 }, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, 1)]
+        [DataRow(-50, 30, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, 1)]
+        [DataRow(33, 54, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, new int[] { 54, 43, 57, 11, 145, 34, 113, 199, 0 }, 2)]
+        public void ActiveRedrawAssistantTestStartedDrawing(int x, int y, int[] xCoords, int[] yCoords, int resultingCount)
+        {
+            List<Point> testPoints = new List<Point>();
+            for (int i = 0; i < xCoords.Length; i++)
+            {
+                testPoints.Add(new Point(xCoords[i], yCoords[i]));
+            }
+            List<Tuple<bool, Line>> testInputLines = new List<Tuple<bool, Line>>();
+            List<Line> testRedrawLines = new List<Line> { new Line(testPoints, 0) };
+            RedrawAssistant testAssistant = GetAssistant(testRedrawLines);
+            //Setting Marker Radius to 1 so that only the functionality of the RedrawAssistant is checked 
+            //and not the functionality of the Circle algorithm.
+            testAssistant.SetMarkerRadius(1);
+            List<HashSet<Point>> tickResult = testAssistant.Tick(new Point(x, y), testInputLines, -1);
+
+
+            Assert.AreEqual(resultingCount, tickResult.Count);
+            if(resultingCount == 1)
+            {
+                foreach(Point p in tickResult[0])
+                {
+                    Assert.AreEqual(testPoints[0], p);
+                }
+            }
+            if(resultingCount == 2)
+            {
+                foreach (Point p in tickResult[0])
+                {
+                    Assert.AreEqual(testPoints[0], p);
+                }
+                foreach (Point p in tickResult[1])
+                {
+                    Assert.AreEqual(testPoints[testPoints.Count-1], p);
+                }
+            }
+        }
+
+        [DataTestMethod]
+        [DataRow(17, 20, new int[] { 54, 43, 57, 11, 145, 34, 113, 299, 0 }, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 },
+             new int[] { 77, 20, 3, 74, 28 }, new int[] { 40, 50, 20, 77, 28}, 2, false)]
+        [DataRow(33, 33, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 },
+             new int[] { 42, 140, 30, 30, 30 }, new int[] { 11, 145, 34, 113, 28 }, 2, true)]
+        [DataRow(33, 54, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, new int[] { 54, 43, 57, 11, 145, 34, 113, 199, 0 },
+             new int[] { 43, 57, 11, 145 }, new int[] { 33, 42, 140, 30 }, 2, true)]
+        public void ActiveRedrawAssistantTestMultipleLinesRedrawn(
+            int x, int y, int[] xCoords_one, int[] yCoords_one, int[] xCoords_two, int[] yCoords_two, int resultingCount, bool showingStartAndEnd)
+        {
+            List<Point> testPoints1 = new List<Point>();
+            for (int i = 0; i < xCoords_one.Length; i++)
+            {
+                testPoints1.Add(new Point(xCoords_one[i], yCoords_one[i]));
+            }
+            List<Point> testPoints2 = new List<Point>();
+            for (int i = 0; i < xCoords_two.Length; i++)
+            {
+                testPoints2.Add(new Point(xCoords_two[i], yCoords_two[i]));
+            }
+            List<Tuple<bool, Line>> testInputLines = new List<Tuple<bool, Line>>();
+
+            List<Line> testRedrawLines = new List<Line> { new Line(testPoints1, 0) , new Line(testPoints2, 1) };
+            RedrawAssistant testAssistant = GetAssistant(testRedrawLines);
+            //Setting Marker Radius to 1 so that only the functionality of the RedrawAssistant is checked 
+            //and not the functionality of the Circle algorithm.
+            testAssistant.SetMarkerRadius(1);
+
+            List<HashSet<Point>> tickResult = testAssistant.Tick(new Point(x, y), testInputLines, -1);
+
+            Assert.AreEqual(resultingCount, tickResult.Count);
+            if (showingStartAndEnd)
+            {
+                foreach (Point p in tickResult[0])
+                {
+                    Assert.AreEqual(testPoints1[0], p);
+                }
+                foreach (Point p in tickResult[1])
+                {
+                    Assert.AreEqual(testPoints1[testPoints1.Count - 1], p);
+                }
+            }
+            else
+            {
+                foreach (Point p in tickResult[0])
+                {
+                    Assert.AreEqual(testPoints1[0], p);
+                }
+                foreach (Point p in tickResult[1])
+                {
+                    Assert.AreEqual(testPoints2[0], p);
+                }
+            }
+        }
+
+        [DataTestMethod]
+        [DataRow(17, 20, 17, 20, new int[] { 54, 43, 57, 11, 145, 34, 113, 299, 0 }, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 },
+     new int[] { 77, 20, 3, 74, 28 }, new int[] { 40, 50, 20, 77, 28 }, 2, 2, false, false)]
+
+        [DataRow(33, 33, 2, 2, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 },
+     new int[] { 42, 140, 30, 30, 30 }, new int[] { 11, 145, 34, 113, 28 }, 2, 1, true, false)]
+
+        [DataRow(33, 54, 17, 0, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, new int[] { 54, 43, 57, 11, 145, 34, 113, 199, 0 },
+     new int[] { 43, 57, 11, 145 }, new int[] { 33, 42, 140, 30 }, 2, 2, true, true)]
+        public void ActiveRedrawAssistantTestLineFinished(
+            int x_one, int y_one, int x_two, int y_two, int[] xCoords_one, int[] yCoords_one, int[] xCoords_two, int[] yCoords_two, 
+            int count_one, int count_two, bool showingSE_one, bool showingSE_two)
+        {
+            List<Point> testPoints1 = new List<Point>();
+            for (int i = 0; i < xCoords_one.Length; i++)
+            {
+                testPoints1.Add(new Point(xCoords_one[i], yCoords_one[i]));
+            }
+            List<Point> testPoints2 = new List<Point>();
+            for (int i = 0; i < xCoords_two.Length; i++)
+            {
+                testPoints2.Add(new Point(xCoords_two[i], yCoords_two[i]));
+            }
+            List<Tuple<bool, Line>> testInputLines = new List<Tuple<bool, Line>>();
+
+            List<Line> testRedrawLines = new List<Line> { new Line(testPoints1, 0), new Line(testPoints2, 1) };
+            RedrawAssistant testAssistant = GetAssistant(testRedrawLines);
+            //Setting Marker Radius to 1 so that only the functionality of the RedrawAssistant is checked 
+            //and not the functionality of the Circle algorithm.
+            testAssistant.SetMarkerRadius(1);
+
+            List<HashSet<Point>> tickResult1 = testAssistant.Tick(new Point(x_one, y_one), testInputLines, -1);
+            List<HashSet<Point>> tickResult2 = testAssistant.Tick(new Point(x_two, y_two), testInputLines, 0);
+
+            Assert.AreEqual(count_one, tickResult1.Count);
+            if (showingSE_one)
+            {
+                foreach(Point p in tickResult1[0])
+                {
+                    Assert.AreEqual(testPoints1[0], p);
+                }
+                foreach (Point p in tickResult1[1])
+                {
+                    Assert.AreEqual(testPoints1[testPoints1.Count - 1], p);
+                }
+            }
+            else
+            {
+                foreach (Point p in tickResult1[0])
+                {
+                    Assert.AreEqual(testPoints1[0], p);
+                }
+                foreach (Point p in tickResult1[1])
+                {
+                    Assert.AreEqual(testPoints2[0], p);
+                }
+            }
+
+            Assert.AreEqual(count_two, tickResult2.Count);
+            if(count_two == 2)
+            {
+                if (showingSE_two)
+                {
+                    foreach (Point p in tickResult2[0])
+                    {
+                        Assert.AreEqual(testPoints1[0], p);
+                    }
+                    foreach (Point p in tickResult2[1])
+                    {
+                        Assert.AreEqual(testPoints1[testPoints1.Count - 1], p);
+                    }
+                }
+                else
+                {
+                    foreach (Point p in tickResult2[0])
+                    {
+                        Assert.AreEqual(testPoints1[0], p);
+                    }
+                    foreach (Point p in tickResult2[1])
+                    {
+                        Assert.AreEqual(testPoints2[0], p);
+                    }
+                }
+            }
+            if(count_two == 1)
+            {
+                foreach (Point p in tickResult2[0])
+                {
+                    Assert.AreEqual(testPoints2[0], p);
+                }
+            }
+        }
+    }
 }

+ 8 - 6
SketchAssistant/SketchAssistant/Form1.cs

@@ -764,14 +764,16 @@ namespace SketchAssistant
         /// Cast according to the Type definitions in the class variable section.</returns>
         public List<Tuple<String, Object>>GetAllVariables()
         {
-            var objArr = new Object[] { currentState, fileImporter, openFileDialog, leftImage, leftLineList,
-                rightImage, currentLine, rightLineList, mousePressed, currentCursorPosition, previousCursorPosition,
-                cursorPositions, rightGraph, isFilledMatrix, linesMatrix, deletionRadius, historyOfActions,
-                overlayItems, redrawAss, markerRadius };
+            var objArr = new (String, object)[] { ("currentState", currentState), ("fileImporter", fileImporter), ("openFileDialog", openFileDialog),
+                ("leftImage", leftImage), ("leftLineList", leftLineList), ("rightImage", rightImage), ("currentLine", currentLine),
+                ("rightLineList", rightLineList), ("mousePressed", mousePressed), ("currentCursorPosition", currentCursorPosition),
+                ("previousCursorPosition", previousCursorPosition), ("cursorPositions", cursorPositions), ("rightGraph", rightGraph),
+                ("isFilledMatrix", isFilledMatrix), ("linesMatrix", linesMatrix), ("deletionRadius", deletionRadius),
+                ("historyOfActions", historyOfActions), ("overlayItems", overlayItems), ("redrawAss", redrawAss), ("markerRadius", markerRadius) };
             var varArr = new List<Tuple<String, Object>>();
-            foreach(Object obj in objArr)
+            foreach((String, object) obj in objArr)
             {
-                varArr.Add(new Tuple<string, object>(nameof(obj), obj));
+                varArr.Add(new Tuple<string, object>(obj.Item1, obj.Item2));
             }
             return varArr;
         }

+ 4 - 3
SketchAssistant/SketchAssistant/RedrawAssistant.cs

@@ -8,7 +8,7 @@ using System.Drawing;
 
 namespace SketchAssistant
 {
-    class RedrawAssistant
+    public class RedrawAssistant
     {
         /// <summary>
         /// The lines of the left image, with a boolean indicating if they have been redrawn
@@ -67,13 +67,14 @@ namespace SketchAssistant
             }
             SetMarkerRadius(5);
         }
+
         /// <summary>
         /// The main functionality of the RedrawAssistant, which updates the Assistant according to the inputs given.
         /// </summary>
         /// <param name="currentPoint">The current position of the cursor, as a point</param>
         /// <param name="rightLines">The lines on the right canvas</param>
         /// <param name="currLineID">The id of the currently finished line, -1 if no line was finished.</param>
-        /// <returns></returns>
+        /// <returns>A List of HashSets of Points, which are markers for the user to redraw lines.</returns>
         public List<HashSet<Point>> Tick(Point currentPoint, List<Tuple<bool, Line>> rightLines, int currLineID)
         {
             List<HashSet<Point>> returnList = new List<HashSet<Point>>();
@@ -192,7 +193,7 @@ namespace SketchAssistant
         /// <summary>
         /// Will calculate the start and endpoints of the given line.
         /// </summary>
-        /// <param name="line">The line.</param>
+        /// <param name="line">The given line.</param>
         private Tuple<HashSet<Point>, HashSet<Point>> CalculateStartAndEnd(Line line)
         {
             var circle0 = GeometryCalculator.FilledCircleAlgorithm(line.GetStartPoint(), markerRadius);