aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Tests/Common/DoubleToleranceConstraint.cs
blob: eb71aeb52cc981d76bdf8fc2130df115d7a76d64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using NUnit.Framework;
using NUnit.Framework.Constraints;
using OpenSim.Tests.Common;

namespace OpenSim.Tests.Common
{
    public class DoubleToleranceConstraint : ANumericalToleranceConstraint
    {

        private double _baseValue;
        private double _valueToBeTested;

        public DoubleToleranceConstraint(double baseValue, double tolerance) : base(tolerance)
        {
            _baseValue = baseValue;
        }
        
        ///<summary>
        ///Test whether the constraint is satisfied by a given value            
        ///</summary>
        ///<param name="valueToBeTested">The value to be tested</param>
        ///<returns>
        ///True for success, false for failure
        ///</returns>
        public override bool Matches(object valueToBeTested)
        {
            if (valueToBeTested == null)
            {
                throw new ArgumentException("Constraint cannot be used upon null values.");
            }
            if( valueToBeTested.GetType() != typeof(double))
            {
                throw new ArgumentException("Constraint cannot be used upon non double-values.");
            }

            _valueToBeTested = (double)valueToBeTested;

            return IsWithinDoubleConstraint(_valueToBeTested, _baseValue );
        }

        public override void WriteDescriptionTo(MessageWriter writer)
        {
            writer.WriteExpectedValue(string.Format("A value {0} within tolerance of plus or minus {1}",_baseValue,_tolerance));
        }

        public override void WriteActualValueTo(MessageWriter writer)
        {
            writer.WriteActualValue(_valueToBeTested);        
        }
    }
}