|
| 1 | +// Copyright (c) 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). |
| 2 | +// https://github.com/angularsen/UnitsNet |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in |
| 12 | +// all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +// THE SOFTWARE. |
| 21 | + |
| 22 | +using System; |
| 23 | +using UnitsNet.Units; |
| 24 | +using Xunit; |
| 25 | + |
| 26 | +namespace UnitsNet.Tests |
| 27 | +{ |
| 28 | + public class UnitSystemTests |
| 29 | + { |
| 30 | + [Fact] |
| 31 | + public void ConstructorImplementedProperly() |
| 32 | + { |
| 33 | + var baseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, |
| 34 | + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); |
| 35 | + |
| 36 | + var unitSystem = new UnitSystem(baseUnits); |
| 37 | + |
| 38 | + Assert.Equal(unitSystem.BaseUnits, baseUnits); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void ConstructorThrowsArgumentNullExceptionForNullBaseUnits() |
| 43 | + { |
| 44 | + Assert.Throws<ArgumentNullException>(() => new UnitSystem(null)); |
| 45 | + } |
| 46 | + |
| 47 | + [Theory] |
| 48 | + [InlineData(LengthUnit.Undefined, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] |
| 49 | + [InlineData(LengthUnit.Meter, MassUnit.Undefined, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] |
| 50 | + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Undefined, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] |
| 51 | + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Undefined, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] |
| 52 | + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Undefined, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] |
| 53 | + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Undefined, LuminousIntensityUnit.Candela)] |
| 54 | + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Undefined)] |
| 55 | + public void ConstructorThrowsArgumentExceptionWithUndefinedUnits(LengthUnit length, MassUnit mass, DurationUnit time, ElectricCurrentUnit current, |
| 56 | + TemperatureUnit temperature, AmountOfSubstanceUnit amount, LuminousIntensityUnit luminousIntensity) |
| 57 | + { |
| 58 | + var baseUnits = new BaseUnits(length, mass, time, current, temperature, amount, luminousIntensity); |
| 59 | + Assert.Throws<ArgumentException>(() => new UnitSystem(baseUnits)); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public void EqualsObjectIsImplementedCorrectly() |
| 64 | + { |
| 65 | + var unitSystem1 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, |
| 66 | + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); |
| 67 | + |
| 68 | + var unitSystem2 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, |
| 69 | + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); |
| 70 | + |
| 71 | + var unitSystem3 = new UnitSystem(new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, |
| 72 | + ElectricCurrentUnit.Ampere, TemperatureUnit.DegreeFahrenheit, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); |
| 73 | + |
| 74 | + Assert.True(unitSystem1.Equals((object)unitSystem2)); |
| 75 | + Assert.False(unitSystem1.Equals((object)unitSystem3)); |
| 76 | + |
| 77 | + Assert.False(unitSystem1.Equals("Some object.")); |
| 78 | + Assert.False(unitSystem1.Equals((IFormatProvider)null)); |
| 79 | + } |
| 80 | + |
| 81 | + [Fact] |
| 82 | + public void EqualsUnitSystemIsImplementedCorrectly() |
| 83 | + { |
| 84 | + var unitSystem1 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, |
| 85 | + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); |
| 86 | + |
| 87 | + var unitSystem2 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, |
| 88 | + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); |
| 89 | + |
| 90 | + var unitSystem3 = new UnitSystem(new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, |
| 91 | + ElectricCurrentUnit.Ampere, TemperatureUnit.DegreeFahrenheit, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); |
| 92 | + |
| 93 | + Assert.True(unitSystem1.Equals(unitSystem2)); |
| 94 | + Assert.True(unitSystem2.Equals(unitSystem1)); |
| 95 | + |
| 96 | + Assert.False(unitSystem1.Equals(unitSystem3)); |
| 97 | + Assert.False(unitSystem3.Equals(unitSystem1)); |
| 98 | + |
| 99 | + Assert.False(unitSystem1.Equals(null)); |
| 100 | + } |
| 101 | + |
| 102 | + [Fact] |
| 103 | + public void EqualityOperatorIsImplementedCorrectly() |
| 104 | + { |
| 105 | + var unitSystem1 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, |
| 106 | + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); |
| 107 | + |
| 108 | + var unitSystem2 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, |
| 109 | + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); |
| 110 | + |
| 111 | + var unitSystem3 = new UnitSystem(new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, |
| 112 | + ElectricCurrentUnit.Ampere, TemperatureUnit.DegreeFahrenheit, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); |
| 113 | + |
| 114 | + Assert.True(unitSystem1 == unitSystem2); |
| 115 | + Assert.True(unitSystem2 == unitSystem1); |
| 116 | + |
| 117 | + Assert.False(unitSystem1 == unitSystem3); |
| 118 | + Assert.False(unitSystem3 == unitSystem1); |
| 119 | + |
| 120 | + Assert.False(unitSystem1 == null); |
| 121 | + Assert.False(null == unitSystem1); |
| 122 | + |
| 123 | + UnitSystem nullUnitSystem1 = null; |
| 124 | + UnitSystem nullUnitSystem2 = null; |
| 125 | + |
| 126 | + Assert.True(nullUnitSystem1 == nullUnitSystem2); |
| 127 | + } |
| 128 | + |
| 129 | + [Fact] |
| 130 | + public void InequalityOperatorIsImplementedCorrectly() |
| 131 | + { |
| 132 | + var unitSystem1 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, |
| 133 | + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); |
| 134 | + |
| 135 | + var unitSystem2 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, |
| 136 | + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); |
| 137 | + |
| 138 | + var unitSystem3 = new UnitSystem(new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, |
| 139 | + ElectricCurrentUnit.Ampere, TemperatureUnit.DegreeFahrenheit, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); |
| 140 | + |
| 141 | + Assert.False(unitSystem1 != unitSystem2); |
| 142 | + Assert.False(unitSystem2 != unitSystem1); |
| 143 | + |
| 144 | + Assert.True(unitSystem1 != unitSystem3); |
| 145 | + Assert.True(unitSystem3 != unitSystem1); |
| 146 | + |
| 147 | + Assert.True(unitSystem1 != null); |
| 148 | + Assert.True(null != unitSystem1); |
| 149 | + |
| 150 | + UnitSystem nullUnitSystem1 = null; |
| 151 | + UnitSystem nullUnitSystem2 = null; |
| 152 | + |
| 153 | + Assert.False(nullUnitSystem1 != nullUnitSystem2); |
| 154 | + } |
| 155 | + |
| 156 | + [Fact] |
| 157 | + public void SIUnitSystemHasCorrectBaseUnits() |
| 158 | + { |
| 159 | + var siBaseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, |
| 160 | + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); |
| 161 | + |
| 162 | + Assert.Equal(LengthUnit.Meter, UnitSystem.SI.BaseUnits.Length); |
| 163 | + Assert.Equal(MassUnit.Kilogram, UnitSystem.SI.BaseUnits.Mass); |
| 164 | + Assert.Equal(DurationUnit.Second, UnitSystem.SI.BaseUnits.Time); |
| 165 | + Assert.Equal(ElectricCurrentUnit.Ampere, UnitSystem.SI.BaseUnits.Current); |
| 166 | + Assert.Equal(TemperatureUnit.Kelvin, UnitSystem.SI.BaseUnits.Temperature); |
| 167 | + Assert.Equal(AmountOfSubstanceUnit.Mole, UnitSystem.SI.BaseUnits.Amount); |
| 168 | + Assert.Equal(LuminousIntensityUnit.Candela, UnitSystem.SI.BaseUnits.LuminousIntensity); |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments