Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CSMath.Tests/AssertUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Xunit;
using CSMath.Extensions;
using Xunit;

namespace CSMath.Tests;

Expand Down
28 changes: 28 additions & 0 deletions CSMath.Tests/Extensions/DoubleExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using CSMath.Extensions;
using Xunit;

namespace CSMath.Tests.Extensions;

public class DoubleExtensionsTests
{
[Theory]
[InlineData(0.05, 0.1, true)]
[InlineData(0.2, 0.1, false)]
public void IsZero_CustomThreshold_ReturnsExpected(double value, double threshold, bool expected)
{
Assert.Equal(expected, value.IsZero(threshold));
}

[Theory]
[InlineData(0.0, true)]
[InlineData(5e-13, true)]
[InlineData(-5e-13, true)]
[InlineData(1e-12, true)]
[InlineData(-1e-12, true)]
[InlineData(1.0, false)]
[InlineData(-1.0, false)]
public void IsZero_ReturnsExpected(double value, bool expected)
{
Assert.Equal(expected, value.IsZero());
}
}
44 changes: 44 additions & 0 deletions CSMath.Tests/Extensions/IntExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using CSMath.Extensions;
using Xunit;

namespace CSMath.Tests.Extensions;

public class IntExtensionsTests
{
[Theory]
[InlineData(0, true)]
[InlineData(2, true)]
[InlineData(-2, true)]
[InlineData(1, false)]
[InlineData(-1, false)]
[InlineData(int.MaxValue, false)]
[InlineData(int.MinValue, true)]
public void IsEven_ReturnsExpected(int value, bool expected)
{
Assert.Equal(expected, value.IsEven());
}

[Theory]
[InlineData(0, false)]
[InlineData(1, false)]
[InlineData(-1, true)]
[InlineData(int.MaxValue, false)]
[InlineData(int.MinValue, true)]
public void IsNegative_ReturnsExpected(int value, bool expected)
{
Assert.Equal(expected, value.IsNegative());
}

[Theory]
[InlineData(0, false)]
[InlineData(2, false)]
[InlineData(-2, false)]
[InlineData(1, true)]
[InlineData(-1, true)]
[InlineData(int.MaxValue, true)]
[InlineData(int.MinValue, false)]
public void IsOdd_ReturnsExpected(int value, bool expected)
{
Assert.Equal(expected, value.IsOdd());
}
}
1 change: 0 additions & 1 deletion CSMath.Tests/Geometry/Arc2DTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CSMath.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

Expand Down
11 changes: 6 additions & 5 deletions CSMath.Tests/Geometry/Circle2DTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using CSMath.Extensions;
using CSMath.Geometry;
using System.Linq;
using Xunit;
Expand Down Expand Up @@ -114,8 +115,8 @@ public void FindIntersections_WithLineNegativeDirection_ReturnsTwoIntersections(
Assert.Equal(2, intersections.Length);

// Both points should be at x=0
Assert.True(MathHelper.IsZero(intersections[0].X, 1e-10));
Assert.True(MathHelper.IsZero(intersections[1].X, 1e-10));
Assert.True(intersections[0].X.IsZero(1e-10));
Assert.True(intersections[1].X.IsZero(1e-10));

// Both points should be on the circle
double distance1 = intersections[0].GetLength();
Expand All @@ -137,9 +138,9 @@ public void FindIntersections_WithLineThroughCenter_ReturnsTwoIntersections()
// Assert
Assert.Equal(2, intersections.Length);
Assert.True(MathHelper.IsEqual(5.0, intersections[0].X, 1e-10) || MathHelper.IsEqual(-5.0, intersections[0].X, 1e-10));
Assert.True(MathHelper.IsZero(intersections[0].Y, 1e-10));
Assert.True(intersections[0].Y.IsZero(1e-10));
Assert.True(MathHelper.IsEqual(5.0, intersections[1].X, 1e-10) || MathHelper.IsEqual(-5.0, intersections[1].X, 1e-10));
Assert.True(MathHelper.IsZero(intersections[1].Y, 1e-10));
Assert.True(intersections[1].Y.IsZero(1e-10));
}

[Fact]
Expand Down Expand Up @@ -213,6 +214,6 @@ public void FindIntersections_WithTangentLine_ReturnsOneIntersection()
// Assert
Assert.Single(intersections);
Assert.True(MathHelper.IsEqual(5.0, intersections[0].X, 1e-10));
Assert.True(MathHelper.IsZero(intersections[0].Y, 1e-10));
Assert.True(intersections[0].Y.IsZero(1e-10));
}
}
3 changes: 2 additions & 1 deletion CSMath.Tests/Geometry/Segment2DTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CSMath.Geometry;
using CSMath.Extensions;
using CSMath.Geometry;
using Xunit;

namespace CSMath.Tests.Geometry;
Expand Down
3 changes: 2 additions & 1 deletion CSMath.Tests/Geometry/Segment3DTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CSMath.Geometry;
using CSMath.Extensions;
using CSMath.Geometry;
using Xunit;

