Skip to content

Commit

Permalink
Add unit tests for tensors
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmada committed Dec 10, 2023
1 parent c63e5dc commit d2726e5
Show file tree
Hide file tree
Showing 5 changed files with 315 additions and 0 deletions.
9 changes: 9 additions & 0 deletions NetFabric.Numerics.sln
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetFabric.Numerics.Geodesy.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetFabric.Numerics.Tensors", "src\NetFabric.Numerics.Tensors\NetFabric.Numerics.Tensors.csproj", "{8D2EE0A5-32A2-4019-AD55-7E9B70B76D67}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{EC0C1351-970E-48A9-BDA3-3E5E2B095402}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetFabric.Numerics.Tensors.UnitTests", "src\NetFabric.Numerics.Tensors.UnitTests\NetFabric.Numerics.Tensors.UnitTests.csproj", "{31E6CCB0-E04A-4947-BF74-E8E70D5594E5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -96,6 +100,10 @@ Global
{8D2EE0A5-32A2-4019-AD55-7E9B70B76D67}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8D2EE0A5-32A2-4019-AD55-7E9B70B76D67}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8D2EE0A5-32A2-4019-AD55-7E9B70B76D67}.Release|Any CPU.Build.0 = Release|Any CPU
{31E6CCB0-E04A-4947-BF74-E8E70D5594E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{31E6CCB0-E04A-4947-BF74-E8E70D5594E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{31E6CCB0-E04A-4947-BF74-E8E70D5594E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{31E6CCB0-E04A-4947-BF74-E8E70D5594E5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -104,6 +112,7 @@ Global
{A04C4E36-5F33-43F8-A7EE-15D81C7F8A58} = {762517CA-3F22-4DC6-955C-4F4FC69EB670}
{BA0837F2-730C-4537-B08B-5FFD2D91AF30} = {762517CA-3F22-4DC6-955C-4F4FC69EB670}
{83BDB68A-4F4B-4D7D-A7C7-55CC9F03657B} = {762517CA-3F22-4DC6-955C-4F4FC69EB670}
{31E6CCB0-E04A-4947-BF74-E8E70D5594E5} = {EC0C1351-970E-48A9-BDA3-3E5E2B095402}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AACC30AE-BE57-461F-817B-2AE0F001A33F}
Expand Down
95 changes: 95 additions & 0 deletions src/NetFabric.Numerics.Tensors.UnitTests/AddTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System.Linq;

namespace NetFabric.Numerics.Tensors.UnitTests;

public class AddTests
{
public static TheoryData<int> AddData
=> new() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37 };

[Theory]
[MemberData(nameof(AddData))]
public void Add_Short_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).Select(value => (short)value).ToArray();
var y = Enumerable.Range(0, count).Select(value => (short)(value + 1)).ToArray();
var result = new short[count];
var expected = Enumerable.Range(0, count).Select(value => (short)(value + value + 1)).ToArray();

// act
Tensor.Add<short>(x, y, result);

// assert
result.Should().Equal(expected);
}

[Theory]
[MemberData(nameof(AddData))]
public void Add_Int_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).ToArray();
var y = Enumerable.Range(0, count).Select(value => value + 1).ToArray();
var result = new int[count];
var expected = Enumerable.Range(0, count).Select(value => value + value + 1).ToArray();

// act
Tensor.Add<int>(x, y, result);

// assert
result.Should().Equal(expected);
}

[Theory]
[MemberData(nameof(AddData))]
public void Add_Long_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).Select(value => (long)value).ToArray();
var y = Enumerable.Range(0, count).Select(value => (long)(value + 1)).ToArray();
var result = new long[count];
var expected = Enumerable.Range(0, count).Select(value => (long)(value + value + 1)).ToArray();

// act
Tensor.Add<long>(x, y, result);

// assert
result.Should().Equal(expected);
}

[Theory]
[MemberData(nameof(AddData))]
public void Add_Float_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).Select(value => (float)value).ToArray();
var y = Enumerable.Range(0, count).Select(value => (float)(value + 1)).ToArray();
var result = new float[count];
var expected = Enumerable.Range(0, count).Select(value => (float)(value + value + 1)).ToArray();

// act
Tensor.Add<float>(x, y, result);

// assert
result.Should().Equal(expected);
}

[Theory]
[MemberData(nameof(AddData))]
public void Add_Double_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).Select(value => (double)value).ToArray();
var y = Enumerable.Range(0, count).Select(value => (double)(value + 1)).ToArray();
var result = new double[count];
var expected = Enumerable.Range(0, count).Select(value => (double)(value + value + 1)).ToArray();

