Beispiel #1
0
            public void ThrowsInDesignModeIfMissingUnit(bool isDesignMode)
            {
                Wpf.Is.DesignMode = isDesignMode;
                var converter = new LengthConverter
                {
                    UnitInput = UnitInput.ScalarOnly
                };

                var length = Length.FromMetres(1.2);

                Assert.AreEqual(null, converter.Unit);
                var expected = "Unit cannot be null.\r\n" +
                               "Must be specified Explicitly or in StringFormat or in Binding.StringFormat.";

                if (isDesignMode)
                {
                    var exception = Assert.Throws <InvalidOperationException>(() => converter.Convert(length, typeof(string), null, null));
                    Assert.AreEqual(expected, exception.Message);
                }
                else
                {
                    var actual = converter.Convert(length, typeof(string), null, null);
                    Assert.AreEqual(expected, actual);
                }
            }
            public void ErrorWhenBindingStringFormatAndExplicitStringFormat()
            {
                var converter = new LengthConverter
                {
                    StringFormat = "F1 mm"
                };

                var providerMock = new ServiceProviderMock
                {
                    BindingStringFormat = "F2 cm"
                };

                converter.ProvideValue(providerMock.Object);
                var length = Length.FromMillimetres(1234);

                Wpf.Is.DesignMode = true;
                var ex       = Assert.Throws <InvalidOperationException>(() => converter.Convert(length, typeof(string), null, null));
                var expected = "ValueFormat cannot be set when Binding.StringFormat is a unit format.\r\n" +
                               "Ambiguous units StringFormat: {0:F1} mm Binding.StringFormat: {0:F2} cm";

                Assert.AreEqual(expected, ex.Message);

                Wpf.Is.DesignMode = false;
                var convert = converter.Convert(length, typeof(string), null, null);

                Assert.AreEqual(expected, convert);
            }
Beispiel #3
0
        /// <summary>
        /// Calculates the volume between the two horizons
        /// and above the fluid contact
        /// </summary>
        /// <param name="baseHorizonRelativeDepth">Base Horizon depth in relation to top horizon in meters</param>
        /// <param name="fluidContactDepth">Fluid contact dept in meters</param>
        /// <param name="resultUnit"></param>
        /// <returns></returns>
        internal void CalculateVolume(double baseHorizonRelativeDepth, double fluidContactDepth, LengthUnit lengthUnit)
        {
            double totalVolume = 0;

            var cellArea     = TopHorizon.CellArea;
            var baseDepth    = LengthConverter.Convert(baseHorizonRelativeDepth, lengthUnit, TopHorizon.LengthUnit);
            var fluidContact = LengthConverter.Convert(fluidContactDepth, lengthUnit, TopHorizon.LengthUnit);

            foreach (var topNodeDepth in TopHorizon.Nodes)
            {
                //Step 1: Determine if base depth or fluid contact will be considered.
                double actualBaseDepth = Math.Min(topNodeDepth + baseDepth, fluidContact);

                //Step 2: Determine height between top horizon and actualBaseDepth.
                //Height greater than zero means there is oil & gas to be calculated
                //Negative value means top horizon is under fluid contact and will not be calculated
                double height = actualBaseDepth - topNodeDepth;

                if (height > 0) //top horizon is above
                {
                    totalVolume += height * cellArea;
                }
            }

            CalculatedVolume = totalVolume;
        }
        public void LengthConvert_TestSuccess()
        {
            LengthConverter length = new LengthConverter();
            double          result = length.Convert(500);//pass sta da testiramo

            Assert.AreNotEqual(320.443, result, "FOr test true assert is true:", null);
        }
            public void Null(Type targetType, object expected)
            {
                var converter = new LengthConverter
                {
                    Unit      = LengthUnit.Centimetres,
                    UnitInput = UnitInput.ScalarOnly
                };

                var actual = converter.Convert(null, targetType, null, null);

                Assert.AreEqual(expected, actual);
            }
            public void WithExplicitUnit(Type targetType, object expected)
            {
                var converter = new LengthConverter
                {
                    Unit = LengthUnit.Centimetres,
                };

                var length = Length.FromMillimetres(12);
                var actual = converter.Convert(length, targetType, null, null);

                Assert.AreEqual(expected, actual);
            }
            public void WithStringFormat(Type targetType, string stringFormat, string culture, object expected)
            {
                var converter = new LengthConverter
                {
                    StringFormat = stringFormat
                };

                var length = Length.FromMillimetres(1234);
                var actual = converter.Convert(length, targetType, null, CultureInfo.GetCultureInfo(culture));

                Assert.AreEqual(expected, actual);
            }
