Split the big test file into smaller tests

This commit is contained in:
Matthew Leibowitz 2017-03-20 22:07:49 +02:00
Родитель 863a6ba217
Коммит fee8fe9b5e
4 изменённых файлов: 323 добавлений и 230 удалений

Просмотреть файл

@ -11,7 +11,7 @@ namespace SkiaSharp.Tests
private const int PRECISION = 4; private const int PRECISION = 4;
[Test] [Test]
public void RectanlgeHasCorrectProperties() public void RectangleHasCorrectProperties()
{ {
var rect = new SKRect(15, 25, 55, 75); var rect = new SKRect(15, 25, 55, 75);
@ -28,7 +28,7 @@ namespace SkiaSharp.Tests
} }
[Test] [Test]
public void RectanlgeOffsetsCorrectly() public void RectangleOffsetsCorrectly()
{ {
var expected = new SKRect(25, 30, 65, 80); var expected = new SKRect(25, 30, 65, 80);
@ -43,7 +43,7 @@ namespace SkiaSharp.Tests
} }
[Test] [Test]
public void RectanlgeInflatesCorrectly() public void RectangleInflatesCorrectly()
{ {
var rect = new SKRect(15, 25, 55, 75); var rect = new SKRect(15, 25, 55, 75);
@ -61,7 +61,7 @@ namespace SkiaSharp.Tests
} }
[Test] [Test]
public void RectanlgeStandardizeCorrectly() public void RectangleStandardizeCorrectly()
{ {
var rect = new SKRect(5, 5, 15, 15); var rect = new SKRect(5, 5, 15, 15);
Assert.AreEqual(10, rect.Width); Assert.AreEqual(10, rect.Width);
@ -86,7 +86,7 @@ namespace SkiaSharp.Tests
} }
[Test] [Test]
public void RectanlgeAspectFitIsCorrect() public void RectangleAspectFitIsCorrect()
{ {
var bigRect = SKRect.Create(5, 5, 20, 20); var bigRect = SKRect.Create(5, 5, 20, 20);
var tallSize = new SKSize(5, 10); var tallSize = new SKSize(5, 10);
@ -106,7 +106,7 @@ namespace SkiaSharp.Tests
} }
[Test] [Test]
public void RectanlgeAspectFillIsCorrect() public void RectangleAspectFillIsCorrect()
{ {
var bigRect = SKRect.Create(5, 5, 20, 20); var bigRect = SKRect.Create(5, 5, 20, 20);
var tallSize = new SKSize(5, 10); var tallSize = new SKSize(5, 10);
@ -124,229 +124,5 @@ namespace SkiaSharp.Tests
Assert.AreEqual(40, fitWide.Width); Assert.AreEqual(40, fitWide.Width);
Assert.AreEqual(20, fitWide.Height); Assert.AreEqual(20, fitWide.Height);
} }
[Test]
public unsafe void FixedImageMaskIsHandledCorrectly()
{
byte rawMask = 1 << 7 | 0 << 6 | 0 << 5 | 1 << 4 | 1 << 3 | 0 << 2 | 1 << 1 | 1;
var buffer = new byte[] { rawMask };
var bounds = new SKRectI(0, 0, 8, 1);
UInt32 rowBytes = 1;
var format = SKMaskFormat.BW;
fixed (void* bufferPtr = buffer)
{
var mask = SKMask.Create(buffer, bounds, rowBytes, format);
Assert.AreEqual(rawMask, mask.GetAddr1(0, 0));
}
}
[Test]
public void MonochromeMaskBufferIsCopied()
{
byte rawMask = 1 << 7 | 0 << 6 | 0 << 5 | 1 << 4 | 1 << 3 | 0 << 2 | 1 << 1 | 1;
var buffer = new byte[] { rawMask };
var bounds = new SKRectI(0, 0, 8, 1);
UInt32 rowBytes = 1;
var format = SKMaskFormat.BW;
var mask = SKMask.Create(buffer, bounds, rowBytes, format);
Assert.AreEqual(rawMask, mask.GetAddr1(0, 0));
mask.FreeImage();
}
[Test]
public void Alpha8MaskBufferIsCopied()
{
var buffer = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
var bounds = new SKRectI(0, 0, 4, 2);
UInt32 rowBytes = 4;
var format = SKMaskFormat.A8;
var mask = SKMask.Create(buffer, bounds, rowBytes, format);
Assert.AreEqual(buffer[0], mask.GetAddr8(0, 0));
Assert.AreEqual(buffer[1], mask.GetAddr8(1, 0));
Assert.AreEqual(buffer[2], mask.GetAddr8(2, 0));
Assert.AreEqual(buffer[3], mask.GetAddr8(3, 0));
Assert.AreEqual(buffer[4], mask.GetAddr8(0, 1));
Assert.AreEqual(buffer[5], mask.GetAddr8(1, 1));
Assert.AreEqual(buffer[6], mask.GetAddr8(2, 1));
Assert.AreEqual(buffer[7], mask.GetAddr8(3, 1));
mask.FreeImage();
}
[Test]
public void ThirtyTwoBitMaskBufferIsCopied()
{
var buffer = new byte[]
{
0, 0, 255, 255,
255, 0, 0, 255,
0, 0, 255, 255,
255, 0, 0, 255,
0, 0, 255, 255,
255, 0, 0, 255,
0, 0, 255, 255,
255, 0, 0, 255
};
var bounds = new SKRectI(0, 0, 4, 2);
UInt32 rowBytes = 16;
var format = SKMaskFormat.Argb32;
var mask = SKMask.Create(buffer, bounds, rowBytes, format);
var red = SKColors.Red;
var blue = SKColors.Blue;
Assert.AreEqual((uint)red, mask.GetAddr32(0, 0));
Assert.AreEqual((uint)blue, mask.GetAddr32(1, 0));
Assert.AreEqual((uint)red, mask.GetAddr32(2, 0));
Assert.AreEqual((uint)blue, mask.GetAddr32(3, 0));
Assert.AreEqual((uint)red, mask.GetAddr32(0, 1));
Assert.AreEqual((uint)blue, mask.GetAddr32(1, 1));
Assert.AreEqual((uint)red, mask.GetAddr32(2, 1));
Assert.AreEqual((uint)blue, mask.GetAddr32(3, 1));
mask.FreeImage();
}
[Test]
public void Matrix44CreatesIdentity()
{
var matrix = SKMatrix44.CreateIdentity();
var expectedRowMajor = new float[] {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
};
var rowMajor = matrix.ToRowMajor();
Assert.AreEqual(expectedRowMajor, rowMajor);
}
[Test]
public void Matrix44Inverts()
{
var rowMajor = new float[] {
1, 2, 3, 0,
0, 1, 4, 0,
5, 6, 1, 0,
0, 0, 0, 1,
};
var expectedRowMajor = new float[] {
-11.5f, 8, 2.5f, 0,
10, -7, -2, 0,
-2.5f, 2, 0.5f, 0,
0, 0, 0, 1,
};
var determinant = 2f;
var matrix = SKMatrix44.FromRowMajor(rowMajor);
Assert.AreEqual(rowMajor, matrix.ToRowMajor());
Assert.AreEqual(determinant, matrix.Determinant());
var inverted = matrix.Invert();
Assert.AreEqual(1f / determinant, inverted.Determinant());
var actualRowMajor = inverted.ToRowMajor();
Assert.AreEqual(expectedRowMajor, actualRowMajor);
}
[Test]
public void Matrix44ConvertsToMatrix()
{
var rowMajor44 = new float[] {
2, 3, 4, 5,
4, 6, 8, 10,
6, 9, 12, 15,
8, 12, 16, 20,
};
var rowMajor = new float[] {
rowMajor44[0], rowMajor44[1], rowMajor44[3],
rowMajor44[4], rowMajor44[5], rowMajor44[7],
rowMajor44[12], rowMajor44[13], rowMajor44[15],
};
var matrix44 = SKMatrix44.FromRowMajor(rowMajor44);
Assert.AreEqual(rowMajor, matrix44.Matrix.Values);
matrix44 = SKMatrix44.CreateRotationDegrees(0, 0, 1, 45);
Assert.AreEqual(SKMatrix.MakeRotationDegrees(45).Values, matrix44.Matrix.Values);
}
[Test]
public void Matrix44MapsScalars()
{
// translate
var matrixTranslate = SKMatrix44.CreateTranslate(10, 20, 0);
var resultTranslateZero = matrixTranslate.MapScalars(0, 0, 0, 1);
var resultTranslateValue = matrixTranslate.MapScalars(5, 25, 0, 1);
Assert.AreEqual(new[] { 10f, 20f, 0f, 1f }, resultTranslateZero);
Assert.AreEqual(new[] { 15f, 45f, 0f, 1f }, resultTranslateValue);
// rotate
var matrixRotate = SKMatrix44.CreateRotationDegrees(0, 1, 0, 90);
var resultRotateZero = matrixRotate.MapScalars(0, 0, 0, 1);
var resultRotateValue = matrixRotate.MapScalars(5, 25, 0, 1);
Assert.AreEqual(new[] { 0f, 0f, 0f, 1f }, resultRotateZero);
Assert.AreEqual(new[] { 0f, 25f, -5f, 1f }, resultRotateValue.Select(v => (int)(v / EPSILON) * EPSILON));
}
[Test]
public void Matrix44MapsPoints()
{
// translate
var matrixTranslate = SKMatrix44.CreateTranslate(10, 20, 0);
var resultTranslateZero = matrixTranslate.MapPoint(SKPoint.Empty);
var resultTranslateValue = matrixTranslate.MapPoint(new SKPoint(5, 25));
Assert.AreEqual(new SKPoint(10f, 20f), resultTranslateZero);
Assert.AreEqual(new SKPoint(15f, 45f), resultTranslateValue);
// rotate
var matrixRotate = SKMatrix44.CreateRotationDegrees(0, 1, 0, 90);
var resultRotateZero = matrixRotate.MapPoint(SKPoint.Empty);
var resultRotateValue = matrixRotate.MapPoint(new SKPoint(5, 25));
Assert.AreEqual(new SKPoint(0f, 0f), resultRotateZero);
Assert.AreEqual(0, resultRotateValue.X, PRECISION);
Assert.AreEqual(25, resultRotateValue.Y, PRECISION);
}
[Test]
public void Matrix44MapsPointsBulk()
{
var rnd = new Random();
var matrixTranslate = SKMatrix44.CreateTranslate(10, 25, 0);
// generate some points
var points = new SKPoint[1000];
var results = new SKPoint[points.Length];
for (int i = 0; i < points.Length; i++)
{
points[i] = new SKPoint(rnd.Next(1000) / 10f, rnd.Next(1000) / 10f);
results[i] = new SKPoint(points[i].X + 10, points[i].Y + 25);
}
var actualResults = matrixTranslate.MapPoints(points);
Assert.AreEqual(results, actualResults);
}
} }
} }

