Skip to content

Commit 15f71cb

Browse files
committed
Added Guard.IsCloseTo overloads for nint type
1 parent 423c0ed commit 15f71cb

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

Microsoft.Toolkit/Diagnostics/Guard.Comparable.Numeric.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,5 +207,65 @@ public static void IsNotCloseTo(double value, double target, double delta, strin
207207

208208
ThrowHelper.ThrowArgumentExceptionForIsNotCloseTo(value, target, delta, name);
209209
}
210+
211+
/// <summary>
212+
/// Asserts that the input value must be within a given distance from a specified value.
213+
/// </summary>
214+
/// <param name="value">The input <see langword="nint"/> value to test.</param>
215+
/// <param name="target">The target <see langword="nint"/> value to test for.</param>
216+
/// <param name="delta">The maximum distance to allow between <paramref name="value"/> and <paramref name="target"/>.</param>
217+
/// <param name="name">The name of the input parameter being tested.</param>
218+
/// <exception cref="ArgumentException">Thrown if (<paramref name="value"/> - <paramref name="target"/>) > <paramref name="delta"/>.</exception>
219+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
220+
public static void IsCloseTo(nint value, nint target, nuint delta, string name)
221+
{
222+
nuint difference;
223+
224+
if (value >= target)
225+
{
226+
difference = (nuint)(value - target);
227+
}
228+
else
229+
{
230+
difference = (nuint)(target - value);
231+
}
232+
233+
if (difference <= delta)
234+
{
235+
return;
236+
}
237+
238+
ThrowHelper.ThrowArgumentExceptionForIsCloseTo(value, target, delta, name);
239+
}
240+
241+
/// <summary>
242+
/// Asserts that the input value must not be within a given distance from a specified value.
243+
/// </summary>
244+
/// <param name="value">The input <see langword="nint"/> value to test.</param>
245+
/// <param name="target">The target <see langword="nint"/> value to test for.</param>
246+
/// <param name="delta">The maximum distance to allow between <paramref name="value"/> and <paramref name="target"/>.</param>
247+
/// <param name="name">The name of the input parameter being tested.</param>
248+
/// <exception cref="ArgumentException">Thrown if (<paramref name="value"/> - <paramref name="target"/>) &lt;= <paramref name="delta"/>.</exception>
249+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
250+
public static void IsNotCloseTo(nint value, nint target, nuint delta, string name)
251+
{
252+
nuint difference;
253+
254+
if (value >= target)
255+
{
256+
difference = (nuint)(value - target);
257+
}
258+
else
259+
{
260+
difference = (nuint)(target - value);
261+
}
262+
263+
if (difference > delta)
264+
{
265+
return;
266+
}
267+
268+
ThrowHelper.ThrowArgumentExceptionForIsNotCloseTo(value, target, delta, name);
269+
}
210270
}
211271
}

Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.Comparable.Numeric.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,25 @@ internal static void ThrowArgumentExceptionForIsNotCloseTo(double value, double
9393
{
9494
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(double).ToTypeString()}) must not be within a distance of {delta.ToAssertString()} from {target.ToAssertString()}, was {value.ToAssertString()} and had a distance of {Math.Abs(value - target).ToAssertString()}");
9595
}
96+
97+
/// <summary>
98+
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsCloseTo(nint,nint,nuint,string)"/> fails.
99+
/// </summary>
100+
[MethodImpl(MethodImplOptions.NoInlining)]
101+
[DoesNotReturn]
102+
internal static void ThrowArgumentExceptionForIsCloseTo(nint value, nint target, nuint delta, string name)
103+
{
104+
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(nint).ToTypeString()}) must be within a distance of {delta.ToAssertString()} from {target.ToAssertString()}, was {value.ToAssertString()} and had a distance of {Math.Abs(value - target).ToAssertString()}");
105+
}
106+
107+
/// <summary>
108+
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotCloseTo(nint,nint,nuint,string)"/> fails.
109+
/// </summary>
110+
[MethodImpl(MethodImplOptions.NoInlining)]
111+
[DoesNotReturn]
112+
internal static void ThrowArgumentExceptionForIsNotCloseTo(nint value, nint target, nuint delta, string name)
113+
{
114+
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(nint).ToTypeString()}) must not be within a distance of {delta.ToAssertString()} from {target.ToAssertString()}, was {value.ToAssertString()} and had a distance of {Math.Abs(value - target).ToAssertString()}");
115+
}
96116
}
97117
}

0 commit comments

Comments
 (0)