Skip to content

Fix arithmetic for Temperature #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions UnitsNet.Tests/CustomCode/TemperatureDeltaTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated (once) by \generate-code.bat, but will not be
// regenerated when it already exists. The purpose of creating this file is to make
// it easier to remember to implement all the unit conversion test cases.
//
// Whenever a new unit is added to this unit class and \generate-code.bat is run,
// the base test class will get a new abstract property and cause a compile error
// in this derived class, reminding the developer to implement the test case
// for the new unit.
//
// See https://github.com/anjdreas/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units.
//
// Add CustomCode\UnitClasses\MyUnit.extra.cs files to add code to generated unit classes.
// Add Extensions\MyUnitExtensions.cs to decorate unit classes with new behavior.
// Add UnitDefinitions\MyUnit.json and run GeneratUnits.bat to generate new units or unit classes.
//
// </auto-generated>
//------------------------------------------------------------------------------

// Copyright (c) 2007 Andreas Gullberg Larsen ([email protected]).
// https://github.com/anjdreas/UnitsNet
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.


namespace UnitsNet.Tests.CustomCode
{
public class TemperatureDeltaTests : TemperatureDeltaTestsBase
{
protected override double DegreesCelsiusDeltaInOneKelvinDelta => 1;
protected override double DegreesDelisleDeltaInOneKelvinDelta => -1.5d;
protected override double DegreesFahrenheitDeltaInOneKelvinDelta => 1.8;
protected override double DegreesNewtonDeltaInOneKelvinDelta => 0.33d;
protected override double DegreesRankineDeltaInOneKelvinDelta => 1.8;
protected override double DegreesReaumurDeltaInOneKelvinDelta => 0.8;
protected override double DegreesRoemerDeltaInOneKelvinDelta => 21/40d;
protected override double KelvinsDeltaInOneKelvinDelta => 1;
}
}
94 changes: 93 additions & 1 deletion UnitsNet.Tests/CustomCode/TemperatureTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright(c) 2007 Andreas Gullberg Larsen
// Copyright © 2007 Andreas Gullberg Larsen ([email protected]).
// https://github.com/anjdreas/UnitsNet
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
Expand All @@ -19,6 +19,11 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using NUnit.Framework;
using UnitsNet.Units;

