-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3bcb071
commit bac0ab1
Showing
6 changed files
with
101 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
namespace MeasurementsTool.UnitTypes; | ||
|
||
public enum TemperatureType | ||
{ | ||
// International System | ||
Kelvin, | ||
Celsius, | ||
Fahrenheit, | ||
} | ||
|
||
public static class TemperatureConverter | ||
{ | ||
public static double ConvertTo(this TemperatureType from, TemperatureType to, double value) | ||
{ | ||
if (from == to) | ||
{ | ||
return value; | ||
} | ||
|
||
if (from == TemperatureType.Kelvin) | ||
{ | ||
if (to == TemperatureType.Celsius) | ||
{ | ||
return value - 273.15; | ||
} | ||
if (to == TemperatureType.Fahrenheit) | ||
{ | ||
return value * 9 / 5 - 459.67; | ||
} | ||
} | ||
|
||
if (from == TemperatureType.Celsius) | ||
{ | ||
if (to == TemperatureType.Kelvin) | ||
{ | ||
return value + 273.15; | ||
} | ||
if (to == TemperatureType.Fahrenheit) | ||
{ | ||
return value * 9 / 5 + 32; | ||
} | ||
} | ||
|
||
if (from == TemperatureType.Fahrenheit) | ||
{ | ||
if (to == TemperatureType.Kelvin) | ||
{ | ||
return (value + 459.67) * 5 / 9; | ||
} | ||
if (to == TemperatureType.Celsius) | ||
{ | ||
return (value - 32) * 5 / 9; | ||
} | ||
} | ||
|
||
throw new NotImplementedException("Conversion not implemented"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
namespace MeasurementsTool.UnitTests; | ||
|
||
public sealed class TemperatureTypeTest | ||
{ | ||
[SetUp] | ||
public void Setup() { } | ||
|
||
[Theory] | ||
[TestCase(TemperatureType.Celsius, TemperatureType.Fahrenheit, 50.5, 122.9)] | ||
[TestCase(TemperatureType.Celsius, TemperatureType.Kelvin, 50.5, 323.65)] | ||
[TestCase(TemperatureType.Fahrenheit, TemperatureType.Celsius, 50.5, 10.277777777777779)] | ||
[TestCase(TemperatureType.Fahrenheit, TemperatureType.Kelvin, 50.5, 283.42777777777775)] | ||
[TestCase(TemperatureType.Kelvin, TemperatureType.Celsius, 50.5, -222.64999999999998)] | ||
[TestCase(TemperatureType.Kelvin, TemperatureType.Fahrenheit, 50.5, -368.77)] | ||
public void ConvertTo_International_System_Works(TemperatureType from, TemperatureType to, double value, double expectedResult) | ||
{ | ||
// Act | ||
var conversion = from.ConvertTo( | ||
to: to, | ||
value: value); | ||
|
||
// Assert | ||
conversion | ||
.Should() | ||
.Be(expectedResult); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters