Skip to content

Commit f960131

Browse files
committed
Convert sourcegen tests to verify
1 parent dcfe231 commit f960131

11 files changed

Lines changed: 401 additions & 57 deletions

Src/FastData.SourceGenerator.Tests/SourceGeneratorTests.cs

Lines changed: 22 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,12 @@ public void GenericTest()
2929
Assert.False(func("dontexist"));
3030
}
3131

32-
[Theory]
33-
[InlineData(StructureType.SingleValue)]
34-
public void DataStructureSingleTest(StructureType ds)
35-
{
36-
string source = $"""
37-
using Genbox.FastData.SourceGenerator;
38-
using Genbox.FastData.Enums;
39-
40-
[assembly: FastData<string>("StaticData", ["item1"], StructureType = StructureType.{ds})]
41-
""";
42-
43-
string output = RunGenerator(source);
44-
45-
Assert.Contains("Structure: " + ds, output, StringComparison.Ordinal);
46-
}
47-
4832
[Theory]
4933
[InlineData(StructureType.Array)]
5034
[InlineData(StructureType.Conditional)]
5135
[InlineData(StructureType.BinarySearch)]
52-
[InlineData(StructureType.EytzingerSearch)]
53-
[InlineData(StructureType.PerfectHashGPerf)]
54-
[InlineData(StructureType.HashSetPerfect)]
55-
[InlineData(StructureType.HashSetChain)]
56-
[InlineData(StructureType.HashSetLinear)]
57-
[InlineData(StructureType.KeyLength)]
58-
public void DataStructureTest(StructureType ds)
36+
[InlineData(StructureType.HashSet)]
37+
public async Task DataStructureTest(StructureType ds)
5938
{
6039
string source = $"""
6140
using Genbox.FastData.SourceGenerator;
@@ -65,12 +44,14 @@ public void DataStructureTest(StructureType ds)
6544
""";
6645

6746
string output = RunGenerator(source);
68-
69-
Assert.Contains("Structure: " + ds, output, StringComparison.Ordinal);
47+
await Verify(output)
48+
.UseFileName(ds.ToString())
49+
.UseDirectory("Verify")
50+
.DisableDiff();
7051
}
7152

7253
[Fact]
73-
public void NamespaceTest()
54+
public async Task NamespaceTest()
7455
{
7556
const string source = """
7657
using Genbox.FastData.SourceGenerator;
@@ -79,13 +60,16 @@ public void NamespaceTest()
7960
""";
8061

8162
string output = RunGenerator(source);
82-
Assert.Contains("namespace MyNamespace;", output, StringComparison.Ordinal);
63+
await Verify(output)
64+
.UseFileName("namespace")
65+
.UseDirectory("Verify")
66+
.DisableDiff();
8367
}
8468

