forked from dotnet/corefx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenericVectorTests.netcoreapp.tt
174 lines (149 loc) · 4.92 KB
/
GenericVectorTests.netcoreapp.tt
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<#@ template debug="true" hostSpecific="true" #>
<#@ output extension=".cs" #>
<#@ Assembly Name="System.Core.dll" #>
<#@ Assembly Name="System.Xml.dll" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Runtime.InteropServices" #>
<#@ include file="..\..\Common\src\CoreLib\System\Numerics\GenerationConfig.ttinclude" #><# GenerateCopyrightHeader(); #>
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
<#
foreach (var type in supportedTypes)
{
#>
[Fact]
public void ConstructorWithSpan<#=type.Name#>() => TestConstructorWithSpan<<#=type.Name#>>();
<#
}
#>
private void TestConstructorWithSpan<T>() where T : struct
{
T[] values = GenerateRandomValuesForVector<T>().ToArray();
var valueSpan = new Span<T>(values);
var vector = new Vector<T>(valueSpan);
ValidateVector(vector,
(index, val) =>
{
Assert.Equal(values[index], val);
});
}
<#
foreach (var type in supportedTypes)
{
#>
[Fact]
public void SpanBasedConstructorWithLessElements_<#=type.Name#>() => Assert.Throws<IndexOutOfRangeException>(() => TestSpanBasedConstructorWithLessElements<<#=type.Name#>>());
<#
}
#>
private void TestSpanBasedConstructorWithLessElements<T>() where T : struct
{
T[] values = GenerateRandomValuesForVector<T>(Vector<T>.Count - 1).ToArray();
var vector = new Vector<T>(new Span<T>(values));
}
#endregion Tests for Span based constructor
#region Tests for Array based constructor
<#
foreach (var type in supportedTypes)
{
#>
[Fact]
public void ArrayBasedConstructor_<#=type.Name#>() => TestArrayBasedConstructor<<#=type.Name#>>();
<#
}
#>
private void TestArrayBasedConstructor<T>() where T : struct
{
T[] values = GenerateRandomValuesForVector<T>(Vector<T>.Count).ToArray();
var vector = new Vector<T>(values);
ValidateVector(vector,
(index, val) =>
{
Assert.Equal(values[index], val);
});
}
<#
foreach (var type in supportedTypes)
{
#>
[Fact]
public void ArrayIndexBasedConstructor_<#=type.Name#>() => TestArrayIndexBasedConstructor<<#=type.Name#>>();
<#
}
#>
private void TestArrayIndexBasedConstructor<T>() where T : struct
{
T[] values = GenerateRandomValuesForVector<T>(Vector<T>.Count * 2).ToArray();
int offset = Vector<T>.Count - 1;
var vector = new Vector<T>(values, offset);
ValidateVector(vector,
(index, val) =>
{
Assert.Equal(values[offset + index], val);
});
}
<#
foreach (var type in supportedTypes)
{
#>
[Fact]
public void ArrayBasedConstructorWithLessElements_<#=type.Name#>() => TestArrayBasedConstructorWithLessElements<<#=type.Name#>>();
<#
}
#>
private void TestArrayBasedConstructorWithLessElements<T>() where T : struct
{
T[] values = GenerateRandomValuesForVector<T>(Vector<T>.Count - 1).ToArray();
Assert.Throws<IndexOutOfRangeException>(() => new Vector<T>(values));
}
<#
foreach (var type in supportedTypes)
{
#>
[Fact]
public void ArrayIndexBasedConstructorLessElements_<#=type.Name#>() => TestArrayIndexBasedConstructorLessElements<<#=type.Name#>>();
<#
}
#>
private void TestArrayIndexBasedConstructorLessElements<T>() where T : struct
{
T[] values = GenerateRandomValuesForVector<T>(Vector<T>.Count * 2).ToArray();
Assert.Throws<IndexOutOfRangeException>(() => new Vector<T>(values, Vector<T>.Count + 1));
}
#endregion Tests for Array based constructor
#region Tests for constructors using unsupported types
<#
Type[] unsupportedTypes = new[]
{
typeof(Guid), typeof(DateTime), typeof(char)
};
foreach (var type in unsupportedTypes)
{
#>
[Fact]
public void ConstructorWithUnsupportedTypes_<#=type.Name#>() => TestConstructorWithUnsupportedTypes<<#=type.Name#>>();
<#
}
#>
private void TestConstructorWithUnsupportedTypes<T>() where T : struct
{
Assert.Throws<NotSupportedException>(() => new Vector<T>(new Span<T>(new T[4])));
}
#endregion Tests for constructors using unsupported types
#endregion Constructor Tests
}
}