Skip to content

Commit

Permalink
Issue #24343 Vector Ctor using Span
Browse files Browse the repository at this point in the history
  • Loading branch information
WinCPP committed Mar 5, 2018
1 parent ac312b6 commit e42c52f
Show file tree
Hide file tree
Showing 10 changed files with 945 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/System.Numerics.Vectors/ref/System.Numerics.Vectors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ public partial struct Vector<T> : System.IEquatable<System.Numerics.Vector<T>>,
public Vector(T value) { throw null; }
public Vector(T[] values) { throw null; }
public Vector(T[] values, int index) { throw null; }
#if netcoreapp
public Vector(Span<T> values) { throw null; }
#endif
public static int Count { get { throw null; } }
public T this[int index] { get { throw null; } }
public static System.Numerics.Vector<T> One { get { throw null; } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<PropertyGroup>
<ProjectGuid>{650277B5-9423-4ACE-BB54-2659995B21C7}</ProjectGuid>
<IsPartialFacadeAssembly Condition="'$(TargetGroup)' == 'netfx' OR '$(TargetGroup)' == 'net46'">true</IsPartialFacadeAssembly>
<DefineConstants Condition="'$(TargetGroup)'=='netcoreapp'">$(DefineConstants);netcoreapp</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'net46-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'net46-Release|AnyCPU'" />
Expand Down
19 changes: 15 additions & 4 deletions src/System.Numerics.Vectors/tests/GenericVectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace System.Numerics.Tests
/// <summary>
/// Vector{T} tests that use random number generation and a unified generic test structure
/// </summary>
public class GenericVectorTests
public partial class GenericVectorTests
{
// Static constructor in top-level class\
static System.Numerics.Vector<float> dummy;
Expand Down Expand Up @@ -2699,11 +2699,12 @@ private static void ValidateVector<T>(Vector<T> vector, Action<int, T> indexVali
}
}

internal static T[] GenerateRandomValuesForVector<T>() where T : struct
internal static T[] GenerateRandomValuesForVector<T>(int numValues = int.MinValue) where T : struct
{
int minValue = GetMinValue<T>();
int maxValue = GetMaxValue<T>();
return Util.GenerateRandomValues<T>(Vector<T>.Count, minValue, maxValue);
numValues = numValues == int.MinValue ? Vector<T>.Count : numValues;
return Util.GenerateRandomValues<T>(numValues, minValue, maxValue);
}

internal static int GetMinValue<T>() where T : struct
Expand Down Expand Up @@ -2774,6 +2775,16 @@ internal static T GetValueWithAllOnesSet<T>() where T : struct
}
throw new NotSupportedException();
}

internal static T[] GetArrayOfDefaultValues<T>(int count)
{
T[] arr = new T[count];
for (int index = 0; index < count; ++index)
{
arr[index] = default(T);
}
return arr;
}
#endregion
}
}
}
233 changes: 233 additions & 0 deletions src/System.Numerics.Vectors/tests/GenericVectorTests.netcoreapp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Globalization;
using System.Linq;
using System.Reflection;
using Xunit;
using Xunit.Sdk;

