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
17 changes: 17 additions & 0 deletions PolyShim.Tests/Net60/MathTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using FluentAssertions;
using Xunit;

namespace PolyShim.Tests.Net60;

public class MathTests
{
[Fact]
public void Clamp_Test()
{
// Act & assert
Math.Clamp((nint)5, (nint)1, (nint)10).Should().Be((nint)5);
Math.Clamp((nint)0, (nint)1, (nint)10).Should().Be((nint)1);
Math.Clamp((nint)15, (nint)1, (nint)10).Should().Be((nint)10);
}
}
20 changes: 20 additions & 0 deletions PolyShim.Tests/NetCore20/MathTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using FluentAssertions;
using Xunit;

namespace PolyShim.Tests.NetCore20;

public class MathTests
{
[Fact]
public void Clamp_Test()
{
// Act & assert
Math.Clamp(5, 1, 10).Should().Be(5);
Math.Clamp(0, 1, 10).Should().Be(1);
Math.Clamp(15, 1, 10).Should().Be(10);
Math.Clamp(5.5, 1.0, 10.0).Should().Be(5.5);
Math.Clamp(0.5, 1.0, 10.0).Should().Be(1.0);
Math.Clamp(15.5, 1.0, 10.0).Should().Be(10.0);
}
}
34 changes: 34 additions & 0 deletions PolyShim/Net60/Math.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// The following comment is required to instruct analyzers to skip this file
// <auto-generated/>

#if (NETCOREAPP && !NET6_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD)
#nullable enable
// ReSharper disable RedundantUsingDirective
// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming
// ReSharper disable PartialTypeWithSinglePart

using System;

internal static partial class PolyfillExtensions
{
extension(Math)
{
// https://learn.microsoft.com/dotnet/api/system.math.clamp#system-math-clamp(system-intptr-system-intptr-system-intptr)
public static IntPtr Clamp(IntPtr value, IntPtr min, IntPtr max)
{
var valueAsLong = value.ToInt64();
var minAsLong = min.ToInt64();
var maxAsLong = max.ToInt64();

if (valueAsLong < minAsLong)
return min;

if (valueAsLong > maxAsLong)
return max;

return value;
}
}
}
#endif
32 changes: 32 additions & 0 deletions PolyShim/NetCore20/Math.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// The following comment is required to instruct analyzers to skip this file
// <auto-generated/>

#if (NETCOREAPP && !NETCOREAPP2_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD && !NETSTANDARD2_1_OR_GREATER)
#nullable enable
// ReSharper disable RedundantUsingDirective
// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming
// ReSharper disable PartialTypeWithSinglePart

using System;

internal static partial class PolyfillExtensions
{
extension(Math)
{
// https://learn.microsoft.com/dotnet/api/system.math.clamp
// Technically, the original implementation has overloads for each separate numeric type,
// but this generic version is fully source-compatible and simpler to maintain.
public static T Clamp<T>(T value, T min, T max) where T : IComparable<T>
{
if (value.CompareTo(min) < 0)
return min;

if (value.CompareTo(max) > 0)
return max;

return value;
}
}
}
#endif
Loading