namespace UnitsNet.Tests.CustomCode
{
public class TemperatureTests : TemperatureTestsBase
Expand All @@ -38,5 +43,92 @@ public class TemperatureTests : TemperatureTestsBase
protected override double DegreesRoemerInOneKelvin => -135.378750000;

protected override double KelvinsInOneKelvin => 1;

[SuppressMessage("ReSharper", "ImpureMethodCallOnReadonlyValueField",
Justification = "R# incorrectly identifies method as impure, due to internal method calls.")]
[TestCase(TemperatureUnit.DegreeCelsius, 10, 1, ExpectedResult = "10 °C")]
[TestCase(TemperatureUnit.DegreeCelsius, 10, 5, ExpectedResult = "2 °C")]
[TestCase(TemperatureUnit.DegreeCelsius, 10, -10, ExpectedResult = "-1 °C")]
[TestCase(TemperatureUnit.DegreeFahrenheit, 10, 1, ExpectedResult = "10 °F")]
[TestCase(TemperatureUnit.DegreeFahrenheit, 10, 5, ExpectedResult = "2 °F")]
[TestCase(TemperatureUnit.DegreeFahrenheit, 10, -10, ExpectedResult = "-1 °F")]
public string DividedByTemperatureDeltaEqualsTemperature(TemperatureUnit unit, int temperatureVal, int divisor)
{
Temperature temperature = Temperature.From(temperatureVal, unit);

// Act
Temperature resultTemp = temperature.Divide(divisor, unit);

return resultTemp.ToString(unit, CultureInfo.InvariantCulture, "{0:0} {1}");
}

[SuppressMessage("ReSharper", "ImpureMethodCallOnReadonlyValueField",
Justification = "R# incorrectly identifies method as impure, due to internal method calls.")]
[TestCase(TemperatureUnit.DegreeCelsius, 10, 0, ExpectedResult = "0 °C")]
[TestCase(TemperatureUnit.DegreeCelsius, 10, 5, ExpectedResult = "50 °C")]
[TestCase(TemperatureUnit.DegreeCelsius, 10, -5, ExpectedResult = "-50 °C")]
[TestCase(TemperatureUnit.DegreeFahrenheit, 10, 0, ExpectedResult = "0 °F")]
[TestCase(TemperatureUnit.DegreeFahrenheit, 10, 5, ExpectedResult = "50 °F")]
[TestCase(TemperatureUnit.DegreeFahrenheit, 10, -5, ExpectedResult = "-50 °F")]
public string MultiplyByTemperatureDeltaEqualsTemperature(TemperatureUnit unit, int temperatureVal, int factor)
{
Temperature temperature = Temperature.From(temperatureVal, unit);

// Act
Temperature resultTemp = temperature.Multiply(factor, unit);

return resultTemp.ToString(unit, CultureInfo.InvariantCulture, "{0:0} {1}");
}

[TestCase(TemperatureUnit.DegreeCelsius, -10, 0, ExpectedResult = "-10 °C")]
[TestCase(TemperatureUnit.DegreeCelsius, -10, 10, ExpectedResult = "0 °C")]
[TestCase(TemperatureUnit.DegreeCelsius, -10, 20, ExpectedResult = "10 °C")]
[TestCase(TemperatureUnit.DegreeFahrenheit, -10, 0, ExpectedResult = "-10 °F")]
[TestCase(TemperatureUnit.DegreeFahrenheit, -10, 10, ExpectedResult = "0 °F")]
[TestCase(TemperatureUnit.DegreeFahrenheit, -10, 20, ExpectedResult = "10 °F")]
public string TemperatureDeltaPlusTemperatureEqualsTemperature(TemperatureUnit unit, int deltaVal, int temperatureVal)
{
Temperature temperature = Temperature.From(temperatureVal, unit);
TemperatureDelta delta = TemperatureDelta.From(deltaVal, (TemperatureDeltaUnit) unit);

// Act
Temperature resultTemp = delta + temperature;

return resultTemp.ToString(unit, CultureInfo.InvariantCulture, "{0:0} {1}");
}

[TestCase(TemperatureUnit.DegreeCelsius, 20, 10, ExpectedResult = "10 °C")]
[TestCase(TemperatureUnit.DegreeCelsius, 20, 20, ExpectedResult = "0 °C")]
[TestCase(TemperatureUnit.DegreeCelsius, 20, 30, ExpectedResult = "-10 °C")]
[TestCase(TemperatureUnit.DegreeFahrenheit, 20, 10, ExpectedResult = "10 °F")]
[TestCase(TemperatureUnit.DegreeFahrenheit, 20, 20, ExpectedResult = "0 °F")]
[TestCase(TemperatureUnit.DegreeFahrenheit, 20, 30, ExpectedResult = "-10 °F")]
public string TemperatureMinusTemperatureDeltaEqualsTemperature(TemperatureUnit unit, int temperatureVal, int deltaVal)
{
Temperature temperature = Temperature.From(temperatureVal, unit);
TemperatureDelta delta = TemperatureDelta.From(deltaVal, (TemperatureDeltaUnit) unit);

// Act
Temperature resultTemp = temperature - delta;

return resultTemp.ToString(unit, CultureInfo.InvariantCulture, "{0:0} {1}");
}

[TestCase(TemperatureUnit.DegreeCelsius, -10, 0, ExpectedResult = "-10 °C")]
[TestCase(TemperatureUnit.DegreeCelsius, -10, 10, ExpectedResult = "0 °C")]
[TestCase(TemperatureUnit.DegreeCelsius, -10, 20, ExpectedResult = "10 °C")]
[TestCase(TemperatureUnit.DegreeFahrenheit, -10, 0, ExpectedResult = "-10 °F")]
[TestCase(TemperatureUnit.DegreeFahrenheit, -10, 10, ExpectedResult = "0 °F")]
[TestCase(TemperatureUnit.DegreeFahrenheit, -10, 20, ExpectedResult = "10 °F")]
public string TemperaturePlusTemperatureDeltaEqualsTemperature(TemperatureUnit unit, int temperatureVal, int deltaVal)
{
Temperature temperature = Temperature.From(temperatureVal, unit);
TemperatureDelta delta = TemperatureDelta.From(deltaVal, (TemperatureDeltaUnit) unit);

// Act
Temperature resultTemp = temperature + delta;

return resultTemp.ToString(unit, CultureInfo.InvariantCulture, "{0:0} {1}");
}
}
}
Loading