-
Notifications
You must be signed in to change notification settings - Fork 397
Si unit system #524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Si unit system #524
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a110555
f3c90ed
21d8459
Fixing BaseUnits to not throw exception for undefined units. The Leng…
tmilnthorp 0830706
Adding base/derived check to BaseDimensions
tmilnthorp 8fe2088
Merge remote-tracking branch 'angularsen/v4' into SIUnitSystem
tmilnthorp 812dcfb
53aafef
466f68a
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright (c) 2013 Andreas Gullberg Larsen ([email protected]). | ||
// 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 UnitsNet.Units; | ||
using Xunit; | ||
|
||
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() | ||
{ | ||
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); | ||
} | ||
|
||
[Fact] | ||
public void EqualsObjectIsImplementedCorrectly() | ||
{ | ||
Assert.True(siBaseUnits.Equals((object)siBaseUnitsCopy)); | ||
Assert.False(siBaseUnits.Equals((object)nonSiBaseUnits)); | ||
|
||
Assert.False(siBaseUnits.Equals("Some object.")); | ||
Assert.False(siBaseUnits.Equals((IFormatProvider)null)); | ||
} | ||
|
||
[Fact] | ||
public void EqualsBaseUnitsIsImplementedCorrectly() | ||
{ | ||
Assert.True(siBaseUnits.Equals(siBaseUnitsCopy)); | ||
Assert.True(siBaseUnitsCopy.Equals(siBaseUnits)); | ||
|
||
Assert.False(siBaseUnits.Equals(nonSiBaseUnits)); | ||
Assert.False(nonSiBaseUnits.Equals(siBaseUnits)); | ||
|
||
Assert.False(siBaseUnits.Equals(null)); | ||
} | ||
|
||
[Fact] | ||
public void EqualityOperatorIsImplementedCorrectly() | ||
{ | ||
Assert.True(siBaseUnits == siBaseUnitsCopy); | ||
Assert.True(siBaseUnitsCopy == siBaseUnits); | ||
|
||
Assert.False(siBaseUnits == nonSiBaseUnits); | ||
Assert.False(nonSiBaseUnits == siBaseUnits); | ||
|
||
Assert.False(siBaseUnits == null); | ||
Assert.False(null == siBaseUnits); | ||
|
||
BaseUnits nullBaseUnits1 = null; | ||
BaseUnits nullBaseUnits2 = null; | ||
|
||
Assert.True(nullBaseUnits1 == nullBaseUnits2); | ||
} | ||
|
||
[Fact] | ||
public void InequalityOperatorIsImplementedCorrectly() | ||
{ | ||
Assert.False(siBaseUnits != siBaseUnitsCopy); | ||
Assert.False(siBaseUnitsCopy != siBaseUnits); | ||
|
||
Assert.True(siBaseUnits != nonSiBaseUnits); | ||
Assert.True(nonSiBaseUnits != siBaseUnits); | ||
|
||
Assert.True(siBaseUnits != null); | ||
Assert.True(null != siBaseUnits); | ||
|
||
BaseUnits nullBaseUnits1 = null; | ||
BaseUnits nullBaseUnits2 = null; | ||
|
||
Assert.False(nullBaseUnits1 != nullBaseUnits2); | ||
} | ||
|
||
[Fact] | ||
public void ToStringGivesExpectedResult() | ||
{ | ||
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", siBaseUnits.ToString()); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
// Copyright (c) 2013 Andreas Gullberg Larsen ([email protected]). | ||
// 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 UnitsNet.Units; | ||
using Xunit; | ||
|
||
namespace UnitsNet.Tests | ||
{ | ||
public class UnitSystemTests | ||
{ | ||
[Fact] | ||
public void ConstructorImplementedProperly() | ||
{ | ||
var baseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, | ||
ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); | ||
|
||
var unitSystem = new UnitSystem(baseUnits); | ||
|
||
Assert.Equal(unitSystem.BaseUnits, baseUnits); | ||
} | ||
|
||
[Fact] | ||
public void ConstructorThrowsArgumentNullExceptionForNullBaseUnits() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => 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<ArgumentException>(() => 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); | ||
|
||
UnitSystem nullUnitSystem1 = null; | ||
UnitSystem nullUnitSystem2 = null; | ||
|
||
Assert.True(nullUnitSystem1 == nullUnitSystem2); | ||
} | ||
|
||
[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.True(unitSystem1 != null); | ||
Assert.True(null != unitSystem1); | ||
|
||
UnitSystem nullUnitSystem1 = null; | ||
UnitSystem nullUnitSystem2 = null; | ||
|
||
Assert.False(nullUnitSystem1 != nullUnitSystem2); | ||
} | ||
|
||
[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); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.