Skip to content

Commit eac445c

Browse files
committed
Issue #24343 Vector Ctor using Span
1 parent 60b3376 commit eac445c

11 files changed

+946
-7
lines changed

src/System.Numerics.Vectors/ref/System.Numerics.Vectors.cs

+3
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ public partial struct Vector<T> : System.IEquatable<System.Numerics.Vector<T>>,
301301
public Vector(T value) { throw null; }
302302
public Vector(T[] values) { throw null; }
303303
public Vector(T[] values, int index) { throw null; }
304+
#if netcoreapp
305+
public Vector(Span<T> values) { throw null; }
306+
#endif
304307
public static int Count { get { throw null; } }
305308
public T this[int index] { get { throw null; } }
306309
public static System.Numerics.Vector<T> One { get { throw null; } }

src/System.Numerics.Vectors/ref/System.Numerics.Vectors.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<PropertyGroup>
55
<ProjectGuid>{650277B5-9423-4ACE-BB54-2659995B21C7}</ProjectGuid>
66
<IsPartialFacadeAssembly Condition="'$(TargetGroup)' == 'netfx' OR '$(TargetGroup)' == 'net46'">true</IsPartialFacadeAssembly>
7+
<DefineConstants Condition="'$(TargetGroup)'=='netcoreapp'">$(DefineConstants);netcoreapp</DefineConstants>
78
</PropertyGroup>
89
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'net46-Debug|AnyCPU'" />
910
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'net46-Release|AnyCPU'" />

src/System.Numerics.Vectors/src/System.Numerics.Vectors.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<IsTargetingNetCoreApp Condition="'$(TargetGroup)'=='netcoreapp' OR '$(TargetGroup)'=='uap' OR '$(TargetGroup)'=='uapaot'">true</IsTargetingNetCoreApp>
1111
<IsPartialFacadeAssembly Condition="'$(IsTargetingNetFx)'=='true' OR '$(IsTargetingNetCoreApp)'=='true'">true</IsPartialFacadeAssembly>
1212
<PackageTargetFramework Condition="'$(TargetGroup)' == 'netstandard1.0'">netstandard1.0;portable-net45+win8+wp8+wpa81</PackageTargetFramework>
13+
<DefineConstants Condition="'$(IsTargetingNetCoreApp)' != 'true'">$(DefineConstants);netcoreapp</DefineConstants>
1314
</PropertyGroup>
1415
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'net46-Debug|AnyCPU'" />
1516
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'net46-Release|AnyCPU'" />

src/System.Numerics.Vectors/tests/GenericVectorTests.cs

+15-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace System.Numerics.Tests
1414
/// <summary>
1515
/// Vector{T} tests that use random number generation and a unified generic test structure
1616
/// </summary>
17-
public class GenericVectorTests
17+
public partial class GenericVectorTests
1818
{
1919
// Static constructor in top-level class\
2020
static System.Numerics.Vector<float> dummy;
@@ -2699,11 +2699,12 @@ private static void ValidateVector<T>(Vector<T> vector, Action<int, T> indexVali
26992699
}
27002700
}
27012701

2702-
internal static T[] GenerateRandomValuesForVector<T>() where T : struct
2702+
internal static T[] GenerateRandomValuesForVector<T>(int numValues = int.MinValue) where T : struct
27032703
{
27042704
int minValue = GetMinValue<T>();
27052705
int maxValue = GetMaxValue<T>();
2706-
return Util.GenerateRandomValues<T>(Vector<T>.Count, minValue, maxValue);
2706+
numValues = numValues == int.MinValue ? Vector<T>.Count : numValues;
2707+
return Util.GenerateRandomValues<T>(numValues, minValue, maxValue);
27072708
}
27082709

