Skip to content

Commit

Permalink
Implementing dimensionless base dimension
Browse files Browse the repository at this point in the history
  • Loading branch information
tmilnthorp committed Oct 16, 2018
1 parent 1c02a3d commit 5f625cb
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 7 deletions.
66 changes: 66 additions & 0 deletions UnitsNet.Tests/BaseDimensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,56 @@ namespace UnitsNet.Tests
{
public class BaseDimensionsTests
{
[Fact]
public void ConstructorImplementedCorrectly()
{
var baseDimensions = new BaseDimensions(1, 2, 3, 4, 5, 6, 7);

Assert.True(baseDimensions.Length == 1);
Assert.True(baseDimensions.Mass == 2);
Assert.True(baseDimensions.Time == 3);
Assert.True(baseDimensions.Current == 4);
Assert.True(baseDimensions.Temperature == 5);
Assert.True(baseDimensions.Amount == 6);
Assert.True(baseDimensions.LuminousIntensity == 7);
}

[Theory]
[InlineData(1, 0, 0, 0, 0, 0, 0)]
[InlineData(0, 1, 0, 0, 0, 0, 0)]
[InlineData(0, 0, 1, 0, 0, 0, 0)]
[InlineData(0, 0, 0, 1, 0, 0, 0)]
[InlineData(0, 0, 0, 0, 1, 0, 0)]
[InlineData(0, 0, 0, 0, 0, 1, 0)]
[InlineData(0, 0, 0, 0, 0, 0, 1)]
public void IsBaseImplementedProperly(int length, int mass, int time, int current, int temperature, int amount, int luminousIntensity)
{
var baseDimensions = new BaseDimensions(length, mass, time, current, temperature, amount, luminousIntensity);
var derivedDimensions = new BaseDimensions(length * 2, mass * 2, time * 2, current * 2, temperature * 2, amount * 2, luminousIntensity * 2);

Assert.True(baseDimensions.IsBase());
Assert.False(derivedDimensions.IsBase());
Assert.False(BaseDimensions.Dimensionless.IsBase());
}

[Theory]
[InlineData(2, 0, 0, 0, 0, 0, 0)]
[InlineData(0, 2, 0, 0, 0, 0, 0)]
[InlineData(0, 0, 2, 0, 0, 0, 0)]
[InlineData(0, 0, 0, 2, 0, 0, 0)]
[InlineData(0, 0, 0, 0, 2, 0, 0)]
[InlineData(0, 0, 0, 0, 0, 2, 0)]
[InlineData(0, 0, 0, 0, 0, 0, 2)]
public void IsDerivedImplementedSuccessfully(int length, int mass, int time, int current, int temperature, int amount, int luminousIntensity)
{
var baseDimensions = new BaseDimensions(length / 2, mass / 2, time / 2, current / 2, temperature / 2, amount / 2, luminousIntensity / 2);
var derivedDimensions = new BaseDimensions(length, mass, time, current, temperature, amount, luminousIntensity);

Assert.False(baseDimensions.IsDerived());
Assert.True(derivedDimensions.IsDerived());
Assert.False(BaseDimensions.Dimensionless.IsDerived());
}

[Fact]
public void EqualsWorksAsExpected()
{
Expand Down Expand Up @@ -663,5 +713,21 @@ public void GetHashCodeWorksProperly()
Assert.True(baseDimensions1.GetHashCode() != baseDimensions2.GetHashCode());
Assert.True(baseDimensions1.GetHashCode() == baseDimensions3.GetHashCode());
}

[Fact]
public void DimensionLessPropertyIsCorrect()
{
Assert.True(BaseDimensions.Dimensionless == new BaseDimensions(0, 0, 0, 0, 0, 0, 0));
}

[Fact]
public void IsDimensionLessMethodImplementedCorrectly()
{
Assert.True(BaseDimensions.Dimensionless.IsDimensionless());
Assert.True(new BaseDimensions(0, 0, 0, 0, 0, 0, 0).IsDimensionless());

// Example case
Assert.True(Level.BaseDimensions.IsDimensionless());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public sealed partial class Information : IQuantity

static Information()
{
BaseDimensions = new BaseDimensions(0, 0, 0, 0, 0, 0, 0);
BaseDimensions = BaseDimensions.Dimensionless;
}
/// <summary>
/// Creates the quantity with a value of 0 in the base unit Bit.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public sealed partial class Level : IQuantity

static Level()
{
BaseDimensions = new BaseDimensions(0, 0, 0, 0, 0, 0, 0);
BaseDimensions = BaseDimensions.Dimensionless;
}
/// <summary>
/// Creates the quantity with a value of 0 in the base unit Decibel.
Expand Down
31 changes: 31 additions & 0 deletions UnitsNet/BaseDimensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,35 @@ public BaseDimensions(int length, int mass, int time, int current, int temperatu
LuminousIntensity = luminousIntensity;
}

/// <summary>
/// Checks if this base dimensions object represents a base quantity.
/// </summary>
/// <returns>True if this object represents a base quantity, otherwise false.</returns>
public bool IsBase()
{
var dimensionsArray = new int[]{Length, Mass, Time, Current, Temperature, Amount, LuminousIntensity};
bool onlyOneEqualsOne = dimensionsArray.Where(dimension => dimension == 1).Take(2).Count() == 1;
return onlyOneEqualsOne;
}

/// <summary>
/// Checks if this base dimensions object represents a derived quantity.
/// </summary>
/// <returns>True if this object represents a derived quantity, otherwise false.</returns>
public bool IsDerived()
{
return !IsBase() && !IsDimensionless();
}

/// <summary>
/// Checks if this base dimensions object represents a dimensionless quantity.
/// </summary>
/// <returns>True if this object represents a dimensionless quantity, otherwise false.</returns>
public bool IsDimensionless()
{
return this == Dimensionless;
}

/// <inheritdoc />
public override bool Equals(object obj)
{
Expand Down Expand Up @@ -223,5 +252,7 @@ private static void AppendDimensionString(StringBuilder sb, string name, int val
/// Gets the luminous intensity dimensions (J).
/// </summary>
public int LuminousIntensity{ get; }

public static BaseDimensions Dimensionless { get; } = new BaseDimensions(0, 0, 0, 0, 0, 0, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public partial struct Information : IQuantity<InformationUnit>, IComparable, ICo

static Information()
{
BaseDimensions = new BaseDimensions(0, 0, 0, 0, 0, 0, 0);
BaseDimensions = BaseDimensions.Dimensionless;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion UnitsNet/GeneratedCode/Quantities/Level.NetFramework.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public partial struct Level : IQuantity<LevelUnit>, IComparable, IComparable<Lev

static Level()
{
BaseDimensions = new BaseDimensions(0, 0, 0, 0, 0, 0, 0);
BaseDimensions = BaseDimensions.Dimensionless;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,15 @@ if ($obsoleteAttribute)
static $quantityName()
{
"@; if($baseDimensions)
"@; if($baseDimensions -eq $null -or ( $baseDimensions.Length -eq 0 -and $baseDimensions.Mass -eq 0 -and $baseDimensions.Time -eq 0 -and $baseDimensions.ElectricCurrent -eq 0 -and $baseDimensions.Temperature -eq 0 -and $baseDimensions.AmountOfSubstance -eq 0 -and $baseDimensions.LuminousIntensity -eq 0 ) )
{@"
BaseDimensions = BaseDimensions.Dimensionless;
"@; }
else
{@"
BaseDimensions = new BaseDimensions($($baseDimensions.Length), $($baseDimensions.Mass), $($baseDimensions.Time), $($baseDimensions.ElectricCurrent), $($baseDimensions.Temperature), $($baseDimensions.AmountOfSubstance), $($baseDimensions.LuminousIntensity));
"@; }@"
"@; }
@"
}
"@; # Windows Runtime Component requires a default constructor
if ($wrc) {@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ namespace UnitsNet.Tests
}
[Fact]
public void AllUnitsHaveAtLeastOneAbbreviationSpecified()
public void HasAtLeastOneAbbreviationSpecified()
{
var units = Enum.GetValues(typeof($unitEnumName)).Cast<$unitEnumName>();
foreach(var unit in units)
Expand All @@ -275,6 +275,12 @@ namespace UnitsNet.Tests
var defaultAbbreviation = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit);
}
}
[Fact]
public void BaseDimensionsShouldNeverBeNull()
{
Assert.False($quantityName.BaseDimensions is null);
}
}
}
"@;
Expand Down

0 comments on commit 5f625cb

Please sign in to comment.