Beispiel #8
0
            public void SettingStringFormatHappyPath(string stringFormat)
            {
                var converter = new LengthConverter
                {
                    StringFormat = stringFormat
                };

                var convert = converter.Convert(Length.FromMillimetres(12.34), typeof(string), null, CultureInfo.InvariantCulture);

                Assert.AreEqual("12.3 mm", convert);
                Assert.AreEqual(LengthUnit.Millimetres, converter.Unit);
                Assert.AreEqual(UnitInput.SymbolRequired, converter.UnitInput);
            }
Beispiel #9
0
            public void ValueFormatAndUnit(UnitInput?unitInput, object expectedValue, UnitInput?expectedUnitInput)
            {
                Wpf.Is.DesignMode = true;
                var converter = new LengthConverter
                {
                    ValueFormat = "F4",
                    Unit        = LengthUnit.Centimetres,
                    UnitInput   = unitInput
                };

                Assert.AreEqual(expectedValue, converter.Convert(Length.FromMillimetres(12.34), typeof(string), null, CultureInfo.InvariantCulture));
                Assert.AreEqual(expectedUnitInput, converter.UnitInput);
                Assert.AreEqual(LengthUnit.Centimetres, converter.Unit);
            }
            public void WithBindingStringFormat(Type targetType, string stringFormat)
            {
                var converter    = new LengthConverter();
                var providerMock = new ServiceProviderMock
                {
                    BindingStringFormat = stringFormat
                };

                converter.ProvideValue(providerMock.Object);
                var length = Length.FromMillimetres(1234);
                var actual = converter.Convert(length, targetType, null, null);

                Assert.AreEqual(length, actual);
            }
Beispiel #11
0
            public void BindingStringFormatHappyPath(string stringFormat)
            {
                var converter    = new LengthConverter();
                var providerMock = new ServiceProviderMock
                {
                    BindingStringFormat = stringFormat
                };

                converter.ProvideValue(providerMock.Object);
                var length  = Length.FromMillimetres(12.3);
                var convert = converter.Convert(length, typeof(string), null, CultureInfo.InvariantCulture);

                Assert.AreEqual(length, convert);
                Assert.AreEqual(LengthUnit.Centimetres, converter.Unit);
                Assert.AreEqual(UnitInput.SymbolRequired, converter.UnitInput);
            }
Beispiel #12
0
    public void LengthConverter_Convert()
    {
        decimal result = LengthConverter.Convert(LengthUnit.Feet, LengthUnit.Inch, 1);

        Assert.AreEqual(12m, result);

        result = LengthConverter.Convert(LengthUnit.Inch, LengthUnit.Feet, 12);
        Assert.AreEqual(decimal.One, result);

        result = LengthConverter.Convert(LengthUnit.Mile, LengthUnit.Kilometer, 10);
        Assert.AreEqual(16.09344m, result);

        result = LengthConverter.Convert(LengthUnit.Meter, LengthUnit.Feet, 10);
        //Assert.AreEqual(32.808398950131235d, result);
        Assert.AreEqual(32.808398950131233595800524934m, result);
    }
            public void WithBindingStringFormatNull(Type targetType, object expected)
            {
                var converter = new LengthConverter {
                    Unit = LengthUnit.Metres
                };
                var providerMock = new ServiceProviderMock
                {
                    BindingStringFormat = null
                };

                converter.ProvideValue(providerMock.Object);
                var length = Length.FromMillimetres(1234);
                var actual = converter.Convert(length, targetType, null, CultureInfo.GetCultureInfo("sv-SE"));

                Assert.AreEqual(expected, actual);
            }
