|
| 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