forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTensorPrimitives.Generic.cs
More file actions
3072 lines (2651 loc) · 147 KB
/
Copy pathTensorPrimitives.Generic.cs
File metadata and controls
3072 lines (2651 loc) · 147 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics.Arm;
using System.Runtime.Intrinsics.X86;
using Xunit;
using Xunit.Sdk;
// Tests specific to .NET Core generic APIs.
// Some of the tests are written with functionality abstracted into helpers that provide the core operation: this
// is done when the tests are shared with legacy float-specific tests. Tests that don't need to be shared access
// the generic APIs directly.
namespace System.Numerics.Tensors.Tests
{
public class ConvertTests
{
[Fact]
[SkipOnCoreClr("Depends heavily on folded type comparisons", RuntimeTestModes.JitMinOpts)]
public void ConvertTruncatingAndSaturating()
{
// A few cases. More exhaustive testing is done in the OuterLoop test.
ConvertTruncatingImpl<float, double>();
ConvertTruncatingImpl<double, float>();
ConvertTruncatingImpl<long, byte>();
ConvertTruncatingImpl<short, uint>();
ConvertTruncatingImpl<Half, int>();
ConvertSaturatingImpl<float, double>();
ConvertSaturatingImpl<double, float>();
ConvertSaturatingImpl<long, byte>();
ConvertSaturatingImpl<short, uint>();
ConvertSaturatingImpl<Half, int>();
}
[OuterLoop]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBuiltWithAggressiveTrimming))]
[SkipOnCoreClr("Depends heavily on folded type comparisons", RuntimeTestModes.JitMinOpts)]
public void ConvertTruncatingAndSaturating_Outerloop()
{
MethodInfo convertTruncatingImpl = typeof(ConvertTests).GetMethod(nameof(ConvertTruncatingImpl), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
Assert.NotNull(convertTruncatingImpl);
MethodInfo convertSaturatingImpl = typeof(ConvertTests).GetMethod(nameof(ConvertSaturatingImpl), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
Assert.NotNull(convertSaturatingImpl);
Type[] types =
[
typeof(sbyte), typeof(byte),
typeof(short), typeof(ushort), typeof(char),
typeof(int), typeof(uint),
typeof(long), typeof(ulong),
typeof(nint), typeof(nuint),
typeof(Half), typeof(float), typeof(double), typeof(NFloat),
typeof(Int128), typeof(UInt128),
];
foreach (Type from in types)
{
foreach (Type to in types)
{
convertTruncatingImpl.MakeGenericMethod(from, to).Invoke(null, null);
convertSaturatingImpl.MakeGenericMethod(from, to).Invoke(null, null);
}
}
}
[Fact]
public void ConvertChecked()
{
// Conversions that never overflow. This isn't an exhaustive list; just a sampling.
ConvertCheckedImpl<byte, byte>();
ConvertCheckedImpl<byte, short>();
ConvertCheckedImpl<byte, uint>();
ConvertCheckedImpl<byte, long>();
ConvertCheckedImpl<byte, float>();
ConvertCheckedImpl<Half, Half>();
ConvertCheckedImpl<Half, float>();
ConvertCheckedImpl<Half, double>();
ConvertCheckedImpl<float, double>();
ConvertCheckedImpl<double, double>();
// Conversions that may overflow. This isn't an exhaustive list; just a sampling.
ConvertCheckedImpl<float, int>(42f, float.MaxValue);
ConvertCheckedImpl<long, int>(42, int.MaxValue + 1L);
}
private static void ConvertTruncatingImpl<TFrom, TTo>()
where TFrom : unmanaged, INumber<TFrom>
where TTo : unmanaged, INumber<TTo>
{
AssertExtensions.Throws<ArgumentException>("destination", () => TensorPrimitives.ConvertTruncating<TFrom, TTo>(new TFrom[3], new TTo[2]));
Random rand = new(42);
foreach (int tensorLength in Helpers.TensorLengthsIncluding0)
{
using BoundedMemory<TFrom> source = BoundedMemory.Allocate<TFrom>(tensorLength);
using BoundedMemory<TTo> destination = BoundedMemory.Allocate<TTo>(tensorLength);
Span<TFrom> sourceSpan = source.Span;
for (int i = 0; i < tensorLength; i++)
{
sourceSpan[i] = TFrom.CreateTruncating(new Int128(
(ulong)rand.NextInt64(long.MinValue, long.MaxValue),
(ulong)rand.NextInt64(long.MinValue, long.MaxValue)));
}
TensorPrimitives.ConvertTruncating<TFrom, TTo>(source.Span, destination.Span);
for (int i = 0; i < tensorLength; i++)
{
if (!Helpers.IsEqualWithTolerance(TTo.CreateTruncating(source.Span[i]), destination.Span[i]))
{
throw new XunitException($"{typeof(TFrom).Name} => {typeof(TTo).Name}. Input: {source.Span[i]}. Actual: {destination.Span[i]}. Expected: {TTo.CreateTruncating(source.Span[i])}.");
}
}
}
}
private static void ConvertSaturatingImpl<TFrom, TTo>()
where TFrom : unmanaged, INumber<TFrom>
where TTo : unmanaged, INumber<TTo>
{
AssertExtensions.Throws<ArgumentException>("destination", () => TensorPrimitives.ConvertSaturating<TFrom, TTo>(new TFrom[3], new TTo[2]));
Random rand = new(42);
foreach (int tensorLength in Helpers.TensorLengthsIncluding0)
{
using BoundedMemory<TFrom> source = BoundedMemory.Allocate<TFrom>(tensorLength);
using BoundedMemory<TTo> destination = BoundedMemory.Allocate<TTo>(tensorLength);
Span<TFrom> sourceSpan = source.Span;
for (int i = 0; i < tensorLength; i++)
{
sourceSpan[i] = TFrom.CreateTruncating(new Int128(
(ulong)rand.NextInt64(long.MinValue, long.MaxValue),
(ulong)rand.NextInt64(long.MinValue, long.MaxValue)));
}
TensorPrimitives.ConvertSaturating<TFrom, TTo>(source.Span, destination.Span);
for (int i = 0; i < tensorLength; i++)
{
if (!Helpers.IsEqualWithTolerance(TTo.CreateSaturating(source.Span[i]), destination.Span[i]))
{
throw new XunitException($"{typeof(TFrom).Name} => {typeof(TTo).Name}. Input: {source.Span[i]}. Actual: {destination.Span[i]}. Expected: {TTo.CreateSaturating(source.Span[i])}.");
}
}
}
}
private static void ConvertCheckedImpl<TFrom, TTo>()
where TFrom : unmanaged, INumber<TFrom>
where TTo : unmanaged, INumber<TTo>
{
AssertExtensions.Throws<ArgumentException>("destination", () => TensorPrimitives.ConvertChecked<TFrom, TTo>(new TFrom[3], new TTo[2]));
foreach (int tensorLength in Helpers.TensorLengthsIncluding0)
{
using BoundedMemory<TFrom> source = BoundedMemory.Allocate<TFrom>(tensorLength);
using BoundedMemory<TTo> destination = BoundedMemory.Allocate<TTo>(tensorLength);
Random rand = new(42);
Span<TFrom> sourceSpan = source.Span;
for (int i = 0; i < tensorLength; i++)
{
sourceSpan[i] = TFrom.CreateTruncating(new Int128(
(ulong)rand.NextInt64(long.MinValue, long.MaxValue),
(ulong)rand.NextInt64(long.MinValue, long.MaxValue)));
}
TensorPrimitives.ConvertChecked<TFrom, TTo>(source.Span, destination.Span);
for (int i = 0; i < tensorLength; i++)
{
if (!Helpers.IsEqualWithTolerance(TTo.CreateChecked(source.Span[i]), destination.Span[i]))
{
throw new XunitException($"{typeof(TFrom).Name} => {typeof(TTo).Name}. Input: {source.Span[i]}. Actual: {destination.Span[i]}. Expected: {TTo.CreateChecked(source.Span[i])}.");
}
}
}
}
private static void ConvertCheckedImpl<TFrom, TTo>(TFrom valid, TFrom invalid)
where TFrom : unmanaged, INumber<TFrom>
where TTo : unmanaged, INumber<TTo>
{
foreach (int tensorLength in Helpers.TensorLengths)
{
using BoundedMemory<TFrom> source = BoundedMemory.Allocate<TFrom>(tensorLength);
using BoundedMemory<TTo> destination = BoundedMemory.Allocate<TTo>(tensorLength);
// Test with valid
source.Span.Fill(valid);
TensorPrimitives.ConvertChecked<TFrom, TTo>(source.Span, destination.Span);
foreach (TTo result in destination.Span)
{
Assert.True(Helpers.IsEqualWithTolerance(TTo.CreateChecked(valid), result));
}
// Test with at least one invalid
foreach (int invalidPosition in new[] { 0, tensorLength / 2, tensorLength - 1 })
{
source.Span.Fill(valid);
source.Span[invalidPosition] = invalid;
Assert.Throws<OverflowException>(() => TensorPrimitives.ConvertChecked<TFrom, TTo>(source.Span, destination.Span));
}
}
}
#if !SNT_NET8_TESTS
[Fact]
public void ConvertToInteger()
{
ConvertToIntegerImpl<Half, ushort>();
ConvertToIntegerImpl<Half, int>();
ConvertToIntegerImpl<Half, uint>();
ConvertToIntegerImpl<Half, ulong>();
ConvertToIntegerImpl<float, ushort>();
ConvertToIntegerImpl<float, int>();
ConvertToIntegerImpl<float, uint>();
ConvertToIntegerImpl<float, ulong>();
ConvertToIntegerImpl<double, ushort>();
ConvertToIntegerImpl<double, int>();
ConvertToIntegerImpl<double, long>();
ConvertToIntegerImpl<double, ulong>();
}
[Fact]
public void ConvertToIntegerNative()
{
ConvertToIntegerNativeImpl<Half, ushort>();
ConvertToIntegerNativeImpl<Half, int>();
ConvertToIntegerNativeImpl<Half, uint>();
ConvertToIntegerNativeImpl<Half, ulong>();
ConvertToIntegerNativeImpl<float, ushort>();
ConvertToIntegerNativeImpl<float, int>();
ConvertToIntegerNativeImpl<float, uint>();
ConvertToIntegerNativeImpl<float, ulong>();
ConvertToIntegerNativeImpl<double, ushort>();
ConvertToIntegerNativeImpl<double, int>();
ConvertToIntegerNativeImpl<double, long>();
ConvertToIntegerNativeImpl<double, ulong>();
}
private static void ConvertToIntegerImpl<TFrom, TTo>()
where TFrom : unmanaged, IFloatingPoint<TFrom>
where TTo : unmanaged, IBinaryInteger<TTo>
{
AssertExtensions.Throws<ArgumentException>("destination", () => TensorPrimitives.ConvertToInteger<TFrom, TTo>(new TFrom[3], new TTo[2]));
Random rand = new(42);
foreach (int tensorLength in Helpers.TensorLengthsIncluding0)
{
using BoundedMemory<TFrom> source = BoundedMemory.Allocate<TFrom>(tensorLength);
using BoundedMemory<TTo> destination = BoundedMemory.Allocate<TTo>(tensorLength);
Span<TFrom> sourceSpan = source.Span;
for (int i = 0; i < tensorLength; i++)
{
sourceSpan[i] = TFrom.CreateTruncating(rand.NextDouble() * int.MaxValue);
}
TensorPrimitives.ConvertToInteger(source.Span, destination.Span);
for (int i = 0; i < tensorLength; i++)
{
TTo expected = TFrom.ConvertToInteger<TTo>(source.Span[i]);
if (!Helpers.IsEqualWithTolerance(expected, destination.Span[i]))
{
throw new XunitException($"{typeof(TFrom).Name} => {typeof(TTo).Name}. Input: {source.Span[i]}. Expected: {expected}. Actual: {destination.Span[i]}.");
}
}
}
}
private static void ConvertToIntegerNativeImpl<TFrom, TTo>()
where TFrom : unmanaged, IFloatingPoint<TFrom>
where TTo : unmanaged, IBinaryInteger<TTo>
{
AssertExtensions.Throws<ArgumentException>("destination", () => TensorPrimitives.ConvertToIntegerNative<TFrom, TTo>(new TFrom[3], new TTo[2]));
Random rand = new(42);
foreach (int tensorLength in Helpers.TensorLengthsIncluding0)
{
using BoundedMemory<TFrom> source = BoundedMemory.Allocate<TFrom>(tensorLength);
using BoundedMemory<TTo> destination = BoundedMemory.Allocate<TTo>(tensorLength);
Span<TFrom> sourceSpan = source.Span;
for (int i = 0; i < tensorLength; i++)
{
sourceSpan[i] = TFrom.CreateTruncating(rand.NextDouble() * int.MaxValue);
}
TensorPrimitives.ConvertToIntegerNative(source.Span, destination.Span);
for (int i = 0; i < tensorLength; i++)
{
TTo expected = TFrom.ConvertToIntegerNative<TTo>(source.Span[i]);
if (!Helpers.IsEqualWithTolerance(expected, destination.Span[i]))
{
throw new XunitException($"{typeof(TFrom).Name} => {typeof(TTo).Name}. Input: {source.Span[i]}. Expected: {expected}. Actual: {destination.Span[i]}.");
}
}
}
}
#endif
}
// The tests for some types have been marked as OuterLoop simply to decrease inner loop testing time.
public class DoubleGenericTensorPrimitives : GenericFloatingPointNumberTensorPrimitivesTests<double> { }
public class SingleGenericTensorPrimitives : GenericFloatingPointNumberTensorPrimitivesTests<float> { }
public class HalfGenericTensorPrimitives : GenericFloatingPointNumberTensorPrimitivesTests<Half>
{
protected override void AssertEqualTolerance(Half expected, Half actual, Half? tolerance = null) =>
base.AssertEqualTolerance(expected, actual, tolerance ?? Half.CreateTruncating(0.001));
}
[OuterLoop]
public class NFloatGenericTensorPrimitives : GenericFloatingPointNumberTensorPrimitivesTests<NFloat> { }
[OuterLoop]
public class SByteGenericTensorPrimitives : GenericSignedIntegerTensorPrimitivesTests<sbyte> { }
public class Int16GenericTensorPrimitives : GenericSignedIntegerTensorPrimitivesTests<short> { }
[OuterLoop]
public class Int32GenericTensorPrimitives : GenericSignedIntegerTensorPrimitivesTests<int> { }
public class Int64GenericTensorPrimitives : GenericSignedIntegerTensorPrimitivesTests<long> { }
[OuterLoop]
public class IntPtrGenericTensorPrimitives : GenericSignedIntegerTensorPrimitivesTests<nint> { }
public class Int128GenericTensorPrimitives : GenericSignedIntegerTensorPrimitivesTests<Int128> { }
public class ByteGenericTensorPrimitives : GenericIntegerTensorPrimitivesTests<byte> { }
[OuterLoop]
public class UInt16GenericTensorPrimitives : GenericIntegerTensorPrimitivesTests<ushort> { }
[OuterLoop]
public class CharGenericTensorPrimitives : GenericIntegerTensorPrimitivesTests<char> { }
public class UInt32GenericTensorPrimitives : GenericIntegerTensorPrimitivesTests<uint> { }
[OuterLoop]
public class UInt64GenericTensorPrimitives : GenericIntegerTensorPrimitivesTests<ulong> { }
public class UIntPtrGenericTensorPrimitives : GenericIntegerTensorPrimitivesTests<nuint> { }
[OuterLoop]
public class UInt128GenericTensorPrimitives : GenericIntegerTensorPrimitivesTests<UInt128> { }
public unsafe abstract class GenericFloatingPointNumberTensorPrimitivesTests<T> : GenericNumberTensorPrimitivesTests<T>
where T : unmanaged, IFloatingPointIeee754<T>, IMinMaxValue<T>
{
protected override T Cosh(T x) => T.Cosh(x);
protected override void Cosh(ReadOnlySpan<T> x, Span<T> destination) => TensorPrimitives.Cosh(x, destination);
protected override T CosineSimilarity(ReadOnlySpan<T> x, ReadOnlySpan<T> y) => TensorPrimitives.CosineSimilarity(x, y);
protected override T Distance(ReadOnlySpan<T> x, ReadOnlySpan<T> y) => TensorPrimitives.Distance(x, y);
protected override void Exp(ReadOnlySpan<T> x, Span<T> destination) => TensorPrimitives.Exp(x, destination);
protected override T Exp(T x) => T.Exp(x);
protected override T Log(T x) => T.Log(x);
protected override void Log(ReadOnlySpan<T> x, Span<T> destination) => TensorPrimitives.Log(x, destination);
protected override T Log2(T x) => T.Log2(x);
protected override void Log2(ReadOnlySpan<T> x, Span<T> destination) => TensorPrimitives.Log2(x, destination);
protected override T Norm(ReadOnlySpan<T> x) => TensorPrimitives.Norm(x);
protected override void Sigmoid(ReadOnlySpan<T> x, Span<T> destination) => TensorPrimitives.Sigmoid(x, destination);
protected override void Sinh(ReadOnlySpan<T> x, Span<T> destination) => TensorPrimitives.Sinh(x, destination);
protected override T Sinh(T x) => T.Sinh(x);
protected override void SoftMax(ReadOnlySpan<T> x, Span<T> destination) => TensorPrimitives.SoftMax(x, destination);
protected override T Sqrt(T x) => T.Sqrt(x);
protected override void Tanh(ReadOnlySpan<T> x, Span<T> destination) => TensorPrimitives.Tanh(x, destination);
protected override T Tanh(T x) => T.Tanh(x);
protected override T NaN => T.NaN;
protected override T NextRandom() => T.CreateTruncating((Random.NextDouble() * 2) - 1); // For testing purposes, get a mix of negative and positive values.
protected override IEnumerable<T> GetSpecialValues()
{
// NaN
yield return T.CreateTruncating(BitConverter.UInt32BitsToSingle(0xFFC0_0000)); // -qNaN / float.NaN
yield return T.CreateTruncating(BitConverter.UInt32BitsToSingle(0xFFFF_FFFF)); // -qNaN / all-bits-set
yield return T.CreateTruncating(BitConverter.UInt32BitsToSingle(0x7FC0_0000)); // +qNaN
yield return T.CreateTruncating(BitConverter.UInt32BitsToSingle(0xFFA0_0000)); // -sNaN
yield return T.CreateTruncating(BitConverter.UInt32BitsToSingle(0x7FA0_0000)); // +sNaN
// +Infinity, -Infinity
yield return T.PositiveInfinity;
yield return T.NegativeInfinity;
// +0, -0
yield return T.Zero;
yield return T.NegativeZero;
// +1, -1
yield return T.One;
yield return T.NegativeOne;
// Subnormals
yield return T.Epsilon;
yield return -T.Epsilon;
yield return T.CreateTruncating(BitConverter.UInt32BitsToSingle(0x007F_FFFF));
yield return T.CreateTruncating(BitConverter.UInt32BitsToSingle(0x807F_FFFF));
// Normals
yield return T.CreateTruncating(BitConverter.UInt32BitsToSingle(0x0080_0000));
yield return T.CreateTruncating(BitConverter.UInt32BitsToSingle(0x8080_0000));
yield return T.CreateTruncating(float.MinValue);
yield return T.CreateTruncating(float.MaxValue);
yield return T.CreateTruncating(double.MinValue);
yield return T.CreateTruncating(double.MaxValue);
// Other known constants
yield return T.E;
yield return T.Pi;
yield return T.Tau;
}
protected override void SetSpecialValues(Span<T> x, Span<T> y)
{
int pos;
// NaNs
pos = Random.Next(x.Length);
x[pos] = T.NaN;
y[pos] = T.CreateTruncating(BitConverter.UInt32BitsToSingle(0x7FC0_0000));
// +Infinity, -Infinity
pos = Random.Next(x.Length);
x[pos] = T.PositiveInfinity;
y[pos] = T.NegativeInfinity;
// +Zero, -Zero
pos = Random.Next(x.Length);
x[pos] = T.Zero;
y[pos] = T.NegativeZero;
// +Epsilon, -Epsilon
pos = Random.Next(x.Length);
x[pos] = T.Epsilon;
y[pos] = -T.Epsilon;
// Same magnitude, opposite sign
pos = Random.Next(x.Length);
x[pos] = T.CreateTruncating(5);
y[pos] = T.CreateTruncating(-5);
}
#region Span -> Destination
public static IEnumerable<object[]> SpanDestinationFunctionsToTest()
{
// The current trigonometric algorithm depends on hardware FMA support for best precision.
T? trigTolerance = IsFmaSupported ? null : Helpers.DetermineTolerance<T>(doubleTolerance: 1e-10, floatTolerance: 1e-4f);
yield return Create(TensorPrimitives.Acosh, T.Acosh);
yield return Create(TensorPrimitives.AcosPi, T.AcosPi);
yield return Create(TensorPrimitives.Acos, T.Acos);
yield return Create(TensorPrimitives.Asinh, T.Asinh);
yield return Create(TensorPrimitives.AsinPi, T.AsinPi);
yield return Create(TensorPrimitives.Asin, T.Asin, trigTolerance);
yield return Create(TensorPrimitives.Atanh, T.Atanh);
yield return Create(TensorPrimitives.AtanPi, T.AtanPi);
yield return Create(TensorPrimitives.Atan, T.Atan);
yield return Create(TensorPrimitives.BitDecrement, T.BitDecrement);
yield return Create(TensorPrimitives.BitIncrement, T.BitIncrement);
yield return Create(TensorPrimitives.Cbrt, T.Cbrt, Helpers.DetermineTolerance<T>(doubleTolerance: 1e-13));
yield return Create(TensorPrimitives.Ceiling, T.Ceiling);
yield return Create(TensorPrimitives.Cos, T.Cos, trigTolerance);
yield return Create(TensorPrimitives.Cosh, T.Cosh, Helpers.DetermineTolerance<T>(doubleTolerance: 1e-14));
yield return Create(TensorPrimitives.CosPi, T.CosPi, trigTolerance ?? Helpers.DetermineTolerance<T>(floatTolerance: 1e-5f));
yield return Create(TensorPrimitives.Decrement, f => --f);
yield return Create(TensorPrimitives.DegreesToRadians, T.DegreesToRadians);
yield return Create(TensorPrimitives.Exp, T.Exp);
yield return Create(TensorPrimitives.Exp2, T.Exp2, Helpers.DetermineTolerance<T>(doubleTolerance: 1e-14, floatTolerance: 1e-5f));
yield return Create(TensorPrimitives.Exp10, T.Exp10, Helpers.DetermineTolerance<T>(doubleTolerance: 1e-13, floatTolerance: 1e-5f));
yield return Create(TensorPrimitives.ExpM1, T.ExpM1);
yield return Create(TensorPrimitives.Exp2M1, T.Exp2M1, Helpers.DetermineTolerance<T>(doubleTolerance: 1e-14, floatTolerance: 1e-5f));
yield return Create(TensorPrimitives.Exp10M1, T.Exp10M1, Helpers.DetermineTolerance<T>(doubleTolerance: 1e-13, floatTolerance: 1e-5f));
yield return Create(TensorPrimitives.Floor, T.Floor);
yield return Create(TensorPrimitives.Increment, f => ++f);
yield return Create(TensorPrimitives.Log, T.Log);
yield return Create(TensorPrimitives.Log2, T.Log2);
yield return Create(TensorPrimitives.Log10, T.Log10);
yield return Create(TensorPrimitives.LogP1, T.LogP1);
yield return Create(TensorPrimitives.Log2P1, T.Log2P1);
yield return Create(TensorPrimitives.Log10P1, T.Log10P1);
yield return Create(TensorPrimitives.RadiansToDegrees, T.RadiansToDegrees);
yield return Create(TensorPrimitives.Reciprocal, f => T.One / f);
yield return Create(TensorPrimitives.ReciprocalEstimate, T.ReciprocalEstimate, T.CreateTruncating(Helpers.DefaultToleranceForEstimates));
yield return Create(TensorPrimitives.ReciprocalSqrt, f => T.One / T.Sqrt(f));
#if !SNT_NET8_TESTS
// Avoid running with the net8 tests due to: https://github.com/dotnet/runtime/issues/101846
yield return Create(TensorPrimitives.ReciprocalSqrtEstimate, T.ReciprocalSqrtEstimate, T.CreateTruncating(Helpers.DefaultToleranceForEstimates));
#endif
yield return Create(TensorPrimitives.Round, T.Round);
yield return Create(TensorPrimitives.Sin, T.Sin, trigTolerance);
yield return Create(TensorPrimitives.Sinh, T.Sinh, Helpers.DetermineTolerance<T>(doubleTolerance: 1e-14));
yield return Create(TensorPrimitives.SinPi, T.SinPi, Helpers.DetermineTolerance<T>(doubleTolerance: 1e-13, floatTolerance: 1e-4f));
yield return Create(TensorPrimitives.Sqrt, T.Sqrt);
yield return Create(TensorPrimitives.Tan, T.Tan, trigTolerance);
yield return Create(TensorPrimitives.Tanh, T.Tanh);
yield return Create(TensorPrimitives.TanPi, T.TanPi);
yield return Create(TensorPrimitives.Truncate, T.Truncate);
static object[] Create(SpanDestinationDelegate tensorPrimitivesMethod, Func<T, T> expectedMethod, T? tolerance = null)
=> new object[] { tensorPrimitivesMethod, expectedMethod, tolerance };
}
[Theory]
[MemberData(nameof(SpanDestinationFunctionsToTest))]
public void SpanDestinationFunctions_AllLengths(SpanDestinationDelegate tensorPrimitivesMethod, Func<T, T> expectedMethod, T? tolerance = null)
{
Assert.All(Helpers.TensorLengthsIncluding0, tensorLength =>
{
using BoundedMemory<T> x = CreateAndFillTensor(tensorLength);
using BoundedMemory<T> destination = CreateTensor(tensorLength);
tensorPrimitivesMethod(x.Span, destination.Span);
for (int i = 0; i < tensorLength; i++)
{
AssertEqualTolerance(expectedMethod(x[i]), destination[i], tolerance);
}
});
}
[Theory]
[MemberData(nameof(SpanDestinationFunctionsToTest))]
public void SpanDestinationFunctions_InPlace(SpanDestinationDelegate tensorPrimitivesMethod, Func<T, T> expectedMethod, T? tolerance = null)
{
Assert.All(Helpers.TensorLengthsIncluding0, tensorLength =>
{
using BoundedMemory<T> x = CreateAndFillTensor(tensorLength);
T[] xOrig = x.Span.ToArray();
tensorPrimitivesMethod(x.Span, x.Span);
for (int i = 0; i < tensorLength; i++)
{
AssertEqualTolerance(expectedMethod(xOrig[i]), x[i], tolerance);
}
});
}
[Theory]
[MemberData(nameof(SpanDestinationFunctionsToTest))]
public void SpanDestinationFunctions_SpecialValues(SpanDestinationDelegate tensorPrimitivesMethod, Func<T, T> expectedMethod, T? tolerance = null)
{
Assert.All(Helpers.TensorLengths, tensorLength =>
{
using BoundedMemory<T> x = CreateAndFillTensor(tensorLength);
using BoundedMemory<T> destination = CreateTensor(tensorLength);
RunForEachSpecialValue(() =>
{
tensorPrimitivesMethod(x.Span, destination.Span);
for (int i = 0; i < tensorLength; i++)
{
AssertEqualTolerance(expectedMethod(x[i]), destination[i], tolerance);
}
}, x);
});
}
[Theory]
[MemberData(nameof(SpanDestinationFunctionsToTest))]
public void SpanDestinationFunctions_ValueRange(SpanDestinationDelegate tensorPrimitivesMethod, Func<T, T> expectedMethod, T? tolerance = null)
{
Assert.All(VectorLengthAndIteratedRange(ConvertFromSingle(-100f), ConvertFromSingle(100f), ConvertFromSingle(3f)), arg =>
{
T[] x = new T[arg.Length];
T[] dest = new T[arg.Length];
x.AsSpan().Fill(arg.Element);
tensorPrimitivesMethod(x.AsSpan(), dest.AsSpan());
T expected = expectedMethod(arg.Element);
foreach (T actual in dest)
{
AssertEqualTolerance(expected, actual, tolerance);
}
});
}
#pragma warning disable xUnit1026 // Theory methods should use all of their parameters
[Theory]
[MemberData(nameof(SpanDestinationFunctionsToTest))]
public void SpanDestinationFunctions_ThrowsForTooShortDestination(SpanDestinationDelegate tensorPrimitivesMethod, Func<T, T> expectedMethod, T? tolerance = null)
{
_ = expectedMethod;
_ = tolerance;
Assert.All(Helpers.TensorLengths, tensorLength =>
{
using BoundedMemory<T> x = CreateAndFillTensor(tensorLength);
using BoundedMemory<T> destination = CreateTensor(tensorLength - 1);
AssertExtensions.Throws<ArgumentException>("destination", () => tensorPrimitivesMethod(x.Span, destination.Span));
});
}
[Theory]
[MemberData(nameof(SpanDestinationFunctionsToTest))]
public void SpanDestinationFunctions_ThrowsForOverlappingInputsWithOutputs(SpanDestinationDelegate tensorPrimitivesMethod, Func<T, T> expectedMethod, T? tolerance = null)
{
_ = expectedMethod;
_ = tolerance;
T[] array = new T[10];
AssertExtensions.Throws<ArgumentException>("destination", () => tensorPrimitivesMethod(array.AsSpan(1, 2), array.AsSpan(0, 2)));
AssertExtensions.Throws<ArgumentException>("destination", () => tensorPrimitivesMethod(array.AsSpan(1, 2), array.AsSpan(2, 2)));
}
#pragma warning restore xUnit1026
#endregion
#region Span,Span -> Destination
public static IEnumerable<object[]> SpanSpanDestinationFunctionsToTest()
{
yield return Create(TensorPrimitives.Atan2, T.Atan2);
yield return Create(TensorPrimitives.Atan2Pi, T.Atan2Pi);
yield return Create(TensorPrimitives.CopySign, T.CopySign);
yield return Create(TensorPrimitives.Hypot, T.Hypot);
yield return Create(TensorPrimitives.Ieee754Remainder, T.Ieee754Remainder);
yield return Create(TensorPrimitives.Log, T.Log);
yield return Create(TensorPrimitives.Max, T.Max);
yield return Create(TensorPrimitives.MaxNumber, T.MaxNumber);
yield return Create(TensorPrimitives.MaxMagnitude, T.MaxMagnitude);
yield return Create(TensorPrimitives.MaxMagnitudeNumber, T.MaxMagnitudeNumber);
yield return Create(TensorPrimitives.Min, T.Min);
yield return Create(TensorPrimitives.MinNumber, T.MinNumber);
yield return Create(TensorPrimitives.MinMagnitude, T.MinMagnitude);
yield return Create(TensorPrimitives.MinMagnitudeNumber, T.MinMagnitudeNumber);
yield return Create(TensorPrimitives.Pow, T.Pow, Helpers.DetermineTolerance<T>(doubleTolerance: 1e-13, floatTolerance: 1e-5f));
static object[] Create(SpanSpanDestinationDelegate tensorPrimitivesMethod, Func<T, T, T> expectedMethod, T? tolerance = null)
=> new object[] { tensorPrimitivesMethod, expectedMethod, tolerance };
}
[Theory]
[MemberData(nameof(SpanSpanDestinationFunctionsToTest))]
public void SpanSpanDestination_AllLengths(SpanSpanDestinationDelegate tensorPrimitivesMethod, Func<T, T, T> expectedMethod, T? tolerance = null)
{
Assert.All(Helpers.TensorLengthsIncluding0, tensorLength =>
{
using BoundedMemory<T> x = CreateAndFillTensor(tensorLength);
using BoundedMemory<T> y = CreateAndFillTensor(tensorLength);
using BoundedMemory<T> destination = CreateTensor(tensorLength);
tensorPrimitivesMethod(x, y, destination);
for (int i = 0; i < tensorLength; i++)
{
AssertEqualTolerance(expectedMethod(x[i], y[i]), destination[i], tolerance);
}
});
}
[Theory]
[MemberData(nameof(SpanSpanDestinationFunctionsToTest))]
public void SpanSpanDestination_InPlace(SpanSpanDestinationDelegate tensorPrimitivesMethod, Func<T, T, T> expectedMethod, T? tolerance = null)
{
Assert.All(Helpers.TensorLengthsIncluding0, tensorLength =>
{
using BoundedMemory<T> x = CreateAndFillTensor(tensorLength);
T[] xOrig = x.Span.ToArray();
tensorPrimitivesMethod(x, x, x);
for (int i = 0; i < tensorLength; i++)
{
AssertEqualTolerance(expectedMethod(xOrig[i], xOrig[i]), x[i], tolerance);
}
});
}
[Theory]
[MemberData(nameof(SpanSpanDestinationFunctionsToTest))]
public void SpanSpanDestination_SpecialValues(SpanSpanDestinationDelegate tensorPrimitivesMethod, Func<T, T, T> expectedMethod, T? tolerance = null)
{
Assert.All(Helpers.TensorLengths, tensorLength =>
{
using BoundedMemory<T> x = CreateAndFillTensor(tensorLength);
using BoundedMemory<T> y = CreateAndFillTensor(tensorLength);
using BoundedMemory<T> destination = CreateTensor(tensorLength);
RunForEachSpecialValue(() =>
{
tensorPrimitivesMethod(x.Span, y.Span, destination.Span);
for (int i = 0; i < tensorLength; i++)
{
AssertEqualTolerance(expectedMethod(x[i], y[i]), destination[i], tolerance);
}
}, x);
RunForEachSpecialValue(() =>
{
tensorPrimitivesMethod(x.Span, y.Span, destination.Span);
for (int i = 0; i < tensorLength; i++)
{
AssertEqualTolerance(expectedMethod(x[i], y[i]), destination[i], tolerance);
}
}, y);
});
}
[Theory]
[MemberData(nameof(SpanSpanDestinationFunctionsToTest))]
public void SpanSpanDestination_ThrowsForMismatchedLengths(SpanSpanDestinationDelegate tensorPrimitivesMethod, Func<T, T, T> expectedMethod, T? tolerance = null)
{
_ = expectedMethod;
_ = tolerance;
Assert.All(Helpers.TensorLengths, tensorLength =>
{
using BoundedMemory<T> x = CreateAndFillTensor(tensorLength);
using BoundedMemory<T> y = CreateAndFillTensor(tensorLength - 1);
using BoundedMemory<T> destination = CreateTensor(tensorLength);
Assert.Throws<ArgumentException>(() => tensorPrimitivesMethod(x, y, destination));
Assert.Throws<ArgumentException>(() => tensorPrimitivesMethod(y, x, destination));
});
}
[Theory]
[MemberData(nameof(SpanSpanDestinationFunctionsToTest))]
public void SpanSpanDestination_ThrowsForTooShortDestination(SpanSpanDestinationDelegate tensorPrimitivesMethod, Func<T, T, T> expectedMethod, T? tolerance = null)
{
_ = expectedMethod;
_ = tolerance;
Assert.All(Helpers.TensorLengths, tensorLength =>
{
using BoundedMemory<T> x = CreateAndFillTensor(tensorLength);
using BoundedMemory<T> y = CreateAndFillTensor(tensorLength);
using BoundedMemory<T> destination = CreateTensor(tensorLength - 1);
AssertExtensions.Throws<ArgumentException>("destination", () => tensorPrimitivesMethod(x, y, destination));
});
}
[Theory]
[MemberData(nameof(SpanSpanDestinationFunctionsToTest))]
public void SpanSpanDestination_ThrowsForOverlappingInputsWithOutputs(SpanSpanDestinationDelegate tensorPrimitivesMethod, Func<T, T, T> expectedMethod, T? tolerance = null)
{
_ = expectedMethod;
_ = tolerance;
T[] array = new T[10];
AssertExtensions.Throws<ArgumentException>("destination", () => tensorPrimitivesMethod(array.AsSpan(1, 2), array.AsSpan(5, 2), array.AsSpan(0, 2)));
AssertExtensions.Throws<ArgumentException>("destination", () => tensorPrimitivesMethod(array.AsSpan(1, 2), array.AsSpan(5, 2), array.AsSpan(2, 2)));
AssertExtensions.Throws<ArgumentException>("destination", () => tensorPrimitivesMethod(array.AsSpan(1, 2), array.AsSpan(5, 2), array.AsSpan(4, 2)));
AssertExtensions.Throws<ArgumentException>("destination", () => tensorPrimitivesMethod(array.AsSpan(1, 2), array.AsSpan(5, 2), array.AsSpan(6, 2)));
}
#endregion
#region Span,Scalar -> Destination
public static IEnumerable<object[]> SpanScalarDestinationFunctionsToTest()
{
yield return Create(TensorPrimitives.Atan2, T.Atan2);
yield return Create(TensorPrimitives.Atan2Pi, T.Atan2Pi);
yield return Create(TensorPrimitives.CopySign, T.CopySign);
yield return Create(TensorPrimitives.Ieee754Remainder, T.Ieee754Remainder);
yield return Create(TensorPrimitives.Pow, T.Pow, Helpers.DetermineTolerance<T>(doubleTolerance: 1e-13, floatTolerance: 1e-5f));
yield return Create(TensorPrimitives.Log, T.Log);
yield return Create(TensorPrimitives.Max, T.Max);
yield return Create(TensorPrimitives.MaxNumber, T.MaxNumber);
yield return Create(TensorPrimitives.MaxMagnitude, T.MaxMagnitude);
yield return Create(TensorPrimitives.MaxMagnitudeNumber, T.MaxMagnitudeNumber);
yield return Create(TensorPrimitives.Min, T.Min);
yield return Create(TensorPrimitives.MinNumber, T.MinNumber);
yield return Create(TensorPrimitives.MinMagnitude, T.MinMagnitude);
yield return Create(TensorPrimitives.MinMagnitudeNumber, T.MinMagnitudeNumber);
static object[] Create(SpanScalarDestinationDelegate<T, T, T> tensorPrimitivesMethod, Func<T, T, T> expectedMethod, T? tolerance = null)
=> new object[] { tensorPrimitivesMethod, expectedMethod, tolerance };
}
[Theory]
[MemberData(nameof(SpanScalarDestinationFunctionsToTest))]
public void SpanScalarDestination_AllLengths(SpanScalarDestinationDelegate<T, T, T> tensorPrimitivesMethod, Func<T, T, T> expectedMethod, T? tolerance = null)
{
Assert.All(Helpers.TensorLengthsIncluding0, tensorLength =>
{
using BoundedMemory<T> x = CreateAndFillTensor(tensorLength);
T y = NextRandom();
using BoundedMemory<T> destination = CreateTensor(tensorLength);
tensorPrimitivesMethod(x, y, destination);
for (int i = 0; i < tensorLength; i++)
{
AssertEqualTolerance(expectedMethod(x[i], y), destination[i], tolerance);
}
});
}
[Theory]
[MemberData(nameof(SpanScalarDestinationFunctionsToTest))]
public void SpanScalarDestination_InPlace(SpanScalarDestinationDelegate<T, T, T> tensorPrimitivesMethod, Func<T, T, T> expectedMethod, T? tolerance = null)
{
Assert.All(Helpers.TensorLengthsIncluding0, tensorLength =>
{
using BoundedMemory<T> x = CreateAndFillTensor(tensorLength);
T y = NextRandom();
T[] xOrig = x.Span.ToArray();
tensorPrimitivesMethod(x, y, x);
for (int i = 0; i < tensorLength; i++)
{
AssertEqualTolerance(expectedMethod(xOrig[i], y), x[i], tolerance);
}
});
}
[Theory]
[MemberData(nameof(SpanScalarDestinationFunctionsToTest))]
public void SpanScalarDestination_SpecialValues(SpanScalarDestinationDelegate<T, T, T> tensorPrimitivesMethod, Func<T, T, T> expectedMethod, T? tolerance = null)
{
Assert.All(Helpers.TensorLengths, tensorLength =>
{
using BoundedMemory<T> x = CreateAndFillTensor(tensorLength);
T y = NextRandom();
using BoundedMemory<T> destination = CreateTensor(tensorLength);
RunForEachSpecialValue(() =>
{
tensorPrimitivesMethod(x.Span, y, destination.Span);
for (int i = 0; i < tensorLength; i++)
{
AssertEqualTolerance(expectedMethod(x[i], y), destination[i], tolerance);
}
}, x);
});
}
[Theory]
[MemberData(nameof(SpanScalarDestinationFunctionsToTest))]
public void SpanScalarDestination_ThrowsForTooShortDestination(SpanScalarDestinationDelegate<T, T, T> tensorPrimitivesMethod, Func<T, T, T> expectedMethod, T? tolerance = null)
{
_ = expectedMethod;
_ = tolerance;
Assert.All(Helpers.TensorLengths, tensorLength =>
{
using BoundedMemory<T> x = CreateAndFillTensor(tensorLength);
T y = NextRandom();
using BoundedMemory<T> destination = CreateTensor(tensorLength - 1);
AssertExtensions.Throws<ArgumentException>("destination", () => tensorPrimitivesMethod(x, y, destination));
});
}
[Theory]
[MemberData(nameof(SpanScalarDestinationFunctionsToTest))]
public void SpanScalarDestination_ThrowsForOverlappingInputsWithOutputs(SpanScalarDestinationDelegate<T, T, T> tensorPrimitivesMethod, Func<T, T, T> expectedMethod, T? tolerance = null)
{
_ = expectedMethod;
_ = tolerance;
T[] array = new T[10];
AssertExtensions.Throws<ArgumentException>("destination", () => tensorPrimitivesMethod(array.AsSpan(1, 2), default, array.AsSpan(0, 2)));
AssertExtensions.Throws<ArgumentException>("destination", () => tensorPrimitivesMethod(array.AsSpan(1, 2), default, array.AsSpan(2, 2)));
}
#endregion
#region Scalar,Span -> Destination
public static IEnumerable<object[]> ScalarSpanFloatDestinationFunctionsToTest()
{
yield return Create(TensorPrimitives.Atan2, T.Atan2);
yield return Create(TensorPrimitives.Atan2Pi, T.Atan2Pi);
yield return Create(TensorPrimitives.Pow, T.Pow, Helpers.DetermineTolerance<T>(floatTolerance: 1e-5f));
yield return Create(TensorPrimitives.Ieee754Remainder, T.Ieee754Remainder);
static object[] Create(ScalarSpanDestinationDelegate tensorPrimitivesMethod, Func<T, T, T> expectedMethod, T? tolerance = null)
=> new object[] { tensorPrimitivesMethod, expectedMethod, tolerance };
}
[Theory]
[MemberData(nameof(ScalarSpanFloatDestinationFunctionsToTest))]
public void SpanScalarFloatDestination_AllLengths(ScalarSpanDestinationDelegate tensorPrimitivesMethod, Func<T, T, T> expectedMethod, T? tolerance = null)
{
Assert.All(Helpers.TensorLengthsIncluding0, tensorLength =>
{
T x = NextRandom();
using BoundedMemory<T> y = CreateAndFillTensor(tensorLength);
using BoundedMemory<T> destination = CreateTensor(tensorLength);
tensorPrimitivesMethod(x, y, destination);
for (int i = 0; i < tensorLength; i++)
{
AssertEqualTolerance(expectedMethod(x, y[i]), destination[i], tolerance);
}
});
}
[Theory]
[MemberData(nameof(ScalarSpanFloatDestinationFunctionsToTest))]
public void SpanScalarFloatDestination_InPlace(ScalarSpanDestinationDelegate tensorPrimitivesMethod, Func<T, T, T> expectedMethod, T? tolerance = null)
{
Assert.All(Helpers.TensorLengthsIncluding0, tensorLength =>
{
T x = NextRandom();
using BoundedMemory<T> y = CreateAndFillTensor(tensorLength);
T[] yOrig = y.Span.ToArray();
tensorPrimitivesMethod(x, y, y);
for (int i = 0; i < tensorLength; i++)
{
AssertEqualTolerance(expectedMethod(x, yOrig[i]), y[i], tolerance);
}
});
}
[Theory]
[MemberData(nameof(ScalarSpanFloatDestinationFunctionsToTest))]
public void ScalarSpanDestination_SpecialValues(ScalarSpanDestinationDelegate tensorPrimitivesMethod, Func<T, T, T> expectedMethod, T? tolerance = null)
{
Assert.All(Helpers.TensorLengths, tensorLength =>
{
T x = NextRandom();
using BoundedMemory<T> y = CreateAndFillTensor(tensorLength);
using BoundedMemory<T> destination = CreateTensor(tensorLength);
RunForEachSpecialValue(() =>
{
tensorPrimitivesMethod(x, y.Span, destination.Span);
for (int i = 0; i < tensorLength; i++)
{
AssertEqualTolerance(expectedMethod(x, y[i]), destination[i], tolerance);
}
}, y);
});
}
[Theory]
[MemberData(nameof(ScalarSpanFloatDestinationFunctionsToTest))]
public void SpanScalarFloatDestination_ThrowsForTooShortDestination(ScalarSpanDestinationDelegate tensorPrimitivesMethod, Func<T, T, T> expectedMethod, T? tolerance = null)
{
_ = expectedMethod;
_ = tolerance;
Assert.All(Helpers.TensorLengths, tensorLength =>
{
T x = NextRandom();
using BoundedMemory<T> y = CreateAndFillTensor(tensorLength);
using BoundedMemory<T> destination = CreateTensor(tensorLength - 1);
AssertExtensions.Throws<ArgumentException>("destination", () => tensorPrimitivesMethod(x, y, destination));
});
}
[Theory]
[MemberData(nameof(ScalarSpanFloatDestinationFunctionsToTest))]
public void SpanScalarFloatDestination_ThrowsForOverlappingInputsWithOutputs(ScalarSpanDestinationDelegate tensorPrimitivesMethod, Func<T, T, T> expectedMethod, T? tolerance = null)
{
_ = expectedMethod;
_ = tolerance;
T[] array = new T[10];
AssertExtensions.Throws<ArgumentException>("destination", () => tensorPrimitivesMethod(default, array.AsSpan(1, 2), array.AsSpan(0, 2)));
AssertExtensions.Throws<ArgumentException>("destination", () => tensorPrimitivesMethod(default, array.AsSpan(1, 2), array.AsSpan(2, 2)));
}
#endregion
#region Span,Int,Span -> Destination
public static IEnumerable<object[]> SpanIntDestinationFunctionsToTest()
{
yield return Create(TensorPrimitives.RootN, T.RootN, Helpers.DetermineTolerance<T>(doubleTolerance: 1e-13));
yield return Create(TensorPrimitives.ScaleB, T.ScaleB);
static object[] Create(SpanScalarDestinationDelegate<T, int, T> tensorPrimitivesMethod, Func<T, int, T> expectedMethod, T? tolerance = null)
=> new object[] { tensorPrimitivesMethod, expectedMethod, tolerance };
}
[Theory]
[MemberData(nameof(SpanIntDestinationFunctionsToTest))]
public void SpanIntDestination_AllLengths(SpanScalarDestinationDelegate<T, int, T> tensorPrimitivesMethod, Func<T, int, T> expectedMethod, T? tolerance = null)
{
Assert.All(Helpers.TensorLengthsIncluding0, tensorLength =>
{
using BoundedMemory<T> x = CreateAndFillTensor(tensorLength);
int y = Random.Next(1, 10);
using BoundedMemory<T> destination = CreateTensor(tensorLength);
tensorPrimitivesMethod(x, y, destination);
for (int i = 0; i < tensorLength; i++)
{
AssertEqualTolerance(expectedMethod(x[i], y), destination[i], tolerance);
}
});
}
[Theory]