Beispiel #14
0
        /// <summary>Convert the numbers to the new unit.</summary>
        /// <param name="numbers">The numbers used in the convertion.</param>
        /// <returns>The result of the convertion execution.</returns>
        /// <exception cref="ArgumentNullException">When numbers is null.</exception>
        /// <exception cref="ArgumentException">When the length of numbers do not equal <see cref="ArgumentCount"/>.</exception>
        public double Convert(double[] numbers)
        {
            base.Validate(numbers);
            double fromValue = numbers[0];

            switch (current.UnitType)
            {
            case UnitType.Length:
                return(LengthConverter.Convert(
                           (LengthUnit)current.FromUnit,
                           (LengthUnit)current.ToUnit,
                           fromValue));

            case UnitType.Mass:
                return(MassConverter.Convert(
                           (MassUnit)current.FromUnit,
                           (MassUnit)current.ToUnit,
                           fromValue));

            case UnitType.Speed:
                return(SpeedConverter.Convert(
                           (SpeedUnit)current.FromUnit,
                           (SpeedUnit)current.ToUnit,
                           fromValue));

            case UnitType.Temperature:
                return(TemperatureConverter.Convert(
                           (TemperatureUnit)current.FromUnit,
                           (TemperatureUnit)current.ToUnit,
                           fromValue));

            case UnitType.Time:
                return(TimeConverter.Convert(
                           (TimeUnit)current.FromUnit,
                           (TimeUnit)current.ToUnit,
                           fromValue));

            case UnitType.Volume:
                return(VolumeConverter.Convert(
                           (VolumeUnit)current.FromUnit,
                           (VolumeUnit)current.ToUnit,
                           fromValue));

            default:
                throw new ArgumentOutOfRangeException("numbers");
            }
        }
            public void ErrorWhenProvideValueTargetThrows()
            {
                var converter = new LengthConverter {
                    Unit = LengthUnit.Metres
                };
                var providerMock = new ServiceProviderMock();

                providerMock.ProvideValueTargetMock.Setup(x => x.TargetObject).Throws <NullReferenceException>();
                Wpf.Is.DesignMode = true;
                Assert.Throws <InvalidOperationException>(() => converter.ProvideValue(providerMock.Object));

                Wpf.Is.DesignMode = false;
                var length = Length.FromMillimetres(1234);
                var actual = converter.Convert(length, typeof(string), null, null);

                Assert.AreEqual("Touching provideValueTarget?.TargetObject threw", actual);
            }
Beispiel #16
0
        public override float ReadSingle <TUnitType>(TUnitType desiredUnit)
        {
            string s = ReadString();
            int    c = s.Length - 1;

            while (c > 0)
            {
                if (char.IsDigit(s[c]))
                {
                    c++;
                    break;
                }
                c--;
            }

            string Unit = s.Substring(c).ToLowerInvariant();

            s = s.Substring(0, c);
            float parsedNumber;

            if (!float.TryParse(s, out parsedNumber))
            {
                throw new InvalidDataException("Unable to parse " + s + " to a valid single in block " + Token);
            }

            if (desiredUnit is UnitOfLength)
            {
                if (!LengthConverter.KnownUnits.ContainsKey(Unit))
                {
                    throw new InvalidDataException("Unknown or unexpected length unit " + Unit + " encountered in block " + Token);
                }

                parsedNumber = (float)lengthConverter.Convert(parsedNumber, LengthConverter.KnownUnits[Unit], (UnitOfLength)(object)desiredUnit);
            }
            else if (desiredUnit is UnitOfWeight)
            {
                if (!WeightConverter.KnownUnits.ContainsKey(Unit))
                {
                    throw new InvalidDataException("Unknown or unexpected weight unit " + Unit + " encountered in block " + Token);
                }

                parsedNumber = (float)weightConverter.Convert(parsedNumber, WeightConverter.KnownUnits[Unit], (UnitOfWeight)(object)desiredUnit);
            }
            return(parsedNumber);
        }
