From 43c87d70a6459a99608ec9b2cbf6f82c9e3eb1cb Mon Sep 17 00:00:00 2001 From: smellilac Date: Mon, 12 Aug 2024 14:19:20 +0300 Subject: [PATCH] Added CompareTo to Vector2 and unitTest --- Hypercube.Math/Vectors/Vector2.cs | 30 +++++- Hypercube.UnitTests/Math/CompareToTest.cs | 111 ++++++++++++++++++++++ 2 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 Hypercube.UnitTests/Math/CompareToTest.cs diff --git a/Hypercube.Math/Vectors/Vector2.cs b/Hypercube.Math/Vectors/Vector2.cs index a9c3441..ecc8155 100644 --- a/Hypercube.Math/Vectors/Vector2.cs +++ b/Hypercube.Math/Vectors/Vector2.cs @@ -5,7 +5,7 @@ namespace Hypercube.Math.Vectors; [StructLayout(LayoutKind.Sequential)] -public readonly partial struct Vector2 : IEquatable +public readonly partial struct Vector2 : IEquatable, IComparable { public static readonly Vector2 NaN = new(float.NaN, float.NaN); public static readonly Vector2 Zero = new(0, 0); @@ -262,4 +262,32 @@ public static float Cross(Vector2 a, Vector2 b) { return a.X * b.Y - a.Y * b.X; } + + public int CompareTo(Vector2 other) + { + return Length.CompareTo(other.Length); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public int CompareTo(Vector2 other, ComparisonType comparisonType) + { + switch (comparisonType) + { + case ComparisonType.XComponent: + return X.CompareTo(other.X); + case ComparisonType.YComponent: + return Y.CompareTo(other.Y); + case ComparisonType.Angle: + return Angle.CompareTo(other.Angle); + default: + throw new ArgumentOutOfRangeException(nameof(comparisonType), comparisonType, "Invalid ComparisonType"); + } + } + + public enum ComparisonType + { + XComponent, + YComponent, + Angle + } } \ No newline at end of file diff --git a/Hypercube.UnitTests/Math/CompareToTest.cs b/Hypercube.UnitTests/Math/CompareToTest.cs new file mode 100644 index 0000000..ea9ea3f --- /dev/null +++ b/Hypercube.UnitTests/Math/CompareToTest.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Hypercube.Math.Vectors; +using System.Text; +using System.Threading.Tasks; +using static Hypercube.Math.Vectors.Vector2; + +namespace Hypercube.UnitTests.Math; + +[TestFixture] +public class CompareToTest +{ + [Test] + public void CompareTo_LengthComparison_ReturnsExpectedResult() + { + // Arrange + Vector2 vector1 = new Vector2(3, 4); // Length = 5 + Vector2 vector2 = new Vector2(6, 8); // Length = 10 + + // Act + int result = vector1.CompareTo(vector2); + + // Assert + Assert.Less(result, 0); + } + + [Test] + public void CompareTo_XComponentComparison_ReturnsExpectedResult() + { + // Arrange + Vector2 vector1 = new Vector2(3, 4); + Vector2 vector2 = new Vector2(6, 4); + + // Act + int result = vector1.CompareTo(vector2, ComparisonType.XComponent); + + // Assert + Assert.Less(result, 0); // Expecting vector1 to be "less than" vector2 based on X component + } + + [Test] + public void CompareTo_YComponentComparison_ReturnsExpectedResult() + { + // Arrange + Vector2 vector1 = new Vector2(3, 4); + Vector2 vector2 = new Vector2(3, 2); + + // Act + int result = vector1.CompareTo(vector2, ComparisonType.YComponent); + + // Assert + Assert.Greater(result, 0); // Expecting vector1 to be "greater than" vector2 based on Y component + } + + [Test] + public void CompareTo_AngleComparison_ReturnsExpectedResult() + { + // Arrange + Vector2 vector1 = new Vector2(0, 1); // Angle = π/2 (90 degrees) + Vector2 vector2 = new Vector2(1, 0); // Angle = 0 degrees + + // Act + int result = vector1.CompareTo(vector2, ComparisonType.Angle); + + // Assert + Assert.Greater(result, 0); // Expecting vector1 to be "greater than" vector2 based on Angle + } + + [Test] + public void CompareTo_SameVectors_ReturnsZero() + { + // Arrange + Vector2 vector1 = new Vector2(3, 4); + Vector2 vector2 = new Vector2(3, 4); + + // Act & Assert + Assert.AreEqual(0, vector1.CompareTo(vector2)); + Assert.AreEqual(0, vector1.CompareTo(vector2, ComparisonType.XComponent)); + Assert.AreEqual(0, vector1.CompareTo(vector2, ComparisonType.YComponent)); + Assert.AreEqual(0, vector1.CompareTo(vector2, ComparisonType.Angle)); + } + + [Test] + public void CompareTo_DifferentComparisons_ReturnDifferentResults() + { + // Arrange + Vector2 vector1 = new Vector2(1, 2); + Vector2 vector2 = new Vector2(2, 1); + + // Act + int lengthComparison = vector1.CompareTo(vector2); + int xComponentComparison = vector1.CompareTo(vector2, ComparisonType.XComponent); + int yComponentComparison = vector1.CompareTo(vector2, ComparisonType.YComponent); + + // Assert + Assert.AreNotEqual(lengthComparison, xComponentComparison); + Assert.AreNotEqual(xComponentComparison, yComponentComparison); + } + + [Test] + public void CompareTo_InvalidComparisonType_ThrowsArgumentOutOfRangeException() + { + // Arrange + Vector2 vector1 = new Vector2(3, 4); + Vector2 vector2 = new Vector2(3, 4); + + // Act & Assert + Assert.Throws(() => vector1.CompareTo(vector2, (ComparisonType)999)); + } +}