Exemplo n.º 1
0
        public void ConvertOneFootToMeters()
        {
            var oneFoot = new Measurement(US.Length.Foot, 1M);

            decimal meters = _graph.Convert(oneFoot, SI.Length.Meter);

            meters.Should().Be(0.3048M);
        }
Exemplo n.º 2
0
        public static double ConvertToMetre(double d, string unitMeasure)
        {
            Unit meter             = new Unit("meter").IsAlsoCalled("metre").CanBeAbbreviated("m");
            Unit angstrom          = new Unit("ångström").CanBeAbbreviated("Å");
            Unit nanometer         = new Unit("nanometer").CanBeAbbreviated("nm");
            Unit astronomical_unit = new Unit("astronomical unit").CanBeAbbreviated("ua");
            Unit chain             = new Unit("chain").CanBeAbbreviated("ch");
            Unit fathom            = new Unit("fathom");
            Unit femtometer        = new Unit("femtometer").CanBeAbbreviated("fm");
            Unit fermi             = new Unit("fermi");
            Unit foot       = new Unit("foot").CanBeAbbreviated("ft");
            Unit inch       = new Unit("inch").CanBeAbbreviated("in");
            Unit light_year = new Unit("light year").CanBeAbbreviated("l. y.");
            Unit microinch  = new Unit("microinch");
            Unit micrometer = new Unit("micrometer").CanBeAbbreviated("μm");
            Unit micron     = new Unit("micron").CanBeAbbreviated("μ");
            Unit mil        = new Unit("mil");
            Unit mile       = new Unit("mile").CanBeAbbreviated("mi");
            Unit kilometer  = new Unit("kilometer").CanBeAbbreviated("km");
            Unit parsec     = new Unit("parsec").CanBeAbbreviated("pc");
            Unit pica       = new Unit("pica");
            Unit millimeter = new Unit("millimeter").CanBeAbbreviated("mm");
            Unit point      = new Unit("point");
            Unit rod        = new Unit("rod").CanBeAbbreviated("rd");
            Unit yard       = new Unit("yard").CanBeAbbreviated("yd");

            ConversionGraph system = new ConversionGraph();

            system.AddConversion(
                Conversions.From(angstrom).To(meter).MultiplyBy(1.0E-10m),
                Conversions.From(angstrom).To(nanometer).MultiplyBy(1.0E-01m),
                Conversions.From(astronomical_unit).To(meter).MultiplyBy(1.495979E+11m),
                Conversions.From(chain).To(meter).MultiplyBy(2.011684E+1m),
                Conversions.From(fathom).To(meter).MultiplyBy(1.828804E+00m),
                Conversions.From(fermi).To(meter).MultiplyBy(1.0E-15m),
                Conversions.From(fermi).To(femtometer).MultiplyBy(1.0E+00m),
                Conversions.From(foot).To(meter).MultiplyBy(3.048E-01m),
                Conversions.From(inch).To(meter).MultiplyBy(2.54E-02m),
                Conversions.From(light_year).To(meter).MultiplyBy(9.46073E+15m),
                Conversions.From(microinch).To(meter).MultiplyBy(2.54E-08m),
                Conversions.From(microinch).To(micrometer).MultiplyBy(2.54E-02m),
                Conversions.From(micron).To(meter).MultiplyBy(1.0E-06m),
                Conversions.From(micron).To(micrometer).MultiplyBy(1.0E+00m),
                Conversions.From(mil).To(meter).MultiplyBy(2.54E-05m),
                Conversions.From(mil).To(millimeter).MultiplyBy(2.54E-02m),
                Conversions.From(mile).To(meter).MultiplyBy(1.609344E+03m),
                Conversions.From(mile).To(kilometer).MultiplyBy(1.609344E+00m),
                Conversions.From(parsec).To(meter).MultiplyBy(3.085678E+16m),
                Conversions.From(pica).To(meter).MultiplyBy(4.233333E-03m),
                Conversions.From(pica).To(millimeter).MultiplyBy(4.233333E+00m),
                Conversions.From(point).To(meter).MultiplyBy(3.527778E-04m),
                Conversions.From(point).To(millimeter).MultiplyBy(3.527778E-01m),
                Conversions.From(rod).To(meter).MultiplyBy(5.029210E+00m),
                Conversions.From(yard).To(meter).MultiplyBy(9.144E-01m));

            Measurement measurement = new Measurement(unitMeasure, (decimal)d);

            return((double)system.Convert(measurement, meter));
        }
