Browse Source

Fixed color test

flabbet 5 years ago
parent
commit
f2fcd26a30
1 changed files with 18 additions and 8 deletions
  1. 18 8
      PixiEditorTests/WorkspaceTests/ColorsTests/ExtendedColorTests.cs

+ 18 - 8
PixiEditorTests/WorkspaceTests/ColorsTests/ExtendedColorTests.cs

@@ -10,6 +10,8 @@ namespace PixiEditorTests.WorkspaceTests.ColorsTests
     [TestFixture]
     [TestFixture]
     public class ExtendedColorTests
     public class ExtendedColorTests
     {
     {
+        private const int AcceptableMaringOfError = 1;
+
         [TestCase()]
         [TestCase()]
         public void ChangeColorBrightnessIsNotTheSameTest()
         public void ChangeColorBrightnessIsNotTheSameTest()
         {
         {
@@ -24,17 +26,22 @@ namespace PixiEditorTests.WorkspaceTests.ColorsTests
             Assert.AreEqual(Colors.Black, newColor);
             Assert.AreEqual(Colors.Black, newColor);
         }
         }
 
 
+
+        //Acceptable margin of error is 1
         [TestCase(0,0,0,0,0,0)]
         [TestCase(0,0,0,0,0,0)]
         [TestCase(255,255,255,0,0,100)]
         [TestCase(255,255,255,0,0,100)]
         [TestCase(182,55,55,0,53.6f,46.5f)]
         [TestCase(182,55,55,0,53.6f,46.5f)]
         [TestCase(20,47,255,233,100,53.9f)]
         [TestCase(20,47,255,233,100,53.9f)]
-        [TestCase(137, 43,226, 270,75.9f,52.7f)] //Theoretically 170 should be 171, but error margin of 1 is acceptable
+        [TestCase(137, 43,226, 271,75.9f,52.7f)]
         public void RgbToHslTest(int r, int g, int b, int h, float s, float l)
         public void RgbToHslTest(int r, int g, int b, int h, float s, float l)
         {
         {
             Tuple<int, float, float> hsl = ExColor.RgbToHsl(r, g, b);
             Tuple<int, float, float> hsl = ExColor.RgbToHsl(r, g, b);
-            Assert.AreEqual(h, hsl.Item1);
-            Assert.AreEqual(Math.Round(s), Math.Round(hsl.Item2));
-            Assert.AreEqual(Math.Round(l), Math.Round(hsl.Item3));
+            float marginOfErrorH = Math.Abs(hsl.Item1 - h);
+            float marginOfErrorS = Math.Abs(hsl.Item2 - s);
+            float marginOfErrorL = Math.Abs(hsl.Item3 - l);
+            Assert.LessOrEqual(marginOfErrorH, AcceptableMaringOfError);
+            Assert.LessOrEqual(marginOfErrorS, AcceptableMaringOfError);
+            Assert.LessOrEqual(marginOfErrorL, AcceptableMaringOfError);
 
 
         }
         }
 
 
@@ -42,13 +49,16 @@ namespace PixiEditorTests.WorkspaceTests.ColorsTests
         [TestCase(0, 0, 100, 255, 255, 255)]
         [TestCase(0, 0, 100, 255, 255, 255)]
         [TestCase(0, 53.6f, 46.5f, 182, 55, 55)]
         [TestCase(0, 53.6f, 46.5f, 182, 55, 55)]
         [TestCase(297, 100, 17.1f, 82, 0, 87)]
         [TestCase(297, 100, 17.1f, 82, 0, 87)]
-        [TestCase(271, 75.9f, 52.7f, 137, 42, 226)] //Same as above, but with 43 instead of 42
+        [TestCase(271, 75.9f, 52.7f, 137, 43, 226)]
         public void HslToRgbTest(int h, float s, float l, int r, int g, int b)
         public void HslToRgbTest(int h, float s, float l, int r, int g, int b)
         {
         {
             Color rgb = ExColor.HslToRGB(h, s, l);
             Color rgb = ExColor.HslToRGB(h, s, l);
-            Assert.AreEqual(r, rgb.R);
-            Assert.AreEqual(g, rgb.G);
-            Assert.AreEqual(b, rgb.B);
+            int marginOfErrorR = Math.Abs(rgb.R - r);
+            int marginOfErrorG = Math.Abs(rgb.G - g);
+            int marginOfErrorB = Math.Abs(rgb.B - b);
+            Assert.LessOrEqual(marginOfErrorR, AcceptableMaringOfError);
+            Assert.LessOrEqual(marginOfErrorG, AcceptableMaringOfError);
+            Assert.LessOrEqual(marginOfErrorB, AcceptableMaringOfError);
 
 
         }
         }
     }
     }