8569
[Theory]
8670
[InlineData(ClassVisibility.Internal)]
8771
[InlineData(ClassVisibility.Public)]
88-
public void ClassVisibilityTest(ClassVisibility visibility)
72+
public async Task ClassVisibilityTest(ClassVisibility visibility)
8973
{
9074
string source = $"""
9175
using Genbox.FastData.SourceGenerator;
@@ -96,25 +80,17 @@ public void ClassVisibilityTest(ClassVisibility visibility)
9680

9781
string output = RunGenerator(source);
9882

99-
switch (visibility)
100-
{
101-
case ClassVisibility.Internal:
102-
Assert.Contains("internal static class StaticData", output, StringComparison.Ordinal);
103-
break;
104-
case ClassVisibility.Public:
105-
Assert.Contains("public static class StaticData", output, StringComparison.Ordinal);
106-
break;
107-
case ClassVisibility.Unknown:
108-
default:
109-
throw new ArgumentOutOfRangeException(nameof(visibility), visibility, null);
110-
}
83+
await Verify(output)
84+
.UseFileName(visibility.ToString())
85+
.UseDirectory("Verify")
86+
.DisableDiff();
11187
}
11288

11389
[Theory]
11490
[InlineData(ClassType.Static)]
11591
[InlineData(ClassType.Instance)]
11692
[InlineData(ClassType.Struct)]
117-
public void ClassTypeTest(ClassType type)
93+
public async Task ClassTypeTest(ClassType type)
11894
{
11995
string source = $"""
12096
using Genbox.FastData.SourceGenerator;
@@ -125,21 +101,10 @@ public void ClassTypeTest(ClassType type)
125101

126102
string output = RunGenerator(source);
127103

128-
switch (type)
129-
{
130-
case ClassType.Static:
131-
Assert.Contains("static class StaticData", output, StringComparison.Ordinal);
132-
break;
133-
case ClassType.Instance:
134-
Assert.Contains("partial class StaticData", output, StringComparison.Ordinal);
135-
break;
136-
case ClassType.Struct:
137-
Assert.Contains("partial struct StaticData", output, StringComparison.Ordinal);
138-
break;
139-
case ClassType.Unknown:
140-
default:
141-
throw new ArgumentOutOfRangeException(nameof(type), type, null);
142-
}
104+
await Verify(output)
105+
.UseFileName(type.ToString())
106+
.UseDirectory("Verify")
107+
.DisableDiff();
143108
}
144109

145110
//TODO: Test StorageOptions
@@ -149,7 +114,7 @@ private static string RunGenerator(string source)
149114
string output = SourceGenHelper.RunSourceGenerator<Internal.FastDataSourceGenerator>(source, false, out var compilerDiagnostics, out var codeGenDiagnostics);
150115
Assert.Empty(compilerDiagnostics);
151116
Assert.Empty(codeGenDiagnostics);
152-
Assert.NotEmpty(output); //We test source later in order to show diagnostics first
117+
Assert.NotEmpty(output); //We test the source later to show diagnostics first
153118