// act
Tensor.Add<double>(x, y, result);

// assert
result.Should().Equal(expected);
}

}
92 changes: 92 additions & 0 deletions src/NetFabric.Numerics.Tensors.UnitTests/AddValueTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System.Linq;

namespace NetFabric.Numerics.Tensors.UnitTests;

public class AddValueTests
{
public const int constValue = 42;

public static TheoryData<int> AddData
=> new() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37 };

[Theory]
[MemberData(nameof(AddData))]
public void Add_Value_Short_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).Select(value => (short)value).ToArray();
var result = new short[count];
var expected = Enumerable.Range(0, count).Select(value => (short)(value + constValue)).ToArray();

// act
Tensor.Add<short>(x, constValue, result);

// assert
result.Should().Equal(expected);
}

[Theory]
[MemberData(nameof(AddData))]
public void Add_Value_Int_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).ToArray();
var result = new int[count];
var expected = Enumerable.Range(0, count).Select(value => value + constValue).ToArray();

// act
Tensor.Add<int>(x, constValue, result);

// assert
result.Should().Equal(expected);
}

[Theory]
[MemberData(nameof(AddData))]
public void Add_Value_Long_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).Select(value => (long)value).ToArray();
var result = new long[count];
var expected = Enumerable.Range(0, count).Select(value => (long)(value + constValue)).ToArray();

// act
Tensor.Add<long>(x, constValue, result);

// assert
result.Should().Equal(expected);
}

[Theory]
[MemberData(nameof(AddData))]
public void Add_Value_Float_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).Select(value => (float)value).ToArray();
var result = new float[count];
var expected = Enumerable.Range(0, count).Select(value => (float)(value + constValue)).ToArray();

// act
Tensor.Add<float>(x, constValue, result);

// assert
result.Should().Equal(expected);
}

[Theory]
[MemberData(nameof(AddData))]
public void Add_Value_Double_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).Select(value => (double)value).ToArray();
var result = new double[count];
var expected = Enumerable.Range(0, count).Select(value => (double)(value + constValue)).ToArray();

// act
Tensor.Add<double>(x, constValue, result);

// assert
result.Should().Equal(expected);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<RootNamespace>NetFabric.Numerics.Tensors.UnitsTests</RootNamespace>
</PropertyGroup>

<ItemGroup>
<Using Include="FluentAssertions" />
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NetFabric.Numerics.Tensors\NetFabric.Numerics.Tensors.csproj" />
</ItemGroup>

</Project>
85 changes: 85 additions & 0 deletions src/NetFabric.Numerics.Tensors.UnitTests/SumTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System.Linq;

namespace NetFabric.Numerics.Tensors.UnitTests;

public class SumTests
{
public static TheoryData<int> SumData
=> new() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37 };

[Theory]
[MemberData(nameof(SumData))]
public void Sum_Short_Should_Succeed(int count)
{
// arrange
var source = Enumerable.Range(0, count).Select(value => (short)value).ToArray();
var expected = Enumerable.Range(0, count).Sum();

// act
var result = Tensor.Sum<short>(source);

// assert
result.Should().Be((short)expected);
}

[Theory]
[MemberData(nameof(SumData))]
public void Sum_Int_Should_Succeed(int count)
{
// arrange
var source = Enumerable.Range(0, count).ToArray();
var expected = source.Sum();

// act
var result = Tensor.Sum<int>(source);

// assert
result.Should().Be(expected);
}

[Theory]
[MemberData(nameof(SumData))]
public void Sum_Long_Should_Succeed(int count)
{
// arrange
var source = Enumerable.Range(0, count).Select(value => (long)value).ToArray();
var expected = source.Sum();

// act
var result = Tensor.Sum<long>(source);

// assert
result.Should().Be(expected);
}

[Theory]
[MemberData(nameof(SumData))]
public void Sum_Float_Should_Succeed(int count)
{
// arrange
var source = Enumerable.Range(0, count).Select(value => (float)value).ToArray();
var expected = source.Sum();

// act
var result = Tensor.Sum<float>(source);

// assert
result.Should().Be(expected);
}

[Theory]
[MemberData(nameof(SumData))]
public void Sum_Double_Should_Succeed(int count)
{
// arrange
var source = Enumerable.Range(0, count).Select(value => (double)value).ToArray();
var expected = source.Sum();

// act
var result = Tensor.Sum<double>(source);

// assert
result.Should().Be(expected);
}

}

0 comments on commit d2726e5

Please sign in to comment.