From 785b7871d129635340da82d91167ffacd014b8fc Mon Sep 17 00:00:00 2001 From: stefan meyer <stefan.meyer@student.tugraz.at> Date: Thu, 7 Sep 2017 23:37:35 +0200 Subject: [PATCH] finished for refactoring. --- VectoCommon/VectoCommon/Utils/SI.cs | 101 ++--------------------- VectoCommon/VectoCommon/Utils/SIUtils.cs | 50 ++--------- VectoCore/VectoCoreTest/Utils/SITest.cs | 18 ++-- 3 files changed, 20 insertions(+), 149 deletions(-) diff --git a/VectoCommon/VectoCommon/Utils/SI.cs b/VectoCommon/VectoCommon/Utils/SI.cs index ec96cd20ba..a219dd41ba 100644 --- a/VectoCommon/VectoCommon/Utils/SI.cs +++ b/VectoCommon/VectoCommon/Utils/SI.cs @@ -1104,12 +1104,8 @@ namespace TUGraz.VectoCommon.Utils { Val = val; - //_reciproc = false; - //_reverse = false; - SIUnits = new int[7] { 0, 0, 0, 0, 0, 0, 0 }; - //_exponent = 1; if (double.IsNaN(val)) { @@ -1137,9 +1133,6 @@ namespace TUGraz.VectoCommon.Utils { Val = val; isMass = isMassParam; - //_reciproc = reciproc; - //_reverse = reverse; - //_exponent = exponent; SIUnits = siunits; @@ -1170,81 +1163,12 @@ namespace TUGraz.VectoCommon.Utils //[DebuggerHidden] //protected SI(SI si, double? factor = null, int[] siUnitsParam = null, // bool? reciproc = null, bool? reverse = null, int? exponent = null) - protected SI(SI si, double factor , int[] siUnitsParam, int exponent, bool reverse, UnitInstance.IsMass isMassParam) + protected SI(SI si, double factor , int[] siUnitsParam, UnitInstance.IsMass isMassParam) { - // Val = si.Val; - Val = si.Val * factor; + Val = si.Val / factor; isMass = isMassParam; SIUnits = siUnitsParam; - //_reciproc = reciproc ?? si._reciproc; - //_reverse = reverse ?? si._reverse; - //_exponent = exponent ?? si._exponent; - - - //if (siUnitsParam == null) //////////////???? - //{ - // siUnitsParam = new int[] { 0, 0, 0, 0, 0, 0, 0 }; - //} - - - ////if (_reciproc) - ////{ - ////siUnitsParam = SIUtils.SIUnitsMultFactor(siUnitsParam, -1); - ////} - - ////if (_reverse) - //if (reverse) - //{ - // // compare the si Units - // if (!SIUtils.CompareSIUnits(siUnitsParam, si.SIUnits)) - // { - // throw new VectoException( - // "Unit missing. Conversion not possible. [{0}] does not contain a [{1}].", - // GetUnitString(siUnitsParam), si.GetUnitString()); - // } - - // SIUnits = si.SIUnits; - - // factor = 1 / factor; - - // ////_reverse = false; - - //} - //else - //{ - //SIUnits = SIUtils.AdditionTheSIUnits(si.SIUnits, siUnitsParam); - - // //SIUnits = SIUtils.AdditionTheSIUnits(si.SIUnits, SIUtils.SIUnitsMultFactor(siUnitsParam, _exponent)); - //} - - - //if (_reciproc) - //{ - // if (factor.HasValue) - // { - // //Val /= (factor.Value * _exponent); - // Val /= Math.Pow(factor.Value, _exponent); - // } - - //} - //else - //{ - // if (factor.HasValue) - // { - // //Val *= (factor.Value * _exponent); - // Val *= Math.Pow(factor, exponent); - // } - //} - - //if (_reverse) - //if (reverse) - //{ - // // Val /= factor; - // //Val /= factor.Value; - // //_reverse = false; - //} - if (double.IsNaN(Val)) @@ -1285,32 +1209,22 @@ namespace TUGraz.VectoCommon.Utils } double factorValue = si.Getfactor; - int exp = si.GetExponent; if ((isMass & UnitInstance.IsMass.IsGramm) == UnitInstance.IsMass.IsGramm) { - factorValue /= 1000; + factorValue *= 1000; } if((si.GetGrammMode() & UnitInstance.IsMass.IsGramm) == UnitInstance.IsMass.IsGramm) { - factorValue *= 1000; + factorValue /= 1000; isMass |= UnitInstance.IsMass.IsGramm; } - //if (si.GetGrammMode() == UnitInstance.GrammMode.Gramm) - //{ - // factorValue *= 1000.0; - //} - //if ((si.GetGrammMode() & UnitInstance.IsMass.IsGramm) == UnitInstance.IsMass.IsGramm) - //{ - // factorValue *= 1000.0; - //} - //return new SI(this, siUnitsParam: si.GetSIUnits(), factor: factorValue, - // exponent: 1, reciproc: false, reverse: true); - return new SI(this, siUnitsParam: si.GetSIUnits(), factor: factorValue, exponent: exp, - reverse: true, isMassParam: isMass); + + return new SI(this, siUnitsParam: si.GetSIUnits(), factor: factorValue, + isMassParam: isMass); } /// <summary> @@ -1622,7 +1536,6 @@ namespace TUGraz.VectoCommon.Utils - #region Operators /// <summary> diff --git a/VectoCommon/VectoCommon/Utils/SIUtils.cs b/VectoCommon/VectoCommon/Utils/SIUtils.cs index d72f189ad8..742e356c88 100644 --- a/VectoCommon/VectoCommon/Utils/SIUtils.cs +++ b/VectoCommon/VectoCommon/Utils/SIUtils.cs @@ -63,7 +63,7 @@ namespace TUGraz.VectoCommon.Utils { get { - return new UnitInstance(new int[7] { 0, 0, 0, 0, 0, 0, 0 }, 1, 1, 1);//, UnitInstance.GrammMode.NoMass); + return new UnitInstance(new int[7] { 0, 0, 0, 0, 0, 0, 0 }, 1, 1, 1); } } @@ -159,8 +159,9 @@ namespace TUGraz.VectoCommon.Utils public struct UnitInstance { - private int[] units; + // kg, m, s, A, K, mol, cd + private int[] units; private double factorValue; private int exponent; private int reciproc; @@ -175,32 +176,19 @@ namespace TUGraz.VectoCommon.Utils } private IsMass isMassOption; - //public enum GrammMode - //{ - // NoMass, - // Gramm, - // Kilo, - // KiloGramm - //} - //private GrammMode grammMode; public UnitInstance(int[] param_units, - double param_factor, int param_exponent, int param_reciproc)//, - //GrammMode param_grammMode) + double param_factor, int param_exponent, int param_reciproc) { units = param_units; factorValue = param_factor; exponent = param_exponent; reciproc = param_reciproc; - //grammMode = param_grammMode; isMassOption = IsMass.IsKiloGramm; } - //public GrammMode GetGrammMode() - //{ - // return grammMode; - //} + public IsMass GetGrammMode() { @@ -217,28 +205,13 @@ namespace TUGraz.VectoCommon.Utils { return factorValue; } - //return factorValue; - } - public int GetExponent - { - get - { - return exponent; - } } + public UnitInstance Gramm { get { - //if (grammMode == GrammMode.NoMass) - //{ - // grammMode = GrammMode.Gramm; - //} - //else if (grammMode == GrammMode.Kilo) - //{ - // grammMode = GrammMode.KiloGramm; - //} units[0] += 1 * reciproc * exponent; @@ -299,15 +272,6 @@ namespace TUGraz.VectoCommon.Utils { isMassOption = (isMassOption | IsMass.IsKilo); - //if (grammMode == GrammMode.NoMass) - //{ - // grammMode = GrammMode.Kilo; - //} - //else if (grammMode == GrammMode.Gramm) - //{ - // grammMode = GrammMode.KiloGramm; - //} - factorValue *= Math.Pow(1000, exponent * reciproc); //return new UnitInstance(units, factorValue, exponent, reciproc, grammMode); return this; @@ -504,7 +468,7 @@ namespace TUGraz.VectoCommon.Utils isMassOption = (isMassOption & ~IsMass.IsKilo); int ReciprocAndExponent = reciproc * exponent; - units[2] += 3 * ReciprocAndExponent; + units[1] += 3 * ReciprocAndExponent; factorValue /= Math.Pow(1000, ReciprocAndExponent); return this; } diff --git a/VectoCore/VectoCoreTest/Utils/SITest.cs b/VectoCore/VectoCoreTest/Utils/SITest.cs index 8bf61afdfc..0d55ea6242 100644 --- a/VectoCore/VectoCoreTest/Utils/SITest.cs +++ b/VectoCore/VectoCoreTest/Utils/SITest.cs @@ -162,7 +162,7 @@ namespace TUGraz.VectoCore.Tests.Utils //kg = kg.ConvertTo().Gramm.Clone(); kg = kg.ConvertTo(Unit.SI.Gramm).Clone(); Assert.AreEqual(5000, kg.Value()); - Assert.AreEqual("5000.0000 [g]", kg.ToString()); //not tested + Assert.AreEqual("5000.0000 [g]", kg.ToString()); var x = 5.SI(); Assert.AreEqual((2.0 / 5.0).SI(), 2 / x); @@ -638,19 +638,12 @@ namespace TUGraz.VectoCore.Tests.Utils var sig1 = 5.SI(Unit.SI.Gramm); Assert.AreEqual(5, sig1.Value()); + Assert.AreEqual("5.0000 [g]", sig1.ToString()); UnitInstance sikg = Unit.SI.Kilo.Gramm; Assert.AreEqual("kg", 1.SI().GetUnitString(sikg.GetSIUnits())); - UnitInstance sig = Unit.SI.Gramm; - //Assert.AreEqual("g", 1.SI().GetUnitString(sig.GetSIUnits())); - - //Assert.AreEqual(5000, kg.Value()); //not tested - // Assert.AreEqual("5000.0000 [g]", kg.ToString()); //not tested - - - UnitInstance ui1 = Unit.SI.Kilo.Gramm.Meter.Per.Square.Second; Assert.AreEqual("kgm/s^2", 1.SI().GetUnitString(ui1.GetSIUnits())); @@ -684,13 +677,14 @@ namespace TUGraz.VectoCore.Tests.Utils var val2 = 7.SI(Unit.SI.Cubic.Dezi.Meter).ConvertTo(Unit.SI.Cubic.Dezi.Meter); - Assert.AreEqual("0.0070 [m^3]", val2.ToOutputFormat(showUnit: true)); + Assert.AreEqual(7,val2.Value()); // 7 dm^3 + var val3 = 5.SI(Unit.SI.Cubic.Dezi.Meter).ConvertTo(Unit.SI.Cubic.Centi.Meter); - Assert.AreEqual("0.0050 [m^3]", val3.ToOutputFormat(showUnit: true)); + Assert.AreEqual(5000, val3.Value()); // 5000 cm^3 var val4 = 5.SI(Unit.SI.Cubic.Centi.Meter).ConvertTo(Unit.SI.Cubic.Dezi.Meter); - Assert.AreEqual("0.000005 [m^3]", val4.ToOutputFormat(6, showUnit: true)); + AssertHelper.AreRelativeEqual(0.005, val4.Value()); // 0.005 dm^3 var uni1 = Unit.SI.Kilo.Meter.Per.Hour; -- GitLab