Skip to content

Commit 4c2df8b

Browse files
committed
Add object support for C# generator
1 parent 289c8c9 commit 4c2df8b

File tree

39 files changed

+1753
-782
lines changed

39 files changed

+1753
-782
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using Genbox.FastData.Enums;
2+
using Genbox.FastData.Generator.CSharp.Internal.Framework;
3+
using Genbox.FastData.Generator.Extensions;
4+
using Genbox.FastData.Generator.Framework;
5+
using Genbox.FastData.InternalShared;
6+
using Genbox.FastData.InternalShared.Helpers;
7+
using Genbox.FastData.InternalShared.TestClasses;
8+
using Genbox.FastData.InternalShared.TestClasses.TheoryData;
9+
using static Genbox.FastData.Generator.Helpers.FormatHelper;
10+
using static Genbox.FastData.InternalShared.Helpers.TestHelper;
11+
12+
namespace Genbox.FastData.Generator.CSharp.Tests;
13+
14+
public class FeatureTests
15+
{
16+
[Theory]
17+
[ClassData(typeof(SimpleTestVectorTheoryData))]
18+
public async Task ObjectSupportTest<T>(TestVector<T> vector)
19+
{
20+
Person[] values =
21+
[
22+
new Person { Age = 1, Name = "Bob", Other = new Person { Name = "Anna", Age = 4 } },
23+
new Person { Age = 2, Name = "Billy" },
24+
new Person { Age = 3, Name = "Bibi" },
25+
];
26+
27+
GeneratorSpec spec = Generate(id => CSharpCodeGenerator.Create(new CSharpCodeGeneratorConfig(id)), vector, values);
28+
29+
string id = $"{nameof(ObjectSupportTest)}-{spec.Identifier}";
30+
31+
await Verify(spec.Source)
32+
.UseFileName(id)
33+
.UseDirectory("Features")
34+
.DisableDiff();
35+
36+
CSharpLanguageDef langDef = new CSharpLanguageDef();
37+
TypeMap map = new TypeMap(langDef.TypeDefinitions, GeneratorEncoding.ASCII);
38+
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);
57+
Assert.True(contains());
58+
}
59+
60+
internal class Person
61+
{
62+
public int Age { get; set; }
63+
public string Name { get; set; }
64+
public Person Other { get; set; }
65+
}
66+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
public class Person
9+
{
10+
public Person(int age, string name, Person other)
11+
{
12+
Age = age;
13+
Name = name;
14+
Other = other;
15+
}
16+
int Age { get; set; }
17+
string Name { get; set; }
18+
Person Other { get; set; }
19+
20+
};
21+
22+
internal static class ArrayStructure_Int32_3
23+
{
24+
private static readonly int[] _keys = new int[] {
25+
1, 2, 3
26+
};
27+
28+
29+
public static bool Contains(int key)
30+
{
31+
if (key < 1 || key > 3)
32+
return false;
33+
34+
for (int i = 0; i < 3; i++)
35+
{
36+
if (key == _keys[i])
37+
return true;
38+
}
39+
return false;
40+
}
41+
private static readonly Person[] _values = {
42+
new Person(1, "Bob", new Person(4, "Anna", null)), new Person(2, "Billy", null), new Person(3, "Bibi", null)
43+
};
44+
45+
46+
public static bool TryLookup(int key, out Person? value)
47+
{
48+
value = default;
49+
50+
if (key < 1 || key > 3)
51+
return false;
52+
53+
for (uint i = 0; i < 3; i++)
54+
{
55+
if (_keys[i] == key)
56+
{
57+
value = _values[i];
58+
return true;
59+
}
60+
}
61+
62+
value = default;
63+
return false;
64+
}
65+
66+
public const uint ItemCount = 3;
67+
public const int MinKey = 1;
68+
public const int MaxKey = 3;
69+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
public class Person
9+
{
10+
public Person(int age, string name, Person other)
11+
{
12+
Age = age;
13+
Name = name;
14+
Other = other;
15+
}
16+
int Age { get; set; }
17+
string Name { get; set; }
18+
Person Other { get; set; }
19+
20+
};
21+
22+
internal static class BinarySearchStructure_Int32_3
23+
{
24+
private static readonly int[] _keys = new int[] {
25+
1, 2, 3
26+
};
27+
28+
29+
public static bool Contains(int key)
30+
{
31+
if (key < 1 || key > 3)
32+
return false;
33+
34+
int lo = 0;
35+
int hi = 2;
36+
while (lo <= hi)
37+
{
38+
int i = lo + ((hi - lo) >> 1);
39+
int order = _keys[i].CompareTo(key);
40+
41+
if (order == 0)
42+
return true;
43+
if (order < 0)
44+
lo = i + 1;
45+
else
46+
hi = i - 1;
47+
}
48+
49+
return ~lo >= 0;
50+
}
51+
private static readonly Person[] _values = {
52+
new Person(1, "Bob", new Person(4, "Anna", null)), new Person(2, "Billy", null), new Person(3, "Bibi", null)
53+
};
54+
55+
56+
public static bool TryLookup(int key, out Person? value)
57+
{
58+
value = default;
59+
60+
if (key < 1 || key > 3)
61+
return false;
62+
63+
int lo = 0;
64+
int hi = 2;
65+
while (lo <= hi)
66+
{
67+
int i = lo + ((hi - lo) >> 1);
68+
int order = _keys[i].CompareTo(key);
69+
70+
if (order == 0)
71+
{
72+
value = _values[i];
73+
return true;
74+
}
75+
if (order < 0)
76+
lo = i + 1;
77+
else
78+
hi = i - 1;
79+
}
80+
81+
value = default;
82+
return false;
83+
}
84+
85+
public const uint ItemCount = 3;
86+
public const int MinKey = 1;
87+
public const int MaxKey = 3;
88+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
public class Person
9+
{
10+
public Person(int age, string name, Person other)
11+
{
12+
Age = age;
13+
Name = name;
14+
Other = other;
15+
}
16+
int Age { get; set; }
17+
string Name { get; set; }
18+
Person Other { get; set; }
19+
20+
};
21+
22+
internal static class ConditionalStructure_Int32_3
23+
{
24+
25+
public static bool Contains(int key)
26+
{
27+
28+
29+
switch (key)
30+
{
31+
case 1:
32+
case 2:
33+
case 3:
34+
return true;
35+
default:
36+
return false;
37+
}
38+
}
39+
private static readonly Person[] _values = {
40+
new Person(1, "Bob", new Person(4, "Anna", null)), new Person(2, "Billy", null), new Person(3, "Bibi", null)
41+
};
42+
43+
44+
public static bool TryLookup(int key, out Person? value)
45+
{
46+
value = default;
47+
48+
switch (key)
49+
{
50+
case 1:
51+
value = _values[0];
52+
return true;
53+
case 2:
54+
value = _values[1];
55+
return true;
56+
case 3:
57+
value = _values[2];
58+
return true;
59+
60+
}
61+
value = default;
62+
return false;
63+
}
64+
65+
public const uint ItemCount = 3;
66+
public const int MinKey = 1;
67+
public const int MaxKey = 3;
68+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// <auto-generated />
2+
// This file is auto-generated. Do not edit manually.
3+
// Structure: HashTable (HashTablePerfect)
4+
#nullable enable
5+
using System;
6+
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
8+
public class Person
9+
{
10+
public Person(int age, string name, Person other)
11+
{
12+
Age = age;
13+
Name = name;
14+
Other = other;
15+
}
16+
int Age { get; set; }
17+
string Name { get; set; }
18+
Person Other { get; set; }
19+
20+
};
21+
22+
internal static class HashTablePerfectStructure_Int32_3
23+
{
24+
[StructLayout(LayoutKind.Auto)]
25+
private struct E
26+
{
27+
internal int Key;
28+
29+
internal Person Value;
30+
31+
internal E(int key, Person value)
32+
{
33+
Key = key;
34+
35+
Value = value;
36+
}
37+
}
38+
private static readonly E[] _entries = {
39+
new E(3, new Person(1, "Bob", new Person(4, "Anna", null))), new E(1, new Person(2, "Billy", null)), new E(2, new Person(3, "Bibi", null))
40+
};
41+
42+
43+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
44+
private static ulong Hash(int value)
45+
{
46+
return (ulong)value;
47+
}
48+
49+
50+
public static bool Contains(int key)
51+
{
52+
if (key < 1 || key > 3)
53+
return false;
54+
55+
ulong hash = Hash(key);
56+
uint index = (uint)(hash % 3);
57+
ref var entry = ref _entries[index];
58+
59+
return key == entry.Key;
60+
}
61+
62+
public static bool TryLookup(int key, out Person value)
63+
{
64+
value = default;
65+
if (key < 1 || key > 3)
66+
return false;
67+
68+
ulong hash = Hash(key);
69+
uint index = (uint)(hash % 3);
70+
ref E entry = ref _entries[index];
71+
72+
if (key == entry.Key)
73+
{
74+
value = entry.Value;
75+
return true;
76+
}
77+
78+
value = default;
79+
return false;
80+
}
81+
82+
public const uint ItemCount = 3;
83+
public const int MinKey = 1;
84+
public const int MaxKey = 3;
85+
}

0 commit comments

Comments
 (0)