Skip to content

Commit 9b559fa

Browse files
committed
Migrate float nan/zero optimization to a feature test
1 parent 4b29641 commit 9b559fa

12 files changed

+187
-64
lines changed

Src/FastData.Generator.CPlusPlus.Tests/FeatureTests.cs

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,62 @@ await Verify(spec.Source)
3535
CPlusPlusLanguageDef langDef = new CPlusPlusLanguageDef();
3636
TypeMap map = new TypeMap(langDef.TypeDefinitions, GeneratorEncoding.ASCII);
3737

38-
string testSource = $$"""
39-
#include <string>
40-
#include <iostream>
38+
string source = $$"""
39+
#include <string>
40+
#include <iostream>
4141
42-
{{spec.Source}}
42+
{{spec.Source}}
4343
44-
int main()
45-
{
46-
const Person* res;
47-
{{FormatList(vector.Keys, x => $"""
48-
if (!{spec.Identifier}::try_lookup({map.ToValueLabel(x)}, res))
49-
return 0;
50-
""", "\n")}}
44+
int main()
45+
{
46+
const Person* res;
47+
{{FormatList(vector.Keys, x => $"""
48+
if (!{spec.Identifier}::try_lookup({map.ToValueLabel(x)}, res))
49+
return 0;
50+
""", "\n")}}
5151
52-
return 1;
53-
}
54-
""";
52+
return 1;
53+
}
54+
""";
5555

56-
string executable = context.Compiler.Compile(id, testSource);
56+
string executable = context.Compiler.Compile(id, source);
57+
Assert.Equal(1, RunProcess(executable));
58+
}
59+
60+
[Theory]
61+
[ClassData(typeof(FloatNaNZeroTestVectorTheoryData))]
62+
public async Task FloatNaNOrZeroHashSupportTest<T>(TestVector<T> vector)
63+
{
64+
GeneratorSpec spec = Generate(id => CPlusPlusCodeGenerator.Create(new CPlusPlusCodeGeneratorConfig(id)), vector);
65+
66+
string id = $"{nameof(FloatNaNOrZeroHashSupportTest)}-{spec.Identifier}";
67+
68+
await Verify(spec.Source)
69+
.UseFileName(id)
70+
.UseDirectory("Features")
71+
.DisableDiff();
72+
73+
CPlusPlusLanguageDef langDef = new CPlusPlusLanguageDef();
74+
TypeMap map = new TypeMap(langDef.TypeDefinitions, GeneratorEncoding.ASCII);
75+
76+
string source = $$"""
77+
#include <string>
78+
#include <iostream>
79+
80+
{{spec.Source}}
81+
82+
int main()
83+
{
84+
{{FormatList(vector.Keys, x => $"""
85+
if (!{spec.Identifier}::contains({map.ToValueLabel(x)}))
86+
return 0;
87+
""", "\n")}}
88+
89+
return 1;
90+
}
91+
""";
92+
93+
string executable = context.Compiler.Compile(id, source);
5794
Assert.Equal(1, RunProcess(executable));
5895
}
5996

Src/FastData.Generator.CPlusPlus.Tests/Vectors/HashTableStructure_Double_5_HashZeroOrNaN.verified.txt renamed to Src/FastData.Generator.CPlusPlus.Tests/Features/FloatNaNOrZeroHashSupportTest-HashTableStructure_Double_5.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <limits>
77
#include <string_view>
88

