public void TestSystemFilterAsLeaf() { // Setup var sys = new MeasurementSystem { Key = "test", Parent = MeasurementCorpus.FindSystem("siCommon") }; MeasurementCorpus.AllSystems.Add(sys); // Test Assert.IsTrue(MeasurementCorpus.SystemFilter(sys)); MeasurementCorpus.Options.IgnoredSystemsForUnits.Add(sys); Assert.IsFalse(MeasurementCorpus.SystemFilter(sys)); MeasurementCorpus.Options.IgnoredSystemsForUnits.Clear(); Assert.IsTrue(MeasurementCorpus.SystemFilter(sys)); MeasurementCorpus.Options.IgnoredSystemsForUnits.Add(sys.Parent); Assert.IsTrue(MeasurementCorpus.SystemFilter(sys)); Assert.IsFalse(MeasurementCorpus.SystemFilter(sys.Parent)); Assert.IsFalse(MeasurementCorpus.SystemFilter(sys.Parent.Parent)); MeasurementCorpus.Options.AllowedSystemsForUnits.Add(sys.Parent.Parent); Assert.IsTrue(MeasurementCorpus.SystemFilter(sys.Parent.Parent)); Assert.IsTrue(MeasurementCorpus.SystemFilter(sys.Parent.Parent.Parent)); // Cleanup MeasurementCorpus.AllSystems.Remove(sys); }
public void TestUnitFindMultiple() { List <Unit> results = MeasurementCorpus.FindUnitsPartial("sq"); Assert.AreEqual(28, results.Count); Assert.AreEqual("squareMetre", results[0].Key); }
public void TestSerialization() { TestIfOnMono(); string json = MeasurementCorpus.CreateQuantity(2, "hour").ToJson(); Assert.AreEqual("{\"dimensions\":[{\"dimensionName\":\"time\",\"unitName\":\"hour\"}],\"value\":2}", json); }
public void TestSystemFind() { Assert.IsNull(MeasurementCorpus.FindSystem("xyz")); Assert.IsNull(MeasurementCorpus.FindSystem("siC")); Assert.AreEqual("si", MeasurementCorpus.FindSystem("si").Key); Assert.AreEqual("australia", MeasurementCorpus.FindSystem("Common Australian Metric (SI)").Key); }
public void TestUnitComparer() { var unit1 = new Unit { Key = "test1", DimensionDefinition = MeasurementCorpus.FindDimension("time") }; unit1.MeasurementSystems.Add(MeasurementCorpus.FindSystem("si")); var unit2 = new Unit { Key = "test1", DimensionDefinition = MeasurementCorpus.FindDimension("time") }; unit2.MeasurementSystems.Add(MeasurementCorpus.FindSystem("si")); Assert.AreEqual(0, UnitComparer.Comparer.Compare(null, null)); Assert.AreEqual(1, UnitComparer.Comparer.Compare(unit1, null)); Assert.AreEqual(-1, UnitComparer.Comparer.Compare(null, unit2)); Assert.AreEqual(0, UnitComparer.Comparer.Compare(unit1, unit2)); unit2.Key = "test0"; Assert.AreEqual(-1, UnitComparer.Comparer.Compare(unit1, unit2)); unit2.Key = "test1"; unit1.IsRare = true; Assert.AreEqual(-1, UnitComparer.Comparer.Compare(unit1, unit2)); unit1.IsRare = false; unit2.IsEstimation = true; Assert.AreEqual(1, UnitComparer.Comparer.Compare(unit1, unit2)); unit2.IsEstimation = false; }
public void TestSystemComparer() { var sys1 = new MeasurementSystem { Key = "test1" }; var sys2 = new MeasurementSystem { Key = "test1" }; Assert.AreEqual(0, MeasurementSystemComparer.Comparer.Compare(null, null)); Assert.AreEqual(1, MeasurementSystemComparer.Comparer.Compare(sys1, null)); Assert.AreEqual(-1, MeasurementSystemComparer.Comparer.Compare(null, sys2)); Assert.AreEqual(0, MeasurementSystemComparer.Comparer.Compare(sys1, sys2)); sys1.Key = "test0"; Assert.AreEqual(1, MeasurementSystemComparer.Comparer.Compare(sys1, sys2)); sys1.Key = "test1"; sys1.Parent = MeasurementCorpus.FindSystem("siCommon"); Assert.AreEqual(-1, MeasurementSystemComparer.Comparer.Compare(sys1, sys2)); sys2.Parent = MeasurementCorpus.FindSystem("siCommon"); Assert.AreEqual(0, MeasurementSystemComparer.Comparer.Compare(sys1, sys2)); sys1.Parent = MeasurementCorpus.FindSystem("si"); Assert.AreEqual(1, MeasurementSystemComparer.Comparer.Compare(sys1, sys2)); }
public void TestDimensionFindMultiple() { List <DimensionDefinition> results = MeasurementCorpus.FindDimensionsPartial("ELECTRIC"); Assert.AreEqual(4, results.Count); Assert.AreEqual("electricCurrent", results[0].Key); Assert.AreEqual("electricResistance", results[3].Key); }
public void TestFormattingDimensionsOnly() { string result = MeasurementCorpus.CreateQuantity(2, "hour").Format(new QuantityFormatInfo { FormatParts = QuantityFormatInfo.QuantityParts.Dimensions }); Assert.AreEqual("h", result); }
public void TestFormattingMulitpleDimensions() { string result = MeasurementCorpus.CreateQuantity(2, new List <string> { "hour", "metre" }).Format(new QuantityFormatInfo()); Assert.AreEqual("2 h·m", result); }
public void TestSystemFindPartial() { Assert.IsNull(MeasurementCorpus.FindSystemPartial("xyz")); Assert.AreEqual("si", MeasurementCorpus.FindSystemPartial("si").Key); Assert.AreEqual("siCommon", MeasurementCorpus.FindSystemPartial("siC").Key); Assert.AreEqual("natural", MeasurementCorpus.FindSystemPartial("Natural Units").Key); Assert.AreEqual("qcd", MeasurementCorpus.FindSystemPartial("Chromo").Key); }
public void TestPrefixFindMultiple() { List <Prefix> results = MeasurementCorpus.FindPrefixesPartial("to"); Assert.AreEqual(4, results.Count); Assert.AreEqual("atto", results[0].Key); Assert.AreEqual("zepto", results[3].Key); }
public void TestFormattingMulitpleDimensionsWithDifferentPowers() { string result = MeasurementCorpus.CreateQuantity(2, new List <Dimension> { new Dimension("hour", 2), new Dimension("metre", -3) }).Format(new QuantityFormatInfo()); Assert.AreEqual("2 h²·m⁻³", result); }
public void TestPrefixFind() { MeasurementCorpus.Options.UseRarePrefixes = true; Assert.IsNull(MeasurementCorpus.FindPrefix("xyz")); Assert.IsNull(MeasurementCorpus.FindPrefix("cent")); Assert.AreEqual("kilo", MeasurementCorpus.FindPrefix("kilo").Key); Assert.AreEqual("mega", MeasurementCorpus.FindPrefix("M").Key); }
public void TestSystemFindMultiple() { List <MeasurementSystem> results = MeasurementCorpus.FindSystemsPartial("Metric"); Assert.AreEqual(6, results.Count); Assert.AreEqual("metric", results[0].Key); Assert.AreEqual("canada", results[5].Key); }
public void TestDimensionFindPartial() { Assert.IsNull(MeasurementCorpus.FindDimensionPartial("foo123")); Assert.AreEqual("time", MeasurementCorpus.FindDimensionPartial("IME").Key); Assert.AreEqual("time", MeasurementCorpus.FindDimensionPartial("dur").Key); Assert.AreEqual("electricCurrent", MeasurementCorpus.FindDimensionPartial("current").Key); Assert.AreEqual("electricPotential", MeasurementCorpus.FindDimensionPartial("electricPot").Key); Assert.AreEqual("planeAngle", MeasurementCorpus.FindDimensionPartial("β").Key); }
public void TestPrefixFindPartial() { MeasurementCorpus.Options.UseRarePrefixes = true; Assert.IsNull(MeasurementCorpus.FindPrefixPartial("xyz")); Assert.AreEqual("centi", MeasurementCorpus.FindPrefixPartial("cent").Key); Assert.AreEqual("kilo", MeasurementCorpus.FindPrefixPartial("kilo").Key); Assert.AreEqual("kibi", MeasurementCorpus.FindPrefixPartial("kib").Key); Assert.AreEqual("micro", MeasurementCorpus.FindPrefixPartial("Mi").Key); Assert.AreEqual("mebi", MeasurementCorpus.FindPrefixPartial("Mi", false).Key); }
public void TestDimensionFind() { Assert.IsNull(MeasurementCorpus.FindDimension("foo123")); Assert.IsNull(MeasurementCorpus.FindDimension("temp")); Assert.AreEqual("time", MeasurementCorpus.FindDimension("TIME").Key); Assert.AreEqual("electricCurrent", MeasurementCorpus.FindDimension("electric current").Key); Assert.AreEqual("electricResistance", MeasurementCorpus.FindDimension("R").Key); Assert.AreEqual("area", MeasurementCorpus.FindDimension("a").Key); Assert.AreEqual("length", MeasurementCorpus.FindDimension("radius").Key); Assert.AreEqual("planeAngle", MeasurementCorpus.FindDimension("β").Key); }
public void TestUnitFind() { Assert.IsNull(MeasurementCorpus.FindUnit("xyz")); Assert.IsNull(MeasurementCorpus.FindUnit("seco")); Assert.AreEqual("second", MeasurementCorpus.FindUnit("seconds").Key); Assert.AreEqual("time", MeasurementCorpus.FindUnit("seconds").DimensionDefinition.Key); Assert.AreEqual("second", MeasurementCorpus.FindUnit("s").Key); Assert.AreEqual("metre", MeasurementCorpus.FindUnit("meter").Key); Assert.AreEqual("acreFoot", MeasurementCorpus.FindUnit("acre feet").Key); Assert.AreEqual("squareYard", MeasurementCorpus.FindUnit("sq yd").Key); }
public void TestFormattingNewDefaults() { var info = (NumberFormatInfo)CultureInfo.CurrentCulture.NumberFormat.Clone(); info.NumberDecimalSeparator = "*"; info.NumberGroupSeparator = ".."; info.NumberGroupSizes = new[] { 2 }; string result = MeasurementCorpus.CreateQuantity(231223423.23, "hour").Format(new QuantityFormatInfo(info) { DefaultDoubleFormat = "N" }); Assert.AreEqual("2..31..22..34..23*23 h", result); }
public void TestMultiplyComplexCommensurableQuantities() { MeasurementCorpus.Options.AllowDerivedDimensions = false; Quantity result = new Quantity(3.2, new[] { "minute", "metre", "coulomb" }) .Multiply(new Quantity(3, new[] { "second", "mile", "coulomb" })); MeasurementCorpus.ResetToDefaultOptions(); Assert.AreEqual(257.49504, result.Value, 0.0001); Assert.AreEqual(3, result.Dimensions.Count); foreach (Dimension dim in result.Dimensions) { Assert.AreEqual(2, dim.Power); } }
public void TestPrefixFilter() { var prefix = new Prefix { Key = "test", Type = PrefixType.Si }; Assert.IsTrue(MeasurementCorpus.PrefixFilter(prefix)); prefix.Type = PrefixType.SiUnofficial; Assert.IsFalse(MeasurementCorpus.PrefixFilter(prefix)); MeasurementCorpus.Options.UseUnofficalPrefixes = true; Assert.IsTrue(MeasurementCorpus.PrefixFilter(prefix)); prefix.IsRare = true; Assert.IsFalse(MeasurementCorpus.PrefixFilter(prefix)); MeasurementCorpus.Options.UseRarePrefixes = true; Assert.IsTrue(MeasurementCorpus.PrefixFilter(prefix)); }
public void TestUnitFilter() { var unit = new Unit { Key = "test" }; unit.MeasurementSystems.Add(MeasurementCorpus.FindSystem("si")); Assert.IsTrue(MeasurementCorpus.UnitFilter(unit)); unit.IsRare = true; Assert.IsFalse(MeasurementCorpus.UnitFilter(unit)); MeasurementCorpus.Options.UseRareUnits = true; Assert.IsTrue(MeasurementCorpus.UnitFilter(unit)); unit.IsEstimation = true; Assert.IsFalse(MeasurementCorpus.UnitFilter(unit)); MeasurementCorpus.Options.UseEstimatedUnits = true; Assert.IsTrue(MeasurementCorpus.UnitFilter(unit)); MeasurementCorpus.Options.IgnoredSystemsForUnits.Add(unit.MeasurementSystems[0]); Assert.IsFalse(MeasurementCorpus.UnitFilter(unit)); }
public void UpdateDerived() { Derived.Clear(); if (string.IsNullOrEmpty(DerivedString)) { return; } foreach (Match match in Regex.Matches(DerivedString, @"(^\w+|([\*|/])(\w+))")) { if (match.Success) { string type; string systemName; if (!string.IsNullOrEmpty(match.Groups[3].Value)) { type = match.Groups[2].Value; systemName = match.Groups[3].Value; } else { type = "*"; systemName = match.Groups[1].Value; } if (systemName != "1") { Unit baseUnit = MeasurementCorpus.FindBaseUnit(systemName); if (baseUnit == null) { throw new Exception("All derived entries must be the name of a dimension or the '1' placeholder"); } if (type != "*" && type != "/") { throw new Exception("Derived divider is not valid - must be either '*' or '/'"); } int power = (type == "*") ? 1 : -1; Derived.Add(new Dimension(baseUnit, power)); } } } var computedValue = new DoubleWrapper(1); Derived = Derived.SimpleSimplify(ref computedValue); }
public void TestDimensionFilter() { var def = new DimensionDefinition { Key = "test" }; Assert.IsTrue(MeasurementCorpus.DimensionFilter(def)); def.Vector = true; Assert.IsFalse(MeasurementCorpus.DimensionFilter(def)); MeasurementCorpus.Options.AllowVectorDimensions = true; Assert.IsTrue(MeasurementCorpus.DimensionFilter(def)); def.DerivedString = "temperature/time"; def.UpdateDerived(); Assert.IsTrue(MeasurementCorpus.DimensionFilter(def)); MeasurementCorpus.Options.AllowDerivedDimensions = false; Assert.IsFalse(MeasurementCorpus.DimensionFilter(def)); MeasurementCorpus.Options.AllowDerivedDimensions = true; Assert.IsTrue(MeasurementCorpus.DimensionFilter(def)); MeasurementCorpus.Options.IgnoredDimensions.Add(def); Assert.IsFalse(MeasurementCorpus.DimensionFilter(def)); }
public void TestSystemFilterAsRoot() { // Setup var sys = new MeasurementSystem { Key = "test" }; MeasurementCorpus.AllSystems.Add(sys); MeasurementCorpus.RootSystems.Add(sys); // Test Assert.IsTrue(MeasurementCorpus.SystemFilter(sys)); MeasurementCorpus.Options.IgnoredSystemsForUnits.Add(sys); Assert.IsFalse(MeasurementCorpus.SystemFilter(sys)); MeasurementCorpus.Options.AllowedSystemsForUnits.Add(sys); Assert.IsFalse(MeasurementCorpus.SystemFilter(sys)); // Ignored Overrides Allowed MeasurementCorpus.Options.IgnoredSystemsForUnits.Clear(); Assert.IsTrue(MeasurementCorpus.SystemFilter(sys)); // Cleanup MeasurementCorpus.AllSystems.Remove(sys); MeasurementCorpus.RootSystems.Remove(sys); }
public void TestDimensionFindPartialNoInput() { MeasurementCorpus.FindDimensionPartial(""); }
public void TestDimensionFindNoInput() { MeasurementCorpus.FindDimension(""); }
public void TestSystemFindPartialNoInput() { MeasurementCorpus.FindSystemPartial(""); }
public void TestPrefixFindPartialNoInput() { MeasurementCorpus.FindPrefixPartial(""); }
public void TestPrefixFindNoInput() { MeasurementCorpus.FindPrefix(""); }