Commit 0f1186d9 authored by HeinrichAD's avatar HeinrichAD
Browse files

Unit Test Expected Size correction

Unit Test Expected Size correction with same buffer like in Unit Test
MultiThreadingTest.

Add image comparer.
parent 69778f59
......@@ -7,12 +7,21 @@ using System.IO;
namespace Svg.UnitTests
{
/// <summary>
/// Test Class of rendering SVGs with marker-end elements.
/// Based on Issue 212.
/// </summary>
/// <remarks>
/// Test use the following embedded resources:
/// - Issue212_MakerEnd\OperatingPlan.svg
/// </remarks>
[TestClass]
public class MarkerEndTest : SvgTestHelper
{
protected override string TestResource { get { return GetFullResourceString("Issue212_MakerEnd.OperatingPlan.svg"); } }
protected override int ExpectedSize { get { return 5410; } }
protected override int ExpectedSize { get { return 5000; } } //5321 //5410
[TestMethod]
......@@ -25,6 +34,7 @@ namespace Svg.UnitTests
[TestMethod]
public void TestArrowCodeCreation()
{
// Sample code from Issue 212. Thanks to podostro.
const int width = 50;
const int height = 50;
......
......@@ -24,7 +24,7 @@ namespace Svg.UnitTests
public class MetafileRenderingTest : SvgTestHelper
{
protected override string TestResource { get { return GetFullResourceString("Issue210_Metafile.3DSceneSnapshotBIG.svg"); } }
protected override int ExpectedSize { get { return 12896; } }
protected override int ExpectedSize { get { return 12500; } } //12896
[TestMethod]
......
......@@ -21,8 +21,8 @@ namespace Svg.UnitTests
private const string PrivateFont = "Issue204_PrivateFont.BrushScriptMT2.ttf";
//private const string PrivateFontName = "Brush Script MT2";
protected override int ExpectedSize { get { return 3512; } }
protected override int ExpectedSize { get { return 3200; } } //3512
[TestMethod]
public void TestPrivateFont()
......
......@@ -23,7 +23,7 @@ namespace Svg.UnitTests
throw new NotImplementedException(msg);
}
}
/// <summary>
/// Full Unit Test resource string for test file.
......@@ -72,7 +72,7 @@ namespace Svg.UnitTests
protected virtual string GetFullResourceString(string resourcePath)
{
const string DefaultResourcesDir = "Resources";
return string.Format("{0}.{1}.{2}",
return string.Format("{0}.{1}.{2}",
this.GetType().Assembly.GetName().Name,
DefaultResourcesDir,
resourcePath);
......@@ -231,5 +231,88 @@ namespace Svg.UnitTests
Assert.IsTrue(ms.Length >= ExpectedSize, "Svg file does not match expected minimum size.");
}
}
/// <summary>
/// Compare Images.
/// </summary>
/// <param name="img1">Image 1.</param>
/// <param name="img2">Image 2.</param>
/// <returns>If images are completely equal: true; otherwise: false</returns>
protected virtual bool ImagesAreEqual(Bitmap img1, Bitmap img2)
{
float imgEqualPercentage; // To ignore.
return ImagesAreEqual(img1, img2, out imgEqualPercentage);
}
/// <summary>
/// Compare Images.
/// </summary>
/// <param name="img1">Image 1.</param>
/// <param name="img2">Image 2.</param>
/// <param name="imgEqualPercentage">Image equal value in percentage. 0.0% == completely unequal. 100.0% == completely equal.</param>
/// <returns>If images are completely equal: true; otherwise: false</returns>
protected virtual bool ImagesAreEqual(Bitmap img1, Bitmap img2, out float imgEqualPercentage)
{
Bitmap imgDiff; // To ignore.
return ImagesAreEqual(img1, img2, out imgEqualPercentage, out imgDiff);
}
/// <summary>
/// Compare Images.
/// </summary>
/// <param name="img1">Image 1.</param>
/// <param name="img2">Image 2.</param>
/// <param name="imgEqualPercentage">Image equal value in percentage. 0.0% == completely unequal. 100.0% == completely equal.</param>
/// <param name="imgDiff">Image with red pixel where <paramref name="img1"/> and <paramref name="img2"/> are unequal.</param>
/// <returns>If images are completely equal: true; otherwise: false</returns>
protected virtual bool ImagesAreEqual(Bitmap img1, Bitmap img2, out float imgEqualPercentage, out Bitmap imgDiff)
{
// Defaults.
var diffColor = Color.Red;
// Reset.
imgEqualPercentage = 0;
imgDiff = null;
// Requirements.
if (img1 == null)
return false;
if (img2 == null)
return false;
if (img1.Size.Width < 1 && img1.Height < 1)
return false;
if (!img1.Size.Equals(img2.Size))
return false;
// Compare bitmaps.
imgDiff = new Bitmap(img1.Size.Width, img1.Size.Height);
int diffPixelCount = 0;
for (int i = 0; i < img1.Width; ++i)
{
for (int j = 0; j < img1.Height; ++j)
{
Color color;
if ((color = img1.GetPixel(i, j)) == img2.GetPixel(i, j))
{
imgDiff.SetPixel(i, j, color);
}
else
{
++diffPixelCount;
imgDiff.SetPixel(i, j, diffColor);
}
}
}
// Calculate percentage.
int totalPixelCount = img1.Width * img1.Height;
var imgDiffFactor = ((float)diffPixelCount / totalPixelCount);
imgEqualPercentage = imgDiffFactor * 100;
return (imgDiffFactor == 1f);
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment