Skip to content

Commit 97fdc4b

Browse files
committed
Add support for querying for non-existing values
1 parent 9b559fa commit 97fdc4b

File tree

9 files changed

+80
-51
lines changed

9 files changed

+80
-51
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ int main()
4141
return 0;
4242
""", "\n")}}
4343
44+
{{FormatList(vector.NotPresent, x => $"""
45+
if ({spec.Identifier}::contains({map.ToValueLabel(x)}))
46+
return 0;
47+
""", "\n")}}
48+
4449
return 1;
4550
}
4651
""";

Src/FastData.Generator.CPlusPlus.Tests/Vectors/SingleValueStructure_String_1.verified.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ class SingleValueStructure_String_1 final {
1010
public:
1111
[[nodiscard]]
1212
static constexpr bool contains(const std::string_view key) noexcept {
13-
return key == "value]";
13+
return key == "value";
1414
}
1515

1616
static constexpr size_t item_count = 1;
17-
static constexpr size_t min_key_length = 6;
18-
static constexpr size_t max_key_length = 6;
17+
static constexpr size_t min_key_length = 5;
18+
static constexpr size_t max_key_length = 5;
1919
};

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public static bool Run()
4141
return false;
4242
""", "\n")}}
4343
44+
{{FormatList(vector.NotPresent, x => $"""
45+
if ({spec.Identifier}.Contains({map.ToValueLabel(x)}))
46+
return false;
47+
""", "\n")}}
48+
4449
return true;
4550
}
4651
}

Src/FastData.Generator.CSharp.Tests/Vectors/SingleValueStructure_String_1.verified.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ internal static class SingleValueStructure_String_1
1111

1212
public static bool Contains(string key)
1313
{
14-
return StringComparer.Ordinal.Equals(key, "value]");
14+
return StringComparer.Ordinal.Equals(key, "value");
1515
}
1616

1717
public const uint ItemCount = 1;
18-
public const uint MinKeyLength = 6;
19-
public const uint MaxKeyLength = 6;
18+
public const uint MinKeyLength = 5;
19+
public const uint MaxKeyLength = 5;
2020
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ fn main() {
4040
}
4141
""", "\n")}}
4242
43+
{{FormatList(vector.NotPresent, x => $$"""
44+
if {{spec.Identifier}}::contains({{map.ToValueLabel(x)}}) {
45+
std::process::exit(0);
46+
}
47+
""", "\n")}}
48+
4349
std::process::exit(1);
4450
}
4551
""");

Src/FastData.Generator.Rust.Tests/Vectors/SingleValueStructure_String_1.verified.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ pub struct SingleValueStructure_String_1;
1111
impl SingleValueStructure_String_1 {
1212
#[must_use]
1313
pub fn contains(key: &'static str) -> bool {
14-
key == "value]"
14+
key == "value"
1515
}
1616

1717
pub const ITEM_COUNT: usize = 1;
18-
pub const MIN_KEY_LENGTH: usize = 6;
19-
pub const MAX_KEY_LENGTH: usize = 6;
18+
pub const MIN_KEY_LENGTH: usize = 5;
19+
pub const MAX_KEY_LENGTH: usize = 5;
2020
}

Src/FastData.Generator/Helpers/FormatHelper.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public static string FormatColumns<T>(ReadOnlySpan<T> items, Func<T, string> Ren
2222

2323
public static string FormatColumns<T>(ReadOnlySpan<T> items, Func<int, T, string> Render, int indent = 8, int columns = 10)
2424
{
25+
if (items.Length == 0)
26+
return string.Empty;
27+
2528
StringBuilder sb = new StringBuilder();
2629
int count = 0;
2730

@@ -49,16 +52,19 @@ public static string FormatColumns<T>(ReadOnlySpan<T> items, Func<int, T, string
4952
return sb.ToString();
5053
}
5154

52-
public static string FormatList<T>(T[] data, Func<T, string> render, string delim = ", ")
55+
public static string FormatList<T>(T[] items, Func<T, string> render, string delim = ", ")
5356
{
54-
return FormatList(data.AsReadOnlySpan(), render, delim);
57+
return FormatList(items.AsReadOnlySpan(), render, delim);
5558
}
5659

57-
public static string FormatList<T>(ReadOnlySpan<T> data, Func<T, string> render, string delim = ", ")
60+
public static string FormatList<T>(ReadOnlySpan<T> items, Func<T, string> render, string delim = ", ")
5861
{
62+
if (items.Length == 0)
63+
return string.Empty;
64+
5965
StringBuilder sb = new StringBuilder();
6066

61-
foreach (T item in data)
67+
foreach (T item in items)
6268
{
6369
sb.Append(render(item));
6470
sb.Append(delim);

Src/FastData.InternalShared/Helpers/TestVectorHelper.cs

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ public static IEnumerable<ITestData> GetBenchmarkData()
105105
}
106106
}
107107

108-
private static IEnumerable<ITestVector> GenerateTestVectors(IEnumerable<(Type, object[])> data, string? postfix = null, params Type[] dataStructs)
108+
private static IEnumerable<ITestVector> GenerateTestVectors(IEnumerable<(Type, object[], object[])> data, string? postfix = null, params Type[] dataStructs)
109109
{
110-
foreach ((Type vt, object[] values) in data)
110+
foreach ((Type vt, object[] values, object[] notValues) in data)
111111
{
112112
foreach (Type st in dataStructs)
113113
{
@@ -118,9 +118,15 @@ private static IEnumerable<ITestVector> GenerateTestVectors(IEnumerable<(Type, o
118118
arr.SetValue(values[i], i);
119119
}
120120

121+
Array arr2 = Array.CreateInstance(vt, notValues.Length);
122+
for (int i = 0; i < notValues.Length; i++)
123+
{
124+
arr2.SetValue(notValues[i], i);
125+
}
126+
121127
//Create an instance of TestVector<T> and give it the type of the structure
122128
Type vector = typeof(TestVector<>).MakeGenericType(vt);
123-
yield return (ITestVector)Activator.CreateInstance(vector, st, arr, postfix)!;
129+
yield return (ITestVector)Activator.CreateInstance(vector, st, arr, arr2, postfix)!;
124130
}
125131
}
126132
}
@@ -129,57 +135,57 @@ private static IEnumerable<ITestVector> GenerateTestVectors(IEnumerable<(Type, o
129135
private static IEnumerable<ITestVector> GenerateTestVectors<T>(IEnumerable<T[]> data, params Type[] dataStructs)
130136
{
131137
Type type = typeof(T);
132-
return GenerateTestVectors(data.Select(x => (type, x.Cast<object>().ToArray())), null, dataStructs);
138+
return GenerateTestVectors(data.Select(x => (type, x.Cast<object>().ToArray(), Array.Empty<object>())), null, dataStructs);
133139
}
134140

135-
private static IEnumerable<(Type type, object[] value)> GetEdgeCaseValues() =>
141+
private static IEnumerable<(Type type, object[] value, object[] notValue)> GetEdgeCaseValues() =>
136142
[
137143

138144
// We want to test edge values
139-
(typeof(sbyte), [sbyte.MinValue, (sbyte)-1, (sbyte)0, (sbyte)1, sbyte.MaxValue]),
140-
(typeof(byte), [(byte)0, (byte)1, byte.MaxValue]),
145+
(typeof(sbyte), [sbyte.MinValue, (sbyte)-1, (sbyte)0, (sbyte)1, sbyte.MaxValue], [(sbyte)-2, (sbyte)2]),
146+
(typeof(byte), [(byte)0, (byte)1, byte.MaxValue], [(byte)2, (byte)3]),
141147

142148
//We keep it within ASCII range as C#'s char does not translate to other languages
143-
(typeof(char), ['\0', 'a', (char)127]),
144-
(typeof(double), [double.MinValue, 0.0, 1.0, double.MaxValue]),
145-
(typeof(float), [float.MinValue, -1f, 0f, 1f, float.MaxValue]),
146-
(typeof(short), [short.MinValue, (short)-1, (short)0, (short)1, short.MaxValue]),
147-
(typeof(ushort), [(ushort)0, (ushort)1, (ushort)2, ushort.MaxValue]),
148-
(typeof(int), [int.MinValue, -1, 0, 1, int.MaxValue]),
149-
(typeof(uint), [0U, 1U, 2U, uint.MaxValue]),
150-
(typeof(long), [long.MinValue, -1L, 0L, 1L, long.MaxValue]),
151-
(typeof(ulong), [0UL, 1UL, 2UL, ulong.MaxValue]),
152-
(typeof(string), ["a", "item", new string('a', 255)])
149+
(typeof(char), ['\0', 'a', (char)127], [(char)1, 'b']),
150+
(typeof(double), [double.MinValue, 0.0, 1.0, double.MaxValue], [1.1, 2.0]),
151+
(typeof(float), [float.MinValue, -1f, 0f, 1f, float.MaxValue], [1.1f, 2.0f]),
152+
(typeof(short), [short.MinValue, (short)-1, (short)0, (short)1, short.MaxValue], [(short)-2, (short)2]),
153+
(typeof(ushort), [(ushort)0, (ushort)1, (ushort)2, ushort.MaxValue], [(ushort)3, (ushort)4]),
154+
(typeof(int), [int.MinValue, -1, 0, 1, int.MaxValue], [-2, 2]),
155+
(typeof(uint), [0U, 1U, 2U, uint.MaxValue], [3U, 4U]),
156+
(typeof(long), [long.MinValue, -1L, 0L, 1L, long.MaxValue], [-2L, 2L]),
157+
(typeof(ulong), [0UL, 1UL, 2UL, ulong.MaxValue], [3UL, 4UL]),
158+
(typeof(string), ["a", "item", new string('a', 255)], ["b", "item2"])
153159
];
154160

155-
private static IEnumerable<(Type type, object[] value)> GetFloatSpecialCases() =>
161+
private static IEnumerable<(Type type, object[] value, object[] notValue)> GetFloatSpecialCases() =>
156162
[
157163

158164
//If we don't have zero or NaN, we can use a simple hash
159-
(typeof(float), [1, 2, 3, 4, 5]),
160-
(typeof(double), [1, 2, 3, 4, 5]),
165+
(typeof(float), [1, 2, 3, 4, 5], []),
166+
(typeof(double), [1, 2, 3, 4, 5], []),
161167
];
162168

163-
private static IEnumerable<(Type type, object[] value)> GetDataOfSize(int size) =>
169+
private static IEnumerable<(Type type, object[] value, object[] notValue)> GetDataOfSize(int size) =>
164170
[
165-
(typeof(int), Enumerable.Range(0, size).Select(x => x).Cast<object>().ToArray()),
166-
(typeof(float), Enumerable.Range(0, size).Select(x => (float)x).Cast<object>().ToArray()),
167-
(typeof(string), Enumerable.Range(0, size).Select(x => x.ToString(NumberFormatInfo.InvariantInfo)).Cast<object>().ToArray())
171+
(typeof(int), Enumerable.Range(0, size).Select(x => x).Cast<object>().ToArray(), Enumerable.Range(size, size * 2).Select(x => x).Cast<object>().ToArray()),
172+
(typeof(float), Enumerable.Range(0, size).Select(x => (float)x).Cast<object>().ToArray(), Enumerable.Range(size, size * 2).Select(x => (float)x).Cast<object>().ToArray()),
173+
(typeof(string), Enumerable.Range(0, size).Select(x => x.ToString(NumberFormatInfo.InvariantInfo)).Cast<object>().ToArray(), Enumerable.Range(size, size * 2).Select(x => x.ToString(NumberFormatInfo.InvariantInfo)).Cast<object>().ToArray())
168174
];
169175

170-
private static IEnumerable<(Type type, object[] value)> GetSingleValues() =>
176+
private static IEnumerable<(Type type, object[] value, object[] notValue)> GetSingleValues() =>
171177
[
172-
(typeof(sbyte), [(sbyte)1]),
173-
(typeof(byte), [(byte)1]),
174-
(typeof(char), ['a']),
175-
(typeof(double), [1.0]),
176-
(typeof(float), [1f]),
177-
(typeof(short), [(short)1]),
178-
(typeof(ushort), [(ushort)1]),
179-
(typeof(int), [1]),
180-
(typeof(uint), [1U]),
181-
(typeof(long), [1L]),
182-
(typeof(ulong), [1UL]),
183-
(typeof(string), ["value]"])
178+
(typeof(sbyte), [(sbyte)1], [(sbyte)2]),
179+
(typeof(byte), [(byte)1], [(byte)2]),
180+
(typeof(char), ['a'], ['b']),
181+
(typeof(double), [1.0], [2.0]),
182+
(typeof(float), [1f], [2f]),
183+
(typeof(short), [(short)1], [(short)2]),
184+
(typeof(ushort), [(ushort)1], [(ushort)2]),
185+
(typeof(int), [1], [2]),
186+
(typeof(uint), [1U], [2U]),
187+
(typeof(long), [1L], [2L]),
188+
(typeof(ulong), [1UL], [2UL]),
189+
(typeof(string), ["value"], ["eulav"])
184190
];
185191
}

Src/FastData.InternalShared/TestClasses/TestVector.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
namespace Genbox.FastData.InternalShared.TestClasses;
55

6-
public class TestVector<TKey>(Type type, TKey[] keys, string? postfix = null) : ITestVector, IXunitSerializable
6+
public class TestVector<TKey>(Type type, TKey[] keys, TKey[] notPresent, string? postfix = null) : ITestVector, IXunitSerializable
77
{
88
private readonly DataType _dataType = Enum.Parse<DataType>(typeof(TKey).Name);
99

1010
public TKey[] Keys { get; private set; } = keys;
11+
public TKey[] NotPresent { get; } = notPresent;
1112
public Type Type { get; private set; } = type;
1213

1314
public string Identifier => $"{Type.Name.Replace("`1", "", StringComparison.Ordinal).Replace("`2", "", StringComparison.Ordinal)}_{_dataType}_{Keys.Length}" + (postfix != null ? $"_{postfix}" : "");

0 commit comments

Comments
 (0)