-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
14 changed files
with
473 additions
and
32 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
66 changes: 66 additions & 0 deletions
66
src/NetFabric.Numerics.Tensors.UnitTests/IndexOfMaxNumberTests.cs
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,66 @@ | ||
using System.Numerics.Tensors; | ||
|
||
namespace NetFabric.Numerics.Tensors.UnitTests; | ||
|
||
public class IndexOfMaxNumberTests | ||
{ | ||
public static TheoryData<int> MaxData | ||
=> 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 }; | ||
|
||
static void IndexOfMaxNumber_Should_Succeed<T>(int count) | ||
where T : struct, INumber<T>, IMinMaxValue<T> | ||
{ | ||
// arrange | ||
var source = new T[count]; | ||
var max = T.MinValue; | ||
var expected = -1; | ||
var random = new Random(42); | ||
for (var index = 0; index < source.Length; index++) | ||
{ | ||
var value = T.CreateChecked(random.Next(100) - 50); | ||
source[index] = value; | ||
if (value > max) | ||
{ | ||
max = value; | ||
expected = index; | ||
} | ||
} | ||
|
||
// act | ||
var result = TensorOperations.IndexOfMaxNumber<T>(source); | ||
|
||
// assert | ||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(MaxData))] | ||
public void IndexOfMaxNumber_Short_Should_Succeed(int count) | ||
=> IndexOfMaxNumber_Should_Succeed<short>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(MaxData))] | ||
public void IndexOfMaxNumber_Int_Should_Succeed(int count) | ||
=> IndexOfMaxNumber_Should_Succeed<int>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(MaxData))] | ||
public void IndexOfMaxNumber_Long_Should_Succeed(int count) | ||
=> IndexOfMaxNumber_Should_Succeed<long>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(MaxData))] | ||
public void IndexOfMaxNumber_Half_Should_Succeed(int count) | ||
=> IndexOfMaxNumber_Should_Succeed<Half>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(MaxData))] | ||
public void IndexOfMaxNumber_Float_Should_Succeed(int count) | ||
=> IndexOfMaxNumber_Should_Succeed<float>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(MaxData))] | ||
public void IndexOfMaxNumber_Double_Should_Succeed(int count) | ||
=> IndexOfMaxNumber_Should_Succeed<float>(count); | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
src/NetFabric.Numerics.Tensors.UnitTests/IndexOfMaxSumNumberTests.cs
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,70 @@ | ||
using System.Numerics.Tensors; | ||
|
||
namespace NetFabric.Numerics.Tensors.UnitTests; | ||
|
||
public class IndexOfMaxSumNumberTests | ||
{ | ||
public static TheoryData<int> MaxData | ||
=> 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 }; | ||
|
||
static void IndexOfMaxSumNumber_Should_Succeed<T>(int count) | ||
where T : struct, INumber<T>, IMinMaxValue<T> | ||
{ | ||
// arrange | ||
var left = new T[count]; | ||
var right = new T[count]; | ||
var max = T.MinValue; | ||
var expected = -1; | ||
var random = new Random(42); | ||
for (var index = 0; index < left.Length; index++) | ||
{ | ||
var valueLeft = T.CreateChecked(random.Next(100) - 50); | ||
var valueRight = T.CreateChecked(random.Next(100) - 50); | ||
left[index] = valueLeft; | ||
right[index] = valueRight; | ||
var sum = valueLeft + valueRight; | ||
if (sum > max) | ||
{ | ||
max = sum; | ||
expected = index; | ||
} | ||
} | ||
|
||
// act | ||
var result = TensorOperations.IndexOfMaxSumNumber<T>(left, right); | ||
|
||
// assert | ||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(MaxData))] | ||
public void IndexOfMaxSumNumber_Short_Should_Succeed(int count) | ||
=> IndexOfMaxSumNumber_Should_Succeed<short>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(MaxData))] | ||
public void IndexOfMaxSumNumber_Int_Should_Succeed(int count) | ||
=> IndexOfMaxSumNumber_Should_Succeed<int>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(MaxData))] | ||
public void IndexOfMaxSumNumber_Long_Should_Succeed(int count) | ||
=> IndexOfMaxSumNumber_Should_Succeed<long>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(MaxData))] | ||
public void IndexOfMaxSumNumber_Half_Should_Succeed(int count) | ||
=> IndexOfMaxSumNumber_Should_Succeed<Half>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(MaxData))] | ||
public void IndexOfMaxSumNumber_Float_Should_Succeed(int count) | ||
=> IndexOfMaxSumNumber_Should_Succeed<float>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(MaxData))] | ||
public void IndexOfMaxSumNumber_Double_Should_Succeed(int count) | ||
=> IndexOfMaxSumNumber_Should_Succeed<float>(count); | ||
|
||
} |
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
Oops, something went wrong.