27092710
internal static int GetMinValue<T>() where T : struct
@@ -2774,6 +2775,16 @@ internal static T GetValueWithAllOnesSet<T>() where T : struct
27742775
}
27752776
throw new NotSupportedException();
27762777
}
2778+
2779+
internal static T[] GetArrayOfDefaultValues<T>(int count)
2780+
{
2781+
T[] arr = new T[count];
2782+
for (int index = 0; index < count; ++index)
2783+
{
2784+
arr[index] = default(T);
2785+
}
2786+
return arr;
2787+
}
27772788
#endregion
27782789
}
2779-
}
2790+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Globalization;
7+
using System.Linq;
8+
using System.Reflection;
9+
using Xunit;
10+
using Xunit.Sdk;
11+
12+
namespace System.Numerics.Tests
13+
{
14+
/// <summary>
15+
/// Vector{T} tests that use random number generation and a unified generic test structure
16+
/// </summary>
17+
public partial class GenericVectorTests
18+
{
19+
#region Constructor Tests
20+
21+
#region Tests for Span based constructor
22+
[Fact]
23+
public void ConstructorWithSpanByte() => TestConstructorWithSpan<Byte>();
24+
[Fact]
25+
public void ConstructorWithSpanSByte() => TestConstructorWithSpan<SByte>();
26+
[Fact]
27+
public void ConstructorWithSpanUInt16() => TestConstructorWithSpan<UInt16>();
28+
[Fact]
29+
public void ConstructorWithSpanInt16() => TestConstructorWithSpan<Int16>();
30+
[Fact]
31+
public void ConstructorWithSpanUInt32() => TestConstructorWithSpan<UInt32>();
32+
[Fact]
33+
public void ConstructorWithSpanInt32() => TestConstructorWithSpan<Int32>();
34+
[Fact]
35+
public void ConstructorWithSpanUInt64() => TestConstructorWithSpan<UInt64>();
36+
[Fact]
37+
public void ConstructorWithSpanInt64() => TestConstructorWithSpan<Int64>();
38+
[Fact]
39+
public void ConstructorWithSpanSingle() => TestConstructorWithSpan<Single>();
40+
[Fact]
41+
public void ConstructorWithSpanDouble() => TestConstructorWithSpan<Double>();
42+
43+
private void TestConstructorWithSpan<T>() where T : struct
44+
{
45+
T[] values = GenerateRandomValuesForVector<T>().ToArray();
46+
Span<T> valueSpan = new Span<T>(values);
47+
48+
var vector = new Vector<T>(valueSpan);
49+
ValidateVector(vector,
50+
(index, val) =>
51+
{
52+
Assert.Equal(values[index], val);
53+
});
54+
}
55+
56+
[Fact]
57+
public void SpanBasedConstructorWithLessElements_Byte() => Assert.Throws<IndexOutOfRangeException>(() => TestSpanBasedConstructorWithLessElements<Byte>());
58+
[Fact]
59+
public void SpanBasedConstructorWithLessElements_SByte() => Assert.Throws<IndexOutOfRangeException>(() => TestSpanBasedConstructorWithLessElements<SByte>());
60+
[Fact]
61+
public void SpanBasedConstructorWithLessElements_UInt16() => Assert.Throws<IndexOutOfRangeException>(() => TestSpanBasedConstructorWithLessElements<UInt16>());
62+
[Fact]
63+
public void SpanBasedConstructorWithLessElements_Int16() => Assert.Throws<IndexOutOfRangeException>(() => TestSpanBasedConstructorWithLessElements<Int16>());
64+
[Fact]
65+
public void SpanBasedConstructorWithLessElements_UInt32() => Assert.Throws<IndexOutOfRangeException>(() => TestSpanBasedConstructorWithLessElements<UInt32>());
66+
[Fact]
67+
public void SpanBasedConstructorWithLessElements_Int32() => Assert.Throws<IndexOutOfRangeException>(() => TestSpanBasedConstructorWithLessElements<Int32>());
68+
[Fact]
69+
public void SpanBasedConstructorWithLessElements_UInt64() => Assert.Throws<IndexOutOfRangeException>(() => TestSpanBasedConstructorWithLessElements<UInt64>());
70+
[Fact]
71+
public void SpanBasedConstructorWithLessElements_Int64() => Assert.Throws<IndexOutOfRangeException>(() => TestSpanBasedConstructorWithLessElements<Int64>());
72+
[Fact]
73+
public void SpanBasedConstructorWithLessElements_Single() => Assert.Throws<IndexOutOfRangeException>(() => TestSpanBasedConstructorWithLessElements<Single>());
74+
[Fact]
75+
public void SpanBasedConstructorWithLessElements_Double() => Assert.Throws<IndexOutOfRangeException>(() => TestSpanBasedConstructorWithLessElements<Double>());
76+
77+
private void TestSpanBasedConstructorWithLessElements<T>() where T : struct
78+
{
79+
T[] values = GenerateRandomValuesForVector<T>(Vector<T>.Count - 1).ToArray();
80+
Span<T> valueSpan = new Span<T>(values);
81+
82+
Vector<T> v = new Vector<T>(valueSpan);
83+
}
84+
85+
#endregion Tests for Span based constructor
86+
87+
#region Tests for Array based constructor
88+
89+
[Fact]
90+
public void ArrayBasedConstructor_Byte() => TestArrayBasedConstructor<Byte>();
91+
[Fact]
92+
public void ArrayBasedConstructor_SByte() => TestArrayBasedConstructor<SByte>();
93+
[Fact]
94+
public void ArrayBasedConstructor_UInt16() => TestArrayBasedConstructor<UInt16>();
95+
[Fact]
96+
public void ArrayBasedConstructor_Int16() => TestArrayBasedConstructor<Int16>();
97+
[Fact]
98+
public void ArrayBasedConstructor_UInt32() => TestArrayBasedConstructor<UInt32>();
99+
[Fact]
100+
public void ArrayBasedConstructor_Int32() => TestArrayBasedConstructor<Int32>();
101+
[Fact]
102+
public void ArrayBasedConstructor_UInt64() => TestArrayBasedConstructor<UInt64>();
103+
[Fact]
104+
public void ArrayBasedConstructor_Int64() => TestArrayBasedConstructor<Int64>();
105+
[Fact]
106+
public void ArrayBasedConstructor_Single() => TestArrayBasedConstructor<Single>();
107+
[Fact]
108+
public void ArrayBasedConstructor_Double() => TestArrayBasedConstructor<Double>();
109+
110+
private void TestArrayBasedConstructor<T>() where T : struct
111+
{
112+
T[] values = GenerateRandomValuesForVector<T>(Vector<T>.Count).ToArray();
113+
Vector<T> vector = new Vector<T>(values);
114+
ValidateVector(vector,
115+
(index, val) =>
116+
{
117+
Assert.Equal(values[index], val);
118+
});
119+
}
120+
121+
[Fact]
122+
public void ArrayIndexBasedConstructor_Byte() => TestArrayIndexBasedConstructor<Byte>();
123+
[Fact]
124+
public void ArrayIndexBasedConstructor_SByte() => TestArrayIndexBasedConstructor<SByte>();
125+
[Fact]
126+
public void ArrayIndexBasedConstructor_UInt16() => TestArrayIndexBasedConstructor<UInt16>();
127+
[Fact]
128+
public void ArrayIndexBasedConstructor_Int16() => TestArrayIndexBasedConstructor<Int16>();
129+
[Fact]
130+
public void ArrayIndexBasedConstructor_UInt32() => TestArrayIndexBasedConstructor<UInt32>();
131+
[Fact]
132+
public void ArrayIndexBasedConstructor_Int32() => TestArrayIndexBasedConstructor<Int32>();
133+
[Fact]
134+
public void ArrayIndexBasedConstructor_UInt64() => TestArrayIndexBasedConstructor<UInt64>();
135+
[Fact]
136+
public void ArrayIndexBasedConstructor_Int64() => TestArrayIndexBasedConstructor<Int64>();
137+
[Fact]
138+
public void ArrayIndexBasedConstructor_Single() => TestArrayIndexBasedConstructor<Single>();
139+
[Fact]
140+
public void ArrayIndexBasedConstructor_Double() => TestArrayIndexBasedConstructor<Double>();
141+
142+
private void TestArrayIndexBasedConstructor<T>() where T : struct
143+
{
144+
T[] values = GenerateRandomValuesForVector<T>(Vector<T>.Count * 2).ToArray();
145+
int offset = Vector<T>.Count - 1;
146+
Vector<T> vector = new Vector<T>(values, offset);
147+
ValidateVector(vector,
148+
(index, val) =>
149+
{
150+
Assert.Equal(values[offset + index], val);
151+
});
152+
}
153+
154+
[Fact]
155+
public void ArrayBasedConstructorWithLessElements_Byte() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayBasedConstructorWithLessElements<Byte>());
156+
[Fact]
157+
public void ArrayBasedConstructorWithLessElements_SByte() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayBasedConstructorWithLessElements<SByte>());
158+
[Fact]
159+
public void ArrayBasedConstructorWithLessElements_UInt16() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayBasedConstructorWithLessElements<UInt16>());
160+
[Fact]
161+
public void ArrayBasedConstructorWithLessElements_Int16() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayBasedConstructorWithLessElements<Int16>());
162+
[Fact]
163+
public void ArrayBasedConstructorWithLessElements_UInt32() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayBasedConstructorWithLessElements<UInt32>());
164+
[Fact]
165+
public void ArrayBasedConstructorWithLessElements_Int32() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayBasedConstructorWithLessElements<Int32>());
166+
[Fact]
167+
public void ArrayBasedConstructorWithLessElements_UInt64() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayBasedConstructorWithLessElements<UInt64>());
168+
[Fact]
169+
public void ArrayBasedConstructorWithLessElements_Int64() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayBasedConstructorWithLessElements<Int64>());
170+
[Fact]
171+
public void ArrayBasedConstructorWithLessElements_Single() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayBasedConstructorWithLessElements<Single>());
172+
[Fact]
173+
public void ArrayBasedConstructorWithLessElements_Double() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayBasedConstructorWithLessElements<Double>());
174+
175+
private void TestArrayBasedConstructorWithLessElements<T>() where T : struct
176+
{
177+
T[] values = GenerateRandomValuesForVector<T>(Vector<T>.Count - 1).ToArray();
178+
Vector<T> v = new Vector<T>(values);
179+
}
180+
181+
[Fact]
182+
public void ArrayIndexBasedConstructorLessElements_Byte() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayIndexBasedConstructorLessElements<Byte>());
183+
[Fact]
184+
public void ArrayIndexBasedConstructorLessElements_SByte() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayIndexBasedConstructorLessElements<SByte>());
185+
[Fact]
186+
public void ArrayIndexBasedConstructorLessElements_UInt16() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayIndexBasedConstructorLessElements<UInt16>());
187+
[Fact]
188+
public void ArrayIndexBasedConstructorLessElements_Int16() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayIndexBasedConstructorLessElements<Int16>());
189+
[Fact]
190+
public void ArrayIndexBasedConstructorLessElements_UInt32() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayIndexBasedConstructorLessElements<UInt32>());
191+
[Fact]
192+
public void ArrayIndexBasedConstructorLessElements_Int32() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayIndexBasedConstructorLessElements<Int32>());
193+
[Fact]
194+
public void ArrayIndexBasedConstructorLessElements_UInt64() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayIndexBasedConstructorLessElements<UInt64>());
195+
[Fact]
196+
public void ArrayIndexBasedConstructorLessElements_Int64() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayIndexBasedConstructorLessElements<Int64>());
197+
[Fact]
198+
public void ArrayIndexBasedConstructorLessElements_Single() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayIndexBasedConstructorLessElements<Single>());
199+
[Fact]
200+
public void ArrayIndexBasedConstructorLessElements_Double() => Assert.Throws<IndexOutOfRangeException>(() => TestArrayIndexBasedConstructorLessElements<Double>());
201+
202+
private void TestArrayIndexBasedConstructorLessElements<T>() where T : struct
203+
{
204+
T[] values = GenerateRandomValuesForVector<T>(Vector<T>.Count * 2).ToArray();
205+
Vector<T> v = new Vector<T>(values, Vector<T>.Count + 1);
206+
}
207+
208+
#endregion Tests for Array based constructor
209+
210+
#region Tests for constructors using unsupported types
211+
212+
[Fact]
213+
public void ConstructorWithUnsupportedTypes_Guid() => TestConstructorWithUnsupportedTypes<Guid>();
214+
[Fact]
215+
public void ConstructorWithUnsupportedTypes_DateTime() => TestConstructorWithUnsupportedTypes<DateTime>();
216+
[Fact]
217+
public void ConstructorWithUnsupportedTypes_Char() => TestConstructorWithUnsupportedTypes<Char>();
218+
219+
private void TestConstructorWithUnsupportedTypes<T>() where T : struct
220+
{
221+
Assert.Throws<NotSupportedException>(() =>
222+
{
223+
T[] arr = GetArrayOfDefaultValues<T>(4);
224+
Span<T> span = new Span<T>(arr);
225+
Vector<T> vector = new Vector<T>(span);
226+
});
227+
}
228+
229+
#endregion Tests for constructors using unsupported types
230+
231+
#endregion Constructor Tests
232+
}
233+
}

0 commit comments

Comments
 (0)