Beispiel #17
0
            public void SettingStringFormatFailsIfBadFormat(string badFormat)
            {
                var converter = new LengthConverter {
                    Unit = LengthUnit.Centimetres
                };

                Wpf.Is.DesignMode = true;
                var ex       = Assert.Throws <FormatException>(() => converter.StringFormat = badFormat);
                var expected = $"Error parsing: '{badFormat}' for Gu.Units.LengthUnit";

                Assert.AreEqual(expected, ex.Message);

                Wpf.Is.DesignMode      = false;
                converter.StringFormat = badFormat;
                var convert = converter.Convert(Length.Zero, typeof(string), null, null);

                Assert.AreEqual(expected, convert);
            }
            public void WithExplicitUnitAndBindingNonUnitStringFormat(Type targetType, object expected)
            {
                var converter = new LengthConverter
                {
                    Unit = LengthUnit.Centimetres,
                };

                var providerMock = new ServiceProviderMock
                {
                    BindingStringFormat = "{}{0:F2}"
                };

                converter.ProvideValue(providerMock.Object);
                var length = Length.FromMillimetres(12);
                var actual = converter.Convert(length, targetType, null, null);

                Assert.AreEqual(expected, actual);
            }
Beispiel #19
0
        public void Convert()
        {
            double result = LengthConverter.Convert(
                LengthUnit.Feet, LengthUnit.Inch, 1);

            Assert.AreEqual(12, result);

            result = LengthConverter.Convert(
                LengthUnit.Inch, LengthUnit.Feet, 12);
            Assert.AreEqual(1, result);

            result = LengthConverter.Convert(
                LengthUnit.Mile, LengthUnit.Kilometer, 10);
            Assert.AreEqual(16.09344, result);

            result = LengthConverter.Convert(
                LengthUnit.Meter, LengthUnit.Feet, 10);
            Assert.AreEqual(32.808398950131235, result);
        }
