Skip to content

Commit

Permalink
Finding inverses now
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanLarsson committed Nov 6, 2014
1 parent 4c8bc05 commit 2698f7c
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 4 deletions.
10 changes: 10 additions & 0 deletions Gu.Units.Generator.Tests/MockSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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" };
Expand Down Expand Up @@ -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;
}
}
}
3 changes: 2 additions & 1 deletion Gu.Units.Generator.Tests/QuantityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
7 changes: 7 additions & 0 deletions Gu.Units.Generator/Descriptors/OperatorOverload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
6 changes: 5 additions & 1 deletion Gu.Units.Generator/Descriptors/Quantity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
2 changes: 1 addition & 1 deletion Gu.Units.Generator/Descriptors/UnitAndPower.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 12 additions & 0 deletions Gu.Units.Generator/Templates/Quantity.tt
Original file line number Diff line number Diff line change
Expand Up @@ -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 #>);
}
<#
}
#>
Expand Down
5 changes: 5 additions & 0 deletions Gu.Units/Area.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions Gu.Units/Flexibility.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion Gu.Units/FractionUnit.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ public struct FractionUnit : IUnit
/// The <see cref="T:Gu.Units.Fractions"/> unit
/// Contains coonversion logic to from and formatting.
/// </summary>
public static readonly FractionUnit Fractions = new FractionUnit(1.0, "/x");
public static readonly FractionUnit Fractions = new FractionUnit(1.0, "ul");
/// <summary>
/// The <see cref="T:Gu.Units.Fractions"/> unit
/// Contains conversion logic to from and formatting.
/// </summary>
public static readonly FractionUnit ul = Fractions;

/// <summary>
/// The <see cref="T:Gu.Units.PartsPerMillion"/> unit
Expand Down
5 changes: 5 additions & 0 deletions Gu.Units/Frequency.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions Gu.Units/Length.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions Gu.Units/Stiffness.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions Gu.Units/Time.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 2698f7c

Please sign in to comment.