Skip to content

Commit

Permalink
Added some tests & fixed bug in code generation for conversion formula.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanLarsson committed Nov 7, 2014
1 parent 2698f7c commit 092ae25
Show file tree
Hide file tree
Showing 19 changed files with 225 additions and 293 deletions.
2 changes: 1 addition & 1 deletion Gu.Units.Generator.Tests/QuantityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void TimeOverloads()
public void Inversions()
{
var actual = _settings.Time.Inverse;
Assert.AreEqual(_settings.Frequency.ToString(), actual.ToString());
Assert.AreEqual("1 / Time = Frequency", actual.ToString());
Assert.IsNull(_settings.Length.Inverse);
}
}
Expand Down
29 changes: 29 additions & 0 deletions Gu.Units.Generator/Descriptors/InverseOverload.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace Gu.Units.Generator
{
using System.Linq;

public class InverseOverload
{
public InverseOverload(Quantity original, Quantity inverted)
{
this.Original = original;
this.Inverted = inverted;
}

public Quantity Original { get; private set; }

public Quantity Inverted { get; private set; }

public override string ToString()
{
return string.Format("1 / {0} = {1}", this.Original.ClassName, this.Inverted.ClassName);
}

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));
}
}
}
7 changes: 0 additions & 7 deletions Gu.Units.Generator/Descriptors/OperatorOverload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,5 @@ 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));
}
}
}
11 changes: 8 additions & 3 deletions Gu.Units.Generator/Descriptors/Quantity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,20 @@ public IEnumerable<OperatorOverload> OperatorOverloads
}