Beispiel #20
0
    /// <summary>Convert the numbers to the new unit.</summary>
    /// <param name="numbers">The numbers used in the conversion.</param>
    /// <returns>The result of the conversion execution.</returns>
    /// <exception cref="ArgumentNullException">When numbers is null.</exception>
    /// <exception cref="ArgumentException">When the length of numbers do not equal <see cref="ArgumentCount"/>.</exception>
    public PreciseNumber Evaluate(PreciseNumber[] operands)
    {
        ((IExpression)this).Validate(operands);

        PreciseNumber fromValue = operands[0];

        if (!fromValue.HasValue)
        {
            return(fromValue);
        }

        return(_current.UnitType switch
        {
            UnitType.Length => new PreciseNumber(LengthConverter.Convert((LengthUnit)_current.FromUnit, (LengthUnit)_current.ToUnit, fromValue.Value)),
            UnitType.Mass => new PreciseNumber(MassConverter.Convert((MassUnit)_current.FromUnit, (MassUnit)_current.ToUnit, fromValue.Value)),
            UnitType.Speed => new PreciseNumber(SpeedConverter.Convert((SpeedUnit)_current.FromUnit, (SpeedUnit)_current.ToUnit, fromValue.Value)),
            UnitType.Temperature => new PreciseNumber(TemperatureConverter.Convert((TemperatureUnit)_current.FromUnit, (TemperatureUnit)_current.ToUnit, fromValue.Value)),
            UnitType.Time => new PreciseNumber(TimeConverter.Convert((TimeUnit)_current.FromUnit, (TimeUnit)_current.ToUnit, fromValue.Value)),
            UnitType.Volume => new PreciseNumber(VolumeConverter.Convert((VolumeUnit)_current.FromUnit, (VolumeUnit)_current.ToUnit, fromValue.Value)),
            _ => throw new ArgumentOutOfRangeException(nameof(operands)),
        });
Beispiel #21
0
            public void SettingStringFormatWithUnitWhenUnitIsSet(bool designMode)
            {
                var converter = new LengthConverter
                {
                    Unit = LengthUnit.Centimetres
                };

                var expected = "Unit is set to 'cm' but StringFormat is 'F1 mm'";

                if (designMode)
                {
                    Wpf.Is.DesignMode = true;
                    var ex = Assert.Throws <InvalidOperationException>(() => converter.StringFormat = "F1 mm");
                    Assert.AreEqual(expected, ex.Message);
                }
                else
                {
                    Wpf.Is.DesignMode      = false;
                    converter.StringFormat = "F1 mm";
                    var actual = converter.Convert(Length.FromMetres(1.2), typeof(string), null, CultureInfo.InvariantCulture);
                    Assert.AreEqual(expected, actual);
                }
            }
        public Boolean Validate(IMediaFeature feature, IRenderDevice renderDevice)
        {
            var length = LengthConverter.Convert(feature.Value);

            if (length != null)
            {
                var desired   = length.AsPx(renderDevice, RenderMode.Vertical);
                var available = (Double)renderDevice.ViewPortHeight;

                if (feature.IsMaximum)
                {
                    return(available <= desired);
                }
                else if (feature.IsMinimum)
                {
                    return(available >= desired);
                }

                return(desired == available);
            }

            return(false);
        }
        public Boolean Validate(IMediaFeature feature, IRenderDevice device)
        {
            var length = LengthConverter.Convert(feature.Value);

            if (length != null)
            {
                var desired   = length.AsPixel();
                var available = (Double)device.DeviceWidth;

                if (feature.IsMaximum)
                {
                    return(available <= desired);
                }
                else if (feature.IsMinimum)
                {
                    return(available >= desired);
                }

                return(desired == available);
            }

            return(false);
        }
Beispiel #24
0
 public void ConvertTwoUnknownToFeets()
 {
     Assert.AreEqual(
         2,
         LengthConverter.Convert(2, LengthUnit.Unknown, LengthUnit.Feet));
 }
Beispiel #25
0
 public void ConvertTwoMetersToMeter()
 {
     Assert.AreEqual(
         2,
         LengthConverter.Convert(2, LengthUnit.Meter, LengthUnit.Meter));
 }
Beispiel #26
0
 public void ConvertTwoMetersToUnknown()
 {
     Assert.AreEqual(
         2,
         LengthConverter.Convert(2, LengthUnit.Meter, LengthUnit.Unknown));
 }
Beispiel #27
0
 public void ConvertOneFeetToMeter()
 {
     Assert.AreEqual(1 / FeetToMeterRelation, LengthConverter.Convert(1, LengthUnit.Feet, LengthUnit.Meter));
 }
Beispiel #28
0
        public void ConvertToSame()
        {
            var value = conv.Convert(12, LengthUnits.Metres, LengthUnits.Metres);

            Assert.Equal(12, value);
        }
Beispiel #29
0
 public void ConvertTwoFeetsToFeet()
 {
     Assert.AreEqual(
         2,
         LengthConverter.Convert(2, LengthUnit.Feet, LengthUnit.Feet));
 }
Beispiel #30
0
 public void ConvertZeroFeetToMeter()
 {
     Assert.AreEqual(
         0,
         LengthConverter.Convert(0, LengthUnit.Feet, LengthUnit.Meter));
 }