From 2698f7c0522867eb57a8738305dd4a4c6f44d94f Mon Sep 17 00:00:00 2001 From: Johan Larsson Date: Thu, 6 Nov 2014 18:52:38 +0100 Subject: [PATCH] Finding inverses now --- Gu.Units.Generator.Tests/MockSettings.cs | 10 ++++++++++ Gu.Units.Generator.Tests/QuantityTests.cs | 3 ++- Gu.Units.Generator/Descriptors/OperatorOverload.cs | 7 +++++++ Gu.Units.Generator/Descriptors/Quantity.cs | 6 +++++- Gu.Units.Generator/Descriptors/UnitAndPower.cs | 2 +- Gu.Units.Generator/Templates/Quantity.tt | 12 ++++++++++++ Gu.Units/Area.generated.cs | 5 +++++ Gu.Units/Flexibility.generated.cs | 5 +++++ Gu.Units/FractionUnit.generated.cs | 7 ++++++- Gu.Units/Frequency.generated.cs | 5 +++++ Gu.Units/Length.generated.cs | 5 +++++ Gu.Units/Stiffness.generated.cs | 5 +++++ Gu.Units/Time.generated.cs | 5 +++++ 13 files changed, 73 insertions(+), 4 deletions(-) diff --git a/Gu.Units.Generator.Tests/MockSettings.cs b/Gu.Units.Generator.Tests/MockSettings.cs index 29740610..f3cd7283 100644 --- a/Gu.Units.Generator.Tests/MockSettings.cs +++ b/Gu.Units.Generator.Tests/MockSettings.cs @@ -38,6 +38,9 @@ public class MockSettings : Settings public readonly DerivedUnit Coloumbs; public readonly Quantity ElectricCharge; + public readonly DerivedUnit Hertz; + public readonly Quantity Frequency; + public MockSettings() { Metres = new SiUnit("Metres", "m") { QuantityName = "Length" }; @@ -118,6 +121,13 @@ public MockSettings() }; DerivedUnits.Add(CubicMetres); Volume = CubicMetres.Quantity; + + Hertz = new DerivedUnit("Hertz", "1/s", new UnitAndPower(Seconds, -1)) + { + QuantityName = "Frequency" + }; + DerivedUnits.Add(Hertz); + Frequency = Hertz.Quantity; } } } diff --git a/Gu.Units.Generator.Tests/QuantityTests.cs b/Gu.Units.Generator.Tests/QuantityTests.cs index ca58c788..f7d058b3 100644 --- a/Gu.Units.Generator.Tests/QuantityTests.cs +++ b/Gu.Units.Generator.Tests/QuantityTests.cs @@ -85,7 +85,8 @@ public void TimeOverloads() public void Inversions() { var actual = _settings.Time.Inverse; - Assert.AreEqual(_settings.Quantities); + Assert.AreEqual(_settings.Frequency.ToString(), actual.ToString()); + Assert.IsNull(_settings.Length.Inverse); } } } diff --git a/Gu.Units.Generator/Descriptors/OperatorOverload.cs b/Gu.Units.Generator/Descriptors/OperatorOverload.cs index a631218a..e7b7d014 100644 --- a/Gu.Units.Generator/Descriptors/OperatorOverload.cs +++ b/Gu.Units.Generator/Descriptors/OperatorOverload.cs @@ -133,5 +133,12 @@ private int FindPower(Quantity left, Quantity right, Quantity result) // throw new ArgumentException("message"); //} } + + public static bool IsInverse(Quantity left, Quantity right) + { + var leftParts = UnitParts.CreateFrom(left); + var rightParts = UnitParts.CreateFrom(right); + return leftParts.Flattened.SequenceEqual(rightParts.Flattened.Select(x => x ^ -1)); + } } } \ No newline at end of file diff --git a/Gu.Units.Generator/Descriptors/Quantity.cs b/Gu.Units.Generator/Descriptors/Quantity.cs index ed735c74..41c60c8b 100644 --- a/Gu.Units.Generator/Descriptors/Quantity.cs +++ b/Gu.Units.Generator/Descriptors/Quantity.cs @@ -141,7 +141,11 @@ public Quantity Inverse { get { - throw new NotImplementedException("message"); + if (Settings == null || Settings.Quantities == null) + { + return null; + } + return Settings.Quantities.FirstOrDefault(x => x != null && OperatorOverload.IsInverse(this, x)); } } diff --git a/Gu.Units.Generator/Descriptors/UnitAndPower.cs b/Gu.Units.Generator/Descriptors/UnitAndPower.cs index b1bafadd..69675f96 100644 --- a/Gu.Units.Generator/Descriptors/UnitAndPower.cs +++ b/Gu.Units.Generator/Descriptors/UnitAndPower.cs @@ -101,7 +101,7 @@ public IUnit Parent public static UnitAndPower operator ^(UnitAndPower up, int i) { - return new UnitAndPower(up.Unit, up.Power + i); + return new UnitAndPower(up.Unit, up.Power * i); } public override string ToString() diff --git a/Gu.Units.Generator/Templates/Quantity.tt b/Gu.Units.Generator/Templates/Quantity.tt index f6391ea4..88b8fe45 100644 --- a/Gu.Units.Generator/Templates/Quantity.tt +++ b/Gu.Units.Generator/Templates/Quantity.tt @@ -149,6 +149,18 @@ namespace <#= Settings.Namespace #> { return <#= op.Result.ClassName #>.From<#= op.Result.Unit.ClassName #>(left.<#= op.Left.Unit.ClassName #> <#= op.Operator #> right.<#= op.Right.Unit.ClassName #>); } +<# + } + #> +<# + if(quantity.Inverse != null) + { + #> + + public static <#= quantity.Inverse.ClassName #> operator/(double left, <#= quantity.ClassName #> right) + { + return <#= quantity.Inverse.ClassName #>.From<#= quantity.Inverse.Unit.ClassName #>(left / right.<#= quantity.Unit.ClassName #>); + } <# } #> diff --git a/Gu.Units/Area.generated.cs b/Gu.Units/Area.generated.cs index 53d513bf..98cc3f91 100644 --- a/Gu.Units/Area.generated.cs +++ b/Gu.Units/Area.generated.cs @@ -252,6 +252,11 @@ public static Area FromSquareYard(double squareYard) return VolumetricFlow.FromCubicMetresPerSecond(left.SquareMetres * right.MetresPerSecond); } + public static Flexibility operator /(Area left, Energy right) + { + return Flexibility.FromMetresPerNewton(left.SquareMetres / right.Joules); + } + public static double operator /(Area left, Area right) { return left.SquareMetres / right.SquareMetres; diff --git a/Gu.Units/Flexibility.generated.cs b/Gu.Units/Flexibility.generated.cs index a4b957b2..173108ae 100644 --- a/Gu.Units/Flexibility.generated.cs +++ b/Gu.Units/Flexibility.generated.cs @@ -114,6 +114,11 @@ public static Flexibility FromMillimetresPerNewton(double millimetresPerNewton) return Area.FromSquareMetres(left.MetresPerNewton * right.Joules); } + public static Stiffness operator /(double left, Flexibility right) + { + return Stiffness.FromNewtonsPerMetre(left / right.MetresPerNewton); + } + public static double operator /(Flexibility left, Flexibility right) { return left.MetresPerNewton / right.MetresPerNewton; diff --git a/Gu.Units/FractionUnit.generated.cs b/Gu.Units/FractionUnit.generated.cs index b34b69bb..806e1ded 100644 --- a/Gu.Units/FractionUnit.generated.cs +++ b/Gu.Units/FractionUnit.generated.cs @@ -12,7 +12,12 @@ public struct FractionUnit : IUnit /// The unit /// Contains coonversion logic to from and formatting. /// - public static readonly FractionUnit Fractions = new FractionUnit(1.0, "/x"); + public static readonly FractionUnit Fractions = new FractionUnit(1.0, "ul"); + /// + /// The unit + /// Contains conversion logic to from and formatting. + /// + public static readonly FractionUnit ul = Fractions; /// /// The unit diff --git a/Gu.Units/Frequency.generated.cs b/Gu.Units/Frequency.generated.cs index bd5f7761..c988ba05 100644 --- a/Gu.Units/Frequency.generated.cs +++ b/Gu.Units/Frequency.generated.cs @@ -191,6 +191,11 @@ public static Frequency FromGigahertz(double gigahertz) return VolumetricFlow.FromCubicMetresPerSecond(left.Hertz * right.CubicMetres); } + public static Time operator /(double left, Frequency right) + { + return Time.FromSeconds(left / right.Hertz); + } + public static double operator /(Frequency left, Frequency right) { return left.Hertz / right.Hertz; diff --git a/Gu.Units/Length.generated.cs b/Gu.Units/Length.generated.cs index 695666c6..f1d1968c 100644 --- a/Gu.Units/Length.generated.cs +++ b/Gu.Units/Length.generated.cs @@ -305,6 +305,11 @@ public static Length FromNauticalMile(double nauticalMile) return SpecificEnergy.FromJoulesPerKilogram(left.Metres * right.MetresPerSecondSquared); } + public static Flexibility operator /(Length left, Force right) + { + return Flexibility.FromMetresPerNewton(left.Metres / right.Newtons); + } + public static double operator /(Length left, Length right) { return left.Metres / right.Metres; diff --git a/Gu.Units/Stiffness.generated.cs b/Gu.Units/Stiffness.generated.cs index 78563855..784caac4 100644 --- a/Gu.Units/Stiffness.generated.cs +++ b/Gu.Units/Stiffness.generated.cs @@ -100,6 +100,11 @@ public static Stiffness FromNewtonsPerMetre(double newtonsPerMetre) return Energy.FromJoules(left.NewtonsPerMetre * right.SquareMetres); } + public static Flexibility operator /(double left, Stiffness right) + { + return Flexibility.FromMetresPerNewton(left / right.NewtonsPerMetre); + } + public static double operator /(Stiffness left, Stiffness right) { return left.NewtonsPerMetre / right.NewtonsPerMetre; diff --git a/Gu.Units/Time.generated.cs b/Gu.Units/Time.generated.cs index be1b8cb2..0138a9fb 100644 --- a/Gu.Units/Time.generated.cs +++ b/Gu.Units/Time.generated.cs @@ -200,6 +200,11 @@ public static Time FromMinutes(double minutes) return ElectricCharge.FromCoulombs(left.Seconds * right.Amperes); } + public static Frequency operator /(double left, Time right) + { + return Frequency.FromHertz(left / right.Seconds); + } + public static double operator /(Time left, Time right) { return left.Seconds / right.Seconds;