Skip to content

Commit 7a455f0

Browse files
authored
Merge pull request #219 from anjdreas/add-temperaturedelta
Fix arithmetic for Temperature Default arithmetic with Temperature removed, they were plain wrong Custom arithmetic for Temperature and TemperatureDelta is added Add Temperature.Multiply() and .Divide(), easy to get wrong TemperatureDelta added with default arithmetic, which should cover the examples you gave above Fixes #218
2 parents 9966cad + 04ced50 commit 7a455f0

15 files changed

+1884
-56
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated (once) by \generate-code.bat, but will not be
4+
// regenerated when it already exists. The purpose of creating this file is to make
5+
// it easier to remember to implement all the unit conversion test cases.
6+
//
7+
// Whenever a new unit is added to this unit class and \generate-code.bat is run,
8+
// the base test class will get a new abstract property and cause a compile error
9+
// in this derived class, reminding the developer to implement the test case
10+
// for the new unit.
11+
//
12+
// See https://github.com/anjdreas/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units.
13+
//
14+
// Add CustomCode\UnitClasses\MyUnit.extra.cs files to add code to generated unit classes.
15+
// Add Extensions\MyUnitExtensions.cs to decorate unit classes with new behavior.
16+
// Add UnitDefinitions\MyUnit.json and run GeneratUnits.bat to generate new units or unit classes.
17+
//
18+
// </auto-generated>
19+
//------------------------------------------------------------------------------
20+
21+
// Copyright (c) 2007 Andreas Gullberg Larsen (anjdreas@gmail.com).
22+
// https://github.com/anjdreas/UnitsNet
23+
//
24+
// Permission is hereby granted, free of charge, to any person obtaining a copy
25+
// of this software and associated documentation files (the "Software"), to deal
26+
// in the Software without restriction, including without limitation the rights
27+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28+
// copies of the Software, and to permit persons to whom the Software is
29+
// furnished to do so, subject to the following conditions:
30+
//
31+
// The above copyright notice and this permission notice shall be included in
32+
// all copies or substantial portions of the Software.
33+
//
34+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
40+
// THE SOFTWARE.
41+
42+
43+
namespace UnitsNet.Tests.CustomCode
44+
{
45+
public class TemperatureDeltaTests : TemperatureDeltaTestsBase
46+
{
47+
protected override double DegreesCelsiusDeltaInOneKelvinDelta => 1;
48+
protected override double DegreesDelisleDeltaInOneKelvinDelta => -1.5d;
49+
protected override double DegreesFahrenheitDeltaInOneKelvinDelta => 1.8;
50+
protected override double DegreesNewtonDeltaInOneKelvinDelta => 0.33d;
51+
protected override double DegreesRankineDeltaInOneKelvinDelta => 1.8;
52+
protected override double DegreesReaumurDeltaInOneKelvinDelta => 0.8;
53+
protected override double DegreesRoemerDeltaInOneKelvinDelta => 21/40d;
54+
protected override double KelvinsDeltaInOneKelvinDelta => 1;
55+
}
56+
}

UnitsNet.Tests/CustomCode/TemperatureTests.cs

+93-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright(c) 2007 Andreas Gullberg Larsen
1+
// Copyright © 2007 Andreas Gullberg Larsen (anjdreas@gmail.com).
22
// https://github.com/anjdreas/UnitsNet
33
//
44
// Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -19,6 +19,11 @@
1919
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020
// THE SOFTWARE.
2121

