本文整理汇总了C#中NUnit.Framework.Constraints.Tolerance类的典型用法代码示例。如果您正苦于以下问题:C# Tolerance类的具体用法?C# Tolerance怎么用?C# Tolerance使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Tolerance类属于NUnit.Framework.Constraints命名空间,在下文中一共展示了Tolerance类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AreEqual
/// <summary>
/// Test two numeric values for equality, performing the usual numeric
/// conversions and using a provided or default tolerance. If the tolerance
/// provided is Empty, this method may set it to a default tolerance.
/// </summary>
/// <param name="expected">The expected value</param>
/// <param name="actual">The actual value</param>
/// <param name="tolerance">A reference to the tolerance in effect</param>
/// <returns>True if the values are equal</returns>
public static bool AreEqual( object expected, object actual, ref Tolerance tolerance )
{
if ( expected is double || actual is double )
return AreEqual( Convert.ToDouble(expected), Convert.ToDouble(actual), ref tolerance );
if ( expected is float || actual is float )
return AreEqual( Convert.ToSingle(expected), Convert.ToSingle(actual), ref tolerance );
if (tolerance.Mode == ToleranceMode.Ulps)
throw new InvalidOperationException("Ulps may only be specified for floating point arguments");
if ( expected is decimal || actual is decimal )
return AreEqual( Convert.ToDecimal(expected), Convert.ToDecimal(actual), tolerance );
if (expected is ulong || actual is ulong)
return AreEqual(Convert.ToUInt64(expected), Convert.ToUInt64(actual), tolerance );
if ( expected is long || actual is long )
return AreEqual( Convert.ToInt64(expected), Convert.ToInt64(actual), tolerance );
if ( expected is uint || actual is uint )
return AreEqual( Convert.ToUInt32(expected), Convert.ToUInt32(actual), tolerance );
return AreEqual( Convert.ToInt32(expected), Convert.ToInt32(actual), tolerance );
}
示例2: EqualConstraintResult
/// <summary>
/// Construct an EqualConstraintResult
/// </summary>
public EqualConstraintResult(EqualConstraint constraint, object actual, bool hasSucceeded)
: base(constraint, actual, hasSucceeded)
{
this.expectedValue = constraint.Arguments[0];
this.tolerance = constraint.Tolerance;
this.caseInsensitive = constraint.CaseInsensitive;
this.clipStrings = constraint.ClipStrings;
this.failurePoints = constraint.FailurePoints;
}
示例3: DisplayDifferences
public override void DisplayDifferences(object expected, object actual, Tolerance tolerance)
{
this.expected = expected;
this.actual = actual;
this.tolerance = tolerance;
}
示例4: AreEqual
/// <summary>
/// Test two numeric values for equality, performing the usual numeric
/// conversions and using a provided or default tolerance. If the tolerance
/// provided is Empty, this method may set it to a default tolerance.
/// </summary>
/// <param name="expected">The expected value</param>
/// <param name="actual">The actual value</param>
/// <param name="tolerance">A reference to the tolerance in effect</param>
/// <returns>True if the values are equal</returns>
public static bool AreEqual(object expected, object actual, ref Tolerance tolerance)
{
bool _double = (expected is double || actual is double);
#if XAMCORE_2_0 && (MONOTOUCH || MONOMAC)
if (!_double && (IntPtr.Size == 8))
_double = (expected is nfloat || actual is nfloat);
#endif
if (_double)
return AreEqual(Convert.ToDouble(expected), Convert.ToDouble(actual), ref tolerance);
bool _float = (expected is float || actual is float);
#if XAMCORE_2_0 && (MONOTOUCH || MONOMAC)
if (!_float && (IntPtr.Size == 4))
_float = (expected is nfloat || actual is nfloat);
#endif
if (_float)
return AreEqual(Convert.ToSingle(expected), Convert.ToSingle(actual), ref tolerance);
if (tolerance.Mode == ToleranceMode.Ulps)
throw new InvalidOperationException("Ulps may only be specified for floating point arguments");
if (expected is decimal || actual is decimal)
return AreEqual(Convert.ToDecimal(expected), Convert.ToDecimal(actual), tolerance);
bool _ulong = (expected is ulong || actual is ulong);
#if XAMCORE_2_0 && (MONOTOUCH || MONOMAC)
if (!_ulong && (IntPtr.Size == 8))
_ulong = (expected is nuint || actual is nuint);
#endif
if (_ulong)
return AreEqual(Convert.ToUInt64(expected), Convert.ToUInt64(actual), tolerance);
bool _long = (expected is long || actual is long);
#if XAMCORE_2_0 && (MONOTOUCH || MONOMAC)
if (!_long && (IntPtr.Size == 8))
_long = (expected is nint || actual is nint);
#endif
if (_long)
return AreEqual(Convert.ToInt64(expected), Convert.ToInt64(actual), tolerance);
bool _uint = (expected is uint || actual is uint);
#if XAMCORE_2_0 && (MONOTOUCH || MONOMAC)
if (!_uint && (IntPtr.Size == 4))
_uint = (expected is nuint || actual is nuint);
#endif
if (_uint)
return AreEqual(Convert.ToUInt32(expected), Convert.ToUInt32(actual), tolerance);
// int or nint on 32bits archs
return AreEqual(Convert.ToInt32(expected), Convert.ToInt32(actual), tolerance);
}
示例5: Setup
public void Setup()
{
tolerance = Tolerance.Empty;
}
示例6: DisplayDifferences
/// <summary>
/// Display Expected and Actual lines for given values, including
/// a tolerance value on the expected line.
/// </summary>
/// <param name="expected">The expected value</param>
/// <param name="actual">The actual value causing the failure</param>
/// <param name="tolerance">The tolerance within which the test was made</param>
public override void DisplayDifferences(object expected, object actual, Tolerance tolerance)
{
WriteExpectedLine(expected, tolerance);
WriteActualLine(actual);
}
示例7: DictionariesEqual
private bool DictionariesEqual(IDictionary x, IDictionary y, ref Tolerance tolerance)
{
if (x.Count != y.Count)
return false;
CollectionTally tally = new CollectionTally(this, x.Keys);
if (!tally.TryRemove(y.Keys) || tally.Count > 0)
return false;
foreach (object key in x.Keys)
if (!AreEqual(x[key], y[key], ref tolerance))
return false;
return true;
}
示例8: AreEqual
/// <summary>
/// Compares two objects for equality within a tolerance.
/// </summary>
public bool AreEqual(object x, object y, ref Tolerance tolerance)
{
this.failurePoints = new List<FailurePoint>();
if (x == null && y == null)
return true;
if (x == null || y == null)
return false;
if (object.ReferenceEquals(x, y))
return true;
Type xType = x.GetType();
Type yType = y.GetType();
EqualityAdapter externalComparer = GetExternalComparer(x, y);
if (externalComparer != null)
return externalComparer.AreEqual(x, y);
if (xType.IsArray && yType.IsArray && !compareAsCollection)
return ArraysEqual((Array)x, (Array)y, ref tolerance);
if (x is IDictionary && y is IDictionary)
return DictionariesEqual((IDictionary)x, (IDictionary)y, ref tolerance);
//if (x is ICollection && y is ICollection)
// return CollectionsEqual((ICollection)x, (ICollection)y, ref tolerance);
if (x is IEnumerable && y is IEnumerable && !(x is string && y is string))
return EnumerablesEqual((IEnumerable)x, (IEnumerable)y, ref tolerance);
if (x is string && y is string)
return StringsEqual((string)x, (string)y);
if (x is Stream && y is Stream)
return StreamsEqual((Stream)x, (Stream)y);
if (x is DirectoryInfo && y is DirectoryInfo)
return DirectoriesEqual((DirectoryInfo)x, (DirectoryInfo)y);
if (Numerics.IsNumericType(x) && Numerics.IsNumericType(y))
return Numerics.AreEqual(x, y, ref tolerance);
if (tolerance != null && tolerance.Value is TimeSpan)
{
TimeSpan amount = (TimeSpan)tolerance.Value;
if (x is DateTime && y is DateTime)
return ((DateTime)x - (DateTime)y).Duration() <= amount;
if (x is TimeSpan && y is TimeSpan)
return ((TimeSpan)x - (TimeSpan)y).Duration() <= amount;
}
if (FirstImplementsIEquatableOfSecond(xType, yType))
return InvokeFirstIEquatableEqualsSecond(x, y);
else if (xType != yType && FirstImplementsIEquatableOfSecond(yType, xType))
return InvokeFirstIEquatableEqualsSecond(y, x);
return x.Equals(y);
}
示例9: AreEqual
/// <summary>
/// Compares two objects for equality within a tolerance.
/// </summary>
public bool AreEqual(object x, object y, ref Tolerance tolerance)
{
this.failurePoints = new List<FailurePoint>();
if (x == null && y == null)
return true;
if (x == null || y == null)
return false;
if (object.ReferenceEquals(x, y))
return true;
Type xType = x.GetType();
Type yType = y.GetType();
EqualityAdapter externalComparer = GetExternalComparer(x, y);
if (externalComparer != null)
return externalComparer.AreEqual(x, y);
if (xType.IsArray && yType.IsArray && !compareAsCollection)
return ArraysEqual((Array)x, (Array)y, ref tolerance);
if (x is IDictionary && y is IDictionary)
return DictionariesEqual((IDictionary)x, (IDictionary)y, ref tolerance);
// Issue #70 - EquivalentTo isn't compatible with IgnoreCase for dictionaries
if (x is DictionaryEntry && y is DictionaryEntry)
return DictionaryEntriesEqual((DictionaryEntry)x, (DictionaryEntry)y, ref tolerance);
// IDictionary<,> will eventually try to compare it's key value pairs when using CollectionTally
if (xType.IsGenericType && xType.GetGenericTypeDefinition() == typeof(KeyValuePair<,>) &&
yType.IsGenericType && yType.GetGenericTypeDefinition() == typeof(KeyValuePair<,>))
{
var keyTolerance = Tolerance.Exact;
object xKey = xType.GetProperty("Key").GetValue(x, null);
object yKey = yType.GetProperty("Key").GetValue(y, null);
object xValue = xType.GetProperty("Value").GetValue(x, null);
object yValue = yType.GetProperty("Value").GetValue(y, null);
return AreEqual(xKey, yKey, ref keyTolerance) && AreEqual(xValue, yValue, ref tolerance);
}
//if (x is ICollection && y is ICollection)
// return CollectionsEqual((ICollection)x, (ICollection)y, ref tolerance);
if (x is IEnumerable && y is IEnumerable && !(x is string && y is string))
return EnumerablesEqual((IEnumerable)x, (IEnumerable)y, ref tolerance);
if (x is string && y is string)
return StringsEqual((string)x, (string)y);
if (x is Stream && y is Stream)
return StreamsEqual((Stream)x, (Stream)y);
if ( x is char && y is char )
return CharsEqual( (char)x, (char)y );
if (x is DirectoryInfo && y is DirectoryInfo)
return DirectoriesEqual((DirectoryInfo)x, (DirectoryInfo)y);
if (Numerics.IsNumericType(x) && Numerics.IsNumericType(y))
return Numerics.AreEqual(x, y, ref tolerance);
if (tolerance != null && tolerance.Value is TimeSpan)
{
TimeSpan amount = (TimeSpan)tolerance.Value;
if (x is DateTime && y is DateTime)
return ((DateTime)x - (DateTime)y).Duration() <= amount;
#if !NETCF
if (x is DateTimeOffset && y is DateTimeOffset)
return ((DateTimeOffset)x - (DateTimeOffset)y).Duration() <= amount;
#endif
if (x is TimeSpan && y is TimeSpan)
return ((TimeSpan)x - (TimeSpan)y).Duration() <= amount;
}
if (FirstImplementsIEquatableOfSecond(xType, yType))
return InvokeFirstIEquatableEqualsSecond(x, y);
else if (xType != yType && FirstImplementsIEquatableOfSecond(yType, xType))
return InvokeFirstIEquatableEqualsSecond(y, x);
return x.Equals(y);
}
示例10: SetUp
public void SetUp()
{
tenPercent = new Tolerance(10.0).Percent;
zeroTolerance = new Tolerance(0);
}
示例11: AreEqual
private static bool AreEqual(decimal expected, decimal actual, Tolerance tolerance)
{
switch (tolerance.Mode)
{
case ToleranceMode.Unset:
return expected.Equals(actual);
case ToleranceMode.Linear:
decimal decimalTolerance = Convert.ToDecimal(tolerance.Value);
if (decimalTolerance > 0m)
return Math.Abs(expected - actual) <= decimalTolerance;
return expected.Equals(actual);
case ToleranceMode.Percent:
if (expected == 0m)
return expected.Equals(actual);
double relativeError = Math.Abs(
(double)(expected - actual) / (double)expected);
return (relativeError <= Convert.ToDouble(tolerance.Value) / 100.0);
default:
throw new ArgumentException("Unknown tolerance mode specified", "mode");
}
}
示例12: TestNumericToleranceRequired
public void TestNumericToleranceRequired()
{
var tolerance = new Tolerance("Five");
Assert.That(() => tolerance.Percent,
Throws.TypeOf<InvalidOperationException>().With.Message.ContainsSubstring("numeric tolerance is required"));
}
示例13: TestModesCanOnlyBeUsedOnce
public void TestModesCanOnlyBeUsedOnce()
{
var tolerance = new Tolerance(5);
Assert.That(() => tolerance.Percent.Ulps,
Throws.TypeOf<InvalidOperationException>().With.Message.ContainsSubstring("multiple tolerance modes"));
}
示例14: SetUp
public void SetUp()
{
tolerance = Tolerance.Empty;
comparer = new NUnitComparer();
}
示例15: WriteExpectedLine
/// <summary>
/// Write the generic 'Expected' line for a given value
/// and tolerance.
/// </summary>
/// <param name="expected">The expected value</param>
/// <param name="tolerance">The tolerance within which the test was made</param>
private void WriteExpectedLine(object expected, Tolerance tolerance)
{
Write(Pfx_Expected);
Write(MsgUtils.FormatValue(expected));
if (tolerance != null && !tolerance.IsEmpty)
{
Write(" +/- ");
Write(MsgUtils.FormatValue(tolerance.Value));
if (tolerance.Mode != ToleranceMode.Linear)
Write(" {0}", tolerance.Mode);
}
WriteLine();
}