Skip to content

Commit

Permalink
Add density units (#205)
Browse files Browse the repository at this point in the history
* Convert KilogramPer* units to GramPer* and add Kilo prefix
  • Loading branch information
salido-m authored and angularsen committed Nov 11, 2016
1 parent 34b2cfc commit beb0703
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 23 deletions.
3 changes: 3 additions & 0 deletions UnitsNet.Tests/CustomCode/DensityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ namespace UnitsNet.Tests.CustomCode
{
public class DensityTests : DensityTestsBase
{
protected override double GramsPerCubicCentimeterInOneKilogramPerCubicMeter => 1e-3;
protected override double GramsPerCubicMeterInOneKilogramPerCubicMeter => 1e3;
protected override double GramsPerCubicMillimeterInOneKilogramPerCubicMeter => 1e-6;
protected override double KilogramsPerCubicCentimeterInOneKilogramPerCubicMeter => 1e-6;

protected override double KilogramsPerCubicMeterInOneKilogramPerCubicMeter => 1;
Expand Down
18 changes: 18 additions & 0 deletions UnitsNet.Tests/GeneratedCode/DensityTestsBase.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ namespace UnitsNet.Tests
// ReSharper disable once PartialTypeWithSinglePart
public abstract partial class DensityTestsBase
{
protected abstract double GramsPerCubicCentimeterInOneKilogramPerCubicMeter { get; }
protected abstract double GramsPerCubicMeterInOneKilogramPerCubicMeter { get; }
protected abstract double GramsPerCubicMillimeterInOneKilogramPerCubicMeter { get; }
protected abstract double KilogramsPerCubicCentimeterInOneKilogramPerCubicMeter { get; }
protected abstract double KilogramsPerCubicMeterInOneKilogramPerCubicMeter { get; }
protected abstract double KilogramsPerCubicMillimeterInOneKilogramPerCubicMeter { get; }
Expand All @@ -65,6 +68,9 @@ public abstract partial class DensityTestsBase
protected abstract double TonnesPerCubicMillimeterInOneKilogramPerCubicMeter { get; }

// ReSharper disable VirtualMemberNeverOverriden.Global
protected virtual double GramsPerCubicCentimeterTolerance { get { return 1e-5; } }
protected virtual double GramsPerCubicMeterTolerance { get { return 1e-5; } }
protected virtual double GramsPerCubicMillimeterTolerance { get { return 1e-5; } }
protected virtual double KilogramsPerCubicCentimeterTolerance { get { return 1e-5; } }
protected virtual double KilogramsPerCubicMeterTolerance { get { return 1e-5; } }
protected virtual double KilogramsPerCubicMillimeterTolerance { get { return 1e-5; } }
Expand All @@ -81,6 +87,9 @@ public abstract partial class DensityTestsBase
public void KilogramPerCubicMeterToDensityUnits()
{
Density kilogrampercubicmeter = Density.FromKilogramsPerCubicMeter(1);
Assert.AreEqual(GramsPerCubicCentimeterInOneKilogramPerCubicMeter, kilogrampercubicmeter.GramsPerCubicCentimeter, GramsPerCubicCentimeterTolerance);
Assert.AreEqual(GramsPerCubicMeterInOneKilogramPerCubicMeter, kilogrampercubicmeter.GramsPerCubicMeter, GramsPerCubicMeterTolerance);
Assert.AreEqual(GramsPerCubicMillimeterInOneKilogramPerCubicMeter, kilogrampercubicmeter.GramsPerCubicMillimeter, GramsPerCubicMillimeterTolerance);
Assert.AreEqual(KilogramsPerCubicCentimeterInOneKilogramPerCubicMeter, kilogrampercubicmeter.KilogramsPerCubicCentimeter, KilogramsPerCubicCentimeterTolerance);
Assert.AreEqual(KilogramsPerCubicMeterInOneKilogramPerCubicMeter, kilogrampercubicmeter.KilogramsPerCubicMeter, KilogramsPerCubicMeterTolerance);
Assert.AreEqual(KilogramsPerCubicMillimeterInOneKilogramPerCubicMeter, kilogrampercubicmeter.KilogramsPerCubicMillimeter, KilogramsPerCubicMillimeterTolerance);
Expand All @@ -96,6 +105,9 @@ public void KilogramPerCubicMeterToDensityUnits()
[Test]
public void FromValueAndUnit()
{
Assert.AreEqual(1, Density.From(1, DensityUnit.GramPerCubicCentimeter).GramsPerCubicCentimeter, GramsPerCubicCentimeterTolerance);
Assert.AreEqual(1, Density.From(1, DensityUnit.GramPerCubicMeter).GramsPerCubicMeter, GramsPerCubicMeterTolerance);
Assert.AreEqual(1, Density.From(1, DensityUnit.GramPerCubicMillimeter).GramsPerCubicMillimeter, GramsPerCubicMillimeterTolerance);
Assert.AreEqual(1, Density.From(1, DensityUnit.KilogramPerCubicCentimeter).KilogramsPerCubicCentimeter, KilogramsPerCubicCentimeterTolerance);
Assert.AreEqual(1, Density.From(1, DensityUnit.KilogramPerCubicMeter).KilogramsPerCubicMeter, KilogramsPerCubicMeterTolerance);
Assert.AreEqual(1, Density.From(1, DensityUnit.KilogramPerCubicMillimeter).KilogramsPerCubicMillimeter, KilogramsPerCubicMillimeterTolerance);
Expand All @@ -112,6 +124,9 @@ public void FromValueAndUnit()
public void As()
{
var kilogrampercubicmeter = Density.FromKilogramsPerCubicMeter(1);
Assert.AreEqual(GramsPerCubicCentimeterInOneKilogramPerCubicMeter, kilogrampercubicmeter.As(DensityUnit.GramPerCubicCentimeter), GramsPerCubicCentimeterTolerance);
Assert.AreEqual(GramsPerCubicMeterInOneKilogramPerCubicMeter, kilogrampercubicmeter.As(DensityUnit.GramPerCubicMeter), GramsPerCubicMeterTolerance);
Assert.AreEqual(GramsPerCubicMillimeterInOneKilogramPerCubicMeter, kilogrampercubicmeter.As(DensityUnit.GramPerCubicMillimeter), GramsPerCubicMillimeterTolerance);
Assert.AreEqual(KilogramsPerCubicCentimeterInOneKilogramPerCubicMeter, kilogrampercubicmeter.As(DensityUnit.KilogramPerCubicCentimeter), KilogramsPerCubicCentimeterTolerance);
Assert.AreEqual(KilogramsPerCubicMeterInOneKilogramPerCubicMeter, kilogrampercubicmeter.As(DensityUnit.KilogramPerCubicMeter), KilogramsPerCubicMeterTolerance);
Assert.AreEqual(KilogramsPerCubicMillimeterInOneKilogramPerCubicMeter, kilogrampercubicmeter.As(DensityUnit.KilogramPerCubicMillimeter), KilogramsPerCubicMillimeterTolerance);
Expand All @@ -128,6 +143,9 @@ public void As()
public void ConversionRoundTrip()
{
Density kilogrampercubicmeter = Density.FromKilogramsPerCubicMeter(1);
Assert.AreEqual(1, Density.FromGramsPerCubicCentimeter(kilogrampercubicmeter.GramsPerCubicCentimeter).KilogramsPerCubicMeter, GramsPerCubicCentimeterTolerance);
Assert.AreEqual(1, Density.FromGramsPerCubicMeter(kilogrampercubicmeter.GramsPerCubicMeter).KilogramsPerCubicMeter, GramsPerCubicMeterTolerance);
Assert.AreEqual(1, Density.FromGramsPerCubicMillimeter(kilogrampercubicmeter.GramsPerCubicMillimeter).KilogramsPerCubicMeter, GramsPerCubicMillimeterTolerance);
Assert.AreEqual(1, Density.FromKilogramsPerCubicCentimeter(kilogrampercubicmeter.KilogramsPerCubicCentimeter).KilogramsPerCubicMeter, KilogramsPerCubicCentimeterTolerance);
Assert.AreEqual(1, Density.FromKilogramsPerCubicMeter(kilogrampercubicmeter.KilogramsPerCubicMeter).KilogramsPerCubicMeter, KilogramsPerCubicMeterTolerance);
Assert.AreEqual(1, Density.FromKilogramsPerCubicMillimeter(kilogrampercubicmeter.KilogramsPerCubicMillimeter).KilogramsPerCubicMeter, KilogramsPerCubicMillimeterTolerance);
Expand Down
3 changes: 3 additions & 0 deletions UnitsNet/GeneratedCode/Enums/DensityUnit.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ namespace UnitsNet.Units
public enum DensityUnit
{
Undefined = 0,
GramPerCubicCentimeter,
GramPerCubicMeter,
GramPerCubicMillimeter,
KilogramPerCubicCentimeter,
KilogramPerCubicMeter,
KilogramPerCubicMillimeter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,108 @@ namespace UnitsNet.Extensions.NumberToDensity
{
public static class NumberToDensityExtensions
{
#region GramPerCubicCentimeter

/// <inheritdoc cref="Density.FromGramsPerCubicCentimeter(double)"/>
public static Density GramsPerCubicCentimeter(this int value) => Density.FromGramsPerCubicCentimeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicCentimeter(double?)"/>
public static Density? GramsPerCubicCentimeter(this int? value) => Density.FromGramsPerCubicCentimeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicCentimeter(double)"/>
public static Density GramsPerCubicCentimeter(this long value) => Density.FromGramsPerCubicCentimeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicCentimeter(double?)"/>
public static Density? GramsPerCubicCentimeter(this long? value) => Density.FromGramsPerCubicCentimeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicCentimeter(double)"/>
public static Density GramsPerCubicCentimeter(this double value) => Density.FromGramsPerCubicCentimeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicCentimeter(double?)"/>
public static Density? GramsPerCubicCentimeter(this double? value) => Density.FromGramsPerCubicCentimeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicCentimeter(double)"/>
public static Density GramsPerCubicCentimeter(this float value) => Density.FromGramsPerCubicCentimeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicCentimeter(double?)"/>
public static Density? GramsPerCubicCentimeter(this float? value) => Density.FromGramsPerCubicCentimeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicCentimeter(double)"/>
public static Density GramsPerCubicCentimeter(this decimal value) => Density.FromGramsPerCubicCentimeter(Convert.ToDouble(value));

/// <inheritdoc cref="Density.FromGramsPerCubicCentimeter(double?)"/>
public static Density? GramsPerCubicCentimeter(this decimal? value) => Density.FromGramsPerCubicCentimeter(value == null ? (double?)null : Convert.ToDouble(value.Value));

#endregion

#region GramPerCubicMeter

/// <inheritdoc cref="Density.FromGramsPerCubicMeter(double)"/>
public static Density GramsPerCubicMeter(this int value) => Density.FromGramsPerCubicMeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicMeter(double?)"/>
public static Density? GramsPerCubicMeter(this int? value) => Density.FromGramsPerCubicMeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicMeter(double)"/>
public static Density GramsPerCubicMeter(this long value) => Density.FromGramsPerCubicMeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicMeter(double?)"/>
public static Density? GramsPerCubicMeter(this long? value) => Density.FromGramsPerCubicMeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicMeter(double)"/>
public static Density GramsPerCubicMeter(this double value) => Density.FromGramsPerCubicMeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicMeter(double?)"/>
public static Density? GramsPerCubicMeter(this double? value) => Density.FromGramsPerCubicMeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicMeter(double)"/>
public static Density GramsPerCubicMeter(this float value) => Density.FromGramsPerCubicMeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicMeter(double?)"/>
public static Density? GramsPerCubicMeter(this float? value) => Density.FromGramsPerCubicMeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicMeter(double)"/>
public static Density GramsPerCubicMeter(this decimal value) => Density.FromGramsPerCubicMeter(Convert.ToDouble(value));

/// <inheritdoc cref="Density.FromGramsPerCubicMeter(double?)"/>
public static Density? GramsPerCubicMeter(this decimal? value) => Density.FromGramsPerCubicMeter(value == null ? (double?)null : Convert.ToDouble(value.Value));

#endregion

#region GramPerCubicMillimeter

/// <inheritdoc cref="Density.FromGramsPerCubicMillimeter(double)"/>
public static Density GramsPerCubicMillimeter(this int value) => Density.FromGramsPerCubicMillimeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicMillimeter(double?)"/>
public static Density? GramsPerCubicMillimeter(this int? value) => Density.FromGramsPerCubicMillimeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicMillimeter(double)"/>
public static Density GramsPerCubicMillimeter(this long value) => Density.FromGramsPerCubicMillimeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicMillimeter(double?)"/>
public static Density? GramsPerCubicMillimeter(this long? value) => Density.FromGramsPerCubicMillimeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicMillimeter(double)"/>
public static Density GramsPerCubicMillimeter(this double value) => Density.FromGramsPerCubicMillimeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicMillimeter(double?)"/>
public static Density? GramsPerCubicMillimeter(this double? value) => Density.FromGramsPerCubicMillimeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicMillimeter(double)"/>
public static Density GramsPerCubicMillimeter(this float value) => Density.FromGramsPerCubicMillimeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicMillimeter(double?)"/>
public static Density? GramsPerCubicMillimeter(this float? value) => Density.FromGramsPerCubicMillimeter(value);

/// <inheritdoc cref="Density.FromGramsPerCubicMillimeter(double)"/>
public static Density GramsPerCubicMillimeter(this decimal value) => Density.FromGramsPerCubicMillimeter(Convert.ToDouble(value));

/// <inheritdoc cref="Density.FromGramsPerCubicMillimeter(double?)"/>
public static Density? GramsPerCubicMillimeter(this decimal? value) => Density.FromGramsPerCubicMillimeter(value == null ? (double?)null : Convert.ToDouble(value.Value));

#endregion

#region KilogramPerCubicCentimeter

/// <inheritdoc cref="Density.FromKilogramsPerCubicCentimeter(double)"/>
Expand Down
Loading

4 comments on commit beb0703

@angularsen
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity Units.NET :: Build and Test Build 250 is now running

@angularsen
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity Units.NET :: Build and Test Build 250 outcome was SUCCESS
Summary: Tests passed: 832 Build time: 0:0:41

@angularsen
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity Units.NET :: Publish NuGets Build 250 is now running

@angularsen
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity Units.NET :: Publish NuGets Build 250 outcome was SUCCESS
Summary: Running Build time: 0:0:12

Please sign in to comment.