예제 #1
0
        public void Inequality_null()
        {
            var ue = new UnitExponents();

            Assert.IsFalse(ue.Equals(null));
            Assert.IsFalse(object.Equals(ue, null));
        }
예제 #2
0
        public void ToStringReflectsDivide()
        {
            var ueMeter          = new UnitExponents(1, 0, 0, 0, 0, 0, 0);
            var ueSecond         = new UnitExponents(0, 0, 1, 0, 0, 0, 0);
            var ueMeterPerSecond = ueMeter / ueSecond;

            Assert.AreEqual("m/s", ueMeterPerSecond.ToString());
        }
예제 #3
0
        public void WorksWithListContains()
        {
            var ue        = new UnitExponents(1, 1, 1, 1, 1, 1, 1);
            var same      = new UnitExponents(1, 1, 1, 1, 1, 1, 1);
            var different = new UnitExponents(2, 1, 1, 1, 1, 1, 1);

            var lst = new System.Collections.Generic.List <UnitExponents>();

            lst.Add(different);
            Assert.IsFalse(lst.Contains(ue));
            lst.Add(same);
            Assert.IsTrue(lst.Contains(ue));
        }
예제 #4
0
        public void Equality()
        {
            var ue1 = new UnitExponents(1, 4, 0, 2, -3, -2, 1);
            var ue2 = new UnitExponents(1, 4, 0, 2, -3, -2, 1);
            var ue3 = new UnitExponents(0, 4, 0, 2, -3, -2, -1);

            Assert.AreEqual(ue1, ue2);
            Assert.AreNotSame(ue1, ue2);

            Assert.IsTrue(ue1 == ue2);
            Assert.IsFalse(ue1 == ue3);
            Assert.IsTrue(ue1.Equals(ue2));
            Assert.IsTrue(object.Equals(ue1, ue2));
        }
예제 #5
0
        public void CanDivide()
        {
            var ue1        = new UnitExponents(1, 0, 0, 2, -3, -2, 1);
            var ue2        = new UnitExponents(0, 4, 1, -1, 0, -2, -1);
            var ueQuotient = ue1 / ue2;

            Assert.AreEqual(1, ueQuotient.Length);
            Assert.AreEqual(-4, ueQuotient.Mass);
            Assert.AreEqual(-1, ueQuotient.Time);
            Assert.AreEqual(3, ueQuotient.ElectricCurrent);
            Assert.AreEqual(-3, ueQuotient.Temperature);
            Assert.AreEqual(0, ueQuotient.AmountOfSubstance);
            Assert.AreEqual(2, ueQuotient.LuminousIntensity);
        }
예제 #6
0
        public void CanMultiply()
        {
            var ue1       = new UnitExponents(1, 0, 0, 2, -3, -2, 1);
            var ue2       = new UnitExponents(0, 4, 1, -1, 0, -2, -1);
            var ueProduct = ue1 * ue2;

            Assert.AreEqual(1, ueProduct.Length);
            Assert.AreEqual(4, ueProduct.Mass);
            Assert.AreEqual(1, ueProduct.Time);
            Assert.AreEqual(1, ueProduct.ElectricCurrent);
            Assert.AreEqual(-3, ueProduct.Temperature);
            Assert.AreEqual(-4, ueProduct.AmountOfSubstance);
            Assert.AreEqual(0, ueProduct.LuminousIntensity);
        }
예제 #7
0
        public override string ToString()
        {
            var nominator = UnitExponents
                            .Select((multiplicity, idx) => new
            {
                SIBaseUnit   = (SIBaseUnit)idx,
                Multiplicity = multiplicity
            })
                            .Where(kvp => kvp.Multiplicity > 0).ToList();
            var denominator = UnitExponents
                              .Select((multiplicity, idx) => new
            {
                SIBaseUnit   = (SIBaseUnit)idx,
                Multiplicity = multiplicity
            })
                              .Where(kvp => kvp.Multiplicity < 0)
                              .ToList();

            if (!nominator.Any() && !denominator.Any())
            {
                return("");
            }

            var nominatorStrings = new List <string>();

            foreach (var unitMultiplicity in nominator)
            {
                var nominatorUnit = unitMultiplicity.SIBaseUnit.StringRepresentation();
                if (unitMultiplicity.Multiplicity > 1)
                {
                    nominatorUnit += "^" + unitMultiplicity.Multiplicity;
                }
                nominatorStrings.Add(nominatorUnit);
            }
            var denominatorStrings = new List <string>();

            foreach (var unitMultiplicity in denominator)
            {
                var denominatorUnit = unitMultiplicity.SIBaseUnit.StringRepresentation();
                if (unitMultiplicity.Multiplicity.Abs() > 1)
                {
                    denominatorUnit += "^" + unitMultiplicity.Multiplicity.Abs();
                }
                denominatorStrings.Add(denominatorUnit);
            }
            var unitString = "";

            if (nominatorStrings.Any())
            {
                unitString += nominatorStrings.Aggregate((a, b) => a + " " + b);
            }
            else
            {
                unitString += "1";
            }
            if (!denominator.Any())
            {
                return(unitString);
            }
            unitString += "/";
            unitString += denominatorStrings.Count > 1
                ? $"({denominatorStrings.Aggregate((a, b) => a + " " + b)})"
                : denominatorStrings.Single();
            return(unitString);
        }
예제 #8
0
 public override int GetHashCode()
 {
     return(UnitExponents?.GetHashCode() ?? 0);
 }