Просмотреть файл

@ -0,0 +1,67 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using NUnit.Framework;
namespace SkiaSharp.Tests
{
public class SKDocumentTest : SKTest
{
[Test]
public void CanCreatePdf()
{
using (var stream = new MemoryStream())
using (var managed = new SKManagedWStream(stream, false))
{
using (var doc = SKDocument.CreatePdf(managed))
{
Assert.IsNotNull(doc);
Assert.IsNotNull(doc.BeginPage(100, 100));
doc.EndPage();
doc.Close();
}
Assert.IsTrue(stream.Length > 0);
Assert.IsTrue(stream.Position > 0);
}
}
[Test]
public void CanCreateXps()
{
// XPS is only supported on Windows
using (var stream = new MemoryStream())
using (var managed = new SKManagedWStream(stream, false))
{
using (var doc = SKDocument.CreateXps(managed))
{
if (IsWindows)
{
Assert.IsNotNull(doc);
Assert.IsNotNull(doc.BeginPage(100, 100));
doc.EndPage();
doc.Close();
}
else
{
Assert.IsNull(doc);
}
}
if (IsWindows)
{
Assert.IsTrue(stream.Length > 0);
Assert.IsTrue(stream.Position > 0);
}
else
{
Assert.IsTrue(stream.Length == 0);
Assert.IsTrue(stream.Position == 0);
}
}
}
}
}