namespace CSMath.Tests.Geometry;
Expand Down
78 changes: 51 additions & 27 deletions CSMath.Tests/MathHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,55 +130,79 @@ public void HalfPI_IsCorrect()
}

[Theory]
[InlineData(0.0, true)]
[InlineData(5e-13, true)]
[InlineData(-5e-13, true)]
[InlineData(1e-12, false)]
[InlineData(-1e-12, false)]
[InlineData(1.0, false)]
[InlineData(-1.0, false)]
public void IsAlmostZero_ReturnsExpected(double value, bool expected)
[InlineData(1.0, 1.05, 0.1, true)]
[InlineData(1.0, 1.5, 0.1, false)]
public void IsEqual_CustomThreshold_ReturnsExpected(double a, double b, double threshold, bool expected)
{
Assert.Equal(expected, MathHelper.IsAlmostZero(value));
Assert.Equal(expected, MathHelper.IsEqual(a, b, threshold));
}

[Theory]
[InlineData(1.0, 1.0000000000005, true)]
[InlineData(5.0, 5.0, true)]
[InlineData(-5.0, 5.0, true)]
[InlineData(-5.0, -5.0, true)]
[InlineData(-5.0, 5.0, false)]
[InlineData(5.0, -5.0, false)]
[InlineData(5.0, 6.0, false)]
[InlineData(1e16, 1e16 + 2, false)]
public void IsEqual_ReturnsExpected(double a, double b, bool expected)
{
Assert.Equal(expected, MathHelper.IsEqual(a, b));
}

[Theory]
[InlineData(1.0, 1.05, 0.1, true)]
[InlineData(1.0, 1.5, 0.1, false)]
public void IsEqual_CustomThreshold_ReturnsExpected(double a, double b, double threshold, bool expected)
[InlineData(-90.0, 270.0)]
[InlineData(-450.0, 270.0)]
[InlineData(90.0, 90.0)]
public void NormalizeAngle_Absolute_ReturnsExpected(double number, double expected)
{
Assert.Equal(expected, MathHelper.IsEqual(a, b, threshold));
Assert.Equal(expected, MathHelper.NormalizeAngle(number, true));
}

[Theory]
[InlineData(0.0, true)]
[InlineData(5e-13, true)]
[InlineData(-5e-13, true)]
[InlineData(1e-12, true)]
[InlineData(-1e-12, true)]
[InlineData(1.0, false)]
[InlineData(-1.0, false)]
public void IsZero_ReturnsExpected(double value, bool expected)
[InlineData(0.0, 0.0)]
[InlineData(180.0, 180.0)]
[InlineData(360.0, 360.0)]
[InlineData(-360.0, -360.0)]
[InlineData(720.0, 0.0)]
[InlineData(450.0, 90.0)]
[InlineData(-90.0, -90.0)]
[InlineData(-180.0, -180.0)]
[InlineData(-450.0, -90.0)]
[InlineData(-720.0, 0.0)]
public void NormalizeAngle_NotAbsolute_ReturnsExpected(double number, double expected)
{
Assert.Equal(expected, MathHelper.IsZero(value));
Assert.Equal(expected, MathHelper.NormalizeAngle(number, false));
}

[Theory]
[InlineData(0.05, 0.1, true)]
[InlineData(0.2, 0.1, false)]
public void IsZero_CustomThreshold_ReturnsExpected(double value, double threshold, bool expected)
[InlineData(0.0, 0.0)]
[InlineData(MathHelper.HalfPI, MathHelper.HalfPI)]
[InlineData(MathHelper.TwoPI, MathHelper.TwoPI)]
[InlineData(-MathHelper.TwoPI, -MathHelper.TwoPI)]
[InlineData(2.0 * MathHelper.TwoPI, 0.0)]
[InlineData(-2.0 * MathHelper.TwoPI, 0.0)]
[InlineData(-MathHelper.HalfPI, -MathHelper.HalfPI)]
[InlineData(-MathHelper.PI, -MathHelper.PI)]
[InlineData(-(MathHelper.TwoPI + MathHelper.HalfPI), -MathHelper.HalfPI)]
public void NormalizeAngleRadians_NotAbsolute_ReturnsExpected(double angle, double expected)
{
Assert.Equal(expected, MathHelper.NormalizeAngleRadians(angle, false), 10);
}

[Theory]
[InlineData(0.0, 0.0)]
[InlineData(MathHelper.HalfPI, MathHelper.HalfPI)]
[InlineData(MathHelper.PI, MathHelper.PI)]
[InlineData(MathHelper.TwoPI, MathHelper.TwoPI)]
[InlineData(-MathHelper.TwoPI, MathHelper.TwoPI)]
[InlineData(2.0 * MathHelper.TwoPI, 0.0)]
[InlineData(MathHelper.TwoPI + MathHelper.HalfPI, MathHelper.HalfPI)]
[InlineData(-MathHelper.HalfPI, MathHelper.ThreeHalfPI)]
[InlineData(-(MathHelper.TwoPI + MathHelper.HalfPI), MathHelper.ThreeHalfPI)]
public void NormalizeAngleRadians_ReturnsExpected(double angle, double expected)
{
Assert.Equal(expected, MathHelper.IsZero(value, threshold));
Assert.Equal(expected, MathHelper.NormalizeAngleRadians(angle), 10);
}

