Beispiel #1
0
 /// <summary>
 ///     Wraps
 ///     <see
 ///         cref="NumericAssertionsExtensions.BeApproximately(NumericAssertions{float}, float, float, string, object[])" />
 ///     to provide the expected comparison epsilon.
 /// </summary>
 /// <param name="parent"></param>
 /// <param name="expectedValue"></param>
 /// <param name="because"></param>
 /// <param name="becauseArgs"></param>
 /// <returns></returns>
 public static AndConstraint <NumericAssertions <float> > BeApproximately(
     this NumericAssertions <float> parent,
     float expectedValue,
     string because = "",
     params object[] becauseArgs
     ) =>
 parent.BeApproximately(
     expectedValue,
     FloatExtensions.ComparisonEpsilon,
     because,
     becauseArgs
     );
        /// <summary>
        /// Asserts a floating point value approximates another value as close as possible.
        /// </summary>
        /// <param name="parent">The <see cref="NumericAssertions{T}"/> object that is being extended.</param>
        /// <param name="expectedValue">
        /// The expected value to compare the actual value with.
        /// </param>
        /// <param name="precision">
        /// The maximum amount of which the two values may differ.
        /// </param>
        /// <param name="reason">
        /// A formatted phrase as is supported by <see cref="string.Format(string,object[])"/> explaining why the assertion 
        /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
        /// </param>
        /// <param name="reasonArgs">
        /// Zero or more objects to format using the placeholders in <see cref="reason"/>.
        /// </param>
        public static AndConstraint<NullableNumericAssertions<float>> BeApproximately(this NullableNumericAssertions<float> parent,
            float expectedValue, float precision, string reason = "",
            params object [] reasonArgs)
        {
            Execute.Assertion
                .ForCondition(parent.Subject != null)
                .BecauseOf(reason, reasonArgs)
                .FailWith("Expected value to approximate {0} +/- {1}{reason}, but it was <null>.", expectedValue, precision);

            var nonNullableAssertions = new NumericAssertions<float>((float) parent.Subject);
            nonNullableAssertions.BeApproximately(expectedValue, precision, reason, reasonArgs);

            return new AndConstraint<NullableNumericAssertions<float>>(parent);
        }
        /// <summary>
        /// Asserts a floating point value approximates another value as close as possible.
        /// </summary>
        /// <param name="parent">The <see cref="NumericAssertions{T}"/> object that is being extended.</param>
        /// <param name="expectedValue">
        /// The expected value to compare the actual value with.
        /// </param>
        /// <param name="precision">
        /// The maximum amount of which the two values may differ.
        /// </param>
        /// <param name="because">
        /// A formatted phrase as is supported by <see cref="string.Format(string,object[])"/> explaining why the assertion
        /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
        /// </param>
        /// <param name="becauseArgs">
        /// Zero or more objects to format using the placeholders in <see cref="because"/>.
        /// </param>
        public static AndConstraint <NullableNumericAssertions <float> > BeApproximately(this NullableNumericAssertions <float> parent,
                                                                                         float expectedValue, float precision, string because = "",
                                                                                         params object [] becauseArgs)
        {
            Execute.Assertion
            .ForCondition(parent.Subject != null)
            .BecauseOf(because, becauseArgs)
            .FailWith("Expected {context:value} to approximate {0} +/- {1}{reason}, but it was <null>.", expectedValue, precision);

            var nonNullableAssertions = new NumericAssertions <float>((float)parent.Subject);

            nonNullableAssertions.BeApproximately(expectedValue, precision, because, becauseArgs);

            return(new AndConstraint <NullableNumericAssertions <float> >(parent));
        }
Beispiel #4
0
        public static AndConstraint <NumericAssertions <float> > BeApproximatelyOrBothNaNOrInfinity(
            this NumericAssertions <float> parent,
            float expectedValue,
            int precisionDigits = 5,
            string because      = "",
            params object[] becauseArgs)
        {
            if (bothMatch(IsNaN) || bothMatch(IsPositiveInfinity) || bothMatch(IsNegativeInfinity))
            {
                return(new AndConstraint <NumericAssertions <float> >(parent));
            }

            var acceptedPrecision = Max(Epsilon, Abs(expectedValue) * (float)Pow(0.1, precisionDigits));

            return(parent.BeApproximately(expectedValue, acceptedPrecision, because, becauseArgs));

            bool bothMatch(Func <float, bool> predicate) => predicate(expectedValue) && predicate((float)parent.Subject);
        }
Beispiel #5
0
 public static AndConstraint <NumericAssertions <double> > BeApproximately(this NumericAssertions <double> parent, decimal expectedValue, string because = "", params object[] becauseArgs) =>
 parent.BeApproximately((double)expectedValue, Math.Abs((double)expectedValue * TestsBase.DoublePrecisionPercentage), because, becauseArgs);
 public static AndConstraint <NumericAssertions <float> > BeApproximately(this NumericAssertions <float> source, float expected,
                                                                          string reason, params object[] reasonArgs)
 {
     return(source.BeApproximately(expected, Single.Epsilon, reason, reasonArgs));
 }
 public static AndConstraint <NumericAssertions <float> > BeApproximately(this NumericAssertions <float> source, float expected)
 {
     return(source.BeApproximately(expected, Single.Epsilon, ""));
 }
 public static AndConstraint <NumericAssertions <double> > BeApproximately(this NumericAssertions <double> source, double expected)
 {
     return(source.BeApproximately(expected, Double.Epsilon, ""));
 }
 public static AndConstraint <NumericAssertions <double> > BeApproximately(this NumericAssertions <double> assertions, double expectedValue)
 {
     return(assertions.BeApproximately(expectedValue, 0.0001));
 }
Beispiel #10
0
 public static AndConstraint <NumericAssertions <decimal> > BeApproximately(this NumericAssertions <decimal> parent, decimal expectedValue,
                                                                            string because = "", params object[] becauseArgs)
 {
     return(parent.BeApproximately(expectedValue, NumericPrecision, because, becauseArgs));
 }