Exemplo n.º 3
0
        public void DollarsToYen()
        {
            var dollar = new Unit("dollar")
                         .UsePluralFormat("{0}s");

            var yen = new Unit("yen");

            var system = new ConversionGraph().RoundToDecimalPlaces(2);

            system.AddConversion(Conversions.One(dollar).In(yen).Is(78.5300M));

            var dollarAmount = new Measurement(dollar, 10);
            var yenAmount    = new Measurement(yen, 10000);

            Assert.That(system.Convert(dollarAmount, yen) == 785.30M);
            Assert.That(system.Convert(yenAmount, dollar) == 127.34M);
        }
Exemplo n.º 4
0
        public void DollarsToYen()
        {
            var dollar = new Unit("dollar")
                .UsePluralFormat("{0}s");

            var yen = new Unit("yen");

            var system = new ConversionGraph().RoundToDecimalPlaces(2);

            system.AddConversion(Conversions.One(dollar).In(yen).Is(78.5300M));

            var dollarAmount = new Measurement(dollar, 10);
            var yenAmount = new Measurement(yen, 10000);

            Assert.That(system.Convert(dollarAmount, yen) == 785.30M);
            Assert.That(system.Convert(yenAmount, dollar) == 127.34M);
        }
Exemplo n.º 5
0
        public void ConvertOneGallonToLiters()
        {
            var gallon = new Measurement(US.Volume.Gallon, 1M);

            decimal liters = _volume.Convert(gallon, SI.Volume.Liter);

            liters.Should().Be(3.7854M);
        }
Exemplo n.º 6
0
        public void IndirectConversion()
        {
            var system = new ConversionGraph();

            var meter     = new Unit("meter");
            var kilometer = new Unit("kilometer");
            var foot      = new Unit("foot");

            system.AddConversion(
                Conversions.From(kilometer).To(meter).MultiplyBy(1000M),
                Conversions.From(meter).To(foot).MultiplyBy(3.28084M)
                );

            var measurement = new Measurement(kilometer, 100M);

            Assert.That(system.Convert(measurement, foot) == 328084M);

            Assert.That(system.Convert(new Measurement(foot, 328084M), kilometer) == 100M);
        }
Exemplo n.º 7
0
        public void IndirectConversion()
        {
            var system = new ConversionGraph();

            var meter = new Unit("meter");
            var kilometer = new Unit("kilometer");
            var foot = new Unit("foot");

            system.AddConversion(
                Conversions.From(kilometer).To(meter).MultiplyBy(1000M),
                Conversions.From(meter).To(foot).MultiplyBy(3.28084M)
                );

            var measurement = new Measurement(kilometer, 100M);

            Assert.That(system.Convert(measurement, foot) == 328084M);

            Assert.That(system.Convert(new Measurement(foot, 328084M), kilometer) == 100M);
        }
Exemplo n.º 8
0
        public void UnitNames()
        {
            var meter = new Unit("meter")
                        .IsAlsoCalled("metre")
                        .CanBeAbbreviated("m", "mtr")
                        .UsePluralFormat("{0}s");

            var feet = new Unit("foot")
                       .PluralizeAs("feet")
                       .CanBeAbbreviated("ft");

            var system = new ConversionGraph()
                         .RoundToDecimalPlaces(5);

            system.AddConversion(Conversions.One(meter).In(feet).Is(3.28084M));

            var meterMeasurement = new Measurement(meter, 1);
            var feetMeasurement  = new Measurement(feet, 2);

            var x = system.Convert(meterMeasurement, feet);

            Assert.That(system.Convert(meterMeasurement, feet) == 3.28084M);
            Assert.That(system.Convert(meterMeasurement, "ft") == 3.28084M);
            Assert.That(system.Convert(feetMeasurement, "metre") == 0.6096M);
            Assert.That(system.Convert(feetMeasurement, "mtr") == 0.6096M);
            Assert.That(system.Convert(feetMeasurement, "m") == 0.6096M);

            Assert.That(meterMeasurement.ToAbbreviatedString() == "1 m");
            Assert.That(meterMeasurement.ToString() == "1 meter");

            Assert.That(feetMeasurement.ToAbbreviatedString() == "2 ft");
            Assert.That(feetMeasurement.ToString() == "2 feet");
        }