9-
class HashTableStructure_Double_5_HashZeroOrNaN final {
9+
class HashTableStructure_Double_5 final {
1010
struct e {
1111
double key;
1212
int8_t next;

Src/FastData.Generator.CPlusPlus.Tests/Vectors/HashTableStructure_Single_5_HashZeroOrNaN.verified.txt renamed to Src/FastData.Generator.CPlusPlus.Tests/Features/FloatNaNOrZeroHashSupportTest-HashTableStructure_Single_5.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <limits>
77
#include <string_view>
88

9-
class HashTableStructure_Single_5_HashZeroOrNaN final {
9+
class HashTableStructure_Single_5 final {
1010
struct e {
1111
float key;
1212
int8_t next;

Src/FastData.Generator.CPlusPlus.Tests/VectorTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ int main()
4646
""";
4747

4848
string executable = context.Compiler.Compile(spec.Identifier, source);
49-
5049
Assert.Equal(1, RunProcess(executable));
5150
}
5251
}

Src/FastData.Generator.CSharp.Tests/FeatureTests.cs

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,61 @@ await Verify(spec.Source)
3636
CSharpLanguageDef langDef = new CSharpLanguageDef();
3737
TypeMap map = new TypeMap(langDef.TypeDefinitions, GeneratorEncoding.ASCII);
3838

39-
string wrapper = $$"""
40-
{{spec.Source}}
41-
42-
public static class Wrapper
43-
{
44-
public static bool Run()
45-
{
46-
{{FormatList(vector.Keys, x => $"""
47-
if (!{spec.Identifier}.TryLookup({map.ToValueLabel(x)}, out _))
48-
return false;
49-
""", "\n")}}
50-
51-
return true;
52-
}
53-
}
54-
""";
55-
56-
Func<bool> contains = CompilationHelper.GetDelegate<Func<bool>>(wrapper, types => types.First(x => x.Name == "Wrapper"), false);
39+
string source = $$"""
40+
{{spec.Source}}
41+
42+
public static class Wrapper
43+
{
44+
public static bool Run()
45+
{
46+
{{FormatList(vector.Keys, x => $"""
47+
if (!{spec.Identifier}.TryLookup({map.ToValueLabel(x)}, out _))
48+
return false;
49+
""", "\n")}}
50+
51+
return true;
52+
}
53+
}
54+
""";
55+
56+
Func<bool> contains = CompilationHelper.GetDelegate<Func<bool>>(source, types => types.First(x => x.Name == "Wrapper"), false);
57+
Assert.True(contains());
58+
}
59+
60+
[Theory]
61+
[ClassData(typeof(FloatNaNZeroTestVectorTheoryData))]
62+
public async Task FloatNaNOrZeroHashSupportTest<T>(TestVector<T> vector)
63+
{
64+
GeneratorSpec spec = Generate(id => CSharpCodeGenerator.Create(new CSharpCodeGeneratorConfig(id)), vector);
65+
66+
string id = $"{nameof(FloatNaNOrZeroHashSupportTest)}-{spec.Identifier}";
67+
68+
await Verify(spec.Source)
69+
.UseFileName(id)
70+
.UseDirectory("Features")
71+
.DisableDiff();
72+
73+
CSharpLanguageDef langDef = new CSharpLanguageDef();
74+
TypeMap map = new TypeMap(langDef.TypeDefinitions, GeneratorEncoding.ASCII);
75+
76+
string source = $$"""
77+
{{spec.Source}}
78+
79+
public static class Wrapper
80+
{
81+
public static bool Run()
82+
{
83+
{{FormatList(vector.Keys, x => $"""
84+
if (!{spec.Identifier}.Contains({map.ToValueLabel(x)}))
85+
return false;
86+
""", "\n")}}
87+
88+
return true;
89+
}
90+
}
91+
""";
92+
93+
Func<bool> contains = CompilationHelper.GetDelegate<Func<bool>>(source, types => types.First(x => x.Name == "Wrapper"), false);
5794
Assert.True(contains());
5895
}
5996

Src/FastData.Generator.CSharp.Tests/Vectors/HashTableStructure_Double_5_HashZeroOrNaN.verified.txt renamed to Src/FastData.Generator.CSharp.Tests/Features/FloatNaNOrZeroHashSupportTest-HashTableStructure_Double_5.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using System;
66
using System.Runtime.CompilerServices;
77
using System.Runtime.InteropServices;
88

9-
internal static class HashTableStructure_Double_5_HashZeroOrNaN
9+
internal static class HashTableStructure_Double_5
1010
{
1111
[StructLayout(LayoutKind.Auto)]
1212
private struct E

Src/FastData.Generator.CSharp.Tests/Vectors/HashTableStructure_Single_5_HashZeroOrNaN.verified.txt renamed to Src/FastData.Generator.CSharp.Tests/Features/FloatNaNOrZeroHashSupportTest-HashTableStructure_Single_5.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using System;
66
using System.Runtime.CompilerServices;
77
using System.Runtime.InteropServices;
88

9-
internal static class HashTableStructure_Single_5_HashZeroOrNaN
9+
internal static class HashTableStructure_Single_5
1010
{
1111
[StructLayout(LayoutKind.Auto)]
1212
private struct E

Src/FastData.Generator.Rust.Tests/FeatureTests.cs

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,57 @@ await Verify(spec.Source)
3535
RustLanguageDef langDef = new RustLanguageDef();
3636
TypeMap map = new TypeMap(langDef.TypeDefinitions, GeneratorEncoding.ASCII);
3737

38-
string testSource = $$"""
39-
#![allow(non_camel_case_types)]
40-
{{spec.Source}}
41-
42-
fn main() {
43-
{{FormatList(vector.Keys, x => $$"""
44-
if ({{spec.Identifier}}::try_lookup({{map.ToValueLabel(x)}}).is_none()) {
45-
std::process::exit(0);
46-
}
47-
""", "\n")}}
48-
49-
std::process::exit(1);
50-
}
51-
""";
52-
53-
string executable = context.Compiler.Compile(id, testSource);
38+
string source = $$"""
39+
#![allow(non_camel_case_types)]
40+
{{spec.Source}}
41+
42+
fn main() {
43+
{{FormatList(vector.Keys, x => $$"""
44+
if ({{spec.Identifier}}::try_lookup({{map.ToValueLabel(x)}}).is_none()) {
45+
std::process::exit(0);
46+
}
47+
""", "\n")}}
48+
49+
std::process::exit(1);
50+
}
51+
""";
52+
53+
string executable = context.Compiler.Compile(id, source);
54+
Assert.Equal(1, RunProcess(executable));
55+
}
56+
57+
[Theory]
58+
[ClassData(typeof(FloatNaNZeroTestVectorTheoryData))]
59+
public async Task FloatNaNOrZeroHashSupportTest<T>(TestVector<T> vector)
60+
{
61+
GeneratorSpec spec = Generate(id => RustCodeGenerator.Create(new RustCodeGeneratorConfig(id)), vector);
62+
63+
string id = $"{nameof(FloatNaNOrZeroHashSupportTest)}-{spec.Identifier}";
64+
65+
await Verify(spec.Source)
66+
.UseFileName(id)
67+
.UseDirectory("Features")
68+
.DisableDiff();
69+
70+
RustLanguageDef langDef = new RustLanguageDef();
71+
TypeMap map = new TypeMap(langDef.TypeDefinitions, GeneratorEncoding.ASCII);
72+
73+
string source = $$"""
74+
#![allow(non_camel_case_types)]
75+
{{spec.Source}}
76+
77+
fn main() {
78+
{{FormatList(vector.Keys, x => $$"""
79+
if (!{{spec.Identifier}}::contains({{map.ToValueLabel(x)}})) {
80+
std::process::exit(0);
81+
}
82+
""", "\n")}}
83+
84+
std::process::exit(1);
85+
}
86+
""";
87+
88+
string executable = context.Compiler.Compile(id, source);
5489
Assert.Equal(1, RunProcess(executable));
5590
}
5691

Src/FastData.Generator.Rust.Tests/Vectors/HashTableStructure_Double_5_HashZeroOrNaN.verified.txt renamed to Src/FastData.Generator.Rust.Tests/Features/FloatNaNOrZeroHashSupportTest-HashTableStructure_Double_5.verified.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
#![allow(unused_unsafe)]
77
use std::ptr;
88

9-
pub struct HashTableStructure_Double_5_HashZeroOrNaN;
9+
pub struct HashTableStructure_Double_5;
1010

11-
impl HashTableStructure_Double_5_HashZeroOrNaN {
11+
impl HashTableStructure_Double_5 {
1212
const BUCKETS: [i8; 5] = [
1313
4, 0, 3, 1, 5
1414
];

Src/FastData.Generator.Rust.Tests/Vectors/HashTableStructure_Single_5_HashZeroOrNaN.verified.txt renamed to Src/FastData.Generator.Rust.Tests/Features/FloatNaNOrZeroHashSupportTest-HashTableStructure_Single_5.verified.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
#![allow(unused_unsafe)]
77
use std::ptr;
88

9-
pub struct HashTableStructure_Single_5_HashZeroOrNaN;
9+
pub struct HashTableStructure_Single_5;
1010

11-
impl HashTableStructure_Single_5_HashZeroOrNaN {
11+
impl HashTableStructure_Single_5 {
1212
const BUCKETS: [i8; 5] = [
1313
0, 1, 4, 3, 5
1414
];

0 commit comments

Comments
 (0)