From a110555b04e4528bef749c6c169238aaf982bd75 Mon Sep 17 00:00:00 2001 From: Tristan Milnthorp Date: Mon, 15 Oct 2018 15:39:35 -0400 Subject: [PATCH 1/7] Adding BaseUnits class. Adding SI unit system. --- UnitsNet.Tests/BaseUnitsTests.cs | 44 ++++++++ UnitsNet.Tests/UnitSystemTests.cs | 55 ++++++++++ UnitsNet/BaseDimensions.cs | 2 + UnitsNet/BaseUnits.cs | 161 ++++++++++++++++++++++++++++++ UnitsNet/UnitSystem.cs | 45 +++++++++ 5 files changed, 307 insertions(+) create mode 100644 UnitsNet.Tests/BaseUnitsTests.cs create mode 100644 UnitsNet.Tests/UnitSystemTests.cs create mode 100644 UnitsNet/BaseUnits.cs create mode 100644 UnitsNet/UnitSystem.cs diff --git a/UnitsNet.Tests/BaseUnitsTests.cs b/UnitsNet.Tests/BaseUnitsTests.cs new file mode 100644 index 0000000000..8562c66ee4 --- /dev/null +++ b/UnitsNet.Tests/BaseUnitsTests.cs @@ -0,0 +1,44 @@ +// Copyright (c) 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). +// https://github.com/angularsen/UnitsNet +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using UnitsNet.Units; +using Xunit; + +namespace UnitsNet.Tests +{ + public class BaseUnitsTests + { + [Fact] + public void Constructor() + { + var baseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + Assert.Equal(LengthUnit.Meter, baseUnits.Length); + Assert.Equal(MassUnit.Kilogram, baseUnits.Mass); + Assert.Equal(DurationUnit.Second, baseUnits.Time); + Assert.Equal(ElectricCurrentUnit.Ampere, baseUnits.Current); + Assert.Equal(TemperatureUnit.Kelvin, baseUnits.Temperature); + Assert.Equal(AmountOfSubstanceUnit.Mole, baseUnits.Amount); + Assert.Equal(LuminousIntensityUnit.Candela, baseUnits.LuminousIntensity); + } + } +} diff --git a/UnitsNet.Tests/UnitSystemTests.cs b/UnitsNet.Tests/UnitSystemTests.cs new file mode 100644 index 0000000000..7405451997 --- /dev/null +++ b/UnitsNet.Tests/UnitSystemTests.cs @@ -0,0 +1,55 @@ +// Copyright (c) 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). +// https://github.com/angularsen/UnitsNet +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using UnitsNet.Units; +using Xunit; + +namespace UnitsNet.Tests +{ + public class UnitSystemTests + { + [Fact] + public void Constructor() + { + var siBaseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + var unitSystem = new UnitSystem(siBaseUnits); + + Assert.Equal(unitSystem.BaseUnits, siBaseUnits); + } + + [Fact] + public void SIUnitSystemHasCorrectBaseUnits() + { + var siBaseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + Assert.Equal(LengthUnit.Meter, UnitSystem.SI.BaseUnits.Length); + Assert.Equal(MassUnit.Kilogram, UnitSystem.SI.BaseUnits.Mass); + Assert.Equal(DurationUnit.Second, UnitSystem.SI.BaseUnits.Time); + Assert.Equal(ElectricCurrentUnit.Ampere, UnitSystem.SI.BaseUnits.Current); + Assert.Equal(TemperatureUnit.Kelvin, UnitSystem.SI.BaseUnits.Temperature); + Assert.Equal(AmountOfSubstanceUnit.Mole, UnitSystem.SI.BaseUnits.Amount); + Assert.Equal(LuminousIntensityUnit.Candela, UnitSystem.SI.BaseUnits.LuminousIntensity); + } + } +} diff --git a/UnitsNet/BaseDimensions.cs b/UnitsNet/BaseDimensions.cs index 7eb558b83d..6e62123e0f 100644 --- a/UnitsNet/BaseDimensions.cs +++ b/UnitsNet/BaseDimensions.cs @@ -107,6 +107,7 @@ public BaseDimensions Divide(BaseDimensions right) } #if !WINDOWS_UWP + /// /// Check if two dimensions are equal. /// @@ -150,6 +151,7 @@ public BaseDimensions Divide(BaseDimensions right) { return left.Divide(right); } + #endif /// diff --git a/UnitsNet/BaseUnits.cs b/UnitsNet/BaseUnits.cs new file mode 100644 index 0000000000..df2f63f092 --- /dev/null +++ b/UnitsNet/BaseUnits.cs @@ -0,0 +1,161 @@ +// Copyright (c) 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). +// https://github.com/angularsen/UnitsNet +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System; +using System.Text; +using UnitsNet.Units; + +namespace UnitsNet +{ + /// + /// Represents the base units for a quantity or + /// + public sealed class BaseUnits + { + /// + public BaseUnits(LengthUnit length, MassUnit mass, DurationUnit time, ElectricCurrentUnit current, + TemperatureUnit temperature, AmountOfSubstanceUnit amount, LuminousIntensityUnit luminousIntensity) + { + if(length == LengthUnit.Undefined) + throw new ArgumentException("The given length unit is undefined.", nameof(length)); + else if(mass == MassUnit.Undefined) + throw new ArgumentException("The given mass unit is undefined.", nameof(mass)); + else if(time == DurationUnit.Undefined) + throw new ArgumentException("The given time unit is undefined.", nameof(time)); + else if(current == ElectricCurrentUnit.Undefined) + throw new ArgumentException("The given electric current unit is undefined.", nameof(current)); + else if(temperature == TemperatureUnit.Undefined) + throw new ArgumentException("The given temperature unit is undefined.", nameof(temperature)); + else if(amount == AmountOfSubstanceUnit.Undefined) + throw new ArgumentException("The given amount of substance unit is undefined.", nameof(amount)); + else if(luminousIntensity == LuminousIntensityUnit.Undefined) + throw new ArgumentException("The given luminous intensity unit is undefined.", nameof(luminousIntensity)); + + Length = length; + Mass = mass; + Time = time; + Current = current; + Temperature = temperature; + Amount = amount; + LuminousIntensity = luminousIntensity; + } + + /// + public override bool Equals(object obj) + { + if(obj is null || !(obj is BaseUnits)) + return false; + + var other = (BaseUnits)obj; + + return Length == other.Length && + Mass == other.Mass && + Time == other.Time && + Current == other.Current && + Temperature == other.Temperature && + Amount == other.Amount && + LuminousIntensity == other.LuminousIntensity; + } + + /// + public override int GetHashCode() + { + return new {Length, Mass, Time, Current, Temperature, Amount, LuminousIntensity}.GetHashCode(); + } + +#if !WINDOWS_UWP + + /// + /// Check if two dimensions are equal. + /// + /// Left. + /// Right. + /// True if equal. + public static bool operator ==(BaseUnits left, BaseUnits right) + { + return left?.Equals(right) == true; + } + + /// + /// Check if two dimensions are unequal. + /// + /// Left. + /// Right. + /// True if not equal. + public static bool operator !=(BaseUnits left, BaseUnits right) + { + return !(left == right); + } + +#endif + + /// + public override string ToString() + { + var sb = new StringBuilder(); + + sb.AppendFormat("[Length]: {0} ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Length)); + sb.AppendFormat("[Mass]: {0} ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Mass)); + sb.AppendFormat("[Time]: {0} ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Time)); + sb.AppendFormat("[Current]: {0} ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Current)); + sb.AppendFormat("[Temperature]: {0} ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Temperature)); + sb.AppendFormat("[Amount]: {0} ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Amount)); + sb.AppendFormat("[LuminousIntensity]: {0} ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(LuminousIntensity)); + + return sb.ToString(); + } + + /// + /// Gets the length unit (L). + /// + public LengthUnit Length { get; } + + /// + /// Gets the mass unit (M). + /// + public MassUnit Mass{ get; } + + /// + /// Gets the time unit (T). + /// + public DurationUnit Time{ get; } + + /// + /// Gets the electric current unit (I). + /// + public ElectricCurrentUnit Current{ get; } + + /// + /// Gets the temperature unit (Θ). + /// + public TemperatureUnit Temperature{ get; } + + /// + /// Gets the amount of substance unit (N). + /// + public AmountOfSubstanceUnit Amount{ get; } + + /// + /// Gets the luminous intensity unit (J). + /// + public LuminousIntensityUnit LuminousIntensity{ get; } + } +} diff --git a/UnitsNet/UnitSystem.cs b/UnitsNet/UnitSystem.cs new file mode 100644 index 0000000000..9047fab101 --- /dev/null +++ b/UnitsNet/UnitSystem.cs @@ -0,0 +1,45 @@ +// Copyright (c) 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). +// https://github.com/angularsen/UnitsNet +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System; +using System.Text; +using UnitsNet.Units; + +namespace UnitsNet +{ + public class UnitSystem + { + public UnitSystem(BaseUnits baseUnits) + { + BaseUnits = baseUnits; + } + + public BaseUnits BaseUnits { get; } + + private static readonly BaseUnits SIBaseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + /// + /// Gets the SI unit system. + /// + public static UnitSystem SI { get; } = new UnitSystem(SIBaseUnits); + } +} From f3c90ed915ac3dea672248bacbf8e0f9fea316ae Mon Sep 17 00:00:00 2001 From: Tristan Milnthorp Date: Tue, 16 Oct 2018 10:49:18 -0400 Subject: [PATCH 2/7] Implementing IEquatable on BaseUnits, and adding some tests for BaseUnits. Adding some doc too. --- UnitsNet.Tests/BaseUnitsTests.cs | 85 +++++++++++++++++++++++++++++++- UnitsNet/BaseUnits.cs | 75 ++++++++++++++++++---------- 2 files changed, 134 insertions(+), 26 deletions(-) diff --git a/UnitsNet.Tests/BaseUnitsTests.cs b/UnitsNet.Tests/BaseUnitsTests.cs index 8562c66ee4..f8464b8767 100644 --- a/UnitsNet.Tests/BaseUnitsTests.cs +++ b/UnitsNet.Tests/BaseUnitsTests.cs @@ -19,6 +19,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. +using System; using UnitsNet.Units; using Xunit; @@ -27,7 +28,7 @@ namespace UnitsNet.Tests public class BaseUnitsTests { [Fact] - public void Constructor() + public void ConstructorSetsUnitsProperly() { var baseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); @@ -40,5 +41,87 @@ public void Constructor() Assert.Equal(AmountOfSubstanceUnit.Mole, baseUnits.Amount); Assert.Equal(LuminousIntensityUnit.Candela, baseUnits.LuminousIntensity); } + + [Theory] + [InlineData(LengthUnit.Undefined, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] + [InlineData(LengthUnit.Meter, MassUnit.Undefined, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Undefined, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Undefined, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Undefined, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Undefined, LuminousIntensityUnit.Candela)] + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Undefined)] + public void ConstructorThrowsExceptionWithUndefinedUnits(LengthUnit length, MassUnit mass, DurationUnit time, ElectricCurrentUnit current, + TemperatureUnit temperature, AmountOfSubstanceUnit amount, LuminousIntensityUnit luminousIntensity) + { + var baseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + Assert.Throws(() => new BaseUnits(length, mass, time, current, temperature, amount, luminousIntensity)); + } + + [Fact] + public void EqualsObjectIsImplementedCorrectly() + { + var baseUnits1 = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + var baseUnits2 = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + Assert.True(baseUnits1.Equals((object)baseUnits2)); + Assert.False(baseUnits1.Equals("Some object.")); + Assert.False(baseUnits1.Equals((IFormatProvider)null)); + } + + [Fact] + public void EqualsBaseUnitsIsImplementedCorrectly() + { + var baseUnits1 = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + var baseUnits2 = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + Assert.True(baseUnits1.Equals(baseUnits2)); + Assert.True(baseUnits2.Equals(baseUnits1)); + Assert.False(baseUnits1.Equals(null)); + } + + [Fact] + public void EqualityOperatorIsImplementedCorrectly() + { + var baseUnits1 = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + var baseUnits2 = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + Assert.True(baseUnits1 == baseUnits2); + Assert.False(baseUnits1 == null); + Assert.False(null == baseUnits1); + } + + [Fact] + public void InequalityOperatorIsImplementedCorrectly() + { + var baseUnits1 = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + var baseUnits2 = new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.DegreeFahrenheit, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + Assert.True(baseUnits1 != baseUnits2); + Assert.True(baseUnits1 != null); + Assert.True(null != baseUnits1); + } + + [Fact] + public void ToStringGivesExpectedResult() + { + var baseUnits1 = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + Assert.Equal("[Length]: m, [Mass]: kg, [Time]: s, [Current]: A, [Temperature]: K, [Amount]: mol, [LuminousIntensity]: cd", baseUnits1.ToString()); + } } } diff --git a/UnitsNet/BaseUnits.cs b/UnitsNet/BaseUnits.cs index df2f63f092..5d85178348 100644 --- a/UnitsNet/BaseUnits.cs +++ b/UnitsNet/BaseUnits.cs @@ -26,27 +26,39 @@ namespace UnitsNet { /// - /// Represents the base units for a quantity or + /// Represents the base units for a quantity. All quantities, both base and derived, can be + /// represented by a combination of these seven base units. /// - public sealed class BaseUnits + public sealed class BaseUnits : IEquatable { - /// + /// + /// Creates an instance of if the base units class that represents the base units for a quantity. + /// All quantities, both base and derived, can be represented by a combination of these seven base units. + /// + /// The length unit (L). + /// The mass unit (M). + /// The time unit (T). + /// The electric current unit (I). + /// The temperature unit (Θ). + /// The amount of substance unit (N). + /// The luminous intensity unit (J). + /// Throws an ArgumentException if any of the units are undefined. public BaseUnits(LengthUnit length, MassUnit mass, DurationUnit time, ElectricCurrentUnit current, TemperatureUnit temperature, AmountOfSubstanceUnit amount, LuminousIntensityUnit luminousIntensity) { if(length == LengthUnit.Undefined) throw new ArgumentException("The given length unit is undefined.", nameof(length)); - else if(mass == MassUnit.Undefined) + if(mass == MassUnit.Undefined) throw new ArgumentException("The given mass unit is undefined.", nameof(mass)); - else if(time == DurationUnit.Undefined) + if(time == DurationUnit.Undefined) throw new ArgumentException("The given time unit is undefined.", nameof(time)); - else if(current == ElectricCurrentUnit.Undefined) + if(current == ElectricCurrentUnit.Undefined) throw new ArgumentException("The given electric current unit is undefined.", nameof(current)); - else if(temperature == TemperatureUnit.Undefined) + if(temperature == TemperatureUnit.Undefined) throw new ArgumentException("The given temperature unit is undefined.", nameof(temperature)); - else if(amount == AmountOfSubstanceUnit.Undefined) + if(amount == AmountOfSubstanceUnit.Undefined) throw new ArgumentException("The given amount of substance unit is undefined.", nameof(amount)); - else if(luminousIntensity == LuminousIntensityUnit.Undefined) + if(luminousIntensity == LuminousIntensityUnit.Undefined) throw new ArgumentException("The given luminous intensity unit is undefined.", nameof(luminousIntensity)); Length = length; @@ -64,7 +76,18 @@ public override bool Equals(object obj) if(obj is null || !(obj is BaseUnits)) return false; - var other = (BaseUnits)obj; + return Equals((BaseUnits)obj); + } + + /// + /// Checks if all of the base units are equal to another instance's. + /// + /// The other instance to check if equal to. + /// True if equal, otherwise false. + public bool Equals(BaseUnits other) + { + if(other is null) + return false; return Length == other.Length && Mass == other.Mass && @@ -84,22 +107,24 @@ public override int GetHashCode() #if !WINDOWS_UWP /// - /// Check if two dimensions are equal. + /// Checks if this instance is equal to another. /// - /// Left. - /// Right. - /// True if equal. + /// The left instance. + /// The right instance. + /// True if equal, otherwise false. + /// public static bool operator ==(BaseUnits left, BaseUnits right) { return left?.Equals(right) == true; } /// - /// Check if two dimensions are unequal. + /// Checks if this instance is not equal to another. /// - /// Left. - /// Right. - /// True if not equal. + /// The left instance. + /// The right instance. + /// True if not equal, otherwise false. + /// public static bool operator !=(BaseUnits left, BaseUnits right) { return !(left == right); @@ -112,13 +137,13 @@ public override string ToString() { var sb = new StringBuilder(); - sb.AppendFormat("[Length]: {0} ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Length)); - sb.AppendFormat("[Mass]: {0} ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Mass)); - sb.AppendFormat("[Time]: {0} ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Time)); - sb.AppendFormat("[Current]: {0} ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Current)); - sb.AppendFormat("[Temperature]: {0} ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Temperature)); - sb.AppendFormat("[Amount]: {0} ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Amount)); - sb.AppendFormat("[LuminousIntensity]: {0} ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(LuminousIntensity)); + sb.AppendFormat("[Length]: {0}, ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Length)); + sb.AppendFormat("[Mass]: {0}, ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Mass)); + sb.AppendFormat("[Time]: {0}, ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Time)); + sb.AppendFormat("[Current]: {0}, ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Current)); + sb.AppendFormat("[Temperature]: {0}, ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Temperature)); + sb.AppendFormat("[Amount]: {0}, ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Amount)); + sb.AppendFormat("[LuminousIntensity]: {0}", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(LuminousIntensity)); return sb.ToString(); } From 21d8459194b54275a8df29acc420a473d5e190e0 Mon Sep 17 00:00:00 2001 From: Tristan Milnthorp Date: Tue, 16 Oct 2018 12:55:50 -0400 Subject: [PATCH 3/7] Fixing BaseUnits to not throw exception for undefined units. The Length quantity for example has a BaseUnits where only Length would be set. Only a UnitSystem needs it all. Moving logic there. Expanding UnitSystem tests and logic. Fixing WRC. --- UnitsNet.Tests/BaseUnitsTests.cs | 49 ++++++++----- UnitsNet.Tests/UnitSystemTests.cs | 114 ++++++++++++++++++++++++++++-- UnitsNet/BaseUnits.cs | 25 +++---- UnitsNet/UnitSystem.cs | 86 +++++++++++++++++++++- 4 files changed, 231 insertions(+), 43 deletions(-) diff --git a/UnitsNet.Tests/BaseUnitsTests.cs b/UnitsNet.Tests/BaseUnitsTests.cs index f8464b8767..7bcf7229ae 100644 --- a/UnitsNet.Tests/BaseUnitsTests.cs +++ b/UnitsNet.Tests/BaseUnitsTests.cs @@ -42,23 +42,6 @@ public void ConstructorSetsUnitsProperly() Assert.Equal(LuminousIntensityUnit.Candela, baseUnits.LuminousIntensity); } - [Theory] - [InlineData(LengthUnit.Undefined, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] - [InlineData(LengthUnit.Meter, MassUnit.Undefined, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] - [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Undefined, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] - [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Undefined, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] - [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Undefined, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] - [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Undefined, LuminousIntensityUnit.Candela)] - [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Undefined)] - public void ConstructorThrowsExceptionWithUndefinedUnits(LengthUnit length, MassUnit mass, DurationUnit time, ElectricCurrentUnit current, - TemperatureUnit temperature, AmountOfSubstanceUnit amount, LuminousIntensityUnit luminousIntensity) - { - var baseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, - ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); - - Assert.Throws(() => new BaseUnits(length, mass, time, current, temperature, amount, luminousIntensity)); - } - [Fact] public void EqualsObjectIsImplementedCorrectly() { @@ -68,7 +51,12 @@ public void EqualsObjectIsImplementedCorrectly() var baseUnits2 = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + var baseUnits3 = new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.DegreeFahrenheit, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + Assert.True(baseUnits1.Equals((object)baseUnits2)); + Assert.False(baseUnits1.Equals((object)baseUnits3)); + Assert.False(baseUnits1.Equals("Some object.")); Assert.False(baseUnits1.Equals((IFormatProvider)null)); } @@ -82,8 +70,15 @@ public void EqualsBaseUnitsIsImplementedCorrectly() var baseUnits2 = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + var baseUnits3 = new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.DegreeFahrenheit, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + Assert.True(baseUnits1.Equals(baseUnits2)); Assert.True(baseUnits2.Equals(baseUnits1)); + + Assert.False(baseUnits1.Equals(baseUnits3)); + Assert.False(baseUnits3.Equals(baseUnits1)); + Assert.False(baseUnits1.Equals(null)); } @@ -96,7 +91,15 @@ public void EqualityOperatorIsImplementedCorrectly() var baseUnits2 = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + var baseUnits3 = new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.DegreeFahrenheit, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + Assert.True(baseUnits1 == baseUnits2); + Assert.True(baseUnits2 == baseUnits1); + + Assert.False(baseUnits1 == baseUnits3); + Assert.False(baseUnits3 == baseUnits1); + Assert.False(baseUnits1 == null); Assert.False(null == baseUnits1); } @@ -107,10 +110,18 @@ public void InequalityOperatorIsImplementedCorrectly() var baseUnits1 = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); - var baseUnits2 = new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, + var baseUnits2 = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + var baseUnits3 = new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.DegreeFahrenheit, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); - Assert.True(baseUnits1 != baseUnits2); + Assert.False(baseUnits1 != baseUnits2); + Assert.False(baseUnits2 != baseUnits1); + + Assert.True(baseUnits1 != baseUnits3); + Assert.True(baseUnits3 != baseUnits1); + Assert.True(baseUnits1 != null); Assert.True(null != baseUnits1); } diff --git a/UnitsNet.Tests/UnitSystemTests.cs b/UnitsNet.Tests/UnitSystemTests.cs index 7405451997..6ffd6a6190 100644 --- a/UnitsNet.Tests/UnitSystemTests.cs +++ b/UnitsNet.Tests/UnitSystemTests.cs @@ -19,6 +19,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. +using System; using UnitsNet.Units; using Xunit; @@ -27,14 +28,119 @@ namespace UnitsNet.Tests public class UnitSystemTests { [Fact] - public void Constructor() + public void ConstructorImplementedProperly() { - var siBaseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + var baseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); - var unitSystem = new UnitSystem(siBaseUnits); + var unitSystem = new UnitSystem(baseUnits); + + Assert.Equal(unitSystem.BaseUnits, baseUnits); + } + + [Fact] + public void ConstructorThrowsArgumentNullExceptionForNullBaseUnits() + { + Assert.Throws(() => new UnitSystem(null)); + } + + [Theory] + [InlineData(LengthUnit.Undefined, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] + [InlineData(LengthUnit.Meter, MassUnit.Undefined, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Undefined, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Undefined, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Undefined, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Undefined, LuminousIntensityUnit.Candela)] + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Undefined)] + public void ConstructorThrowsArgumentExceptionWithUndefinedUnits(LengthUnit length, MassUnit mass, DurationUnit time, ElectricCurrentUnit current, + TemperatureUnit temperature, AmountOfSubstanceUnit amount, LuminousIntensityUnit luminousIntensity) + { + var baseUnits = new BaseUnits(length, mass, time, current, temperature, amount, luminousIntensity); + Assert.Throws(() => new UnitSystem(baseUnits)); + } + + [Fact] + public void EqualsObjectIsImplementedCorrectly() + { + var unitSystem1 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + var unitSystem2 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + var unitSystem3 = new UnitSystem(new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.DegreeFahrenheit, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + Assert.True(unitSystem1.Equals((object)unitSystem2)); + Assert.False(unitSystem1.Equals((object)unitSystem3)); + + Assert.False(unitSystem1.Equals("Some object.")); + Assert.False(unitSystem1.Equals((IFormatProvider)null)); + } + + [Fact] + public void EqualsUnitSystemIsImplementedCorrectly() + { + var unitSystem1 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + var unitSystem2 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + var unitSystem3 = new UnitSystem(new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.DegreeFahrenheit, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + Assert.True(unitSystem1.Equals(unitSystem2)); + Assert.True(unitSystem2.Equals(unitSystem1)); + + Assert.False(unitSystem1.Equals(unitSystem3)); + Assert.False(unitSystem3.Equals(unitSystem1)); + + Assert.False(unitSystem1.Equals(null)); + } + + [Fact] + public void EqualityOperatorIsImplementedCorrectly() + { + var unitSystem1 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + var unitSystem2 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + var unitSystem3 = new UnitSystem(new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.DegreeFahrenheit, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + Assert.True(unitSystem1 == unitSystem2); + Assert.True(unitSystem2 == unitSystem1); + + Assert.False(unitSystem1 == unitSystem3); + Assert.False(unitSystem3 == unitSystem1); + + Assert.False(unitSystem1 == null); + Assert.False(null == unitSystem1); + } + + [Fact] + public void InequalityOperatorIsImplementedCorrectly() + { + var unitSystem1 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + var unitSystem2 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + var unitSystem3 = new UnitSystem(new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.DegreeFahrenheit, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + Assert.False(unitSystem1 != unitSystem2); + Assert.False(unitSystem2 != unitSystem1); + + Assert.True(unitSystem1 != unitSystem3); + Assert.True(unitSystem3 != unitSystem1); - Assert.Equal(unitSystem.BaseUnits, siBaseUnits); + Assert.True(unitSystem1 != null); + Assert.True(null != unitSystem1); } [Fact] diff --git a/UnitsNet/BaseUnits.cs b/UnitsNet/BaseUnits.cs index 5d85178348..196b400430 100644 --- a/UnitsNet/BaseUnits.cs +++ b/UnitsNet/BaseUnits.cs @@ -25,11 +25,15 @@ namespace UnitsNet { +#if !WINDOWS_UWP + public sealed partial class BaseUnits : IEquatable { } +#endif + /// /// Represents the base units for a quantity. All quantities, both base and derived, can be /// represented by a combination of these seven base units. /// - public sealed class BaseUnits : IEquatable + public sealed partial class BaseUnits { /// /// Creates an instance of if the base units class that represents the base units for a quantity. @@ -42,25 +46,9 @@ public sealed class BaseUnits : IEquatable /// The temperature unit (Θ). /// The amount of substance unit (N). /// The luminous intensity unit (J). - /// Throws an ArgumentException if any of the units are undefined. public BaseUnits(LengthUnit length, MassUnit mass, DurationUnit time, ElectricCurrentUnit current, TemperatureUnit temperature, AmountOfSubstanceUnit amount, LuminousIntensityUnit luminousIntensity) { - if(length == LengthUnit.Undefined) - throw new ArgumentException("The given length unit is undefined.", nameof(length)); - if(mass == MassUnit.Undefined) - throw new ArgumentException("The given mass unit is undefined.", nameof(mass)); - if(time == DurationUnit.Undefined) - throw new ArgumentException("The given time unit is undefined.", nameof(time)); - if(current == ElectricCurrentUnit.Undefined) - throw new ArgumentException("The given electric current unit is undefined.", nameof(current)); - if(temperature == TemperatureUnit.Undefined) - throw new ArgumentException("The given temperature unit is undefined.", nameof(temperature)); - if(amount == AmountOfSubstanceUnit.Undefined) - throw new ArgumentException("The given amount of substance unit is undefined.", nameof(amount)); - if(luminousIntensity == LuminousIntensityUnit.Undefined) - throw new ArgumentException("The given luminous intensity unit is undefined.", nameof(luminousIntensity)); - Length = length; Mass = mass; Time = time; @@ -84,6 +72,9 @@ public override bool Equals(object obj) /// /// The other instance to check if equal to. /// True if equal, otherwise false. +#if WINDOWS_UWP + [Windows.Foundation.Metadata.DefaultOverload] +#endif public bool Equals(BaseUnits other) { if(other is null) diff --git a/UnitsNet/UnitSystem.cs b/UnitsNet/UnitSystem.cs index 9047fab101..7a1c8be10c 100644 --- a/UnitsNet/UnitSystem.cs +++ b/UnitsNet/UnitSystem.cs @@ -25,14 +25,94 @@ namespace UnitsNet { - public class UnitSystem +#if !WINDOWS_UWP + public sealed partial class UnitSystem : IEquatable { } +#endif + + public sealed partial class UnitSystem { + /// + /// Creates an instance of a unit system with the specified base units. + /// + /// The base units for the unit system. public UnitSystem(BaseUnits baseUnits) { + if(baseUnits is null) + throw new ArgumentNullException(nameof(baseUnits)); + + if(baseUnits.Length == LengthUnit.Undefined) + throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits)); + if(baseUnits.Mass == MassUnit.Undefined) + throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits)); + if(baseUnits.Time == DurationUnit.Undefined) + throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits)); + if(baseUnits.Current == ElectricCurrentUnit.Undefined) + throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits)); + if(baseUnits.Temperature == TemperatureUnit.Undefined) + throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits)); + if(baseUnits.Amount == AmountOfSubstanceUnit.Undefined) + throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits)); + if(baseUnits.LuminousIntensity == LuminousIntensityUnit.Undefined) + throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits)); + BaseUnits = baseUnits; } - public BaseUnits BaseUnits { get; } + /// + public override bool Equals(object obj) + { + if(obj is null || !(obj is UnitSystem)) + return false; + + return Equals((UnitSystem)obj); + } + +#if WINDOWS_UWP + [Windows.Foundation.Metadata.DefaultOverload] +#endif + public bool Equals(UnitSystem other) + { + if(other is null) + return false; + + return BaseUnits.Equals(other.BaseUnits); + } + +#if !WINDOWS_UWP + + /// + /// Checks if this instance is equal to another. + /// + /// The left instance. + /// The right instance. + /// True if equal, otherwise false. + /// + public static bool operator ==(UnitSystem left, UnitSystem right) + { + return left?.Equals(right) == true; + } + + /// + /// Checks if this instance is equal to another. + /// + /// The left instance. + /// The right instance. + /// True if equal, otherwise false. + /// + public static bool operator !=(UnitSystem left, UnitSystem right) + { + return !(left == right); + } + +#endif + + /// + public override int GetHashCode() + { + return new {BaseUnits}.GetHashCode(); + } + + public BaseUnits BaseUnits{ get; } private static readonly BaseUnits SIBaseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); @@ -40,6 +120,6 @@ public UnitSystem(BaseUnits baseUnits) /// /// Gets the SI unit system. /// - public static UnitSystem SI { get; } = new UnitSystem(SIBaseUnits); + public static UnitSystem SI{ get; } = new UnitSystem(SIBaseUnits); } } From 083070639bc02463bebe6a24d6050644f455f597 Mon Sep 17 00:00:00 2001 From: Tristan Milnthorp Date: Tue, 16 Oct 2018 13:33:08 -0400 Subject: [PATCH 4/7] Adding base/derived check to BaseDimensions --- UnitsNet.Tests/BaseDimensionsTests.cs | 50 ++++++++++++++++++++++++++- UnitsNet/BaseDimensions.cs | 13 +++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/UnitsNet.Tests/BaseDimensionsTests.cs b/UnitsNet.Tests/BaseDimensionsTests.cs index 0f6f00e675..d5185ce2f6 100644 --- a/UnitsNet.Tests/BaseDimensionsTests.cs +++ b/UnitsNet.Tests/BaseDimensionsTests.cs @@ -5,7 +5,55 @@ namespace UnitsNet.Tests public class BaseDimensionsTests { [Fact] - public void EqualityWorksAsExpected() + 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 IsBaseImplementedSuccessfully(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()); + } + + [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()); + } + + [Fact] + public void EqualsWorksAsExpected() { var baseDimensions1 = new BaseDimensions(1, 2, 3, 4, 5, 6, 7); var baseDimensions2 = new BaseDimensions(1, 2, 3, 4, 5, 6, 7); diff --git a/UnitsNet/BaseDimensions.cs b/UnitsNet/BaseDimensions.cs index 6e62123e0f..2c23847bfc 100644 --- a/UnitsNet/BaseDimensions.cs +++ b/UnitsNet/BaseDimensions.cs @@ -21,6 +21,7 @@ using System; using System.Text; +using System.Linq; namespace UnitsNet { @@ -41,6 +42,18 @@ public BaseDimensions(int length, int mass, int time, int current, int temperatu LuminousIntensity = luminousIntensity; } + 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; + } + + public bool IsDerived() + { + return !IsBase(); + } + /// public override bool Equals(object obj) { From 812dcfb017f6833aa3d28a810c2c1d2c56873d40 Mon Sep 17 00:00:00 2001 From: Tristan Milnthorp Date: Tue, 16 Oct 2018 13:54:35 -0400 Subject: [PATCH 5/7] Fix for equality operator when both left and right are null --- UnitsNet.Tests/BaseDimensionsTests.cs | 5 +++++ UnitsNet.Tests/BaseUnitsTests.cs | 5 +++++ UnitsNet.Tests/UnitSystemTests.cs | 5 +++++ UnitsNet/BaseDimensions.cs | 2 +- UnitsNet/BaseUnits.cs | 2 +- UnitsNet/UnitSystem.cs | 2 +- 6 files changed, 18 insertions(+), 3 deletions(-) diff --git a/UnitsNet.Tests/BaseDimensionsTests.cs b/UnitsNet.Tests/BaseDimensionsTests.cs index 33321a7bda..2dc4c95ce8 100644 --- a/UnitsNet.Tests/BaseDimensionsTests.cs +++ b/UnitsNet.Tests/BaseDimensionsTests.cs @@ -80,6 +80,11 @@ public void EqualityOperatorsWorkAsExpected() Assert.False(baseDimensions2 == null); Assert.False(null == baseDimensions2); + + BaseDimensions nullBaseDimensions1 = null; + BaseDimensions nullBaseDimensions2 = null; + + Assert.True(nullBaseDimensions1 == nullBaseDimensions2); } [Fact] diff --git a/UnitsNet.Tests/BaseUnitsTests.cs b/UnitsNet.Tests/BaseUnitsTests.cs index 7bcf7229ae..6617f85f88 100644 --- a/UnitsNet.Tests/BaseUnitsTests.cs +++ b/UnitsNet.Tests/BaseUnitsTests.cs @@ -102,6 +102,11 @@ public void EqualityOperatorIsImplementedCorrectly() Assert.False(baseUnits1 == null); Assert.False(null == baseUnits1); + + BaseUnits nullBaseUnits1 = null; + BaseUnits nullBaseUnits2 = null; + + Assert.True(nullBaseUnits1 == nullBaseUnits2); } [Fact] diff --git a/UnitsNet.Tests/UnitSystemTests.cs b/UnitsNet.Tests/UnitSystemTests.cs index 6ffd6a6190..c6970c00e6 100644 --- a/UnitsNet.Tests/UnitSystemTests.cs +++ b/UnitsNet.Tests/UnitSystemTests.cs @@ -119,6 +119,11 @@ public void EqualityOperatorIsImplementedCorrectly() Assert.False(unitSystem1 == null); Assert.False(null == unitSystem1); + + UnitSystem nullUnitSystem1 = null; + UnitSystem nullUnitSystem2 = null; + + Assert.True(nullUnitSystem1 == nullUnitSystem2); } [Fact] diff --git a/UnitsNet/BaseDimensions.cs b/UnitsNet/BaseDimensions.cs index d5c8a3e002..4da59f07df 100644 --- a/UnitsNet/BaseDimensions.cs +++ b/UnitsNet/BaseDimensions.cs @@ -127,7 +127,7 @@ public BaseDimensions Divide(BaseDimensions right) /// True if equal. public static bool operator ==(BaseDimensions left, BaseDimensions right) { - return left?.Equals(right) == true; + return left is null ? right is null : left.Equals(right); } /// diff --git a/UnitsNet/BaseUnits.cs b/UnitsNet/BaseUnits.cs index 196b400430..16949c0ff6 100644 --- a/UnitsNet/BaseUnits.cs +++ b/UnitsNet/BaseUnits.cs @@ -106,7 +106,7 @@ public override int GetHashCode() /// public static bool operator ==(BaseUnits left, BaseUnits right) { - return left?.Equals(right) == true; + return left is null ? right is null : left.Equals(right); } /// diff --git a/UnitsNet/UnitSystem.cs b/UnitsNet/UnitSystem.cs index 7a1c8be10c..695626b763 100644 --- a/UnitsNet/UnitSystem.cs +++ b/UnitsNet/UnitSystem.cs @@ -89,7 +89,7 @@ public bool Equals(UnitSystem other) /// public static bool operator ==(UnitSystem left, UnitSystem right) { - return left?.Equals(right) == true; + return left is null ? right is null : left.Equals(right); } /// From 53aafef774123a351b429e3d01662d65ac11c025 Mon Sep 17 00:00:00 2001 From: Tristan Milnthorp Date: Tue, 16 Oct 2018 13:56:55 -0400 Subject: [PATCH 6/7] Better add test for != too, in case someone changes the != implementation --- UnitsNet.Tests/BaseDimensionsTests.cs | 5 +++++ UnitsNet.Tests/BaseUnitsTests.cs | 5 +++++ UnitsNet.Tests/UnitSystemTests.cs | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/UnitsNet.Tests/BaseDimensionsTests.cs b/UnitsNet.Tests/BaseDimensionsTests.cs index 2dc4c95ce8..26a81fad36 100644 --- a/UnitsNet.Tests/BaseDimensionsTests.cs +++ b/UnitsNet.Tests/BaseDimensionsTests.cs @@ -101,6 +101,11 @@ public void InequalityOperatorsWorkAsExpected() Assert.True(baseDimensions2 != null); Assert.True(null != baseDimensions2); + + BaseDimensions nullBaseDimensions1 = null; + BaseDimensions nullBaseDimensions2 = null; + + Assert.False(nullBaseDimensions1 != nullBaseDimensions2); } [Fact] diff --git a/UnitsNet.Tests/BaseUnitsTests.cs b/UnitsNet.Tests/BaseUnitsTests.cs index 6617f85f88..16dacb1630 100644 --- a/UnitsNet.Tests/BaseUnitsTests.cs +++ b/UnitsNet.Tests/BaseUnitsTests.cs @@ -129,6 +129,11 @@ public void InequalityOperatorIsImplementedCorrectly() Assert.True(baseUnits1 != null); Assert.True(null != baseUnits1); + + BaseUnits nullBaseUnits1 = null; + BaseUnits nullBaseUnits2 = null; + + Assert.False(nullBaseUnits1 != nullBaseUnits2); } [Fact] diff --git a/UnitsNet.Tests/UnitSystemTests.cs b/UnitsNet.Tests/UnitSystemTests.cs index c6970c00e6..2a52e693c5 100644 --- a/UnitsNet.Tests/UnitSystemTests.cs +++ b/UnitsNet.Tests/UnitSystemTests.cs @@ -146,6 +146,11 @@ public void InequalityOperatorIsImplementedCorrectly() Assert.True(unitSystem1 != null); Assert.True(null != unitSystem1); + + UnitSystem nullUnitSystem1 = null; + UnitSystem nullUnitSystem2 = null; + + Assert.False(nullUnitSystem1 != nullUnitSystem2); } [Fact] From 466f68a045e7802a6bc002091303abe4778ec077 Mon Sep 17 00:00:00 2001 From: Tristan Milnthorp Date: Tue, 16 Oct 2018 16:31:32 -0400 Subject: [PATCH 7/7] Adding doc. Making tests cleaner. Small method name changes, --- UnitsNet.Tests/BaseDimensionsTests.cs | 12 ++-- UnitsNet.Tests/BaseUnitsTests.cs | 91 ++++++++++----------------- UnitsNet/BaseDimensions.cs | 14 ++++- 3 files changed, 49 insertions(+), 68 deletions(-) diff --git a/UnitsNet.Tests/BaseDimensionsTests.cs b/UnitsNet.Tests/BaseDimensionsTests.cs index 26a81fad36..cecb54327c 100644 --- a/UnitsNet.Tests/BaseDimensionsTests.cs +++ b/UnitsNet.Tests/BaseDimensionsTests.cs @@ -28,13 +28,13 @@ public void ConstructorImplementedCorrectly() [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 IsBaseImplementedSuccessfully(int length, int mass, int time, int current, int temperature, int amount, int luminousIntensity) + public void IsBaseQuantityImplementedProperly(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.True(baseDimensions.IsBaseQuantity()); + Assert.False(derivedDimensions.IsBaseQuantity()); } [Theory] @@ -45,13 +45,13 @@ public void IsBaseImplementedSuccessfully(int length, int mass, int time, int cu [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) + public void IsDerivedQuantityImplementedProperly(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.IsDerivedQuantity()); + Assert.True(derivedDimensions.IsDerivedQuantity()); } [Fact] diff --git a/UnitsNet.Tests/BaseUnitsTests.cs b/UnitsNet.Tests/BaseUnitsTests.cs index 16dacb1630..ad3a31370d 100644 --- a/UnitsNet.Tests/BaseUnitsTests.cs +++ b/UnitsNet.Tests/BaseUnitsTests.cs @@ -27,6 +27,15 @@ namespace UnitsNet.Tests { public class BaseUnitsTests { + private static BaseUnits siBaseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + private static BaseUnits siBaseUnitsCopy = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + private static BaseUnits nonSiBaseUnits = new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.DegreeFahrenheit, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + [Fact] public void ConstructorSetsUnitsProperly() { @@ -45,63 +54,36 @@ public void ConstructorSetsUnitsProperly() [Fact] public void EqualsObjectIsImplementedCorrectly() { - var baseUnits1 = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, - ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + Assert.True(siBaseUnits.Equals((object)siBaseUnitsCopy)); + Assert.False(siBaseUnits.Equals((object)nonSiBaseUnits)); - var baseUnits2 = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, - ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); - - var baseUnits3 = new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, - ElectricCurrentUnit.Ampere, TemperatureUnit.DegreeFahrenheit, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); - - Assert.True(baseUnits1.Equals((object)baseUnits2)); - Assert.False(baseUnits1.Equals((object)baseUnits3)); - - Assert.False(baseUnits1.Equals("Some object.")); - Assert.False(baseUnits1.Equals((IFormatProvider)null)); + Assert.False(siBaseUnits.Equals("Some object.")); + Assert.False(siBaseUnits.Equals((IFormatProvider)null)); } [Fact] public void EqualsBaseUnitsIsImplementedCorrectly() { - var baseUnits1 = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, - ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); - - var baseUnits2 = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, - ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + Assert.True(siBaseUnits.Equals(siBaseUnitsCopy)); + Assert.True(siBaseUnitsCopy.Equals(siBaseUnits)); - var baseUnits3 = new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, - ElectricCurrentUnit.Ampere, TemperatureUnit.DegreeFahrenheit, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + Assert.False(siBaseUnits.Equals(nonSiBaseUnits)); + Assert.False(nonSiBaseUnits.Equals(siBaseUnits)); - Assert.True(baseUnits1.Equals(baseUnits2)); - Assert.True(baseUnits2.Equals(baseUnits1)); - - Assert.False(baseUnits1.Equals(baseUnits3)); - Assert.False(baseUnits3.Equals(baseUnits1)); - - Assert.False(baseUnits1.Equals(null)); + Assert.False(siBaseUnits.Equals(null)); } [Fact] public void EqualityOperatorIsImplementedCorrectly() { - var baseUnits1 = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, - ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); - - var baseUnits2 = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, - ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); - - var baseUnits3 = new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, - ElectricCurrentUnit.Ampere, TemperatureUnit.DegreeFahrenheit, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + Assert.True(siBaseUnits == siBaseUnitsCopy); + Assert.True(siBaseUnitsCopy == siBaseUnits); - Assert.True(baseUnits1 == baseUnits2); - Assert.True(baseUnits2 == baseUnits1); + Assert.False(siBaseUnits == nonSiBaseUnits); + Assert.False(nonSiBaseUnits == siBaseUnits); - Assert.False(baseUnits1 == baseUnits3); - Assert.False(baseUnits3 == baseUnits1); - - Assert.False(baseUnits1 == null); - Assert.False(null == baseUnits1); + Assert.False(siBaseUnits == null); + Assert.False(null == siBaseUnits); BaseUnits nullBaseUnits1 = null; BaseUnits nullBaseUnits2 = null; @@ -112,23 +94,14 @@ public void EqualityOperatorIsImplementedCorrectly() [Fact] public void InequalityOperatorIsImplementedCorrectly() { - var baseUnits1 = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, - ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); - - var baseUnits2 = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, - ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); - - var baseUnits3 = new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, - ElectricCurrentUnit.Ampere, TemperatureUnit.DegreeFahrenheit, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); - - Assert.False(baseUnits1 != baseUnits2); - Assert.False(baseUnits2 != baseUnits1); + Assert.False(siBaseUnits != siBaseUnitsCopy); + Assert.False(siBaseUnitsCopy != siBaseUnits); - Assert.True(baseUnits1 != baseUnits3); - Assert.True(baseUnits3 != baseUnits1); + Assert.True(siBaseUnits != nonSiBaseUnits); + Assert.True(nonSiBaseUnits != siBaseUnits); - Assert.True(baseUnits1 != null); - Assert.True(null != baseUnits1); + Assert.True(siBaseUnits != null); + Assert.True(null != siBaseUnits); BaseUnits nullBaseUnits1 = null; BaseUnits nullBaseUnits2 = null; @@ -139,10 +112,10 @@ public void InequalityOperatorIsImplementedCorrectly() [Fact] public void ToStringGivesExpectedResult() { - var baseUnits1 = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + var siBaseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); - Assert.Equal("[Length]: m, [Mass]: kg, [Time]: s, [Current]: A, [Temperature]: K, [Amount]: mol, [LuminousIntensity]: cd", baseUnits1.ToString()); + Assert.Equal("[Length]: m, [Mass]: kg, [Time]: s, [Current]: A, [Temperature]: K, [Amount]: mol, [LuminousIntensity]: cd", siBaseUnits.ToString()); } } } diff --git a/UnitsNet/BaseDimensions.cs b/UnitsNet/BaseDimensions.cs index 4da59f07df..6e82d94ab1 100644 --- a/UnitsNet/BaseDimensions.cs +++ b/UnitsNet/BaseDimensions.cs @@ -42,16 +42,24 @@ public BaseDimensions(int length, int mass, int time, int current, int temperatu LuminousIntensity = luminousIntensity; } - public bool IsBase() + /// + /// Checks if the dimensions represent a base quantity. + /// + /// True if the dimensions represent a base quantity, otherwise false. + public bool IsBaseQuantity() { var dimensionsArray = new int[]{Length, Mass, Time, Current, Temperature, Amount, LuminousIntensity}; bool onlyOneEqualsOne = dimensionsArray.Where(dimension => dimension == 1).Take(2).Count() == 1; return onlyOneEqualsOne; } - public bool IsDerived() + /// + /// Checks the dimensions represent a derived quantity. + /// + /// True if the dimensions represent a derived quantity, otherwise false. + public bool IsDerivedQuantity() { - return !IsBase(); + return !IsBaseQuantity(); } ///