// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Runtime.CompilerServices;
namespace CommunityToolkit.Diagnostics;
///
partial class Guard
{
///
/// Asserts that the input value must be within a given distance from a specified value.
///
/// The input value to test.
/// The target value to test for.
/// The maximum distance to allow between and .
/// The name of the input parameter being tested.
/// Thrown if ( - ) > .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsCloseTo(int value, int target, uint delta, [CallerArgumentExpression("value")] string name = "")
{
uint difference;
if (value >= target)
{
difference = (uint)(value - target);
}
else
{
difference = (uint)(target - value);
}
if (difference <= delta)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsCloseTo(value, target, delta, name);
}
///
/// Asserts that the input value must not be within a given distance from a specified value.
///
/// The input value to test.
/// The target value to test for.
/// The maximum distance to allow between and .
/// The name of the input parameter being tested.
/// Thrown if ( - ) <= .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotCloseTo(int value, int target, uint delta, [CallerArgumentExpression("value")] string name = "")
{
uint difference;
if (value >= target)
{
difference = (uint)(value - target);
}
else
{
difference = (uint)(target - value);
}
if (difference > delta)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNotCloseTo(value, target, delta, name);
}
///
/// Asserts that the input value must be within a given distance from a specified value.
///
/// The input value to test.
/// The target value to test for.
/// The maximum distance to allow between and .
/// The name of the input parameter being tested.
/// Thrown if ( - ) > .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsCloseTo(long value, long target, ulong delta, [CallerArgumentExpression("value")] string name = "")
{
ulong difference;
if (value >= target)
{
difference = (ulong)(value - target);
}
else
{
difference = (ulong)(target - value);
}
if (difference <= delta)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsCloseTo(value, target, delta, name);
}
///
/// Asserts that the input value must not be within a given distance from a specified value.
///
/// The input value to test.
/// The target value to test for.
/// The maximum distance to allow between and .
/// The name of the input parameter being tested.
/// Thrown if ( - ) <= .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotCloseTo(long value, long target, ulong delta, [CallerArgumentExpression("value")] string name = "")
{
ulong difference;
if (value >= target)
{
difference = (ulong)(value - target);
}
else
{
difference = (ulong)(target - value);
}
if (difference > delta)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNotCloseTo(value, target, delta, name);
}
///
/// Asserts that the input value must be within a given distance from a specified value.
///
/// The input value to test.
/// The target value to test for.
/// The maximum distance to allow between and .
/// The name of the input parameter being tested.
/// Thrown if ( - ) > .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsCloseTo(float value, float target, float delta, [CallerArgumentExpression("value")] string name = "")
{
if (Math.Abs(value - target) <= delta)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsCloseTo(value, target, delta, name);
}
///
/// Asserts that the input value must not be within a given distance from a specified value.
///
/// The input value to test.
/// The target value to test for.
/// The maximum distance to allow between and .
/// The name of the input parameter being tested.
/// Thrown if ( - ) <= .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotCloseTo(float value, float target, float delta, [CallerArgumentExpression("value")] string name = "")
{
if (Math.Abs(value - target) > delta)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNotCloseTo(value, target, delta, name);
}
///
/// Asserts that the input value must be within a given distance from a specified value.
///
/// The input value to test.
/// The target value to test for.
/// The maximum distance to allow between and .
/// The name of the input parameter being tested.
/// Thrown if ( - ) > .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsCloseTo(double value, double target, double delta, [CallerArgumentExpression("value")] string name = "")
{
if (Math.Abs(value - target) <= delta)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsCloseTo(value, target, delta, name);
}
///
/// Asserts that the input value must not be within a given distance from a specified value.
///
/// The input value to test.
/// The target value to test for.
/// The maximum distance to allow between and .
/// The name of the input parameter being tested.
/// Thrown if ( - ) <= .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotCloseTo(double value, double target, double delta, [CallerArgumentExpression("value")] string name = "")
{
if (Math.Abs(value - target) > delta)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNotCloseTo(value, target, delta, name);
}
///
/// Asserts that the input value must be within a given distance from a specified value.
///
/// The input value to test.
/// The target value to test for.
/// The maximum distance to allow between and .
/// The name of the input parameter being tested.
/// Thrown if ( - ) > .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsCloseTo(nint value, nint target, nuint delta, [CallerArgumentExpression("value")] string name = "")
{
nuint difference;
if (value >= target)
{
difference = (nuint)(value - target);
}
else
{
difference = (nuint)(target - value);
}
if (difference <= delta)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsCloseTo(value, target, delta, name);
}
///
/// Asserts that the input value must not be within a given distance from a specified value.
///
/// The input value to test.
/// The target value to test for.
/// The maximum distance to allow between and .
/// The name of the input parameter being tested.
/// Thrown if ( - ) <= .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotCloseTo(nint value, nint target, nuint delta, [CallerArgumentExpression("value")] string name = "")
{
nuint difference;
if (value >= target)
{
difference = (nuint)(value - target);
}
else
{
difference = (nuint)(target - value);
}
if (difference > delta)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNotCloseTo(value, target, delta, name);
}
}