Skip to content

Commit 4c01282

Browse files
committed
WIP Runtime dispatch tests
1 parent 8153f9b commit 4c01282

File tree

1 file changed

+105
-3
lines changed

1 file changed

+105
-3
lines changed

test/UTF8ValidationTests.cs

Lines changed: 105 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace tests;
66
using System.Runtime.Intrinsics;
77
using System.Runtime.Intrinsics.X86;
88
using System.Runtime.Intrinsics.Arm;
9-
9+
using BenchmarkDotNet.Disassemblers;
1010

1111
public unsafe class Utf8SIMDValidationTests
1212
{
@@ -19,6 +19,89 @@ public unsafe class Utf8SIMDValidationTests
1919
// int[] outputLengths = { 128, 192, 256, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024, 1088, 1152, 1216, 1280, 1344, 1408, 1472, 1536, 1600, 1664, 1728, 1792, 1856, 1920, 1984, 2048, 2112, 2176, 2240, 2304, 2368, 2432, 2496, 2560, 2624, 2688, 2752, 2816, 2880, 2944, 3008, 3072, 3136, 3200, 3264, 3328, 3392, 3456, 3520, 3584, 3648, 3712, 3776, 3840, 3904, 3968, 4032, 4096, 4160, 4224, 4288, 4352, 4416, 4480, 4544, 4608, 4672, 4736, 4800, 4864, 4928, 4992, 5056, 5120, 5184, 5248, 5312, 5376, 5440, 5504, 5568, 5632, 5696, 5760, 5824, 5888, 5952, 6016, 6080, 6144, 6208, 6272, 6336, 6400, 6464, 6528, 6592, 6656, 6720, 6784, 6848, 6912, 6976, 7040, 7104, 7168, 7232, 7296, 7360, 7424, 7488, 7552, 7616, 7680, 7744, 7808, 7872, 7936, 8000, 8064, 8128, 8192, 8256, 8320, 8384, 8448, 8512, 8576, 8640, 8704, 8768, 8832, 8896, 8960, 9024, 9088, 9152, 9216, 9280, 9344, 9408, 9472, 9536, 9600, 9664, 9728, 9792, 9856, 9920, 9984, 10000 };
2020
static int[] outputLengths = { 128, 256,345, 512,968, 1024, 1000 }; // Example lengths
2121

22+
// public class FactOnArchitectureAttribute : FactAttribute
23+
// {
24+
// public FactOnArchitectureAttribute(System.Runtime.InteropServices.Architecture architecture)
25+
// {
26+
// if (System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture != architecture)
27+
// {
28+
// Skip = $"Test is skipped because it runs only on {architecture} architecture";
29+
// }
30+
// }
31+
// }
32+
33+
[Flags]
34+
public enum TestSystemRequirements
35+
{
36+
None = 0,
37+
Arm64 = 1,
38+
X64Avx512 = 2,
39+
X64Avx2 = 4,
40+
X64Sse = 8,
41+
// Add more as needed
42+
}
43+
44+
public class FactOnSystemRequirementAttribute : FactAttribute
45+
{
46+
private TestSystemRequirements RequiredSystems;
47+
48+
public FactOnSystemRequirementAttribute(TestSystemRequirements requiredSystems)
49+
{
50+
RequiredSystems = requiredSystems;
51+
52+
if (!IsSystemSupported(requiredSystems))
53+
{
54+
Skip = "Test is skipped due to not meeting system requirements.";
55+
}
56+
}
57+
58+
private bool IsSystemSupported(TestSystemRequirements requiredSystems)
59+
{
60+
var currentArchitecture = RuntimeInformation.ProcessArchitecture;
61+
bool isSupported = false;
62+
63+
if (currentArchitecture == Architecture.Arm64 && requiredSystems.HasFlag(TestSystemRequirements.Arm64))
64+
{
65+
isSupported = true;
66+
}
67+
else if (currentArchitecture == Architecture.X64)
68+
{
69+
if (requiredSystems.HasFlag(TestSystemRequirements.X64Avx512) && Vector512.IsHardwareAccelerated && System.Runtime.Intrinsics.X86.Avx512F.IsSupported)
70+
{
71+
isSupported = true;
72+
}
73+
else if (requiredSystems.HasFlag(TestSystemRequirements.X64Avx2) && System.Runtime.Intrinsics.X86.Avx2.IsSupported)
74+
{
75+
isSupported = true;
76+
}
77+
else if (requiredSystems.HasFlag(TestSystemRequirements.X64Sse) && System.Runtime.Intrinsics.X86.Sse.IsSupported)
78+
{
79+
isSupported = true;
80+
}
81+
}
82+
83+
// Implement other architecture checks as needed
84+
85+
return isSupported;
86+
}
87+
}
88+
89+
90+
public class TestIfCondition : FactAttribute
91+
{
92+
public TestIfCondition(Func<bool> condition, string skipReason)
93+
{
94+
// if (condition == null) throw new ArgumentNullException(nameof(condition));
95+
96+
// Only set the Skip property if the condition evaluates to false
97+
if (!condition.Invoke())
98+
{
99+
Skip = skipReason;
100+
}
101+
}
102+
}
103+
104+
22105

23106
public void TestGoodSequences(Utf8ValidationDelegate utf8ValidationDelegate)
24107
{
@@ -186,21 +269,40 @@ public void TooShortErrorTest(Utf8ValidationDelegate utf8ValidationDelegate)
186269

187270
}
188271

189-
[Fact]
272+
// [FactOnArchitecture(System.Runtime.InteropServices.Architecture.X64)]
273+
// [TestIfCondition(RuntimeInformation.ProcessArchitecture == Architecture.X64,"a reason")]
274+
[FactOnSystemRequirementAttribute(TestSystemRequirements.Arm64)]
190275
[Trait("Category", "scalar")]
191276
public void TooShortErrorTestScalar()
192277
{
193278
TooShortErrorTest(SimdUnicode.UTF8.GetPointerToFirstInvalidByteScalar);
194279
}
195280

196-
// Uncomment when SSE is updated
281+
// TODO:Uncomment when SSE is updated
282+
// [FactOnArchitecture(System.Runtime.InteropServices.Architecture.X64)]
197283
// [Fact]
198284
// [Trait("Category", "sse")]
199285
// public void TooShortErrorTestSse()
200286
// {
201287
// TooShortErrorTest(SimdUnicode.UTF8.GetPointerToFirstInvalidByteSse);
202288
// }
203289

290+
// TODO:Uncomment when AVX512 is updated
291+
// [Fact]
292+
// [Trait("Category", "avx512")]
293+
// public void TooShortErrorTestSse()
294+
// {
295+
// TooShortErrorTest(SimdUnicode.UTF8.GetPointerToFirstInvalidByteSse);
296+
// }
297+
298+
// TODO:Uncomment when Arm64 is updated
299+
// [FactOnArchitecture(System.Runtime.InteropServices.Architecture.Arm64)]
300+
// [Trait("Category", "arm64")]
301+
// public void TooShortErrorTestArm64()
302+
// {
303+
// TooShortErrorTest(SimdUnicode.UTF8.GetPointerToFirstInvalidByteArm64);
304+
// }
305+
204306
[Fact]
205307
[Trait("Category", "avx")]
206308
public void TooShortErrorTestAVX()

0 commit comments

Comments
 (0)