public void AddTest() { var a = quantity1.Add(quantity3); var b = quantity1.Add(quantity2); Assert.AreEqual(400.200, a.Amount); Assert.AreEqual("kilomeeter", a.Unit.Name); }
private ComplexMathViewModel SetResults(ComplexMathViewModel quantity, Quantity q1, Quantity q2) { if (quantity.SelectedOperation == "Add") { quantity.ResultQuantity = q1.Add(q2); } else if (quantity.SelectedOperation == "Subtract") { quantity.ResultQuantity = q1.Subtract(q2); } else if (quantity.SelectedOperation == "Multiply") { quantity.ResultQuantity = q1.Multiply(q2); } else if (quantity.SelectedOperation == "Divide") { quantity.ResultQuantity = q1.Divide(q2); } else if (quantity.SelectedOperation == "Compare") { quantity.Result = SetComparingResult(q1, q2); } else if (quantity.SelectedOperation == "Round") { quantity.ResultQuantity = q1.Round(new Rounding()); } if (quantity.Result == null) { quantity.Result = quantity.ResultQuantity.Amount.ToString(); } return(quantity); }
public void TestExceptions() { UnitOfMeasure floz = sys.GetUOM(Unit.BR_FLUID_OUNCE); Quantity q1 = new Quantity(10, sys.GetDay()); Quantity q2 = new Quantity(10, sys.GetUOM(Unit.BR_FLUID_OUNCE)); try { String amount = null; Quantity.CreateAmount(amount); Assert.Fail("create"); } catch (Exception) { } try { q1.Convert(floz); Assert.Fail("Convert"); } catch (Exception) { } try { q1.Add(q2); Assert.Fail("Add"); } catch (Exception) { } try { q1.Subtract(q2); Assert.Fail("Subtract"); } catch (Exception) { } // OK q1.Multiply(q2); // OK q1.Divide(q2); }
public void Blah() { //Test: Quantity construction var tenUnitLengths = new Quantity <Length>(10); var fiveUnitLengths = new Quantity <Length>(5); var twoUnitsOfTime = new Quantity <Time>(2); //Test: Addition var fifteenLengthUnits = tenUnitLengths.Add(fiveUnitLengths); var fourUnitsOfTime = twoUnitsOfTime.Add(twoUnitsOfTime); //var cantAddLengthToTime = tenUnitLengths.Add(twoUnitsOfTime); //Test: Subtraction var threeUnitsOfTime = fourUnitsOfTime.Subtract(new Quantity <Time>(1)); //Test: Multiplication var hundredLengthUnitsSquared = tenUnitLengths.MultipliedBy(tenUnitLengths); var thirtyLengthTimeUnits = fifteenLengthUnits.MultipliedBy(twoUnitsOfTime); //var cantAddProductToSingleDimensionQuantity = thirtyMetreSeconds.Add(fourUnitsOfTime); //Test: Division var fiveLengthPerTimeUnits = tenUnitLengths.DivideBy(twoUnitsOfTime); var twentyLengthUnitsDodgy = fiveLengthPerTimeUnits.MultipliedBy(new Quantity <Time>(4)); //Now, it would be nice if these were to compile, removing the nested Product / Quotient constructors: /* * Quantity<Length> twentyLengthUnitsNice = hundredLengthUnitsSquared.DivideBy(fiveUnitLengths); * * Quantity<Length> twentyLengthUnitsAlsoNice = fiveLengthPerTimeUnits.MultipliedBy(new Quantity<Time>(4)); */ }
private void RunSingleTest() { // for each unit type, execute the quantity operations foreach (KeyValuePair <UnitType, List <UnitOfMeasure> > entry in unitListMap) { // run the matrix foreach (UnitOfMeasure rowUOM in entry.Value) { // row quantity Quantity rowQty = new Quantity(10, rowUOM); foreach (UnitOfMeasure colUOM in entry.Value) { // column qty Quantity colQty = new Quantity(10, colUOM); // arithmetic operations rowQty.Add(colQty); rowQty.Subtract(colQty); // offsets are not supported if (rowUOM.Offset.CompareTo(0) == 0 && colUOM.Offset.CompareTo(0) == 0) { rowQty.Multiply(colQty); rowQty.Divide(colQty); rowQty.Invert(); } rowQty.Convert(colUOM); rowQty.Equals(colQty); double d = rowQty.Amount; UnitOfMeasure uom = rowQty.UOM; rowQty.ToString(); } } } }
public void IncreaseItemQtyBy(NonNegativeQuantity quantity) { Quantity = Quantity.Add(quantity); }
public void TestGenericQuantity() { UnitOfMeasure a = sys.CreateScalarUOM(UnitType.UNCLASSIFIED, "a", "aUOM", "A"); UnitOfMeasure b = sys.CreateScalarUOM(UnitType.UNCLASSIFIED, "b", "b", "B"); b.SetConversion(10, a); double four = 4; double bd = Quantity.CreateAmount("4"); Assert.IsTrue(bd.Equals(four)); bd = Quantity.CreateAmount(4); Assert.IsTrue(bd.Equals(four)); bd = Quantity.CreateAmount((decimal)4.0); Assert.IsTrue(bd.Equals(four)); bd = Quantity.CreateAmount((decimal)4.0f); Assert.IsTrue(bd.Equals(four)); bd = Quantity.CreateAmount(4L); Assert.IsTrue(bd.Equals(four)); bd = Quantity.CreateAmount((short)4); Assert.IsTrue(bd.Equals(four)); // Add Quantity q1 = new Quantity(four, a); Assert.IsFalse(q1.Equals(null)); Quantity q2 = new Quantity(four, b); Quantity q3 = q1.Add(q2); Assert.IsTrue(IsCloseTo(q3.UOM.ScalingFactor, 1, DELTA6)); Assert.IsTrue(q3.UOM.AbscissaUnit.Equals(a)); Assert.IsTrue(IsCloseTo(q3.UOM.Offset, 0, DELTA6)); Assert.IsTrue(IsCloseTo(q3.Amount, 44, DELTA6)); // Subtract q3 = q1.Subtract(q2); Assert.IsTrue(IsCloseTo(q3.UOM.ScalingFactor, 1, DELTA6)); Assert.IsTrue(q3.UOM.AbscissaUnit.Equals(a)); Assert.IsTrue(IsCloseTo(q3.UOM.Offset, 0, DELTA6)); Assert.IsTrue(IsCloseTo(q3.Amount, -36, DELTA6)); // Multiply q3 = q1.Multiply(q2); Assert.IsTrue(IsCloseTo(q3.Amount, 16, DELTA6)); Assert.IsTrue(IsCloseTo(q3.UOM.ScalingFactor, 1, DELTA6)); Assert.IsTrue(IsCloseTo(q3.UOM.Offset, 0, DELTA6)); UnitOfMeasure a2 = sys.CreatePowerUOM(UnitType.UNCLASSIFIED, "a*2", "a*2", "A squared", a, 2); Quantity q4 = q3.Convert(a2); Assert.IsTrue(IsCloseTo(q4.Amount, 160, DELTA6)); Assert.IsTrue(q4.UOM.Equals(a2)); q4 = q3.Divide(q2); Assert.IsTrue(q4.Equals(q1)); Assert.IsTrue(IsCloseTo(q4.Amount, 4, DELTA6)); // Divide q3 = q1.Divide(q2); Assert.IsTrue(IsCloseTo(q3.Amount, 1, DELTA6)); Assert.IsTrue(IsCloseTo(q3.UOM.ScalingFactor, 0.1, DELTA6)); q4 = q3.Multiply(q2); Assert.IsTrue(q4.Equals(q1)); }
public void TestSIQuantity() { double ten = 10; UnitOfMeasure litre = sys.GetUOM(Unit.LITRE); UnitOfMeasure m3 = sys.GetUOM(Unit.CUBIC_METRE); UnitOfMeasure m2 = sys.GetUOM(Unit.SQUARE_METRE); UnitOfMeasure m = sys.GetUOM(Unit.METRE); UnitOfMeasure cm = sys.GetUOM(Prefix.CENTI, m); UnitOfMeasure mps = sys.GetUOM(Unit.METRE_PER_SEC); UnitOfMeasure secPerM = sys.CreateQuotientUOM(UnitType.UNCLASSIFIED, null, "s/m", null, sys.GetSecond(), m); UnitOfMeasure oneOverM = sys.GetUOM(Unit.DIOPTER); UnitOfMeasure fperm = sys.GetUOM(Unit.FARAD_PER_METRE); UnitOfMeasure oneOverCm = sys.CreateScalarUOM(UnitType.UNCLASSIFIED, null, "1/cm", null); oneOverCm.SetConversion(100, oneOverM); Quantity q1 = new Quantity(ten, litre); Quantity q2 = q1.Convert(m3); Assert.IsTrue(IsCloseTo(q2.Amount, 0.01, DELTA6)); Assert.IsTrue(q2.UOM.Equals(m3)); q2 = q1.Convert(litre); Assert.IsTrue(IsCloseTo(q2.Amount, ten, DELTA6)); Assert.IsTrue(q2.UOM.Equals(litre)); // Add q1 = new Quantity(2, m); q2 = new Quantity(2, cm); Quantity q3 = q1.Add(q2); Assert.IsTrue(IsCloseTo(q3.UOM.ScalingFactor, 1, DELTA6)); Assert.IsTrue(q3.UOM.AbscissaUnit.Equals(m)); Assert.IsTrue(IsCloseTo(q3.UOM.Offset, 0, DELTA6)); Assert.IsTrue(IsCloseTo(q3.Amount, 2.02, DELTA6)); Quantity q4 = q3.Convert(cm); Assert.IsTrue(IsCloseTo(q4.Amount, 202, DELTA6)); Assert.IsTrue(q4.UOM.Equals(cm)); // Subtract q3 = q3.Subtract(q1); Assert.IsTrue(IsCloseTo(q3.UOM.ScalingFactor, 1, DELTA6)); Assert.IsTrue(q3.UOM.AbscissaUnit.Equals(m)); Assert.IsTrue(IsCloseTo(q3.Amount, 0.02, DELTA6)); q4 = q3.Convert(cm); Assert.IsTrue(IsCloseTo(q4.Amount, 2, DELTA6)); Assert.IsTrue(q4.UOM.Equals(cm)); // Multiply q3 = q1.Multiply(q2); Assert.IsTrue(IsCloseTo(q3.Amount, 4, DELTA6)); UnitOfMeasure u = q3.UOM; Assert.IsTrue(IsCloseTo(u.ScalingFactor, 0.01, DELTA6)); Assert.IsTrue(u.GetBaseSymbol().Equals(m2.GetBaseSymbol())); q4 = q3.Divide(q3); Assert.IsTrue(IsCloseTo(q4.Amount, 1, DELTA6)); Assert.IsTrue(q4.UOM.Equals(sys.GetOne())); q4 = q3.Divide(q1); Assert.IsTrue(q4.Equals(q2)); q4 = q3.Convert(m2); Assert.IsTrue(IsCloseTo(q4.Amount, 0.04, DELTA6)); Assert.IsTrue(q4.UOM.Equals(m2)); // Divide q3 = q3.Divide(q2); Assert.IsTrue(IsCloseTo(q3.Amount, 2.0, DELTA6)); Assert.IsTrue(q3.UOM.Equals(m)); Assert.IsTrue(q3.Equals(q1)); q3 = q3.Convert(m); Assert.IsTrue(IsCloseTo(q3.Amount, 2.0, DELTA6)); q1 = new Quantity(0, litre); try { q2 = q1.Divide(q1); Assert.Fail("Divide by zero)"); } catch (Exception) { } q1 = q3.Convert(cm).Divide(ten); Assert.IsTrue(IsCloseTo(q1.Amount, 20, DELTA6)); // Invert q1 = new Quantity(10, mps); q2 = q1.Invert(); Assert.IsTrue(IsCloseTo(q2.Amount, 0.1, DELTA6)); Assert.IsTrue(q2.UOM.Equals(secPerM)); q2 = q2.Invert(); Assert.IsTrue(IsCloseTo(q2.Amount, 10, DELTA6)); Assert.IsTrue(q2.UOM.Equals(mps)); q1 = new Quantity(10, cm); q2 = q1.Invert(); Assert.IsTrue(IsCloseTo(q2.Amount, 0.1, DELTA6)); u = q2.UOM; Assert.IsTrue(u.Equals(oneOverCm)); q2 = q2.Convert(m.Invert()); Assert.IsTrue(IsCloseTo(q2.Amount, 10, DELTA6)); Assert.IsTrue(q2.UOM.Equals(oneOverM)); Assert.IsTrue(q2.ToString() != null); // Newton-metres Divided by metres q1 = new Quantity(10, sys.GetUOM(Unit.NEWTON_METRE)); q2 = new Quantity(1, sys.GetUOM(Unit.METRE)); q3 = q1.Divide(q2); Assert.IsTrue(IsCloseTo(q3.Amount, 10, DELTA6)); Assert.IsTrue(q3.UOM.Equals(sys.GetUOM(Unit.NEWTON))); // length multiplied by force q1 = new Quantity(10, sys.GetUOM(Unit.NEWTON)); q2 = new Quantity(1, sys.GetUOM(Unit.METRE)); q3 = q1.Multiply(q2); Assert.IsTrue(IsCloseTo(q3.Amount, 10, DELTA6)); UnitOfMeasure nm1 = q3.UOM; UnitOfMeasure nm2 = sys.GetUOM(Unit.NEWTON_METRE); Assert.IsTrue(nm1.GetBaseSymbol().Equals(nm2.GetBaseSymbol())); q4 = q3.Convert(sys.GetUOM(Unit.JOULE)); Assert.IsTrue(q4.UOM.Equals(sys.GetUOM(Unit.JOULE))); // farads q1 = new Quantity(10, fperm); q2 = new Quantity(1, m); q3 = q1.Multiply(q2); Assert.IsTrue(IsCloseTo(q3.Amount, 10, DELTA6)); Assert.IsTrue(q3.UOM.Equals(sys.GetUOM(Unit.FARAD))); // amps q1 = new Quantity(10, sys.GetUOM(Unit.AMPERE_PER_METRE)); q2 = new Quantity(1, m); q3 = q1.Multiply(q2); Assert.IsTrue(IsCloseTo(q3.Amount, 10, DELTA6)); Assert.IsTrue(q3.UOM.Equals(sys.GetUOM(Unit.AMPERE))); // Boltzmann and Avogadro Quantity boltzmann = sys.GetQuantity(Constant.BOLTZMANN_CONSTANT); Quantity avogadro = sys.GetQuantity(Constant.AVAGADRO_CONSTANT); Quantity gas = sys.GetQuantity(Constant.GAS_CONSTANT); Quantity qR = boltzmann.Multiply(avogadro); Assert.IsTrue(IsCloseTo(qR.UOM.ScalingFactor, gas.UOM.ScalingFactor, DELTA6)); // Sieverts q1 = new Quantity(20, sys.GetUOM(Prefix.MILLI, Unit.SIEVERTS_PER_HOUR)); q2 = new Quantity(24, sys.GetHour()); q3 = q1.Multiply(q2); Assert.IsTrue(IsCloseTo(q3.Amount, 480, DELTA6)); }
public void TestLength() { UnitOfMeasure m = sys.GetUOM(Unit.METRE); UnitOfMeasure cm = sys.GetUOM(Prefix.CENTI, m); UnitOfMeasure m2 = sys.GetUOM(Unit.SQUARE_METRE); const char squared = (char)0xB2; String cmsym = "cm" + squared; UnitOfMeasure cm2 = sys.GetUOM(cmsym); if (cm2 == null) { cm2 = sys.CreatePowerUOM(UnitType.AREA, "square centimetres", cmsym, "centimetres squared", cm, 2); } UnitOfMeasure ft = sys.GetUOM(Unit.FOOT); UnitOfMeasure yd = sys.GetUOM(Unit.YARD); UnitOfMeasure ft2 = sys.GetUOM(Unit.SQUARE_FOOT); UnitOfMeasure in2 = sys.GetUOM(Unit.SQUARE_INCH); double oneHundred = 100; Quantity q1 = new Quantity(1, ft2); Quantity q2 = q1.Convert(in2); Assert.IsTrue(IsCloseTo(q2.Amount, 144, DELTA6)); Assert.IsTrue(IsCloseTo(q2.Convert(ft2).Amount, 1, DELTA6)); q1 = new Quantity(1, sys.GetUOM(Unit.SQUARE_METRE)); q2 = q1.Convert(ft2); Assert.IsTrue(IsCloseTo(q2.Amount, 10.76391041670972, DELTA6)); Assert.IsTrue(IsCloseTo(q2.Convert(m2).Amount, 1, DELTA6)); double bd = 3; q1 = new Quantity(bd, ft); q2 = q1.Convert(yd); Assert.IsTrue(IsCloseTo(q2.Amount, 1, DELTA6)); Assert.IsTrue(IsCloseTo(q2.Convert(ft).Amount, bd, DELTA6)); bd = 0.3048; q1 = new Quantity(1, ft); q2 = q1.Convert(m); Assert.IsTrue(IsCloseTo(q2.Amount, bd, DELTA6)); Assert.IsTrue(IsCloseTo(q2.Convert(ft).Amount, 1, DELTA6)); bd = oneHundred; q1 = new Quantity(bd, cm); q2 = q1.Convert(m); Assert.IsTrue(IsCloseTo(q2.Amount, 1, DELTA6)); Assert.IsTrue(IsCloseTo(q2.Convert(cm).Amount, bd, DELTA6)); // Add bd = 50; q1 = new Quantity(bd, cm); q2 = new Quantity(bd, cm); Quantity q3 = q1.Add(q2); Assert.IsTrue(IsCloseTo(q3.Amount, bd + bd, DELTA6)); Assert.IsTrue(IsCloseTo(q3.Convert(m).Amount, 1, DELTA6)); Quantity q4 = q2.Add(q1); Assert.IsTrue(IsCloseTo(q4.Amount, bd + bd, DELTA6)); Assert.IsTrue(IsCloseTo(q4.Convert(m).Amount, 1, DELTA6)); Assert.IsTrue(q3.Equals(q4)); // Subtract q3 = q1.Subtract(q2); Assert.IsTrue(IsCloseTo(q3.Amount, 0, DELTA6)); Assert.IsTrue(IsCloseTo(q3.Convert(m).Amount, 0, DELTA6)); q4 = q2.Subtract(q1); Assert.IsTrue(IsCloseTo(q4.Amount, 0, DELTA6)); Assert.IsTrue(IsCloseTo(q4.Convert(m).Amount, 0, DELTA6)); Assert.IsTrue(q3.Equals(q4)); // Multiply q3 = q1.Multiply(q2); Assert.IsTrue(IsCloseTo(q3.Amount, 2500, DELTA6)); q4 = q3.Convert(cm2); Assert.IsTrue(IsCloseTo(q4.Amount, 2500, DELTA6)); q4 = q3.Convert(m2); Assert.IsTrue(IsCloseTo(q4.Amount, 0.25, DELTA6)); // Divide q4 = q3.Divide(q1); Assert.IsTrue(q4.Equals(q2)); }
private void Apply(ProductReservationCancelled e) { Reserved = Reserved.Subtract(e.Quantity); Quantity = Quantity.Add(e.Quantity); }
private void Apply(ProductQuantityAdded e) { Quantity = Quantity.Add(e.Quantity); }
public void IncreaseItemQtyBy(Quantity quantity) { Quantity = Quantity.Add(quantity); }