1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-06 01:47:06 +02:00

added a few more tests

This commit is contained in:
Ell 2021-03-18 18:31:59 +01:00
parent 10c1cc8905
commit c71fb58760
4 changed files with 164 additions and 4 deletions

31
Tests/CollectionTests.cs Normal file
View file

@ -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);
}
}
}

77
Tests/DataTests.cs Normal file
View file

@ -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<TestObject>(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);
}
}
}
}

24
Tests/DirectionTests.cs Normal file
View file

@ -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);
}
}
}

View file

@ -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);
}
}