Skip to content

Commit

Permalink
task: added unit tests for new Double and Single floating-point types
Browse files Browse the repository at this point in the history
  • Loading branch information
dtanglr committed Apr 3, 2024
1 parent fe7879e commit b021395
Show file tree
Hide file tree
Showing 20 changed files with 800 additions and 71 deletions.
71 changes: 0 additions & 71 deletions test/Acme.TestLib/Temperature.cs

This file was deleted.

4 changes: 4 additions & 0 deletions test/Acme.TestLib2/Acme.TestLib2.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@
<ProjectReference Include="..\..\src\Primitively.Abstractions\Primitively.Abstractions.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" Visible="false" />
</ItemGroup>

<ItemGroup>
<Folder Include="Double\" />
</ItemGroup>

</Project>
20 changes: 20 additions & 0 deletions test/Acme.TestLib2/Double/Celsius.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Primitively;

// https://en.wikipedia.org/wiki/Celsius
// https://en.wikipedia.org/wiki/Conversion_of_scales_of_temperature

namespace Acme.TestLib2.Double;

[Double(2, Minimum = -273.15d)]
public partial record struct Celsius : ITemperature<Celsius>
{
public static Celsius AbsoluteZero => new(-273.15d);

public static Celsius WaterMeltingPoint => new(0d);

public static Celsius WaterBoilingPoint => new(99.9839d);

public static explicit operator Kelvin(Celsius value) => new(value + 273.15d);
public static explicit operator Fahrenheit(Celsius value) => new((value * (9d / 5d)) + 32d);
public static explicit operator Rankine(Celsius value) => new((value + 273.15d) * (9d / 5d));
}
20 changes: 20 additions & 0 deletions test/Acme.TestLib2/Double/Fahrenheit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Primitively;

// https://en.wikipedia.org/wiki/Celsius
// https://en.wikipedia.org/wiki/Conversion_of_scales_of_temperature

namespace Acme.TestLib2.Double;

[Double(2, Minimum = -459.67d)]
public partial record struct Fahrenheit : ITemperature<Fahrenheit>
{
public static Fahrenheit AbsoluteZero => new(-459.67d);

public static Fahrenheit WaterMeltingPoint => new(32d);

public static Fahrenheit WaterBoilingPoint => new(211.97102d);

public static explicit operator Celsius(Fahrenheit value) => new((value - 32d) * (5d / 9d));
public static explicit operator Kelvin(Fahrenheit value) => new((value + 459.67d) * (5d / 9d));
public static explicit operator Rankine(Fahrenheit value) => new(value + 459.67d);
}
20 changes: 20 additions & 0 deletions test/Acme.TestLib2/Double/Kelvin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Primitively;

// https://en.wikipedia.org/wiki/Celsius
// https://en.wikipedia.org/wiki/Conversion_of_scales_of_temperature

namespace Acme.TestLib2.Double;

[Double(2, Minimum = 0d)]
public partial record struct Kelvin : ITemperature<Kelvin>
{
public static Kelvin AbsoluteZero => new(0d);

public static Kelvin WaterMeltingPoint => new(273.15d);

public static Kelvin WaterBoilingPoint => new(373.1339d);

public static explicit operator Celsius(Kelvin value) => new(value - 273.15d);
public static explicit operator Fahrenheit(Kelvin value) => new((9d / 5d * value) - 459.67d);
public static explicit operator Rankine(Kelvin value) => new(9d / 5d * value);
}
20 changes: 20 additions & 0 deletions test/Acme.TestLib2/Double/Rankine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Primitively;

// https://en.wikipedia.org/wiki/Celsius
// https://en.wikipedia.org/wiki/Conversion_of_scales_of_temperature

namespace Acme.TestLib2.Double;

[Double(2, Minimum = 0d)]
public partial record struct Rankine : ITemperature<Rankine>
{
public static Rankine AbsoluteZero => new(0d);

public static Rankine WaterMeltingPoint => new(491.67d);

public static Rankine WaterBoilingPoint => new(671.64102d);

public static explicit operator Celsius(Rankine value) => new((5d / 9d * value) - 273.15d);
public static explicit operator Fahrenheit(Rankine value) => new(value - 459.67d);
public static explicit operator Kelvin(Rankine value) => new(5d / 9d * value);
}
10 changes: 10 additions & 0 deletions test/Acme.TestLib2/ITemperature.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Acme.TestLib2;

public interface ITemperature<out T> where T : ITemperature<T>
{
#if NET7_0_OR_GREATER
static abstract T AbsoluteZero { get; }
static abstract T WaterMeltingPoint { get; }
static abstract T WaterBoilingPoint { get; }
#endif
}
20 changes: 20 additions & 0 deletions test/Acme.TestLib2/Single/Celsius.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Primitively;

// https://en.wikipefia.org/wiki/Celsius
// https://en.wikipefia.org/wiki/Conversion_of_scales_of_temperature

namespace Acme.TestLib2.Single;

[Single(2, Minimum = -273.15f)]
public partial record struct Celsius : ITemperature<Celsius>
{
public static Celsius AbsoluteZero => new(-273.15f);

public static Celsius WaterMeltingPoint => new(0f);

public static Celsius WaterBoilingPoint => new(99.9839f);

public static explicit operator Kelvin(Celsius value) => new(value + 273.15f);
public static explicit operator Fahrenheit(Celsius value) => new((value * (9f / 5f)) + 32f);
public static explicit operator Rankine(Celsius value) => new((value + 273.15f) * (9f / 5f));
}
20 changes: 20 additions & 0 deletions test/Acme.TestLib2/Single/Fahrenheit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Primitively;