namespace System.Numerics.Tests
{
/// <summary>
/// Vector{T} tests that use random number generation and a unified generic test structure
/// </summary>
public partial class GenericVectorTests
{
#region Constructor Tests

#region Tests for Span based constructor
[Fact]
public void ConstructorWithSpanByte() => TestConstructorWithSpan<Byte>();
[Fact]
public void ConstructorWithSpanSByte() => TestConstructorWithSpan<SByte>();
[Fact]
public void ConstructorWithSpanUInt16() => TestConstructorWithSpan<UInt16>();
[Fact]
public void ConstructorWithSpanInt16() => TestConstructorWithSpan<Int16>();
[Fact]
public void ConstructorWithSpanUInt32() => TestConstructorWithSpan<UInt32>();
[Fact]
public void ConstructorWithSpanInt32() => TestConstructorWithSpan<Int32>();
[Fact]
public void ConstructorWithSpanUInt64() => TestConstructorWithSpan<UInt64>();
[Fact]
public void ConstructorWithSpanInt64() => TestConstructorWithSpan<Int64>();
[Fact]
public void ConstructorWithSpanSingle() => TestConstructorWithSpan<Single>();
[Fact]
public void ConstructorWithSpanDouble() => TestConstructorWithSpan<Double>();

private void TestConstructorWithSpan<T>() where T : struct
{
T[] values = GenerateRandomValuesForVector<T>().ToArray();
Span<T> valueSpan = new Span<T>(values);

var vector = new Vector<T>(valueSpan);
ValidateVector(vector,
(index, val) =>
{
Assert.Equal(values[index], val);
});
}

[Fact]
public void SpanBasedConstructorWithLessElements_Byte() => Assert.Throws<IndexOutOfRangeException>(() => TestSpanBasedConstructorWithLessElements<Byte>());
[Fact]
public void SpanBasedConstructorWithLessElements_SByte() => Assert.Throws<IndexOutOfRangeException>(() => TestSpanBasedConstructorWithLessElements<SByte>());
[Fact]
public void SpanBasedConstructorWithLessElements_UInt16() => Assert.Throws<IndexOutOfRangeException>(() => TestSpanBasedConstructorWithLessElements<UInt16>());
[Fact]
public void SpanBasedConstructorWithLessElements_Int16() => Assert.Throws<IndexOutOfRangeException>(() => TestSpanBasedConstructorWithLessElements<Int16>());
[Fact]
public void SpanBasedConstructorWithLessElements_UInt32() => Assert.Throws<IndexOutOfRangeException>(() => TestSpanBasedConstructorWithLessElements<UInt32>());
[Fact]
public void SpanBasedConstructorWithLessElements_Int32() => Assert.Throws<IndexOutOfRangeException>(() => TestSpanBasedConstructorWithLessElements<Int32>());
[Fact]
public void SpanBasedConstructorWithLessElements_UInt64() => Assert.Throws<IndexOutOfRangeException>(() => TestSpanBasedConstructorWithLessElements<UInt64>());
[Fact]
public void SpanBasedConstructorWithLessElements_Int64() => Assert.Throws<IndexOutOfRangeException>(() => TestSpanBasedConstructorWithLessElements<Int64>());
[Fact]
public void SpanBasedConstructorWithLessElements_Single() => Assert.Throws<IndexOutOfRangeException>(() => TestSpanBasedConstructorWithLessElements<Single>());
[Fact]
public void SpanBasedConstructorWithLessElements_Double() => Assert.Throws<IndexOutOfRangeException>(() => TestSpanBasedConstructorWithLessElements<Double>());

private void TestSpanBasedConstructorWithLessElements<T>() where T : struct
{
T[] values = GenerateRandomValuesForVector<T>(Vector<T>.Count - 1).ToArray();
Span<T> valueSpan = new Span<T>(values);

Vector<T> v = new Vector<T>(valueSpan);
}

#endregion Tests for Span based constructor

#region Tests for Array based constructor

[Fact]
public void ArrayBasedConstructor_Byte() => TestArrayBasedConstructor<Byte>();
[Fact]
public void ArrayBasedConstructor_SByte() => TestArrayBasedConstructor<SByte>();
[Fact]
public void ArrayBasedConstructor_UInt16() => TestArrayBasedConstructor<UInt16>();
[Fact]
public void ArrayBasedConstructor_Int16() => TestArrayBasedConstructor<Int16>();
[Fact]
public void ArrayBasedConstructor_UInt32() => TestArrayBasedConstructor<UInt32>();
[Fact]
public void ArrayBasedConstructor_Int32() => TestArrayBasedConstructor<Int32>();
[Fact]
public void ArrayBasedConstructor_UInt64() => TestArrayBasedConstructor<UInt64>();
[Fact]
public void ArrayBasedConstructor_Int64() => TestArrayBasedConstructor<Int64>();
[Fact]
public void ArrayBasedConstructor_Single() => TestArrayBasedConstructor<Single>();
[Fact]
public void ArrayBasedConstructor_Double() => TestArrayBasedConstructor<Double>();

private void TestArrayBasedConstructor<T>() where T : struct
{
T[] values = GenerateRandomValuesForVector<T>(Vector<T>.Count).ToArray();
Vector<T> vector = new Vector<T>(values);
ValidateVector(vector,
(index, val) =>
{
Assert.Equal(values[index], val);
});
}

[Fact]
public void ArrayIndexBasedConstructor_Byte() => TestArrayIndexBasedConstructor<Byte>();
[Fact]
public void ArrayIndexBasedConstructor_SByte() => TestArrayIndexBasedConstructor<SByte>();
[Fact]
public void ArrayIndexBasedConstructor_UInt16() => TestArrayIndexBasedConstructor<UInt16>();
[Fact]
public void ArrayIndexBasedConstructor_Int16() => TestArrayIndexBasedConstructor<Int16>();
[Fact]
public void ArrayIndexBasedConstructor_UInt32() => TestArrayIndexBasedConstructor<UInt32>();
[Fact]
public void ArrayIndexBasedConstructor_Int32() => TestArrayIndexBasedConstructor<Int32>();
[Fact]
public void ArrayIndexBasedConstructor_UInt64() => TestArrayIndexBasedConstructor<UInt64>();
[Fact]
public void ArrayIndexBasedConstructor_Int64() => TestArrayIndexBasedConstructor<Int64>();
[Fact]
public void ArrayIndexBasedConstructor_Single() => TestArrayIndexBasedConstructor<Single>();
[Fact]
public void ArrayIndexBasedConstructor_Double() => TestArrayIndexBasedConstructor<Double>();

private void TestArrayIndexBasedConstructor<T>() where T : struct
{
T[] values = GenerateRandomValuesForVector<T>(Vector<T>.Count * 2).ToArray();
int offset = Vector<T>.Count - 1;
Vector<T> vector = new Vector<T>(values, offset);
ValidateVector(vector,
(index, val) =>
{
Assert.Equal(values[offset + index], val);
});
}

[Fact]
public void ArrayBasedConstructorWithLessElements_Byte() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayBasedConstructorWithLessElements<Byte>());
[Fact]
public void ArrayBasedConstructorWithLessElements_SByte() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayBasedConstructorWithLessElements<SByte>());
[Fact]
public void ArrayBasedConstructorWithLessElements_UInt16() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayBasedConstructorWithLessElements<UInt16>());
[Fact]
public void ArrayBasedConstructorWithLessElements_Int16() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayBasedConstructorWithLessElements<Int16>());
[Fact]
public void ArrayBasedConstructorWithLessElements_UInt32() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayBasedConstructorWithLessElements<UInt32>());
[Fact]
public void ArrayBasedConstructorWithLessElements_Int32() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayBasedConstructorWithLessElements<Int32>());
[Fact]
public void ArrayBasedConstructorWithLessElements_UInt64() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayBasedConstructorWithLessElements<UInt64>());
[Fact]
public void ArrayBasedConstructorWithLessElements_Int64() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayBasedConstructorWithLessElements<Int64>());
[Fact]
public void ArrayBasedConstructorWithLessElements_Single() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayBasedConstructorWithLessElements<Single>());
[Fact]
public void ArrayBasedConstructorWithLessElements_Double() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayBasedConstructorWithLessElements<Double>());

private void TestArrayBasedConstructorWithLessElements<T>() where T : struct
{
T[] values = GenerateRandomValuesForVector<T>(Vector<T>.Count - 1).ToArray();
Vector<T> v = new Vector<T>(values);
}

[Fact]
public void ArrayIndexBasedConstructorLessElements_Byte() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayIndexBasedConstructorLessElements<Byte>());
[Fact]
public void ArrayIndexBasedConstructorLessElements_SByte() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayIndexBasedConstructorLessElements<SByte>());
[Fact]
public void ArrayIndexBasedConstructorLessElements_UInt16() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayIndexBasedConstructorLessElements<UInt16>());
[Fact]
public void ArrayIndexBasedConstructorLessElements_Int16() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayIndexBasedConstructorLessElements<Int16>());
[Fact]
public void ArrayIndexBasedConstructorLessElements_UInt32() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayIndexBasedConstructorLessElements<UInt32>());
[Fact]
public void ArrayIndexBasedConstructorLessElements_Int32() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayIndexBasedConstructorLessElements<Int32>());
[Fact]
public void ArrayIndexBasedConstructorLessElements_UInt64() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayIndexBasedConstructorLessElements<UInt64>());
[Fact]
public void ArrayIndexBasedConstructorLessElements_Int64() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayIndexBasedConstructorLessElements<Int64>());
[Fact]
public void ArrayIndexBasedConstructorLessElements_Single() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayIndexBasedConstructorLessElements<Single>());
[Fact]
public void ArrayIndexBasedConstructorLessElements_Double() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayIndexBasedConstructorLessElements<Double>());

private void TestArrayIndexBasedConstructorLessElements<T>() where T : struct
{
T[] values = GenerateRandomValuesForVector<T>(Vector<T>.Count * 2).ToArray();
Vector<T> v = new Vector<T>(values, Vector<T>.Count + 1);
}

#endregion Tests for Array based constructor

#region Tests for constructors using unsupported types

[Fact]
public void ConstructorWithUnsupportedTypes_Guid() => TestConstructorWithUnsupportedTypes<Guid>();
[Fact]
public void ConstructorWithUnsupportedTypes_DateTime() => TestConstructorWithUnsupportedTypes<DateTime>();
[Fact]
public void ConstructorWithUnsupportedTypes_Char() => TestConstructorWithUnsupportedTypes<Char>();

private void TestConstructorWithUnsupportedTypes<T>() where T : struct
{
Assert.Throws<NotSupportedException>(() =>
{
T[] arr = GetArrayOfDefaultValues<T>(4);
Span<T> span = new Span<T>(arr);
Vector<T> vector = new Vector<T>(span);
});
}

#endregion Tests for constructors using unsupported types

#endregion Constructor Tests
}
}
Loading

0 comments on commit e42c52f

Please sign in to comment.