102
tests/Tests/SKMaskTest.cs Normal file
Просмотреть файл

@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
namespace SkiaSharp.Tests
{
public class SKMaskTest : SKTest
{
private const float EPSILON = 0.0001f;
private const int PRECISION = 4;
[Test]
public unsafe void FixedImageMaskIsHandledCorrectly()
{
byte rawMask = 1 << 7 | 0 << 6 | 0 << 5 | 1 << 4 | 1 << 3 | 0 << 2 | 1 << 1 | 1;
var buffer = new byte[] { rawMask };
var bounds = new SKRectI(0, 0, 8, 1);
UInt32 rowBytes = 1;
var format = SKMaskFormat.BW;
fixed (void* bufferPtr = buffer)
{
var mask = SKMask.Create(buffer, bounds, rowBytes, format);
Assert.AreEqual(rawMask, mask.GetAddr1(0, 0));
}
}
[Test]
public void MonochromeMaskBufferIsCopied()
{
byte rawMask = 1 << 7 | 0 << 6 | 0 << 5 | 1 << 4 | 1 << 3 | 0 << 2 | 1 << 1 | 1;
var buffer = new byte[] { rawMask };
var bounds = new SKRectI(0, 0, 8, 1);
UInt32 rowBytes = 1;
var format = SKMaskFormat.BW;
var mask = SKMask.Create(buffer, bounds, rowBytes, format);
Assert.AreEqual(rawMask, mask.GetAddr1(0, 0));
mask.FreeImage();
}
[Test]
public void Alpha8MaskBufferIsCopied()
{
var buffer = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
var bounds = new SKRectI(0, 0, 4, 2);
UInt32 rowBytes = 4;
var format = SKMaskFormat.A8;
var mask = SKMask.Create(buffer, bounds, rowBytes, format);
Assert.AreEqual(buffer[0], mask.GetAddr8(0, 0));
Assert.AreEqual(buffer[1], mask.GetAddr8(1, 0));
Assert.AreEqual(buffer[2], mask.GetAddr8(2, 0));
Assert.AreEqual(buffer[3], mask.GetAddr8(3, 0));
Assert.AreEqual(buffer[4], mask.GetAddr8(0, 1));
Assert.AreEqual(buffer[5], mask.GetAddr8(1, 1));
Assert.AreEqual(buffer[6], mask.GetAddr8(2, 1));
Assert.AreEqual(buffer[7], mask.GetAddr8(3, 1));
mask.FreeImage();
}
[Test]
public void ThirtyTwoBitMaskBufferIsCopied()
{
var buffer = new byte[]
{
0, 0, 255, 255,
255, 0, 0, 255,
0, 0, 255, 255,
255, 0, 0, 255,
0, 0, 255, 255,
255, 0, 0, 255,
0, 0, 255, 255,
255, 0, 0, 255
};
var bounds = new SKRectI(0, 0, 4, 2);
UInt32 rowBytes = 16;
var format = SKMaskFormat.Argb32;
var mask = SKMask.Create(buffer, bounds, rowBytes, format);
var red = SKColors.Red;
var blue = SKColors.Blue;
Assert.AreEqual((uint)red, mask.GetAddr32(0, 0));
Assert.AreEqual((uint)blue, mask.GetAddr32(1, 0));
Assert.AreEqual((uint)red, mask.GetAddr32(2, 0));
Assert.AreEqual((uint)blue, mask.GetAddr32(3, 0));
Assert.AreEqual((uint)red, mask.GetAddr32(0, 1));
Assert.AreEqual((uint)blue, mask.GetAddr32(1, 1));
Assert.AreEqual((uint)red, mask.GetAddr32(2, 1));
Assert.AreEqual((uint)blue, mask.GetAddr32(3, 1));
mask.FreeImage();
}
}
}

