Skip to content

Commit 27f4fca

Browse files
committed
Add regression tests for trimming and hash table reordering
1 parent 627ecf1 commit 27f4fca

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

Src/FastData.Tests/FastDataGeneratorTests.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using Genbox.FastData.Enums;
2+
using Genbox.FastData.Generators;
3+
using Genbox.FastData.Generators.Abstracts;
4+
using Genbox.FastData.Generators.Contexts;
25
using Genbox.FastData.InternalShared;
36
using Genbox.FastData.InternalShared.TestClasses;
47

@@ -19,6 +22,54 @@ public void Generate_ThrowOnInvalidType()
1922
Assert.Throws<InvalidOperationException>(() => FastDataGenerator.Generate([DateTime.Now, DateTime.UtcNow], new FastDataConfig(StructureType.Array), new DummyGenerator()));
2023
}
2124

25+
[Fact]
26+
public void GenerateKeyed_HashTablePerfect_ReordersValuesToMatchSlots()
27+
{
28+
int[] keys = [2, 0, 1];
29+
string[] values = ["v2", "v0", "v1"];
30+
FastDataConfig config = new FastDataConfig(StructureType.HashTable) { HashCapacityFactor = 1 };
31+
ContextCaptureGenerator generator = new ContextCaptureGenerator();
32+
33+
FastDataGenerator.GenerateKeyed(keys, values, config, generator);
34+
35+
HashTablePerfectContext<int, string> ctx = Assert.IsType<HashTablePerfectContext<int, string>>(generator.Context);
36+
Assert.NotNull(ctx.Values);
37+
38+
for (int i = 0; i < ctx.Data.Length; i++)
39+
{
40+
KeyValuePair<int, ulong> entry = ctx.Data[i];
41+
string value = ctx.Values![i];
42+
Assert.Equal($"v{entry.Key}", value);
43+
}
44+
}
45+
46+
[Fact]
47+
public void GenerateKeyed_HashTablePerfect_StoresHashCodeWhenCapacityFactorIsHigh()
48+
{
49+
int[] keys = [0, 1, 2];
50+
string[] values = ["v0", "v1", "v2"];
51+
FastDataConfig config = new FastDataConfig(StructureType.HashTable) { HashCapacityFactor = 2 };
52+
ContextCaptureGenerator generator = new ContextCaptureGenerator();
53+
54+
FastDataGenerator.GenerateKeyed(keys, values, config, generator);
55+
56+
HashTablePerfectContext<int, string> ctx = Assert.IsType<HashTablePerfectContext<int, string>>(generator.Context);
57+
Assert.True(ctx.StoreHashCode);
58+
}
59+
60+
[Fact]
61+
public void Generate_Trimming_UsesSuffixFromEnd()
62+
{
63+
string[] keys = ["prefooSUF", "prebarSUF"];
64+
FastDataConfig config = new FastDataConfig(StructureType.Array) { EnableTrimming = true };
65+
TrimCaptureGenerator generator = new TrimCaptureGenerator();
66+
67+
FastDataGenerator.Generate(keys, config, generator);
68+
69+
Assert.Equal("pre", generator.TrimPrefix);
70+
Assert.Equal("SUF", generator.TrimSuffix);
71+
}
72+
2273
// [Fact]
2374
// public async Task Generate_SpanSupport()
2475
// {
@@ -56,4 +107,32 @@ public static TheoryData<ITestData> GetTestData()
56107
}
57108
return data;
58109
}
110+
111+
private sealed class ContextCaptureGenerator : ICodeGenerator
112+
{
113+
public GeneratorEncoding Encoding => GeneratorEncoding.UTF8;
114+
115+
public object? Context { get; private set; }
116+
117+
public string Generate<TKey, TValue>(GeneratorConfig<TKey> genCfg, IContext<TValue> context)
118+
{
119+
Context = context;
120+
return string.Empty;
121+
}
122+
}
123+
124+
private sealed class TrimCaptureGenerator : ICodeGenerator
125+
{
126+
public GeneratorEncoding Encoding => GeneratorEncoding.UTF8;
127+
128+
public string TrimPrefix { get; private set; } = string.Empty;
129+
public string TrimSuffix { get; private set; } = string.Empty;
130+
131+
public string Generate<TKey, TValue>(GeneratorConfig<TKey> genCfg, IContext<TValue> context)
132+
{
133+
TrimPrefix = genCfg.TrimPrefix;
134+
TrimSuffix = genCfg.TrimSuffix;
135+
return string.Empty;
136+
}
137+
}
59138
}

0 commit comments

Comments
 (0)