[Theory]
Expand Down
6 changes: 1 addition & 5 deletions CSMath.Tests/VectorExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using Xunit;
using CSMath;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CSMath.Extensions;

namespace CSMath.Tests;

Expand Down
1 change: 1 addition & 0 deletions CSMath.Tests/VectorTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using CSMath.Extensions;
using System;
using System.Linq;
using Xunit;
Expand Down
3 changes: 2 additions & 1 deletion CSMath.Tests/XYTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Xunit;
using CSMath.Extensions;
using Xunit;
using Xunit.Abstractions;

namespace CSMath.Tests;
Expand Down
4 changes: 3 additions & 1 deletion CSMath/CSMath.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)BoundingBox.cs" />
<Compile Include="$(MSBuildThisFileDirectory)BoundingBoxExtent.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\DoubleExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\IntExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Geometry\Arc2D.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Geometry\Circle2D.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Geometry\CurveExtensions.cs" />
Expand All @@ -29,7 +31,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Quaternion.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Quaternion.operators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Transform.cs" />
<Compile Include="$(MSBuildThisFileDirectory)VectorExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\VectorExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)XY.operators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)XY.cs" />
<Compile Include="$(MSBuildThisFileDirectory)XYZ.operators.cs" />
Expand Down
25 changes: 25 additions & 0 deletions CSMath/Extensions/DoubleExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace CSMath.Extensions;

public static class DoubleExtensions
{
/// <summary>
/// Determines whether the specified double is zero, using a default threshold for comparison.
/// </summary>
/// <param name="value">The double value to evaluate.</param>
/// <returns>True if the number is zero within the default threshold; otherwise, false.</returns>
public static bool IsZero(this double value)
{
return value.IsZero(MathHelper.Epsilon);
}

/// <summary>
/// Determines whether the specified double is zero, using a custom threshold for comparison.
/// </summary>
/// <param name="value">The double value to evaluate.</param>
/// <param name="threshold">The custom threshold for comparison.</param>
/// <returns>True if the number is zero within the specified threshold; otherwise, false.</returns>
public static bool IsZero(this double value, double threshold)
{
return value >= -threshold && value <= threshold;
}
}
34 changes: 34 additions & 0 deletions CSMath/Extensions/IntExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace CSMath.Extensions;

public static class IntExtensions
{
/// <summary>
/// Determines whether the specified integer is even.
/// </summary>
/// <param name="value">The integer to evaluate.</param>
/// <returns>True if the integer is even; otherwise, false.</returns>
public static bool IsEven(this int value)
{
return (value & 1) == 0;
}

/// <summary>
/// Determines whether the specified integer is odd.
/// </summary>
/// <param name="value">The integer to evaluate.</param>
/// <returns>True if the integer is odd; otherwise, false.</returns>
public static bool IsNegative(this int value)
{
return value < 0;
}

/// <summary>
/// Determines whether the specified integer is odd.
/// </summary>
/// <param name="value">The integer to evaluate.</param>
/// <returns>True if the integer is odd; otherwise, false.</returns>
public static bool IsOdd(this int value)
{
return (value & 1) == 1;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;

namespace CSMath;
namespace CSMath.Extensions;

public static class VectorExtensions
{
Expand Down Expand Up @@ -262,7 +262,7 @@ public static bool IsParallel<T>(this T left, T right)
public static bool IsPerpendicular<T>(this T left, T right)
where T : IVector
{
return Dot<T>(left, right) == 0;
return Dot<T>(left, right).IsZero();
}

/// <summary>
Expand All @@ -274,7 +274,7 @@ public static bool IsPerpendicular<T>(this T left, T right)
public static bool IsZero<T>(this T v)
where T : IVector
{
return v.GetLength() == 0;
return v.GetLength().IsZero();
}

/// <summary>
Expand Down Expand Up @@ -393,7 +393,7 @@ public static T RoundZero<T>(this T vector, double threshold = MathHelper.Epsilo

for (int i = 0; i < result.Dimension; i++)
{
result[i] = MathHelper.IsZero(vector[i], threshold) ? 0 : vector[i];
result[i] = vector[i].IsZero(threshold) ? 0 : vector[i];
}

return result;
Expand Down
3 changes: 2 additions & 1 deletion CSMath/Geometry/Arc2D.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using CSMath.Extensions;
using System.Collections.Generic;
using System.Linq;

namespace CSMath.Geometry;
Expand Down
Loading
Loading