|
| 1 | +// Copyright © 2007 by Initial Force AS. All rights reserved. |
| 2 | +// https://github.com/InitialForce/SIUnits |
| 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 NUnit.Framework; |
| 24 | +using UnitsNet.Units; |
| 25 | + |
| 26 | +// Disable build warning CS1718: Comparison made to same variable; did you mean to compare something else? |
| 27 | +#pragma warning disable 1718 |
| 28 | + |
| 29 | +// ReSharper disable once CheckNamespace |
| 30 | +namespace UnitsNet.Tests |
| 31 | +{ |
| 32 | + /// <summary> |
| 33 | + /// Test of Ratio. |
| 34 | + /// </summary> |
| 35 | + [TestFixture] |
| 36 | + public abstract partial class RatioTestsBase |
| 37 | + { |
| 38 | + protected virtual double Delta { get { return 1E-5; } } |
| 39 | + |
| 40 | + public abstract double DecimalFractionsInOneDecimalFraction { get; } |
| 41 | + public abstract double PartsPerBillionsInOneDecimalFraction { get; } |
| 42 | + public abstract double PartsPerMillionsInOneDecimalFraction { get; } |
| 43 | + public abstract double PartsPerThousandsInOneDecimalFraction { get; } |
| 44 | + public abstract double PartsPerTrillionsInOneDecimalFraction { get; } |
| 45 | + public abstract double PercentsInOneDecimalFraction { get; } |
| 46 | + |
| 47 | + [Test] |
| 48 | + public void DecimalFractionToRatioUnits() |
| 49 | + { |
| 50 | + Ratio decimalfraction = Ratio.FromDecimalFractions(1); |
| 51 | + Assert.AreEqual(DecimalFractionsInOneDecimalFraction, decimalfraction.DecimalFractions, Delta); |
| 52 | + Assert.AreEqual(PartsPerBillionsInOneDecimalFraction, decimalfraction.PartsPerBillions, Delta); |
| 53 | + Assert.AreEqual(PartsPerMillionsInOneDecimalFraction, decimalfraction.PartsPerMillions, Delta); |
| 54 | + Assert.AreEqual(PartsPerThousandsInOneDecimalFraction, decimalfraction.PartsPerThousands, Delta); |
| 55 | + Assert.AreEqual(PartsPerTrillionsInOneDecimalFraction, decimalfraction.PartsPerTrillions, Delta); |
| 56 | + Assert.AreEqual(PercentsInOneDecimalFraction, decimalfraction.Percents, Delta); |
| 57 | + } |
| 58 | + |
| 59 | + [Test] |
| 60 | + public void FromValueAndUnit() |
| 61 | + { |
| 62 | + Assert.AreEqual(1, Ratio.From(1, RatioUnit.DecimalFraction).DecimalFractions, Delta); |
| 63 | + Assert.AreEqual(1, Ratio.From(1, RatioUnit.PartsPerBillion).PartsPerBillions, Delta); |
| 64 | + Assert.AreEqual(1, Ratio.From(1, RatioUnit.PartsPerMillion).PartsPerMillions, Delta); |
| 65 | + Assert.AreEqual(1, Ratio.From(1, RatioUnit.PartsPerThousand).PartsPerThousands, Delta); |
| 66 | + Assert.AreEqual(1, Ratio.From(1, RatioUnit.PartsPerTrillion).PartsPerTrillions, Delta); |
| 67 | + Assert.AreEqual(1, Ratio.From(1, RatioUnit.Percent).Percents, Delta); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + [Test] |
| 72 | + public void As() |
| 73 | + { |
| 74 | + var decimalfraction = Ratio.FromDecimalFractions(1); |
| 75 | + Assert.AreEqual(DecimalFractionsInOneDecimalFraction, decimalfraction.As(RatioUnit.DecimalFraction), Delta); |
| 76 | + Assert.AreEqual(PartsPerBillionsInOneDecimalFraction, decimalfraction.As(RatioUnit.PartsPerBillion), Delta); |
| 77 | + Assert.AreEqual(PartsPerMillionsInOneDecimalFraction, decimalfraction.As(RatioUnit.PartsPerMillion), Delta); |
| 78 | + Assert.AreEqual(PartsPerThousandsInOneDecimalFraction, decimalfraction.As(RatioUnit.PartsPerThousand), Delta); |
| 79 | + Assert.AreEqual(PartsPerTrillionsInOneDecimalFraction, decimalfraction.As(RatioUnit.PartsPerTrillion), Delta); |
| 80 | + Assert.AreEqual(PercentsInOneDecimalFraction, decimalfraction.As(RatioUnit.Percent), Delta); |
| 81 | + } |
| 82 | + |
| 83 | + [Test] |
| 84 | + public void ConversionRoundTrip() |
| 85 | + { |
| 86 | + Ratio decimalfraction = Ratio.FromDecimalFractions(1); |
| 87 | + Assert.AreEqual(1, Ratio.FromDecimalFractions(decimalfraction.DecimalFractions).DecimalFractions, Delta); |
| 88 | + Assert.AreEqual(1, Ratio.FromPartsPerBillions(decimalfraction.PartsPerBillions).DecimalFractions, Delta); |
| 89 | + Assert.AreEqual(1, Ratio.FromPartsPerMillions(decimalfraction.PartsPerMillions).DecimalFractions, Delta); |
| 90 | + Assert.AreEqual(1, Ratio.FromPartsPerThousands(decimalfraction.PartsPerThousands).DecimalFractions, Delta); |
| 91 | + Assert.AreEqual(1, Ratio.FromPartsPerTrillions(decimalfraction.PartsPerTrillions).DecimalFractions, Delta); |
| 92 | + Assert.AreEqual(1, Ratio.FromPercents(decimalfraction.Percents).DecimalFractions, Delta); |
| 93 | + } |
| 94 | + |
| 95 | + [Test] |
| 96 | + public void ArithmeticOperators() |
| 97 | + { |
| 98 | + Ratio v = Ratio.FromDecimalFractions(1); |
| 99 | + Assert.AreEqual(-1, -v.DecimalFractions, Delta); |
| 100 | + Assert.AreEqual(2, (Ratio.FromDecimalFractions(3)-v).DecimalFractions, Delta); |
| 101 | + Assert.AreEqual(2, (v + v).DecimalFractions, Delta); |
| 102 | + Assert.AreEqual(10, (v*10).DecimalFractions, Delta); |
| 103 | + Assert.AreEqual(10, (10*v).DecimalFractions, Delta); |
| 104 | + Assert.AreEqual(2, (Ratio.FromDecimalFractions(10)/5).DecimalFractions, Delta); |
| 105 | + Assert.AreEqual(2, Ratio.FromDecimalFractions(10)/Ratio.FromDecimalFractions(5), Delta); |
| 106 | + } |
| 107 | + |
| 108 | + [Test] |
| 109 | + public void ComparisonOperators() |
| 110 | + { |
| 111 | + Ratio oneDecimalFraction = Ratio.FromDecimalFractions(1); |
| 112 | + Ratio twoDecimalFractions = Ratio.FromDecimalFractions(2); |
| 113 | + |
| 114 | + Assert.True(oneDecimalFraction < twoDecimalFractions); |
| 115 | + Assert.True(oneDecimalFraction <= twoDecimalFractions); |
| 116 | + Assert.True(twoDecimalFractions > oneDecimalFraction); |
| 117 | + Assert.True(twoDecimalFractions >= oneDecimalFraction); |
| 118 | + |
| 119 | + Assert.False(oneDecimalFraction > twoDecimalFractions); |
| 120 | + Assert.False(oneDecimalFraction >= twoDecimalFractions); |
| 121 | + Assert.False(twoDecimalFractions < oneDecimalFraction); |
| 122 | + Assert.False(twoDecimalFractions <= oneDecimalFraction); |
| 123 | + } |
| 124 | + |
| 125 | + [Test] |
| 126 | + public void CompareToIsImplemented() |
| 127 | + { |
| 128 | + Ratio decimalfraction = Ratio.FromDecimalFractions(1); |
| 129 | + Assert.AreEqual(0, decimalfraction.CompareTo(decimalfraction)); |
| 130 | + Assert.Greater(decimalfraction.CompareTo(Ratio.Zero), 0); |
| 131 | + Assert.Less(Ratio.Zero.CompareTo(decimalfraction), 0); |
| 132 | + } |
| 133 | + |
| 134 | + [Test] |
| 135 | + [ExpectedException(typeof(ArgumentException))] |
| 136 | + public void CompareToThrowsOnTypeMismatch() |
| 137 | + { |
| 138 | + Ratio decimalfraction = Ratio.FromDecimalFractions(1); |
| 139 | +// ReSharper disable once ReturnValueOfPureMethodIsNotUsed |
| 140 | + decimalfraction.CompareTo(new object()); |
| 141 | + } |
| 142 | + |
| 143 | + [Test] |
| 144 | + [ExpectedException(typeof(ArgumentNullException))] |
| 145 | + public void CompareToThrowsOnNull() |
| 146 | + { |
| 147 | + Ratio decimalfraction = Ratio.FromDecimalFractions(1); |
| 148 | +// ReSharper disable once ReturnValueOfPureMethodIsNotUsed |
| 149 | + decimalfraction.CompareTo(null); |
| 150 | + } |
| 151 | + |
| 152 | + |
| 153 | + [Test] |
| 154 | + public void EqualityOperators() |
| 155 | + { |
| 156 | + Ratio a = Ratio.FromDecimalFractions(1); |
| 157 | + Ratio b = Ratio.FromDecimalFractions(2); |
| 158 | + |
| 159 | +// ReSharper disable EqualExpressionComparison |
| 160 | + Assert.True(a == a); |
| 161 | + Assert.True(a != b); |
| 162 | + |
| 163 | + Assert.False(a == b); |
| 164 | + Assert.False(a != a); |
| 165 | +// ReSharper restore EqualExpressionComparison |
| 166 | + } |
| 167 | + |
| 168 | + [Test] |
| 169 | + public void EqualsIsImplemented() |
| 170 | + { |
| 171 | + Ratio v = Ratio.FromDecimalFractions(1); |
| 172 | + Assert.IsTrue(v.Equals(Ratio.FromDecimalFractions(1))); |
| 173 | + Assert.IsFalse(v.Equals(Ratio.Zero)); |
| 174 | + } |
| 175 | + |
| 176 | + [Test] |
| 177 | + public void EqualsReturnsFalseOnTypeMismatch() |
| 178 | + { |
| 179 | + Ratio decimalfraction = Ratio.FromDecimalFractions(1); |
| 180 | + Assert.IsFalse(decimalfraction.Equals(new object())); |
| 181 | + } |
| 182 | + |
| 183 | + [Test] |
| 184 | + public void EqualsReturnsFalseOnNull() |
| 185 | + { |
| 186 | + Ratio decimalfraction = Ratio.FromDecimalFractions(1); |
| 187 | + Assert.IsFalse(decimalfraction.Equals(null)); |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments