From 43c87d70a6459a99608ec9b2cbf6f82c9e3eb1cb Mon Sep 17 00:00:00 2001 From: smellilac Date: Mon, 12 Aug 2024 14:19:20 +0300 Subject: [PATCH 1/3] 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)); + } +} From b034b60031858f244cd9b5968557bf45798532a2 Mon Sep 17 00:00:00 2001 From: smellilac Date: Fri, 23 Aug 2024 15:16:18 +0300 Subject: [PATCH 2/3] corrections --- Hypercube.Math/Vectors/Vector2.cs | 3 +- Hypercube.UnitTests/Math/CompareToTest.cs | 44 +++++++++++------------ 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/Hypercube.Math/Vectors/Vector2.cs b/Hypercube.Math/Vectors/Vector2.cs index ecc8155..f498426 100644 --- a/Hypercube.Math/Vectors/Vector2.cs +++ b/Hypercube.Math/Vectors/Vector2.cs @@ -263,9 +263,10 @@ public static float Cross(Vector2 a, Vector2 b) return a.X * b.Y - a.Y * b.X; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public int CompareTo(Vector2 other) { - return Length.CompareTo(other.Length); + return LengthSquared.CompareTo(other.LengthSquared); } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/Hypercube.UnitTests/Math/CompareToTest.cs b/Hypercube.UnitTests/Math/CompareToTest.cs index ea9ea3f..d286a63 100644 --- a/Hypercube.UnitTests/Math/CompareToTest.cs +++ b/Hypercube.UnitTests/Math/CompareToTest.cs @@ -9,17 +9,17 @@ namespace Hypercube.UnitTests.Math; [TestFixture] -public class CompareToTest +public sealed 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 + var vector1 = new Vector2(3, 4); // Length = 5 + var vector2 = new Vector2(6, 8); // Length = 10 // Act - int result = vector1.CompareTo(vector2); + var result = vector1.CompareTo(vector2); // Assert Assert.Less(result, 0); @@ -29,11 +29,11 @@ public void CompareTo_LengthComparison_ReturnsExpectedResult() public void CompareTo_XComponentComparison_ReturnsExpectedResult() { // Arrange - Vector2 vector1 = new Vector2(3, 4); - Vector2 vector2 = new Vector2(6, 4); + var vector1 = new Vector2(3, 4); + var vector2 = new Vector2(6, 4); // Act - int result = vector1.CompareTo(vector2, ComparisonType.XComponent); + var result = vector1.CompareTo(vector2, ComparisonType.XComponent); // Assert Assert.Less(result, 0); // Expecting vector1 to be "less than" vector2 based on X component @@ -43,11 +43,11 @@ public void CompareTo_XComponentComparison_ReturnsExpectedResult() public void CompareTo_YComponentComparison_ReturnsExpectedResult() { // Arrange - Vector2 vector1 = new Vector2(3, 4); - Vector2 vector2 = new Vector2(3, 2); + var vector1 = new Vector2(3, 4); + var vector2 = new Vector2(3, 2); // Act - int result = vector1.CompareTo(vector2, ComparisonType.YComponent); + var result = vector1.CompareTo(vector2, ComparisonType.YComponent); // Assert Assert.Greater(result, 0); // Expecting vector1 to be "greater than" vector2 based on Y component @@ -57,11 +57,11 @@ public void CompareTo_YComponentComparison_ReturnsExpectedResult() 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 + var vector1 = new Vector2(0, 1); // Angle = π/2 (90 degrees) + var vector2 = new Vector2(1, 0); // Angle = 0 degrees // Act - int result = vector1.CompareTo(vector2, ComparisonType.Angle); + var result = vector1.CompareTo(vector2, ComparisonType.Angle); // Assert Assert.Greater(result, 0); // Expecting vector1 to be "greater than" vector2 based on Angle @@ -71,8 +71,8 @@ public void CompareTo_AngleComparison_ReturnsExpectedResult() public void CompareTo_SameVectors_ReturnsZero() { // Arrange - Vector2 vector1 = new Vector2(3, 4); - Vector2 vector2 = new Vector2(3, 4); + var vector1 = new Vector2(3, 4); + var vector2 = new Vector2(3, 4); // Act & Assert Assert.AreEqual(0, vector1.CompareTo(vector2)); @@ -85,13 +85,13 @@ public void CompareTo_SameVectors_ReturnsZero() public void CompareTo_DifferentComparisons_ReturnDifferentResults() { // Arrange - Vector2 vector1 = new Vector2(1, 2); - Vector2 vector2 = new Vector2(2, 1); + var vector1 = new Vector2(1, 2); + var 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); + var lengthComparison = vector1.CompareTo(vector2); + var xComponentComparison = vector1.CompareTo(vector2, ComparisonType.XComponent); + var yComponentComparison = vector1.CompareTo(vector2, ComparisonType.YComponent); // Assert Assert.AreNotEqual(lengthComparison, xComponentComparison); @@ -102,8 +102,8 @@ public void CompareTo_DifferentComparisons_ReturnDifferentResults() public void CompareTo_InvalidComparisonType_ThrowsArgumentOutOfRangeException() { // Arrange - Vector2 vector1 = new Vector2(3, 4); - Vector2 vector2 = new Vector2(3, 4); + var vector1 = new Vector2(3, 4); + var vector2 = new Vector2(3, 4); // Act & Assert Assert.Throws(() => vector1.CompareTo(vector2, (ComparisonType)999)); From cac76dfae06ab64ab38d4a10ce468612a8a82686 Mon Sep 17 00:00:00 2001 From: smellilac Date: Mon, 26 Aug 2024 16:01:50 +0300 Subject: [PATCH 3/3] Remove enum and the method using it --- Hypercube.Math/Vectors/Vector2.cs | 21 ++----------- Hypercube.UnitTests/Math/CompareToTest.cs | 37 +++++++---------------- 2 files changed, 13 insertions(+), 45 deletions(-) diff --git a/Hypercube.Math/Vectors/Vector2.cs b/Hypercube.Math/Vectors/Vector2.cs index f498426..3e8b0cc 100644 --- a/Hypercube.Math/Vectors/Vector2.cs +++ b/Hypercube.Math/Vectors/Vector2.cs @@ -270,25 +270,8 @@ public int CompareTo(Vector2 other) } [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 + public int CompareTo(Vector2 other, Func selector) { - XComponent, - YComponent, - Angle + return selector(this).CompareTo(selector(other)); } } \ No newline at end of file diff --git a/Hypercube.UnitTests/Math/CompareToTest.cs b/Hypercube.UnitTests/Math/CompareToTest.cs index d286a63..6f716ee 100644 --- a/Hypercube.UnitTests/Math/CompareToTest.cs +++ b/Hypercube.UnitTests/Math/CompareToTest.cs @@ -1,10 +1,6 @@ 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; +using NUnit.Framework; namespace Hypercube.UnitTests.Math; @@ -22,7 +18,7 @@ public void CompareTo_LengthComparison_ReturnsExpectedResult() var result = vector1.CompareTo(vector2); // Assert - Assert.Less(result, 0); + Assert.Less(result, 0); } [Test] @@ -33,7 +29,7 @@ public void CompareTo_XComponentComparison_ReturnsExpectedResult() var vector2 = new Vector2(6, 4); // Act - var result = vector1.CompareTo(vector2, ComparisonType.XComponent); + var result = vector1.CompareTo(vector2, v => v.X); // Assert Assert.Less(result, 0); // Expecting vector1 to be "less than" vector2 based on X component @@ -47,7 +43,7 @@ public void CompareTo_YComponentComparison_ReturnsExpectedResult() var vector2 = new Vector2(3, 2); // Act - var result = vector1.CompareTo(vector2, ComparisonType.YComponent); + var result = vector1.CompareTo(vector2, v => v.Y); // Assert Assert.Greater(result, 0); // Expecting vector1 to be "greater than" vector2 based on Y component @@ -61,7 +57,7 @@ public void CompareTo_AngleComparison_ReturnsExpectedResult() var vector2 = new Vector2(1, 0); // Angle = 0 degrees // Act - var result = vector1.CompareTo(vector2, ComparisonType.Angle); + var result = vector1.CompareTo(vector2, v => v.Angle); // Assert Assert.Greater(result, 0); // Expecting vector1 to be "greater than" vector2 based on Angle @@ -76,9 +72,9 @@ public void CompareTo_SameVectors_ReturnsZero() // 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)); + Assert.AreEqual(0, vector1.CompareTo(vector2, v => v.X)); + Assert.AreEqual(0, vector1.CompareTo(vector2, v => v.Y)); + Assert.AreEqual(0, vector1.CompareTo(vector2, v => v.Angle)); } [Test] @@ -90,22 +86,11 @@ public void CompareTo_DifferentComparisons_ReturnDifferentResults() // Act var lengthComparison = vector1.CompareTo(vector2); - var xComponentComparison = vector1.CompareTo(vector2, ComparisonType.XComponent); - var yComponentComparison = vector1.CompareTo(vector2, ComparisonType.YComponent); + var xComponentComparison = vector1.CompareTo(vector2, v => v.X); + var yComponentComparison = vector1.CompareTo(vector2, v => v.Y); // Assert Assert.AreNotEqual(lengthComparison, xComponentComparison); Assert.AreNotEqual(xComponentComparison, yComponentComparison); } - - [Test] - public void CompareTo_InvalidComparisonType_ThrowsArgumentOutOfRangeException() - { - // Arrange - var vector1 = new Vector2(3, 4); - var vector2 = new Vector2(3, 4); - - // Act & Assert - Assert.Throws(() => vector1.CompareTo(vector2, (ComparisonType)999)); - } -} +} \ No newline at end of file