Skip to content

Commit f3c61d3

Browse files
authored
Add BaseDimensions for TemperatureDelta & tests for operators (#840)
- Added tests for the multiplication and division operators of all quantities: testing for a mismatch between the dimensions of the operands and the result - Operators marked as obsolete are skipped (e.g. Density / Mass ) - Added the missing BaseDimensions for TemperatureDelta (being the only unit failing the tests)
1 parent c902f0f commit f3c61d3

File tree

4 files changed

+123
-3
lines changed

4 files changed

+123
-3
lines changed

Common/UnitDefinitions/TemperatureDelta.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"Name": "TemperatureDelta",
33
"BaseUnit": "Kelvin",
44
"XmlDoc": "Difference between two temperatures. The conversions are different than for Temperature.",
5+
"BaseDimensions": {
6+
"Θ": 1
7+
},
58
"Units": [
69
{
710
"SingularName": "Kelvin",

UnitsNet.Tests/GeneratedQuantityCodeTests.cs

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using Xunit;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Xunit;
25

36
namespace UnitsNet.Tests
47
{
@@ -27,6 +30,120 @@ public void LengthEquals_GivenMaxError_ReturnsTrueIfWithinError()
2730
"Example of floating precision arithmetic that produces slightly different results.");
2831
Assert.True(Length.FromMeters(1 + 0.39).Equals(Length.FromMeters(1.39), smallError, ComparisonType.Relative), "But the difference is very small");
2932
}
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+
}
30147
}
31148
}
32149
}

UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/TemperatureDelta.g.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet/GeneratedCode/Quantities/TemperatureDelta.g.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)