Skip to content

Commit d7af24e

Browse files
authored
Added SpanTools.ValueStringBuilder.Generator Reference (apache#1390)
1 parent 33c44be commit d7af24e

3 files changed

Lines changed: 118 additions & 1 deletion

File tree

.build/dependencies.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
<NUnitPackageVersion>3.14.0</NUnitPackageVersion>
7171
<RandomizedTestingGeneratorsPackageVersion>2.7.8</RandomizedTestingGeneratorsPackageVersion>
7272
<SharpZipLibPackageVersion>1.4.2</SharpZipLibPackageVersion>
73+
<SpanToolsValueStringBuilderGeneratorPackageVersion>1.0.0-alpha.113</SpanToolsValueStringBuilderGeneratorPackageVersion>
7374
<Spatial4nPackageVersion>0.4.1.1</Spatial4nPackageVersion>
7475
<SystemMemoryPackageVersion>4.6.3</SystemMemoryPackageVersion>
7576
<SystemNetHttpPackageVersion>4.3.4</SystemNetHttpPackageVersion>
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using J2N.Globalization;
2+
using Lucene.Net.Attributes;
3+
using Lucene.Net.Util;
4+
using NUnit.Framework;
5+
using System;
6+
using Assert = Lucene.Net.TestFramework.Assert;
7+
8+
namespace Lucene.Net.Text
9+
{
10+
/*
11+
* Licensed to the Apache Software Foundation (ASF) under one or more
12+
* contributor license agreements. See the NOTICE file distributed with
13+
* this work for additional information regarding copyright ownership.
14+
* The ASF licenses this file to You under the Apache License, Version 2.0
15+
* (the "License"); you may not use this file except in compliance with
16+
* the License. You may obtain a copy of the License at
17+
*
18+
* http://www.apache.org/licenses/LICENSE-2.0
19+
*
20+
* Unless required by applicable law or agreed to in writing, software
21+
* distributed under the License is distributed on an "AS IS" BASIS,
22+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23+
* See the License for the specific language governing permissions and
24+
* limitations under the License.
25+
*/
26+
27+
/// <summary>
28+
/// Smoke tests for <see cref="ValueStringBuilder"/> to ensure it has been generated with
29+
/// the configured values. More thorough tests are performed in SpanTools
30+
/// <a href="https://www.nuget.org/packages/SpanTools.ValueStringBuilder.Generator">
31+
/// https://www.nuget.org/packages/SpanTools.ValueStringBuilder.Generator</a>
32+
/// </summary>
33+
[TestFixture, LuceneNetSpecific]
34+
public class TestValueStringBuilder : LuceneTestCase
35+
{
36+
[Test]
37+
public void TestJavaStyleLastIndexOf()
38+
{
39+
using ValueStringBuilder sb = new(stackalloc char[8]);
40+
sb.Append("aaaaa");
41+
Assert.AreEqual(2, sb.LastIndexOf("aa", 2));
42+
}
43+
44+
[Test]
45+
public void TestJavaStyleFormatting()
46+
{
47+
using CultureContext context = new("pt");
48+
using ValueStringBuilder sb = new(stackalloc char[8]);
49+
sb.Append(123d);
50+
Assert.AreEqual("123.0", sb.ToString());
51+
}
52+
53+
// Tests MaxLength tracking
54+
[Test]
55+
public void TestFitsIntialBuffer_BeyondInitialBuffer_ReturnsMaxLength()
56+
{
57+
using ValueStringBuilder sb = new(stackalloc char[8]);
58+
sb.Append("Hello");
59+
sb.Append("World");
60+
Assert.IsFalse(sb.FitsInitialBuffer(out int charsLength));
61+
Assert.AreEqual(10, charsLength);
62+
}
63+
64+
[Test]
65+
public void TestFitsIntialBuffer_WithinInitialBuffer_ReturnsLength()
66+
{
67+
using ValueStringBuilder sb = new(stackalloc char[8]);
68+
sb.Append("Hello");
69+
sb.Append("W");
70+
Assert.IsTrue(sb.FitsInitialBuffer(out int charsLength));
71+
Assert.AreEqual(6, charsLength);
72+
Assert.AreEqual(6, sb.Length);
73+
}
74+
75+
[Test]
76+
public void TestAsMemory()
77+
{
78+
using ValueStringBuilder sb = new(8);
79+
sb.Append("Hello");
80+
ReadOnlyMemory<char> memory = sb.AsMemory();
81+
sb.Append("World");
82+
Assert.AreNotEqual("HelloWorld", memory.Span.ToString()); // This is guaranteed not to be the same memory as otherMemory
83+
ReadOnlyMemory<char> otherMemory = sb.AsMemory();
84+
Assert.AreEqual("HelloWorld", otherMemory.Span.ToString());
85+
}
86+
87+
[Test]
88+
public void TestPreload()
89+
{
90+
Span<char> destination = stackalloc char[10];
91+
"Hello".AsSpan().CopyTo(destination);
92+
ValueStringBuilder sb = new(destination);
93+
try
94+
{
95+
sb.Length = 5; // "Consume" the preloaded value
96+
sb.Append("World");
97+
Assert.AreEqual("HelloWorld", sb.ToString());
98+
Assert.IsTrue(sb.FitsInitialBuffer(out int charsLength));
99+
Assert.AreEqual(10, charsLength);
100+
Assert.AreEqual(10, sb.Length);
101+
}
102+
finally
103+
{
104+
sb.Dispose(); // Technically not required; best practice to call this
105+
}
106+
}
107+
}
108+
}

src/Lucene.Net/Lucene.Net.csproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@
3636
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
3737
</PropertyGroup>
3838

39-
39+
<PropertyGroup Label="SpanTools.ValueStringBuilder.Generator Settings">
40+
<ValueStringBuilder_Namespace>Lucene.Net.Text</ValueStringBuilder_Namespace>
41+
<ValueStringBuilder_IncludeMaxLengthTracking>true</ValueStringBuilder_IncludeMaxLengthTracking>
42+
<ValueStringBuilder_IncludeAsMemoryMethods>true</ValueStringBuilder_IncludeAsMemoryMethods>
43+
<ValueStringBuilder_IncludeCodepointSupport>true</ValueStringBuilder_IncludeCodepointSupport>
44+
<ValueStringBuilder_UseJavaStyleFormatting>true</ValueStringBuilder_UseJavaStyleFormatting>
45+
<ValueStringBuilder_UseJavaStyleIndexOf>true</ValueStringBuilder_UseJavaStyleIndexOf>
46+
</PropertyGroup>
4047

4148
<PropertyGroup Label="NuGet Package File Paths">
4249
<LuceneNetDotNetDir>$(SolutionDir)src\dotnet\</LuceneNetDotNetDir>
@@ -56,6 +63,7 @@
5663
<ItemGroup>
5764
<PackageReference Include="J2N" Version="$(J2NPackageVersion)" />
5865
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="$(MicrosoftExtensionsConfigurationAbstractionsPackageVersion)" />
66+
<PackageReference Include="SpanTools.ValueStringBuilder.Generator" Version="$(SpanToolsValueStringBuilderGeneratorPackageVersion)" PrivateAssets="all" />
5967
</ItemGroup>
6068

6169
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' ">

0 commit comments

Comments
 (0)