Exemplo n.º 9
0
        public void UnitNames()
        {
            var meter = new Unit("meter")
                .IsAlsoCalled("metre")
                .CanBeAbbreviated("m", "mtr")
                .UsePluralFormat("{0}s");

            var feet = new Unit("foot")
                .PluralizeAs("feet")
                .CanBeAbbreviated("ft");

            var system = new ConversionGraph()
                .RoundToDecimalPlaces(5);

            system.AddConversion(Conversions.One(meter).In(feet).Is(3.28084M));

            var meterMeasurement = new Measurement(meter, 1);
            var feetMeasurement = new Measurement(feet, 2);

            var x = system.Convert(meterMeasurement, feet);

            Assert.That(system.Convert(meterMeasurement, feet) == 3.28084M);
            Assert.That(system.Convert(meterMeasurement, "ft") == 3.28084M);
            Assert.That(system.Convert(feetMeasurement, "metre") == 0.6096M);
            Assert.That(system.Convert(feetMeasurement, "mtr") == 0.6096M);
            Assert.That(system.Convert(feetMeasurement, "m") == 0.6096M);

            Assert.That(meterMeasurement.ToAbbreviatedString() == "1 m");
            Assert.That(meterMeasurement.ToString() == "1 meter");

            Assert.That(feetMeasurement.ToAbbreviatedString() == "2 ft");
            Assert.That(feetMeasurement.ToString() == "2 feet");
        }
Exemplo n.º 10
0
        public void ThirtyTwoDegreesFahrenheitInCelcius()
        {
            var degrees = new Measurement("Fahrenheit", 32M);

            var result = _graph.Convert(degrees, "Celcius");

            result.Should().Be(0M);
        }
Exemplo n.º 11
0
        public void OneMeterInYoctometers()
        {
            var oneMeter = new Measurement(SI.Length.Meter, 1M);

            var result = _length.Convert(oneMeter, SI.Length.Yoctometer);

            var target = Convert.ToDecimal(Math.Pow(10, 24));

            result.Should().Be(target);
        }
Exemplo n.º 12
0
        public static double ConvertToSquareMetre(double d, string unitMeasure)
        {
            Unit square_meter              = new Unit("square meter").CanBeAbbreviated("m2", "m^2");
            Unit acre                      = new Unit("acre");
            Unit are                       = new Unit("are").CanBeAbbreviated("a");
            Unit barn                      = new Unit("barn").CanBeAbbreviated("b");
            Unit circular_mil              = new Unit("circular mil");
            Unit square_millimeter         = new Unit("square millimeter").CanBeAbbreviated("mm2", "mm^2");
            Unit foot_to_the_fourth_power  = new Unit("foot to the fourth power").CanBeAbbreviated("ft4", "ft^4");
            Unit meter_to_the_fourth_power = new Unit("meter to the fourth power").CanBeAbbreviated("m4", "m^4");
            Unit hectare                   = new Unit("hectare").CanBeAbbreviated("ha");
            Unit inch_to_the_fourth_power  = new Unit("inch to the fourth power").CanBeAbbreviated("in4", "in^4");
            Unit square_foot               = new Unit("square foot").CanBeAbbreviated("ft2", "ft^2");
            Unit square_inch               = new Unit("square inch").CanBeAbbreviated("in2", "in^2");
            Unit square_centimeter         = new Unit("square centimeter").CanBeAbbreviated("cm2", "cm^2");
            Unit square_mile               = new Unit("square mile").CanBeAbbreviated("mi2", "mi^2");
            Unit square_kilometer          = new Unit("square kilometer").CanBeAbbreviated("km2", "km^2");
            Unit square_yard               = new Unit("square yard ").CanBeAbbreviated("yd2", "yd^2");

            ConversionGraph system = new ConversionGraph();

            system.AddConversion(
                Conversions.From(acre).To(square_meter).MultiplyBy(4.046873E+03m),
                Conversions.From(are).To(square_meter).MultiplyBy(1.0E+02m),
                Conversions.From(barn).To(square_meter).MultiplyBy(1.0E-28m),
                Conversions.From(circular_mil).To(square_meter).MultiplyBy(5.067075E-10m),
                Conversions.From(circular_mil).To(square_millimeter).MultiplyBy(5.067075E-04m),
                Conversions.From(foot_to_the_fourth_power).To(meter_to_the_fourth_power).MultiplyBy(8.630975E-03m),
                Conversions.From(hectare).To(square_meter).MultiplyBy(1.0E+04m),
                Conversions.From(inch_to_the_fourth_power).To(meter_to_the_fourth_power).MultiplyBy(4.162314E-07m),
                Conversions.From(square_foot).To(square_meter).MultiplyBy(9.290304E-02m),
                Conversions.From(square_inch).To(square_meter).MultiplyBy(6.4516E-04m),
                Conversions.From(square_inch).To(square_centimeter).MultiplyBy(6.4516E+00m),
                Conversions.From(square_mile).To(square_meter).MultiplyBy(2.589988E+06m),
                Conversions.From(square_mile).To(square_kilometer).MultiplyBy(2.589988E+00m),
                Conversions.From(square_yard).To(square_meter).MultiplyBy(8.361274E-01m));

            Measurement measurement = new Measurement(unitMeasure, (decimal)d);

            return((double)system.Convert(measurement, square_meter));
        }
