|
1 |
| -using Xunit; |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Xunit; |
2 | 5 |
|
3 | 6 | namespace UnitsNet.Tests
|
4 | 7 | {
|
@@ -27,6 +30,120 @@ public void LengthEquals_GivenMaxError_ReturnsTrueIfWithinError()
|
27 | 30 | "Example of floating precision arithmetic that produces slightly different results.");
|
28 | 31 | Assert.True(Length.FromMeters(1 + 0.39).Equals(Length.FromMeters(1.39), smallError, ComparisonType.Relative), "But the difference is very small");
|
29 | 32 | }
|
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void HasMultiplicationOperator_GivenMassAndVolume_ReturnsFalse() |
| 36 | + { |
| 37 | + Assert.False(HasMultiplicationOperator(typeof(Mass), typeof(Volume))); |
| 38 | + Assert.DoesNotContain(typeof(Volume), GetMultipliers(typeof(Mass))); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void HasMultiplicationOperator_GivenDensityAndVolume_ReturnsTrue() |
| 43 | + { |
| 44 | + Assert.True(HasMultiplicationOperator(typeof(Density), typeof(Volume))); |
| 45 | + Assert.Contains(typeof(Volume), GetMultipliers(typeof(Density))); |
| 46 | + Assert.Equal(typeof(Mass), GetMultiplicationResult(typeof(Density), typeof(Volume))); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public void HasDivisionOperator_GivenDensityAndVolume_ReturnsFalse() |
| 51 | + { |
| 52 | + Assert.False(HasDivisionOperator(typeof(Density), typeof(Volume))); |
| 53 | + Assert.DoesNotContain(typeof(Volume), GetDivisors(typeof(Density))); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void HasDivisionOperator_GivenMassAndVolume_ReturnsTrue() |
| 58 | + { |
| 59 | + Assert.True(HasDivisionOperator(typeof(Mass), typeof(Volume))); |
| 60 | + Assert.Contains(typeof(Volume), GetDivisors(typeof(Mass))); |
| 61 | + Assert.Equal(typeof(Density), GetDivisionResult(typeof(Mass), typeof(Volume))); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void HasMultiplicationOperator_GivenTwoQuantities_ReturnsTrueIfDimensionsMultiplicationIsValid() |
| 66 | + { |
| 67 | + foreach (var firstQuantity in Quantity.Infos) |
| 68 | + { |
| 69 | + foreach (var divisor in GetMultipliers(firstQuantity.ValueType)) |
| 70 | + { |
| 71 | + var secondQuantity = Quantity.Infos.FirstOrDefault(x => x.ValueType == divisor); |
| 72 | + if (secondQuantity == null) |
| 73 | + { |
| 74 | + continue; // scalers |
| 75 | + } |
| 76 | + var resultDimensions = firstQuantity.BaseDimensions * secondQuantity.BaseDimensions; |
| 77 | + var resultingType = GetMultiplicationResult(firstQuantity.ValueType, secondQuantity.ValueType); |
| 78 | + var resultQuantity = Quantity.Infos.FirstOrDefault(x => x.ValueType == resultingType); |
| 79 | + if (resultQuantity == null) |
| 80 | + { |
| 81 | + continue; // scalers |
| 82 | + } |
| 83 | + Assert.Equal(resultQuantity.BaseDimensions, resultDimensions); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public void HasDivisionOperator_GivenTwoQuantities_ReturnsTrueIfDimensionsDivisionIsValid() |
| 90 | + { |
| 91 | + foreach (var firstQuantity in Quantity.Infos) |
| 92 | + { |
| 93 | + foreach (var divisor in GetDivisors(firstQuantity.ValueType)) |
| 94 | + { |
| 95 | + var secondQuantity = Quantity.Infos.FirstOrDefault(x => x.ValueType == divisor); |
| 96 | + if (secondQuantity == null) |
| 97 | + { |
| 98 | + continue; // scalers |
| 99 | + } |
| 100 | + var resultDimensions = firstQuantity.BaseDimensions / secondQuantity.BaseDimensions; |
| 101 | + var resultingType = GetDivisionResult(firstQuantity.ValueType, secondQuantity.ValueType); |
| 102 | + var resultQuantity = Quantity.Infos.FirstOrDefault(x => x.ValueType == resultingType); |
| 103 | + if (resultQuantity == null) |
| 104 | + { |
| 105 | + continue; // scalers |
| 106 | + } |
| 107 | + Assert.Equal(resultQuantity.BaseDimensions, resultDimensions); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private static bool HasMultiplicationOperator(Type t, Type operandType) |
| 113 | + { |
| 114 | + var operation = t.GetMethod("op_Multiply", new[] { t, operandType }); |
| 115 | + return operation != null && operation.IsSpecialName; |
| 116 | + } |
| 117 | + |
| 118 | + private static bool HasDivisionOperator(Type t, Type operandType) |
| 119 | + { |
| 120 | + var operation = t.GetMethod("op_Division", new[] { t, operandType }); |
| 121 | + return operation != null && operation.IsSpecialName; |
| 122 | + } |
| 123 | + |
| 124 | + private static Type GetMultiplicationResult(Type t, Type operandType) |
| 125 | + { |
| 126 | + var operation = t.GetMethod("op_Multiply", new[] { t, operandType }); |
| 127 | + return operation != null && operation.IsSpecialName ? operation.ReturnType : null; |
| 128 | + } |
| 129 | + |
| 130 | + private static Type GetDivisionResult(Type t, Type operandType) |
| 131 | + { |
| 132 | + var operation = t.GetMethod("op_Division", new[] { t, operandType }); |
| 133 | + return operation != null && operation.IsSpecialName ? operation.ReturnType : null; |
| 134 | + } |
| 135 | + |
| 136 | + private static IEnumerable<Type> GetMultipliers(Type t) |
| 137 | + { |
| 138 | + return t.GetMethods().Where(x => x.IsSpecialName && x.Name == "op_Multiply" && x.CustomAttributes.All(a => a.AttributeType != typeof(ObsoleteAttribute))) |
| 139 | + .SelectMany(x => x.GetParameters().Skip(1).Select(p => p.ParameterType)); |
| 140 | + } |
| 141 | + |
| 142 | + private static IEnumerable<Type> GetDivisors(Type t) |
| 143 | + { |
| 144 | + return t.GetMethods().Where(x => x.IsSpecialName && x.Name == "op_Division" && x.CustomAttributes.All(a => a.AttributeType != typeof(ObsoleteAttribute))) |
| 145 | + .SelectMany(x => x.GetParameters().Skip(1).Select(p => p.ParameterType)); |
| 146 | + } |
30 | 147 | }
|
31 | 148 | }
|
32 | 149 | }
|
0 commit comments