Esempio n. 1
0
        public void DoubleArithmetics()
        {
            var theAdd      = Arithmetics.CreateAddOperation <double>();
            var theSubtract = Arithmetics.CreateSubtractOperation <double>();
            var theMultiply = Arithmetics.CreateMultiplyOperation <double>();
            var theDivide   = Arithmetics.CreateDivideOperation <double>();
            var theNegate   = Arithmetics.CreateNegateOperation <double>();
            var theRound    = Arithmetics.CreateRoundOperation <double>();

            Assert.That(Arithmetics.GetZeroValue <double>(), Is.EqualTo(0.0));
            Assert.That(Arithmetics.GetOneValue <double>(), Is.EqualTo(1.0));
            Assert.That(theAdd(0.3, 0.8), Is.EqualTo(1.1).Within(DoubleTolerance));
            Assert.That(theAdd(9.378, 1.765), Is.EqualTo(11.143).Within(DoubleTolerance));
            Assert.That(theSubtract(11.143, 9.378), Is.EqualTo(1.765).Within(DoubleTolerance));
            Assert.That(theSubtract(1.1, 0.8), Is.EqualTo(0.3).Within(DoubleTolerance));
            Assert.That(theMultiply(5.0, 0.25), Is.EqualTo(1.25).Within(DoubleTolerance));
            Assert.That(theMultiply(3.33, 3.0), Is.EqualTo(9.99).Within(DoubleTolerance));
            Assert.That(theDivide(9.99, 3.0), Is.EqualTo(3.33).Within(DoubleTolerance));
            Assert.That(theDivide(1.25, 0.25), Is.EqualTo(5.0).Within(DoubleTolerance));
            Assert.That(theNegate(3.456), Is.EqualTo(-3.456).Within(DoubleTolerance));
            Assert.That(theNegate(-0.08), Is.EqualTo(0.08).Within(DoubleTolerance));
            Assert.That(theRound(2.6789, 3, MidpointRounding.AwayFromZero), Is.EqualTo(2.679));

            double?theNullableDouble = 1.234;
            var    theNullableRound  = Arithmetics.CreateRoundOperation <double?>();

            Assert.That(theNullableRound(theNullableDouble, 2, MidpointRounding.AwayFromZero), Is.EqualTo(1.23));
            Assert.That(theNullableRound(null, 2, MidpointRounding.AwayFromZero), Is.EqualTo(null));
        }
Esempio n. 2
0
        public void DecimalArithmetics()
        {
            var theAdd      = Arithmetics.CreateAddOperation <decimal>();
            var theSubtract = Arithmetics.CreateSubtractOperation <decimal>();
            var theMultiply = Arithmetics.CreateMultiplyOperation <decimal>();
            var theDivide   = Arithmetics.CreateDivideOperation <decimal>();
            var theNegate   = Arithmetics.CreateNegateOperation <decimal>();
            var theRound    = Arithmetics.CreateRoundOperation <decimal>();

            Assert.That(Arithmetics.GetZeroValue <decimal>(), Is.EqualTo(0.0m));
            Assert.That(Arithmetics.GetOneValue <decimal>(), Is.EqualTo(1.0m));
            Assert.That(Arithmetics.GetMinValue <decimal>(), Is.EqualTo(decimal.MinValue));
            Assert.That(Arithmetics.GetMaxValue <decimal>(), Is.EqualTo(decimal.MaxValue));
            Assert.That(theAdd(0.3m, 0.8m), Is.EqualTo(1.1m));
            Assert.That(theAdd(9.378m, 1.765m), Is.EqualTo(11.143m));
            Assert.That(theSubtract(11.143m, 9.378m), Is.EqualTo(1.765m));
            Assert.That(theSubtract(1.1m, 0.8m), Is.EqualTo(0.3m));
            Assert.That(theMultiply(5.0m, 0.25m), Is.EqualTo(1.25m));
            Assert.That(theMultiply(3.33m, 3.0m), Is.EqualTo(9.99m));
            Assert.That(theDivide(9.99m, 3.0m), Is.EqualTo(3.33m));
            Assert.That(theDivide(1.25m, 0.25m), Is.EqualTo(5.0m));
            Assert.That(theNegate(3.456m), Is.EqualTo(-3.456m));
            Assert.That(theNegate(-0.08m), Is.EqualTo(0.08m));
            Assert.That(theRound(2.3456m, 2, MidpointRounding.AwayFromZero), Is.EqualTo(2.35m));

            decimal?theNullableDecimal = 9.876m;
            var     theNullableRound   = Arithmetics.CreateRoundOperation <decimal?>();

            Assert.That(theNullableRound(theNullableDecimal, 2, MidpointRounding.AwayFromZero), Is.EqualTo(9.88m));
            Assert.That(theNullableRound(null, 2, MidpointRounding.AwayFromZero), Is.EqualTo(null));
        }