Exemplo n.º 13
0
        public static double ConvertToSquareMetre(double d, string unitMeasure) {
            Unit square_meter = new Unit("square meter").CanBeAbbreviated("m2", "m^2");
            Unit acre = new Unit("acre");
            Unit are = new Unit("are").CanBeAbbreviated("a");
            Unit barn = new Unit("barn").CanBeAbbreviated("b");
            Unit circular_mil = new Unit("circular mil");
            Unit square_millimeter = new Unit("square millimeter").CanBeAbbreviated("mm2", "mm^2");
            Unit foot_to_the_fourth_power = new Unit("foot to the fourth power").CanBeAbbreviated("ft4", "ft^4");
            Unit meter_to_the_fourth_power = new Unit("meter to the fourth power").CanBeAbbreviated("m4", "m^4");
            Unit hectare = new Unit("hectare").CanBeAbbreviated("ha");
            Unit inch_to_the_fourth_power = new Unit("inch to the fourth power").CanBeAbbreviated("in4", "in^4");
            Unit square_foot = new Unit("square foot").CanBeAbbreviated("ft2", "ft^2");
            Unit square_inch = new Unit("square inch").CanBeAbbreviated("in2", "in^2");
            Unit square_centimeter = new Unit("square centimeter").CanBeAbbreviated("cm2", "cm^2");
            Unit square_mile = new Unit("square mile").CanBeAbbreviated("mi2", "mi^2");
            Unit square_kilometer = new Unit("square kilometer").CanBeAbbreviated("km2", "km^2");
            Unit square_yard = new Unit("square yard ").CanBeAbbreviated("yd2", "yd^2");

            ConversionGraph system = new ConversionGraph();
            system.AddConversion(
                Conversions.From(acre).To(square_meter).MultiplyBy((decimal)4.046873E+03),
                Conversions.From(are).To(square_meter).MultiplyBy((decimal)1.0E+02),
                Conversions.From(barn).To(square_meter).MultiplyBy((decimal)1.0E-28),
                Conversions.From(circular_mil).To(square_meter).MultiplyBy((decimal)5.067075E-10),
                Conversions.From(circular_mil).To(square_millimeter).MultiplyBy((decimal)5.067075E-04),
                Conversions.From(foot_to_the_fourth_power).To(meter_to_the_fourth_power).MultiplyBy((decimal)8.630975E-03),
                Conversions.From(hectare).To(square_meter).MultiplyBy((decimal)1.0E+04),
                Conversions.From(inch_to_the_fourth_power).To(meter_to_the_fourth_power).MultiplyBy((decimal)4.162314E-07),
                Conversions.From(square_foot).To(square_meter).MultiplyBy((decimal)9.290304E-02),
                Conversions.From(square_inch).To(square_meter).MultiplyBy((decimal)6.4516E-04),
                Conversions.From(square_inch).To(square_centimeter).MultiplyBy((decimal)6.4516E+00),
                Conversions.From(square_mile).To(square_meter).MultiplyBy((decimal)2.589988E+06),
                Conversions.From(square_mile).To(square_kilometer).MultiplyBy((decimal)2.589988E+00),
                Conversions.From(square_yard).To(square_meter).MultiplyBy((decimal)8.361274E-01));

            Measurement measurement = new Measurement(unitMeasure, (decimal)d);
            return (double)system.Convert(measurement, square_meter);
        }
