From c71fb587602d6ca9a15d260c633adf85047c3fe4 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 18 Mar 2021 18:31:59 +0100 Subject: [PATCH] added a few more tests --- Tests/CollectionTests.cs | 31 ++++++++++++++++ Tests/DataTests.cs | 77 ++++++++++++++++++++++++++++++++++++++++ Tests/DirectionTests.cs | 24 +++++++++++++ Tests/NumberTests.cs | 36 ++++++++++++++++--- 4 files changed, 164 insertions(+), 4 deletions(-) create mode 100644 Tests/CollectionTests.cs create mode 100644 Tests/DataTests.cs create mode 100644 Tests/DirectionTests.cs diff --git a/Tests/CollectionTests.cs b/Tests/CollectionTests.cs new file mode 100644 index 0000000..f8eab18 --- /dev/null +++ b/Tests/CollectionTests.cs @@ -0,0 +1,31 @@ +using MLEM.Extensions; +using NUnit.Framework; + +namespace Tests { + public class CollectionTests { + + [Test] + public void TestCombinations() { + var things = new[] { + new[] {'1', '2', '3'}, + new[] {'A', 'B'}, + new[] {'+', '-'} + }; + + var expected = new[] { + new[] {'1', 'A', '+'}, new[] {'1', 'A', '-'}, new[] {'1', 'B', '+'}, new[] {'1', 'B', '-'}, + new[] {'2', 'A', '+'}, new[] {'2', 'A', '-'}, new[] {'2', 'B', '+'}, new[] {'2', 'B', '-'}, + new[] {'3', 'A', '+'}, new[] {'3', 'A', '-'}, new[] {'3', 'B', '+'}, new[] {'3', 'B', '-'} + }; + Assert.AreEqual(things.Combinations(), expected); + + var indices = new[] { + new[] {0, 0, 0}, new[] {0, 0, 1}, new[] {0, 1, 0}, new[] {0, 1, 1}, + new[] {1, 0, 0}, new[] {1, 0, 1}, new[] {1, 1, 0}, new[] {1, 1, 1}, + new[] {2, 0, 0}, new[] {2, 0, 1}, new[] {2, 1, 0}, new[] {2, 1, 1} + }; + Assert.AreEqual(things.IndexCombinations(), indices); + } + + } +} \ No newline at end of file diff --git a/Tests/DataTests.cs b/Tests/DataTests.cs new file mode 100644 index 0000000..32e5718 --- /dev/null +++ b/Tests/DataTests.cs @@ -0,0 +1,77 @@ +using System; +using System.IO; +using Microsoft.Xna.Framework; +using MLEM.Data; +using MLEM.Data.Json; +using MLEM.Misc; +using Newtonsoft.Json; +using NUnit.Framework; + +namespace Tests { + public class DataTests { + + private readonly TestObject testObject = new TestObject(Vector2.One, "test") { + Vec = new Vector2(10, 20), + Point = new Point(20, 30), + Dir = Direction2.Left, + OtherTest = new TestObject(Vector2.One, "other") { + Vec = new Vector2(70, 30), + Dir = Direction2.Right + } + }; + + [Test] + public void TestJsonSerializers() { + var serializer = JsonConverters.AddAll(new JsonSerializer()); + + var writer = new StringWriter(); + serializer.Serialize(writer, this.testObject); + var ret = writer.ToString(); + + Assert.AreEqual(ret, "{\"Vec\":\"10 20\",\"Point\":\"20 30\",\"OtherTest\":{\"Vec\":\"70 30\",\"Point\":\"0 0\",\"OtherTest\":null,\"Dir\":\"Right\"},\"Dir\":\"Left\"}"); + + var read = serializer.Deserialize(new JsonTextReader(new StringReader(ret))); + Assert.AreEqual(this.testObject, read); + } + + [Test] + public void TestCopy() { + var copy = this.testObject.Copy(); + Assert.AreEqual(this.testObject, copy); + Assert.AreSame(this.testObject.OtherTest, copy.OtherTest); + + var deepCopy = this.testObject.DeepCopy(); + Assert.AreEqual(this.testObject, deepCopy); + Assert.AreNotSame(this.testObject.OtherTest, deepCopy.OtherTest); + } + + private class TestObject { + + public Vector2 Vec; + public Point Point; + public Direction2 Dir { get; set; } + public TestObject OtherTest; + + public TestObject(Vector2 test, string test2) { + } + + public override string ToString() { + return $"{nameof(this.Vec)}: {this.Vec}, {nameof(this.Point)}: {this.Point}, {nameof(this.OtherTest)}: {this.OtherTest}, {nameof(this.Dir)}: {this.Dir}"; + } + + protected bool Equals(TestObject other) { + return this.Vec.Equals(other.Vec) && this.Point.Equals(other.Point) && Equals(this.OtherTest, other.OtherTest) && this.Dir == other.Dir; + } + + public override bool Equals(object obj) { + return ReferenceEquals(this, obj) || obj is TestObject other && Equals(other); + } + + public override int GetHashCode() { + return HashCode.Combine(this.Vec, this.Point, this.OtherTest, (int) this.Dir); + } + + } + + } +} \ No newline at end of file diff --git a/Tests/DirectionTests.cs b/Tests/DirectionTests.cs new file mode 100644 index 0000000..01e14bc --- /dev/null +++ b/Tests/DirectionTests.cs @@ -0,0 +1,24 @@ +using System.Drawing; +using Microsoft.Xna.Framework; +using MLEM.Misc; +using NUnit.Framework; + +namespace Tests { + public class DirectionTests { + + [Test] + public void TestDirections() { + Assert.AreEqual(new Vector2(0.5F, 0.5F).ToDirection(), Direction2.DownRight); + Assert.AreEqual(new Vector2(0.25F, 0.5F).ToDirection(), Direction2.DownRight); + Assert.AreEqual(new Vector2(0.15F, 0.5F).ToDirection(), Direction2.Down); + } + + [Test] + public void Test90Directions() { + Assert.AreEqual(new Vector2(0.75F, 0.5F).To90Direction(), Direction2.Right); + Assert.AreEqual(new Vector2(0.5F, 0.5F).To90Direction(), Direction2.Down); + Assert.AreEqual(new Vector2(0.25F, 0.5F).To90Direction(), Direction2.Down); + } + + } +} \ No newline at end of file diff --git a/Tests/NumberTests.cs b/Tests/NumberTests.cs index ce51496..5eb1f44 100644 --- a/Tests/NumberTests.cs +++ b/Tests/NumberTests.cs @@ -1,5 +1,6 @@ using Microsoft.Xna.Framework; using MLEM.Extensions; +using MLEM.Misc; using NUnit.Framework; namespace Tests { @@ -23,13 +24,40 @@ namespace Tests { public void TestEquals() { Assert.IsTrue(0.25F.Equals(0.26F, 0.01F)); Assert.IsFalse(0.25F.Equals(0.26F, 0.009F)); + + Assert.IsTrue(new Vector2(0.25F, 0).Equals(new Vector2(0.3F, 0), 0.1F)); + Assert.IsFalse(new Vector2(0.25F, 0.5F).Equals(new Vector2(0.3F, 0.25F), 0.1F)); + + Assert.IsTrue(new Vector3(0.25F, 0, 3.5F).Equals(new Vector3(0.3F, 0, 3.45F), 0.1F)); + Assert.IsFalse(new Vector3(0.25F, 0.5F, 0).Equals(new Vector3(0.3F, 0.25F, 0), 0.1F)); + + Assert.IsTrue(new Vector4(0.25F, 0, 3.5F, 0.75F).Equals(new Vector4(0.3F, 0, 3.45F, 0.7F), 0.1F)); + Assert.IsFalse(new Vector4(0.25F, 0.5F, 0, 1).Equals(new Vector4(0.3F, 0.25F, 0, 0.9F), 0.1F)); } [Test] - public void TestMatrixOps() { - var matrix = Matrix.CreateRotationX(2) * Matrix.CreateScale(2.5F); - Assert.AreEqual(matrix.Scale(), new Vector3(2.5F)); - Assert.AreEqual(matrix.Rotation(), Quaternion.CreateFromAxisAngle(Vector3.UnitX, 2)); + public void TestPointExtensions() { + Assert.AreEqual(new Point(4, 3).Multiply(3), new Point(12, 9)); + Assert.AreEqual(new Point(17, 12).Divide(3), new Point(5, 4)); + Assert.AreEqual(new Point(4, 12).Transform(Matrix.CreateTranslation(2, 0, 0) * Matrix.CreateScale(2, 2, 2)), new Point(12, 24)); + } + + [Test] + public void TestMatrixOps([Range(0.5F, 2, 0.5F)] float scale, [Range(-1, 1, 1F)] float rotationX) { + var matrix = Matrix.CreateRotationX(rotationX) * Matrix.CreateScale(scale, scale, scale); + Assert.IsTrue(matrix.Scale().Equals(new Vector3(scale), 0.001F), $"{matrix.Scale()} does not equal {new Vector2(scale)}"); + Assert.AreEqual(matrix.Rotation(), Quaternion.CreateFromAxisAngle(Vector3.UnitX, rotationX)); + } + + [Test] + public void TestPenetrate() { + new RectangleF(2, 2, 4, 4).Penetrate(new RectangleF(3, 2, 4, 4), out var normal, out var penetration); + Assert.AreEqual(normal, new Vector2(1, 0)); + Assert.AreEqual(penetration, 3); + + new RectangleF(-10, 10, 5, 5).Penetrate(new RectangleF(25, 25, 10, 10), out normal, out penetration); + Assert.AreEqual(normal, Vector2.Zero); + Assert.AreEqual(penetration, 0); } }