154119
return output;
155120
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// <auto-generated />
2+
// This file is auto-generated. Do not edit manually.
3+
// Structure: Array
4+
#nullable enable
5+
using System;
6+
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
8+
9+
10+
internal static class StaticData
11+
{
12+
private static readonly string[] _entries = new string[] {
13+
"item1", "item2", "item3"
14+
};
15+
16+
17+
public static bool Contains(string value)
18+
{
19+
if (value.Length != 5u)
20+
return false;
21+
22+
for (int i = 0; i < 3; i++)
23+
{
24+
if (StringComparer.Ordinal.Equals(value, _entries[i]))
25+
return true;
26+
}
27+
return false;
28+
}
29+
30+
public const uint ItemCount = 3;
31+
public const uint MinLength = 5;
32+
public const uint MaxLength = 5;
33+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// <auto-generated />
2+
// This file is auto-generated. Do not edit manually.
3+
// Structure: BinarySearch
4+
#nullable enable
5+
using System;
6+
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
8+
9+
10+
internal static class StaticData
11+
{
12+
private static readonly string[] _entries = new string[] {
13+
"item1", "item2", "item3"
14+
};
15+
16+
17+
public static bool Contains(string value)
18+
{
19+
if (value.Length != 5u)
20+
return false;
21+
22+
int lo = 0;
23+
int hi = 2;
24+
while (lo <= hi)
25+
{
26+
int i = lo + ((hi - lo) >> 1);
27+
int order = StringComparer.Ordinal.Compare(_entries[i], value);
28+
29+
if (order == 0)
30+
return true;
31+
if (order < 0)
32+
lo = i + 1;
33+
else
34+
hi = i - 1;
35+
}
36+
37+
return ~lo >= 0;
38+
}
39+
40+
public const uint ItemCount = 3;
41+
public const uint MinLength = 5;
42+
public const uint MaxLength = 5;
43+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// <auto-generated />
2+
// This file is auto-generated. Do not edit manually.
3+
// Structure: Conditional
4+
#nullable enable
5+
using System;
6+
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
8+
9+
10+
internal static class StaticData
11+
{
12+
13+
public static bool Contains(string value)
14+
{
15+
if (value.Length != 5u)
16+
return false;
17+
18+
switch (value)
19+
{
20+
case "item1":
21+
case "item2":
22+
case "item3":
23+
return true;
24+
default:
25+
return false;
26+
}
27+
}
28+
29+
public const uint ItemCount = 3;
30+
public const uint MinLength = 5;
31+
public const uint MaxLength = 5;
32+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// <auto-generated />
2+
// This file is auto-generated. Do not edit manually.
3+
// Structure: HashSet
4+
#nullable enable
5+
using System;
6+
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
8+
9+
10+
internal static class StaticData
11+
{
12+
private static readonly sbyte[] _buckets = new sbyte[] {
13+
1, 3, 2
14+
};
15+
16+
private static readonly E[] _entries = {
17+
new E(17825845005562073391, -1, "item1"), new E(17825845003995989450, -1, "item2"), new E(17825845002429905509, -1, "item3")
18+
};
19+
20+
21+
public static bool Contains(string value)
22+
{
23+
if (value.Length != 5u)
24+
return false;
25+
26+
ulong hash = Hash(value);
27+
uint index = (uint)(hash % 3);
28+
sbyte i = (sbyte)(_buckets[index] - 1);
29+
30+
while (i >= 0)
31+
{
32+
ref E entry = ref _entries[i];
33+
34+
if (entry.HashCode == hash && StringComparer.Ordinal.Equals(value, entry.Value))
35+
return true;
36+
37+
i = entry.Next;
38+
}
39+
40+
return false;
41+
}
42+
43+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
44+
private static ulong Hash(string value)
45+
{
46+
ulong hash = 352654597;
47+
48+
ref char ptr = ref MemoryMarshal.GetReference(value.AsSpan());
49+
int len = value.Length;
50+
51+
while (len-- > 0)
52+
{
53+
hash = (((hash << 5) | (hash >> 27)) + hash) ^ ptr;
54+
ptr = ref Unsafe.Add(ref ptr, 1);
55+
}
56+
57+
return 352654597 + (hash * 1566083941);
58+
}
59+
60+
[StructLayout(LayoutKind.Auto)]
61+
private readonly struct E
62+
{
63+
internal readonly ulong HashCode;
64+
internal readonly sbyte Next;
65+
internal readonly string Value;
66+
67+
internal E(ulong hashCode, sbyte next, string value)
68+
{
69+
HashCode = hashCode;
70+
Next = next;
71+
Value = value;
72+
}
73+
}
74+
75+
public const uint ItemCount = 3;
76+
public const uint MinLength = 5;
77+
public const uint MaxLength = 5;
78+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// <auto-generated />
2+
// This file is auto-generated. Do not edit manually.
3+
// Structure: Auto
4+
#nullable enable
5+
using System;
6+
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
8+
9+
10+
internal partial class StaticData
11+
{
12+
13+
public bool Contains(string value)
14+
{
15+
if (value.Length != 5u)
16+
return false;
17+
18+
switch (value)
19+
{
20+
case "item1":
21+
case "item2":
22+
case "item3":
23+
return true;
24+
default:
25+
return false;
26+
}
27+
}
28+
29+
public const uint ItemCount = 3;
30+
public const uint MinLength = 5;
31+
public const uint MaxLength = 5;
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// <auto-generated />
2+
// This file is auto-generated. Do not edit manually.
3+
// Structure: Auto
4+
#nullable enable
5+
using System;
6+
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
8+
9+
10+
internal static class StaticData
11+
{
12+
13+
public static bool Contains(string value)
14+
{
15+
if (value.Length != 5u)
16+
return false;
17+
18+
switch (value)
19+
{
20+
case "item1":
21+
case "item2":
22+
case "item3":
23+
return true;
24+
default:
25+
return false;
26+
}
27+
}
28+
29+
public const uint ItemCount = 3;
30+
public const uint MinLength = 5;
31+
public const uint MaxLength = 5;
32+
}

0 commit comments

Comments
 (0)