Просмотреть файл

@ -0,0 +1,148 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
namespace SkiaSharp.Tests
{
public class SKMatrixTest : SKTest
{
private const float EPSILON = 0.0001f;
private const int PRECISION = 4;
[Test]
public void Matrix44CreatesIdentity()
{
var matrix = SKMatrix44.CreateIdentity();
var expectedRowMajor = new float[] {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
};
var rowMajor = matrix.ToRowMajor();
Assert.AreEqual(expectedRowMajor, rowMajor);
}
[Test]
public void Matrix44Inverts()
{
var rowMajor = new float[] {
1, 2, 3, 0,
0, 1, 4, 0,
5, 6, 1, 0,
0, 0, 0, 1,
};
var expectedRowMajor = new float[] {
-11.5f, 8, 2.5f, 0,
10, -7, -2, 0,
-2.5f, 2, 0.5f, 0,
0, 0, 0, 1,
};
var determinant = 2f;
var matrix = SKMatrix44.FromRowMajor(rowMajor);
Assert.AreEqual(rowMajor, matrix.ToRowMajor());
Assert.AreEqual(determinant, matrix.Determinant());
var inverted = matrix.Invert();
Assert.AreEqual(1f / determinant, inverted.Determinant());
var actualRowMajor = inverted.ToRowMajor();
Assert.AreEqual(expectedRowMajor, actualRowMajor);
}
[Test]
public void Matrix44ConvertsToMatrix()
{
var rowMajor44 = new float[] {
2, 3, 4, 5,
4, 6, 8, 10,
6, 9, 12, 15,
8, 12, 16, 20,
};
var rowMajor = new float[] {
rowMajor44[0], rowMajor44[1], rowMajor44[3],
rowMajor44[4], rowMajor44[5], rowMajor44[7],
rowMajor44[12], rowMajor44[13], rowMajor44[15],
};
var matrix44 = SKMatrix44.FromRowMajor(rowMajor44);
Assert.AreEqual(rowMajor, matrix44.Matrix.Values);
matrix44 = SKMatrix44.CreateRotationDegrees(0, 0, 1, 45);
Assert.AreEqual(SKMatrix.MakeRotationDegrees(45).Values, matrix44.Matrix.Values);
}
[Test]
public void Matrix44MapsScalars()
{
// translate
var matrixTranslate = SKMatrix44.CreateTranslate(10, 20, 0);
var resultTranslateZero = matrixTranslate.MapScalars(0, 0, 0, 1);
var resultTranslateValue = matrixTranslate.MapScalars(5, 25, 0, 1);
Assert.AreEqual(new[] { 10f, 20f, 0f, 1f }, resultTranslateZero);
Assert.AreEqual(new[] { 15f, 45f, 0f, 1f }, resultTranslateValue);
// rotate
var matrixRotate = SKMatrix44.CreateRotationDegrees(0, 1, 0, 90);
var resultRotateZero = matrixRotate.MapScalars(0, 0, 0, 1);
var resultRotateValue = matrixRotate.MapScalars(5, 25, 0, 1);
Assert.AreEqual(new[] { 0f, 0f, 0f, 1f }, resultRotateZero);
Assert.AreEqual(new[] { 0f, 25f, -5f, 1f }, resultRotateValue.Select(v => (int)(v / EPSILON) * EPSILON));
}
[Test]
public void Matrix44MapsPoints()
{
// translate
var matrixTranslate = SKMatrix44.CreateTranslate(10, 20, 0);
var resultTranslateZero = matrixTranslate.MapPoint(SKPoint.Empty);
var resultTranslateValue = matrixTranslate.MapPoint(new SKPoint(5, 25));
Assert.AreEqual(new SKPoint(10f, 20f), resultTranslateZero);
Assert.AreEqual(new SKPoint(15f, 45f), resultTranslateValue);
// rotate
var matrixRotate = SKMatrix44.CreateRotationDegrees(0, 1, 0, 90);
var resultRotateZero = matrixRotate.MapPoint(SKPoint.Empty);
var resultRotateValue = matrixRotate.MapPoint(new SKPoint(5, 25));
Assert.AreEqual(new SKPoint(0f, 0f), resultRotateZero);
Assert.AreEqual(0, resultRotateValue.X, PRECISION);
Assert.AreEqual(25, resultRotateValue.Y, PRECISION);
}
[Test]
public void Matrix44MapsPointsBulk()
{
var rnd = new Random();
var matrixTranslate = SKMatrix44.CreateTranslate(10, 25, 0);
// generate some points
var points = new SKPoint[1000];
var results = new SKPoint[points.Length];
for (int i = 0; i < points.Length; i++)
{
points[i] = new SKPoint(rnd.Next(1000) / 10f, rnd.Next(1000) / 10f);
results[i] = new SKPoint(points[i].X + 10, points[i].Y + 25);
}
var actualResults = matrixTranslate.MapPoints(points);
Assert.AreEqual(results, actualResults);
}
}
}