Beispiel #1
0
        public void ShouldConvertAtopA() //Case 1666
        {
            var m = new Measurement(2.5, "A");

            Assert.That(Converters.Convert(m, "pA"),
                        Is.EqualTo(new Measurement(2.5e12, "pA")));
        }
 /// <summary>
 /// Do a Measurement conversion on the Measurement passed in, using the
 /// Conversions dictionary stored in the Controller. Override in derived
 /// classes only if you don't want to use the Conversions dictionary in
 /// the Controller, or if you don't want to use the MeasurementConversionTarget
 /// property to specify the unit type to convert into.
 /// </summary>
 /// <param name="incoming">The Measurement to convert</param>
 /// <param name="outgoingUnits">The unit type to conver to; ensure this unit type
 /// is present in the Controller's Conversions dictionary</param>
 /// <returns></returns>
 public virtual IMeasurement Convert(IMeasurement incoming, string outgoingUnits)
 {
     // Grab the Controller at the end of the pipeline; all of the ExternalDevices
     // attached to this stream have a reference to it, just grab the first so we
     // can get to it
     return(Converters.Convert(incoming, outgoingUnits));
 }
Beispiel #3
0
        public void ShouldConvertVTomV() // Case 1666
        {
            var m = new Measurement(2.5, "V");

            Assert.That(Converters.Convert(m, "mV"),
                        Is.EqualTo(new Measurement(2500, "mV")));
        }
Beispiel #4
0
        public void TestSimpleConversion()
        {
            Converters.Register("ImpFeet", "inches",
                                delegate(IMeasurement ft)
            {
                if (ft.BaseUnit == "ImpFeet")
                {
                    return(new Measurement(ft.Quantity * 12, "inches"));
                }
                throw new Exception(String.Format("Illegal conversion: {0} to inches", ft.BaseUnit));
            });
            Converters.Register("ImpYards", "inches",
                                delegate(IMeasurement yd)
            {
                if (yd.BaseUnit == "ImpYards")
                {
                    return(new Measurement(yd.Quantity * 12 * 3, "inches"));
                }
                throw new Exception(String.Format("Illegal conversion: {0} to inches", yd.BaseUnit));
            });

            Measurement tedsHeight = new Measurement(6, "ImpFeet");

            Assert.That(Converters.Convert(tedsHeight, "inches"), Is.EqualTo(new Measurement(72, "inches")));

            tedsHeight = new Measurement(2, "ImpYards");
            Assert.That(Converters.Convert(tedsHeight, "inches"), Is.EqualTo(new Measurement(72, "inches")));
        }
        public void UnknownMeasurementConversionRaisesException()
        {
            // The controller has nothing in its conversions dictionary to start
            // so therefore this should throw an exception
            Controller controller = new Controller();

            Measurement  tedsHeight       = new Measurement(74, "inches");
            IMeasurement tedsHeightInFeet = Converters.Convert(tedsHeight, "feet");
        }
Beispiel #6
0
        public void ShouldHandleScalingInBaseUnitIdentityConversion(
            [Values(-3, -2, -1, 0, 1, 2, 3)] int toExp,
            [Values(-3, -2, -1, 0, 1, 2, 3)] int fromExp)
        {
            var q = 1.5m;
            var m = new Measurement(q, fromExp, "B");

            var siUnits = new InternationalSystem();

            Assert.That(Converters.Convert(m, string.Format("{0}{1}", siUnits.ToPrefix(toExp), "B")),
                        Is.EqualTo(new Measurement(q * (decimal)Math.Pow(10, fromExp - toExp), toExp, "B")));
        }
Beispiel #7
0
 public IInputData DataWithUnits(string unit)
 {
     return(DataWithConversion(m => Converters.Convert(m, unit)));
 }
 /// <summary>
 /// Do a Measurement conversion on the Measurement passed in, using the
 /// Conversions dictionary stored in the Controller. Override in derived
 /// classes only if you don't want to use the Conversions dictionary in
 /// the Controller, or if you don't want to use the MeasurementConversionTarget
 /// property to specify the unit type to convert into.
 /// </summary>
 /// <param name="incoming">The Measurement to convert</param>
 /// <param name="outgoingUnits">The unit type to conver to; ensure this unit type
 /// is present in the Controller's Conversions dictionary</param>
 /// <returns></returns>
 public virtual IMeasurement Convert(IMeasurement incoming, string outgoingUnits)
 {
     // Grab the Controller at the end of the pipeline
     return(Converters.Convert(incoming, outgoingUnits));
 }
 protected override IMeasurement ConvertOutput(IMeasurement deviceOutput)
 {
     return(Converters.Convert(deviceOutput, MeasurementConversionTarget));
 }
Beispiel #10
0
        public void TestIdentityMeasurementConversion()
        {
            Measurement tedsHeight = new Measurement(74, "inches");

            Assert.IsTrue(Converters.Convert(tedsHeight, "inches").Equals(new Measurement(74, "inches")));
        }
Beispiel #11
0
        public void TestExponentialConversion()
        {
            Converters.Register("m", "cm",
                                delegate(IMeasurement m)
            {
                if (m.BaseUnit == "m")
                {
                    // m are 100 cm
                    double q = (double)(m.Quantity * 100);

                    // but only if the exponents match; if they don't, then we need to adjust
                    int exp = m.Exponent;
                    if (exp < 0)
                    {
                        while (exp < 0)
                        {
                            q = q / 10;
                            exp++;
                        }
                    }
                    else if (exp > 0)
                    {
                        while (exp > 0)
                        {
                            q = q * 10;
                            exp--;
                        }
                    }

                    return(new Measurement((decimal)q, "cm"));
                }
                throw new Exception(String.Format("Illegal conversion: {0} to cm", m.BaseUnit));
            });

            Measurement oneMeter = new Measurement(1, "m"); // no exponent (oneMeter.Exponent = 0)

            Assert.That(Converters.Convert(oneMeter, "cm"), Is.EqualTo(new Measurement(100, "cm")));

            Measurement tenMeter = new Measurement(1, 1, "m"); // exponent (tenMeter.Exponent = 1)

            Assert.That(Converters.Convert(tenMeter, "cm"), Is.EqualTo(new Measurement(1000, "cm")));

            // Now go the other way
            Converters.Register("cm", "m",
                                delegate(IMeasurement m)
            {
                if (m.BaseUnit == "cm")
                {
                    // m are 100 cm
                    double q = (double)(m.Quantity / 100);

                    // but only if the exponents match; if they don't, then we need to adjust
                    int exp = m.Exponent;
                    if (exp < 0)
                    {
                        while (exp < 0)
                        {
                            q = q / 10;
                            exp++;
                        }
                    }
                    else if (exp > 0)
                    {
                        while (exp > 0)
                        {
                            q = q * 10;
                            exp--;
                        }
                    }

                    return(new Measurement((decimal)q, "m"));
                }
                throw new Exception(String.Format("Illegal conversion: {0} cm to m", m.BaseUnit));
            });

            var oneCentimeter = new Measurement(1, "cm"); // no exponent

            Assert.That(Converters.Convert(oneCentimeter, "m"), Is.EqualTo(new Measurement(0.01m, "m")));

            var tenCentimeter = new Measurement(1, 1, "cm"); // no exponent (tenCentimeter.Exponent = 1)

            Assert.That(Converters.Convert(tenCentimeter, "m"), Is.EqualTo(new Measurement(0.1m, "m")));
        }