Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add some tests
  • Loading branch information
hrrrrustic committed Nov 11, 2022
commit 39e6e81d996988c287634a223c011f3674597da6
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Numerics;
using Xunit;

namespace System.Tests
Expand Down Expand Up @@ -65,5 +66,70 @@ public static void Ctor_String_Object_String()
Assert.Contains(argumentName, exception.Message);
Assert.Contains(argumentValue.ToString(), exception.Message);
}

private static Action ZeroHelper<T>(T value) where T : INumberBase<T> => () => ArgumentOutOfRangeException.ThrowIfZero(value);
private static Action NegativeOrZeroHelper<T>(T value) where T : INumberBase<T> => () => ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value);
private static Action GreaterThanHelper<T>(T value, T other) where T : IComparable<T> => () => ArgumentOutOfRangeException.ThrowIfGreaterThan(value, other);
private static Action GreaterThanOrEqualHelper<T>(T value, T other) where T : IComparable<T> => () => ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(value, other);
private static Action LessThanHelper<T>(T value, T other) where T : IComparable<T> => () => ArgumentOutOfRangeException.ThrowIfLessThan(value, other);
private static Action LessThanOrEqualHelper<T>(T value, T other) where T : IComparable<T> => () => ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(value, other);

[Fact]
public static void GenericHelpers_ThrowIfZero_Throws()
{
Assert.Throws<ArgumentOutOfRangeException>(ZeroHelper<int>(0));
Assert.Throws<ArgumentOutOfRangeException>(ZeroHelper<uint>(0));

Assert.Throws<ArgumentOutOfRangeException>(ZeroHelper<float>(0.0f));
Assert.Throws<ArgumentOutOfRangeException>(ZeroHelper<float>(-0.0f));
Assert.Throws<ArgumentOutOfRangeException>(ZeroHelper<double>(0));
Assert.Throws<ArgumentOutOfRangeException>(ZeroHelper<double>(+0.0));
Assert.Throws<ArgumentOutOfRangeException>(ZeroHelper<double>(-0.0));
}

[Fact]
public static void GenericHelpers_ThrowIfNegativeZero_Throws()
{
Assert.Throws<ArgumentOutOfRangeException>(NegativeOrZeroHelper<int>(-1));

Assert.Throws<ArgumentOutOfRangeException>(NegativeOrZeroHelper<float>(-0.0f));
Assert.Throws<ArgumentOutOfRangeException>(NegativeOrZeroHelper<double>(-0.0));
}

[Fact]
public static void GenericHelpers_ThrowIfGreaterThan_Throws()
{
Assert.Throws<ArgumentOutOfRangeException>(GreaterThanHelper<int>(1, 0));
Assert.Throws<ArgumentOutOfRangeException>(GreaterThanHelper<uint>(1, 0));
Assert.Throws<ArgumentOutOfRangeException>(GreaterThanHelper<double>(1.000000001, 1));
Assert.Throws<ArgumentOutOfRangeException>(GreaterThanHelper<float>(1.00001f, 1));
}

[Fact]
public static void GenericHelpers_ThrowIfGreaterThanOrEqual_Throws()
{
Assert.Throws<ArgumentOutOfRangeException>(GreaterThanOrEqualHelper<int>(1, 1));
Assert.Throws<ArgumentOutOfRangeException>(GreaterThanOrEqualHelper<uint>(1, 1));
Assert.Throws<ArgumentOutOfRangeException>(GreaterThanOrEqualHelper<double>(1, 1));
Assert.Throws<ArgumentOutOfRangeException>(GreaterThanOrEqualHelper<float>(1, 1));
}

[Fact]
public static void GenericHelpers_ThrowIfLessThan_Throws()
{
Assert.Throws<ArgumentOutOfRangeException>(LessThanHelper<int>(0, 1));
Assert.Throws<ArgumentOutOfRangeException>(LessThanHelper<uint>(0, 1));
Assert.Throws<ArgumentOutOfRangeException>(LessThanHelper<double>(1, 1.000000001));
Assert.Throws<ArgumentOutOfRangeException>(LessThanHelper<float>(1, 1.00001f));
}

[Fact]
public static void GenericHelpers_ThrowIfLessThanOrEqual_Throws()
{
Assert.Throws<ArgumentOutOfRangeException>(LessThanOrEqualHelper<int>(1, 1));
Assert.Throws<ArgumentOutOfRangeException>(LessThanOrEqualHelper<uint>(1, 1));
Assert.Throws<ArgumentOutOfRangeException>(LessThanOrEqualHelper<double>(1, 1));
Assert.Throws<ArgumentOutOfRangeException>(LessThanOrEqualHelper<float>(1, 1));
}
}
}