// https://en.wikipefia.org/wiki/Celsius
// https://en.wikipefia.org/wiki/Conversion_of_scales_of_temperature

namespace Acme.TestLib2.Single;

[Single(2, Minimum = -459.67f)]
public partial record struct Fahrenheit : ITemperature<Fahrenheit>
{
public static Fahrenheit AbsoluteZero => new(-459.67f);

public static Fahrenheit WaterMeltingPoint => new(32f);

public static Fahrenheit WaterBoilingPoint => new(211.97102f);

public static explicit operator Celsius(Fahrenheit value) => new((value - 32f) * (5f / 9f));
public static explicit operator Kelvin(Fahrenheit value) => new((value + 459.67f) * (5f / 9f));
public static explicit operator Rankine(Fahrenheit value) => new(value + 459.67f);
}
20 changes: 20 additions & 0 deletions test/Acme.TestLib2/Single/Kelvin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Primitively;

// https://en.wikipefia.org/wiki/Celsius
// https://en.wikipefia.org/wiki/Conversion_of_scales_of_temperature

namespace Acme.TestLib2.Single;

[Single(2, Minimum = 0f)]
public partial record struct Kelvin : ITemperature<Kelvin>
{
public static Kelvin AbsoluteZero => new(0f);

public static Kelvin WaterMeltingPoint => new(273.15f);

public static Kelvin WaterBoilingPoint => new(373.1339f);

public static explicit operator Celsius(Kelvin value) => new(value - 273.15f);
public static explicit operator Fahrenheit(Kelvin value) => new((9f / 5f * value) - 459.67f);
public static explicit operator Rankine(Kelvin value) => new(9f / 5f * value);
}
20 changes: 20 additions & 0 deletions test/Acme.TestLib2/Single/Rankine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Primitively;

// https://en.wikipefia.org/wiki/Celsius
// https://en.wikipefia.org/wiki/Conversion_of_scales_of_temperature

namespace Acme.TestLib2.Single;

[Single(2, Minimum = 0f)]
public partial record struct Rankine : ITemperature<Rankine>
{
public static Rankine AbsoluteZero => new(0f);

public static Rankine WaterMeltingPoint => new(491.67f);

public static Rankine WaterBoilingPoint => new(671.64102f);

public static explicit operator Celsius(Rankine value) => new((5f / 9f * value) - 273.15f);
public static explicit operator Fahrenheit(Rankine value) => new(value - 459.67f);
public static explicit operator Kelvin(Rankine value) => new(5f / 9f * value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Acme.TestLib2.Double;

namespace Primitively.IntegrationTests.NumericTests.Double;

public class CelsiusTests
{
[Fact]
public void AbsoluteZero_In_Sut_Scale_Converts_To_AbsoluteZero_In_Other_Scales()
{
// Assign
var celsius = Celsius.AbsoluteZero;

// Act
var fahrenheit = (Fahrenheit)celsius;
var kelvin = (Kelvin)celsius;
var rankine = (Rankine)celsius;

// Assert
celsius.Should().BeEquivalentTo(Celsius.AbsoluteZero);
fahrenheit.Should().BeEquivalentTo(Fahrenheit.AbsoluteZero);
kelvin.Should().BeEquivalentTo(Kelvin.AbsoluteZero);
rankine.Should().BeEquivalentTo(Rankine.AbsoluteZero);
}

[Fact]
public void WaterMeltingPoint_In_Sut_Scale_Converts_To_WaterMeltingPoint_In_Other_Scales()
{
// Assign
var celsius = Celsius.WaterMeltingPoint;

// Act
var fahrenheit = (Fahrenheit)celsius;
var kelvin = (Kelvin)celsius;
var rankine = (Rankine)celsius;

// Assert
celsius.Should().BeEquivalentTo(Celsius.WaterMeltingPoint);
fahrenheit.Should().BeEquivalentTo(Fahrenheit.WaterMeltingPoint);
kelvin.Should().BeEquivalentTo(Kelvin.WaterMeltingPoint);
rankine.Should().BeEquivalentTo(Rankine.WaterMeltingPoint);
}

[Fact]
public void WaterBoilingPoint_In_Sut_Scale_Converts_To_WaterBoilingPoint_In_Other_Scales()
{
// Assign
var celsius = Celsius.WaterBoilingPoint;

// Act
var fahrenheit = (Fahrenheit)celsius;
var kelvin = (Kelvin)celsius;
var rankine = (Rankine)celsius;

// Assert
celsius.Should().BeEquivalentTo(Celsius.WaterBoilingPoint);
fahrenheit.Should().BeEquivalentTo(Fahrenheit.WaterBoilingPoint);
kelvin.Should().BeEquivalentTo(Kelvin.WaterBoilingPoint);
rankine.Should().BeEquivalentTo(Rankine.WaterBoilingPoint);
}

[Fact]
public void Minimum_Sut_Scale_Converts_To_Minimum_In_Other_Scales()
{
// Assign
var celsius = new Celsius(Celsius.Minimum);

// Act
double kelvin = (Kelvin)celsius;
double fahrenheit = (Fahrenheit)celsius;
double rankine = (Rankine)celsius;

// Assert
celsius.Should().BeEquivalentTo((Celsius)Celsius.Minimum);
fahrenheit.Should().Be(Fahrenheit.Minimum);
kelvin.Should().Be(Kelvin.Minimum);
rankine.Should().Be(Rankine.Minimum);
}
}
Loading

0 comments on commit b021395

Please sign in to comment.