Skip to content

Commit 1da32ab

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

11 files changed

+933
-13
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

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
44
<PropertyGroup>
55
<ProjectGuid>{650277B5-9423-4ACE-BB54-2659995B21C7}</ProjectGuid>
6-
<IsPartialFacadeAssembly Condition="'$(TargetGroup)' == 'netfx' OR '$(TargetGroup)' == 'net46'">true</IsPartialFacadeAssembly>
6+
<IsTargetingNetFx Condition="'$(TargetGroup)'=='netfx' OR '$(TargetGroup)'=='net46'">true</IsTargetingNetFx>
7+
<IsTargetingNetCoreApp Condition="'$(TargetGroup)'=='netcoreapp' OR '$(TargetGroup)'=='uap'">true</IsTargetingNetCoreApp>
8+
<IsPartialFacadeAssembly Condition="'$(IsTargetingNetFx)'=='true'">true</IsPartialFacadeAssembly>
9+
<DefineConstants Condition="'$(IsTargetingNetCoreApp)'=='true'">$(DefineConstants);netcoreapp</DefineConstants>
710
</PropertyGroup>
811
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'net46-Debug|AnyCPU'" />
912
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'net46-Release|AnyCPU'" />
@@ -22,14 +25,14 @@
2225
<ItemGroup>
2326
<Compile Include="System.Numerics.Vectors.cs" />
2427
</ItemGroup>
25-
<ItemGroup Condition="'$(TargetGroup)' == 'netfx' OR '$(TargetGroup)' == 'net46'">
28+
<ItemGroup Condition="'$(IsTargetingNetFx)'=='true'">
2629
<Reference Include="mscorlib" />
2730
<Reference Include="System.Numerics" />
2831
</ItemGroup>
2932
<ItemGroup Condition="'$(TargetGroup)' == 'netstandard1.0'">
3033
<Reference Include="System.Runtime" />
3134
</ItemGroup>
32-
<ItemGroup Condition="'$(TargetGroup)' == 'netcoreapp' OR '$(TargetGroup)' == 'uap' OR '$(TargetGroup)' == 'uapaot'">
35+
<ItemGroup Condition="'$(IsTargetingNetCoreApp)'=='true'">
3336
<ProjectReference Include="..\..\System.Runtime\ref\System.Runtime.csproj" />
3437
</ItemGroup>
3538
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MembersMustExist : Member 'System.Numerics.Vector<T>..ctor(System.Span<T>)' does not exist in the implementation but it does exist in the contract.

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

+4-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,11 @@ 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 = null) 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+
return Util.GenerateRandomValues<T>(numValues ?? Vector<T>.Count, minValue, maxValue);
27072707
}
27082708

27092709
internal static int GetMinValue<T>() where T : struct
@@ -2776,4 +2776,4 @@ internal static T GetValueWithAllOnesSet<T>() where T : struct
27762776
}
27772777
#endregion
27782778
}
2779-
}
2779+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
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() => TestArrayBasedConstructorWithLessElements<Byte>();
156+
[Fact]
157+
public void ArrayBasedConstructorWithLessElements_SByte() => TestArrayBasedConstructorWithLessElements<SByte>();
158+
[Fact]
159+
public void ArrayBasedConstructorWithLessElements_UInt16() => TestArrayBasedConstructorWithLessElements<UInt16>();
160+
[Fact]
161+
public void ArrayBasedConstructorWithLessElements_Int16() => TestArrayBasedConstructorWithLessElements<Int16>();
162+
[Fact]
163+
public void ArrayBasedConstructorWithLessElements_UInt32() => TestArrayBasedConstructorWithLessElements<UInt32>();
164+
[Fact]
165+
public void ArrayBasedConstructorWithLessElements_Int32() => TestArrayBasedConstructorWithLessElements<Int32>();
166+
[Fact]
167+
public void ArrayBasedConstructorWithLessElements_UInt64() => TestArrayBasedConstructorWithLessElements<UInt64>();
168+
[Fact]
169+
public void ArrayBasedConstructorWithLessElements_Int64() => TestArrayBasedConstructorWithLessElements<Int64>();
170+
[Fact]
171+
public void ArrayBasedConstructorWithLessElements_Single() => TestArrayBasedConstructorWithLessElements<Single>();
172+
[Fact]
173+
public void ArrayBasedConstructorWithLessElements_Double() => TestArrayBasedConstructorWithLessElements<Double>();
174+
175+
private void TestArrayBasedConstructorWithLessElements<T>() where T : struct
176+
{
177+
T[] values = GenerateRandomValuesForVector<T>(Vector<T>.Count - 1).ToArray();
178+
Assert.Throws<IndexOutOfRangeException>(() => new Vector<T>(values));
179+
}
180+
181+
[Fact]
182+
public void ArrayIndexBasedConstructorLessElements_Byte() => TestArrayIndexBasedConstructorLessElements<Byte>();
183+
[Fact]
184+
public void ArrayIndexBasedConstructorLessElements_SByte() => TestArrayIndexBasedConstructorLessElements<SByte>();
185+
[Fact]
186+
public void ArrayIndexBasedConstructorLessElements_UInt16() => TestArrayIndexBasedConstructorLessElements<UInt16>();
187+
[Fact]
188+
public void ArrayIndexBasedConstructorLessElements_Int16() => TestArrayIndexBasedConstructorLessElements<Int16>();
189+
[Fact]
190+
public void ArrayIndexBasedConstructorLessElements_UInt32() => TestArrayIndexBasedConstructorLessElements<UInt32>();
191+
[Fact]
192+
public void ArrayIndexBasedConstructorLessElements_Int32() => TestArrayIndexBasedConstructorLessElements<Int32>();
193+
[Fact]
194+
public void ArrayIndexBasedConstructorLessElements_UInt64() => TestArrayIndexBasedConstructorLessElements<UInt64>();
195+
[Fact]
196+
public void ArrayIndexBasedConstructorLessElements_Int64() => TestArrayIndexBasedConstructorLessElements<Int64>();
197+
[Fact]
198+
public void ArrayIndexBasedConstructorLessElements_Single() => TestArrayIndexBasedConstructorLessElements<Single>();
199+
[Fact]
200+
public void ArrayIndexBasedConstructorLessElements_Double() => TestArrayIndexBasedConstructorLessElements<Double>();
201+
202+
private void TestArrayIndexBasedConstructorLessElements<T>() where T : struct
203+
{
204+
T[] values = GenerateRandomValuesForVector<T>(Vector<T>.Count * 2).ToArray();
205+
Assert.Throws<IndexOutOfRangeException>(() => 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>(() => new Vector<T>(new Span<T>(new T[4])));
222+
}
223+
224+
#endregion Tests for constructors using unsupported types
225+
226+
#endregion Constructor Tests
227+
}
228+
}

0 commit comments

Comments
 (0)