[XmlIgnore]
public Quantity Inverse
public InverseOverload Inverse
{
get
{
if (Settings == null || Settings.Quantities == null)
if (Settings == null || Settings.Quantities == null) // For the template in designtime
{
return null;
}
return Settings.Quantities.FirstOrDefault(x => x != null && OperatorOverload.IsInverse(this, x));
var inverse = Settings.Quantities.FirstOrDefault(x => x != null && InverseOverload.IsInverse(this, x));
if (inverse == null)
{
return null;
}
return new InverseOverload(this, inverse);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Gu.Units.Generator/GeneratorSettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1158,9 +1158,9 @@
</Conversions>
</SiUnit>
<SiUnit>
<ClassName>Fractions</ClassName>
<ClassName>Scalar</ClassName>
<Symbol>ul</Symbol>
<QuantityName>Fraction</QuantityName>
<QuantityName>Unitless</QuantityName>
<Conversions>
<Conversion>
<ClassName>PartsPerMillion</ClassName>
Expand Down
6 changes: 1 addition & 5 deletions Gu.Units.Generator/Gu.Units.Generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
</ApplicationDefinition>
<Compile Include="ConversionsVm.cs" />
<Compile Include="Descriptors\ConversionFormula.cs" />
<Compile Include="Descriptors\InverseOverload.cs" />
<Compile Include="Descriptors\IUnit.cs" />
<Compile Include="Descriptors\Names.cs" />
<Compile Include="Descriptors\OperatorOverload.cs" />
Expand Down Expand Up @@ -192,11 +193,6 @@
<Resource Include="GeneratorSettings.xml">
<SubType>Designer</SubType>
</Resource>
<Resource Include="Templates\Unit.txt">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Unit.tt</DependentUpon>
</Resource>
<Content Include="Templates\QuantityGenerator.txt">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
Expand Down
17 changes: 10 additions & 7 deletions Gu.Units.Generator/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,16 @@
</GroupBox>

<GroupBox Grid.Row="1" Header="Overloads">
<ItemsControl ItemsSource="{Binding Conversions.BaseUnit.Quantity.OperatorOverloads}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type generator:OperatorOverload}">
<TextBlock Text="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<StackPanel>
<ItemsControl ItemsSource="{Binding Conversions.BaseUnit.Quantity.OperatorOverloads}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type generator:OperatorOverload}">
<TextBlock Text="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBlock Text="{Binding Conversions.BaseUnit.Quantity.Inverse}" />
</StackPanel>
</GroupBox>

<GroupBox Grid.Row="2" Header="Prefixes">
Expand Down
4 changes: 2 additions & 2 deletions Gu.Units.Generator/Templates/Quantity.tt
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ namespace <#= Settings.Namespace #>
{
#>

public static <#= quantity.Inverse.ClassName #> operator/(double left, <#= quantity.ClassName #> right)
public static <#= quantity.Inverse.Inverted.ClassName #> operator/(double left, <#= quantity.ClassName #> right)
{
return <#= quantity.Inverse.ClassName #>.From<#= quantity.Inverse.Unit.ClassName #>(left / right.<#= quantity.Unit.ClassName #>);
return <#= quantity.Inverse.Inverted.ClassName #>.From<#= quantity.Inverse.Inverted.Unit.ClassName #>(left / right.<#= quantity.Unit.ClassName #>);
}
<#
}
Expand Down
6 changes: 3 additions & 3 deletions Gu.Units.Generator/Templates/QuantityGenerator.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ Read Settings found 29 units

//-> Force.generated.cs

//-> Fraction.generated.cs

//-> Frequency.generated.cs

//-> Inductance.generated.cs
Expand Down Expand Up @@ -52,6 +50,8 @@ Read Settings found 29 units

//-> Torque.generated.cs

//-> Unitless.generated.cs

//-> Voltage.generated.cs

//-> Volume.generated.cs
Expand Down Expand Up @@ -83,7 +83,7 @@ Read Settings found 29 units
// Gu.Units\Gu.Units\Area.generated.cs
// Gu.Units\Gu.Units\LuminousIntensity.generated.cs
// Gu.Units\Gu.Units\Current.generated.cs
// Gu.Units\Gu.Units\Fraction.generated.cs
// Gu.Units\Gu.Units\Unitless.generated.cs
// Gu.Units\Gu.Units\Angle.generated.cs
// Gu.Units\Gu.Units\Temperature.generated.cs
// Gu.Units\Gu.Units\Time.generated.cs
Expand Down
4 changes: 2 additions & 2 deletions Gu.Units.Generator/Templates/Unit.tt
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ namespace <#= Settings.Namespace #>
if (unit.AnyOffsetConversion)
{
#>
return _conversionFactor * value + _offset;
return (value - _offset) / _conversionFactor;
<#
}
else
Expand All @@ -185,7 +185,7 @@ namespace <#= Settings.Namespace #>
if (unit.AnyOffsetConversion)
{
#>
return (value - _offset) / _conversionFactor;
return _conversionFactor * value + _offset;
<#
}
else
Expand Down
94 changes: 0 additions & 94 deletions Gu.Units.Generator/Templates/Unit.txt

This file was deleted.

6 changes: 3 additions & 3 deletions Gu.Units.Generator/Templates/UnitGenerator.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ Read Settings found 8 units

//-> ForceUnit.generated.cs

//-> FractionUnit.generated.cs

//-> FrequencyUnit.generated.cs

//-> InductanceUnit.generated.cs
Expand Down Expand Up @@ -52,6 +50,8 @@ Read Settings found 8 units

//-> TorqueUnit.generated.cs

//-> UnitlessUnit.generated.cs

//-> VoltageUnit.generated.cs

//-> VolumetricFlowUnit.generated.cs
Expand Down Expand Up @@ -83,7 +83,7 @@ Read Settings found 8 units
// Gu.Units\Gu.Units\AreaUnit.generated.cs
// Gu.Units\Gu.Units\LuminousIntensityUnit.generated.cs
// Gu.Units\Gu.Units\CurrentUnit.generated.cs
// Gu.Units\Gu.Units\FractionUnit.generated.cs
// Gu.Units\Gu.Units\UnitlessUnit.generated.cs
// Gu.Units\Gu.Units\AngleUnit.generated.cs
// Gu.Units\Gu.Units\TemperatureUnit.generated.cs
// Gu.Units\Gu.Units\TimeUnit.generated.cs
Expand Down
10 changes: 0 additions & 10 deletions Gu.Units.Tests/ConversionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,6 @@ public void Convert(ConversionProvider.IConversion<IQuantity> conversion)
Assert.AreEqual(conversion.FromQuantity, conversion.ToQuantity);
}

[Test, Explicit]
public void DegRadFactor()
{
var d = Math.PI / 180;
Console.WriteLine(d.ToString(CultureInfo.InvariantCulture));
Console.WriteLine(d.ToString("G17", CultureInfo.InvariantCulture));
Console.WriteLine(d.ToString("R", CultureInfo.InvariantCulture));
Console.WriteLine((90 * d).ToString("R", CultureInfo.InvariantCulture));
}

[TestCaseSource(typeof(UnitsProvider))]
public void Roundtrip(IUnit unit)
{
Expand Down
10 changes: 10 additions & 0 deletions Gu.Units.Tests/Helpers/ConversionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ public ConversionProvider()
new Conversion<Area>("1.2mm^2","1.2E-6m^2", Area.Parse),
new Conversion<Angle>("90°","1.5707963267948966rad", Angle.Parse),
new Conversion<Angle>("1°","0.017453292519943295rad", Angle.Parse),
new Conversion<Temperature>("0°C","32°F", Temperature.Parse),
new Conversion<Temperature>("100°C","211.99999999999994°F", Temperature.Parse),
new Conversion<Temperature>("100°F","37.777777777777828°C", Temperature.Parse),
new Conversion<Temperature>("0K","-273.15°C", Temperature.Parse),
new Conversion<Temperature>("0K","-459.67°F", Temperature.Parse),
new Conversion<Temperature>("100K","-173.14999999999998°C", Temperature.Parse),
new Conversion<Temperature>("100K","-279.67°F", Temperature.Parse),
new Conversion<Unitless>("1ul","100%", Unitless.Parse),
new Conversion<Unitless>("1%","0.01ul", Unitless.Parse),
new Conversion<Unitless>("1%","10‰", Unitless.Parse),
};

}
Expand Down
12 changes: 6 additions & 6 deletions Gu.Units/Gu.Units.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,6 @@
<Compile Include="ForceUnit.generated.cs">
<DependentUpon>UnitGenerator.txt4</DependentUpon>
</Compile>
<Compile Include="Fraction.generated.cs">
<DependentUpon>QuantityGenerator.txt4</DependentUpon>
</Compile>
<Compile Include="FractionUnit.generated.cs">
<DependentUpon>UnitGenerator.txt4</DependentUpon>
</Compile>
<Compile Include="Frequency.generated.cs">
<DependentUpon>QuantityGenerator.txt4</DependentUpon>
</Compile>
Expand Down Expand Up @@ -217,6 +211,12 @@
<Compile Include="TorqueUnit.generated.cs">
<DependentUpon>UnitGenerator.txt4</DependentUpon>
</Compile>
<Compile Include="Unitless.generated.cs">
<DependentUpon>QuantityGenerator.txt4</DependentUpon>
</Compile>
<Compile Include="UnitlessUnit.generated.cs">
<DependentUpon>UnitGenerator.txt4</DependentUpon>
</Compile>
<Compile Include="Utils\Parser.cs" />
<Compile Include="Utils\XmlExt.cs" />
<Compile Include="Voltage.generated.cs">
Expand Down
4 changes: 2 additions & 2 deletions Gu.Units/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.2.0.0")]
[assembly: AssemblyFileVersion("0.2.0.0")]
[assembly: AssemblyVersion("0.2.1.0")]
[assembly: AssemblyFileVersion("0.2.1.0")]
[assembly: NeutralResourcesLanguageAttribute("en")]
Loading

0 comments on commit 092ae25

Please sign in to comment.