Exemplo n.º 14
0
        public static double ConvertToMetre(double d, string unitMeasure) {
            Unit meter = new Unit("meter").IsAlsoCalled("metre").CanBeAbbreviated("m");
            Unit angstrom = new Unit("ångström").CanBeAbbreviated("Å");
            Unit nanometer = new Unit("nanometer").CanBeAbbreviated("nm");
            Unit astronomical_unit = new Unit("astronomical unit").CanBeAbbreviated("ua");
            Unit chain = new Unit("chain").CanBeAbbreviated("ch");
            Unit fathom = new Unit("fathom");
            Unit femtometer = new Unit("femtometer").CanBeAbbreviated("fm");
            Unit fermi = new Unit("fermi");
            Unit foot = new Unit("foot").CanBeAbbreviated("ft");
            Unit inch = new Unit("inch").CanBeAbbreviated("in");
            Unit light_year = new Unit("light year").CanBeAbbreviated("l. y.");
            Unit microinch = new Unit("microinch");
            Unit micrometer = new Unit("micrometer").CanBeAbbreviated("μm");
            Unit micron = new Unit("micron").CanBeAbbreviated("μ");
            Unit mil = new Unit("mil");
            Unit mile = new Unit("mile").CanBeAbbreviated("mi");
            Unit kilometer = new Unit("kilometer").CanBeAbbreviated("km");
            Unit parsec = new Unit("parsec").CanBeAbbreviated("pc");
            Unit pica = new Unit("pica");
            Unit millimeter = new Unit("millimeter").CanBeAbbreviated("mm");
            Unit point = new Unit("point");
            Unit rod = new Unit("rod").CanBeAbbreviated("rd");
            Unit yard = new Unit("yard").CanBeAbbreviated("yd");

            ConversionGraph system = new ConversionGraph();
            system.AddConversion(
                Conversions.From(angstrom).To(meter).MultiplyBy((decimal)1.0E-10),
                Conversions.From(angstrom).To(nanometer).MultiplyBy((decimal)1.0E-01),
                Conversions.From(astronomical_unit).To(meter).MultiplyBy((decimal)1.495979E+11),
                Conversions.From(chain).To(meter).MultiplyBy((decimal)2.011684E+1),
                Conversions.From(fathom).To(meter).MultiplyBy((decimal)1.828804E+00),
                Conversions.From(fermi).To(meter).MultiplyBy((decimal)1.0E-15),
                Conversions.From(fermi).To(femtometer).MultiplyBy((decimal)1.0E+00),
                Conversions.From(foot).To(meter).MultiplyBy((decimal)3.048E-01),
                Conversions.From(inch).To(meter).MultiplyBy((decimal)2.54E-02),
                Conversions.From(light_year).To(meter).MultiplyBy((decimal)9.46073E+15),
                Conversions.From(microinch).To(meter).MultiplyBy((decimal)2.54E-08),
                Conversions.From(microinch).To(micrometer).MultiplyBy((decimal)2.54E-02),

                Conversions.From(micron).To(meter).MultiplyBy((decimal)1.0E-06),
                Conversions.From(micron).To(micrometer).MultiplyBy((decimal)1.0E+00),
                Conversions.From(mil).To(meter).MultiplyBy((decimal)2.54E-05),
                Conversions.From(mil).To(millimeter).MultiplyBy((decimal)2.54E-02),
                Conversions.From(mile).To(meter).MultiplyBy((decimal)1.609344E+03),
                Conversions.From(mile).To(kilometer).MultiplyBy((decimal)1.609344E+00),
                Conversions.From(parsec).To(meter).MultiplyBy((decimal)3.085678E+16),
                Conversions.From(pica).To(meter).MultiplyBy((decimal)4.233333E-03),
                Conversions.From(pica).To(millimeter).MultiplyBy((decimal)4.233333E+00),
                Conversions.From(point).To(meter).MultiplyBy((decimal)3.527778E-04),
                Conversions.From(point).To(millimeter).MultiplyBy((decimal)3.527778E-01),
                Conversions.From(rod).To(meter).MultiplyBy((decimal)5.029210E+00),
                Conversions.From(yard).To(meter).MultiplyBy((decimal)9.144E-01));

            Measurement measurement = new Measurement(unitMeasure, (decimal)d);
            return (double)system.Convert(measurement, meter);
        }
Exemplo n.º 15
0
 public void OneHourIsSixtyMinutes()
 {
     _time.Convert(new Measurement("hour", 1M), "minute").Should().Be(60M);
 }
Exemplo n.º 16
0
        public void NonexistentSourceUnitsShouldThrowException()
        {
            var oneFoo = new Measurement(new Unit("foo"), 1M);

            Assert.Throws(typeof(ConversionNotFoundException), () => _graph.Convert(oneFoo, US.Length.Foot));
        }