22+
using System.Diagnostics.CodeAnalysis;
23+
using System.Globalization;
24+
using NUnit.Framework;
25+
using UnitsNet.Units;
26+
2227
namespace UnitsNet.Tests.CustomCode
2328
{
2429
public class TemperatureTests : TemperatureTestsBase
@@ -38,5 +43,92 @@ public class TemperatureTests : TemperatureTestsBase
3843
protected override double DegreesRoemerInOneKelvin => -135.378750000;
3944

4045
protected override double KelvinsInOneKelvin => 1;
46+
47+
[SuppressMessage("ReSharper", "ImpureMethodCallOnReadonlyValueField",
48+
Justification = "R# incorrectly identifies method as impure, due to internal method calls.")]
49+
[TestCase(TemperatureUnit.DegreeCelsius, 10, 1, ExpectedResult = "10 °C")]
50+
[TestCase(TemperatureUnit.DegreeCelsius, 10, 5, ExpectedResult = "2 °C")]
51+
[TestCase(TemperatureUnit.DegreeCelsius, 10, -10, ExpectedResult = "-1 °C")]
52+
[TestCase(TemperatureUnit.DegreeFahrenheit, 10, 1, ExpectedResult = "10 °F")]
53+
[TestCase(TemperatureUnit.DegreeFahrenheit, 10, 5, ExpectedResult = "2 °F")]
54+
[TestCase(TemperatureUnit.DegreeFahrenheit, 10, -10, ExpectedResult = "-1 °F")]
55+
public string DividedByTemperatureDeltaEqualsTemperature(TemperatureUnit unit, int temperatureVal, int divisor)
56+
{
57+
Temperature temperature = Temperature.From(temperatureVal, unit);
58+
59+
// Act
60+
Temperature resultTemp = temperature.Divide(divisor, unit);
61+
62+
return resultTemp.ToString(unit, CultureInfo.InvariantCulture, "{0:0} {1}");
63+
}
64+
65+
[SuppressMessage("ReSharper", "ImpureMethodCallOnReadonlyValueField",
66+
Justification = "R# incorrectly identifies method as impure, due to internal method calls.")]
67+
[TestCase(TemperatureUnit.DegreeCelsius, 10, 0, ExpectedResult = "0 °C")]
68+
[TestCase(TemperatureUnit.DegreeCelsius, 10, 5, ExpectedResult = "50 °C")]
69+
[TestCase(TemperatureUnit.DegreeCelsius, 10, -5, ExpectedResult = "-50 °C")]
70+
[TestCase(TemperatureUnit.DegreeFahrenheit, 10, 0, ExpectedResult = "0 °F")]
71+
[TestCase(TemperatureUnit.DegreeFahrenheit, 10, 5, ExpectedResult = "50 °F")]
72+
[TestCase(TemperatureUnit.DegreeFahrenheit, 10, -5, ExpectedResult = "-50 °F")]
73+
public string MultiplyByTemperatureDeltaEqualsTemperature(TemperatureUnit unit, int temperatureVal, int factor)
74+
{
75+
Temperature temperature = Temperature.From(temperatureVal, unit);
76+
77+
// Act
78+
Temperature resultTemp = temperature.Multiply(factor, unit);
79+
80+
return resultTemp.ToString(unit, CultureInfo.InvariantCulture, "{0:0} {1}");
81+
}
82+
83+
[TestCase(TemperatureUnit.DegreeCelsius, -10, 0, ExpectedResult = "-10 °C")]
84+
[TestCase(TemperatureUnit.DegreeCelsius, -10, 10, ExpectedResult = "0 °C")]
85+
[TestCase(TemperatureUnit.DegreeCelsius, -10, 20, ExpectedResult = "10 °C")]
86+
[TestCase(TemperatureUnit.DegreeFahrenheit, -10, 0, ExpectedResult = "-10 °F")]
87+
[TestCase(TemperatureUnit.DegreeFahrenheit, -10, 10, ExpectedResult = "0 °F")]
88+
[TestCase(TemperatureUnit.DegreeFahrenheit, -10, 20, ExpectedResult = "10 °F")]
89+
public string TemperatureDeltaPlusTemperatureEqualsTemperature(TemperatureUnit unit, int deltaVal, int temperatureVal)
90+
{
91+
Temperature temperature = Temperature.From(temperatureVal, unit);
92+
TemperatureDelta delta = TemperatureDelta.From(deltaVal, (TemperatureDeltaUnit) unit);
93+
94+
// Act
95+
Temperature resultTemp = delta + temperature;
96+
97+
return resultTemp.ToString(unit, CultureInfo.InvariantCulture, "{0:0} {1}");
98+
}
99+
100+
[TestCase(TemperatureUnit.DegreeCelsius, 20, 10, ExpectedResult = "10 °C")]
101+
[TestCase(TemperatureUnit.DegreeCelsius, 20, 20, ExpectedResult = "0 °C")]
102+
[TestCase(TemperatureUnit.DegreeCelsius, 20, 30, ExpectedResult = "-10 °C")]
103+
[TestCase(TemperatureUnit.DegreeFahrenheit, 20, 10, ExpectedResult = "10 °F")]
104+
[TestCase(TemperatureUnit.DegreeFahrenheit, 20, 20, ExpectedResult = "0 °F")]
105+
[TestCase(TemperatureUnit.DegreeFahrenheit, 20, 30, ExpectedResult = "-10 °F")]
106+
public string TemperatureMinusTemperatureDeltaEqualsTemperature(TemperatureUnit unit, int temperatureVal, int deltaVal)
107+
{
108+
Temperature temperature = Temperature.From(temperatureVal, unit);
109+
TemperatureDelta delta = TemperatureDelta.From(deltaVal, (TemperatureDeltaUnit) unit);
110+
111+
// Act
112+
Temperature resultTemp = temperature - delta;
113+
114+
return resultTemp.ToString(unit, CultureInfo.InvariantCulture, "{0:0} {1}");
115+
}
116+
117+
[TestCase(TemperatureUnit.DegreeCelsius, -10, 0, ExpectedResult = "-10 °C")]
118+
[TestCase(TemperatureUnit.DegreeCelsius, -10, 10, ExpectedResult = "0 °C")]
119+
[TestCase(TemperatureUnit.DegreeCelsius, -10, 20, ExpectedResult = "10 °C")]
120+
[TestCase(TemperatureUnit.DegreeFahrenheit, -10, 0, ExpectedResult = "-10 °F")]
121+
[TestCase(TemperatureUnit.DegreeFahrenheit, -10, 10, ExpectedResult = "0 °F")]
122+
[TestCase(TemperatureUnit.DegreeFahrenheit, -10, 20, ExpectedResult = "10 °F")]
123+
public string TemperaturePlusTemperatureDeltaEqualsTemperature(TemperatureUnit unit, int temperatureVal, int deltaVal)
124+
{
125+
Temperature temperature = Temperature.From(temperatureVal, unit);
126+
TemperatureDelta delta = TemperatureDelta.From(deltaVal, (TemperatureDeltaUnit) unit);
127+
128+
// Act
129+
Temperature resultTemp = temperature + delta;
130+
131+
return resultTemp.ToString(unit, CultureInfo.InvariantCulture, "{0:0} {1}");
132+
}
41133
}
42134
}

0 commit comments

Comments
 (0)