Skip to content

Commit e1b850d

Browse files
committed
Added tests (working)
1 parent c6af788 commit e1b850d

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

src/Ascii.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public static unsafe bool SIMDIsAscii(this ReadOnlySpan<char> s)
133133
}
134134

135135
[MethodImpl(MethodImplOptions.AggressiveInlining)]
136-
internal static unsafe nuint GetIndexOfFirstNonAsciiByte(byte* pBuffer, nuint bufferLength)
136+
public static unsafe nuint GetIndexOfFirstNonAsciiByte(byte* pBuffer, nuint bufferLength)
137137
{
138138
byte* pBufferEnd = pBuffer + bufferLength;
139139
byte* pCurrent = pBuffer;

test/AsciiTest.cs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace tests;
22
using System.Text;
3+
using SimdUnicode;
34

45
//TODO (Nick Nuon): Test UTF8 Generator works correctly
56

@@ -113,7 +114,7 @@ public void Test_random_ASCII_sequences_of_varying_lengths()
113114
}
114115

115116
// Print the validation results
116-
Console.WriteLine($"For {length}-byte sequences, {validSequencesCount * 100.0 / NUM_TRIALS}% were valid ASCII.");
117+
// Console.WriteLine($"For {length}-byte sequences, {validSequencesCount * 100.0 / NUM_TRIALS}% were valid ASCII.");
117118

118119
// Assertion or check to ensure all sequences were valid ASCII
119120
if (validSequencesCount != NUM_TRIALS)
@@ -123,5 +124,65 @@ public void Test_random_ASCII_sequences_of_varying_lengths()
123124
}
124125
}
125126

127+
128+
[Fact]
129+
// This mimics the no_error_ASCII test
130+
public void TestNoErrorASCII()
131+
{
132+
const int NUM_TRIALS = 1000;
133+
const int LENGTH = 512;
134+
RandomUtf8 utf8Generator = new RandomUtf8(0, 100, 0, 0, 0); // Only ASCII/one-bytes
135+
136+
for (int trial = 0; trial < NUM_TRIALS; trial++)
137+
{
138+
byte[] ascii = utf8Generator.Generate(LENGTH);
139+
140+
unsafe
141+
{
142+
fixed (byte* pAscii = ascii)
143+
{
144+
nuint result = Ascii.GetIndexOfFirstNonAsciiByte(pAscii, (nuint)ascii.Length);
145+
if (result != (nuint)ascii.Length)
146+
{
147+
throw new Exception($"Unexpected non-ASCII character found at index {result}");
148+
}
149+
}
150+
}
151+
}
152+
}
153+
154+
[Fact]
155+
// This mimics the error_ASCII test
156+
public void TestErrorASCII()
157+
{
158+
const int NUM_TRIALS = 1000;
159+
const int LENGTH = 512;
160+
RandomUtf8 utf8Generator = new RandomUtf8(0, 100, 0, 0, 0); // Only ASCII/one-bytes
161+
162+
for (int trial = 0; trial < NUM_TRIALS; trial++)
163+
{
164+
byte[] ascii = utf8Generator.Generate(LENGTH);
165+
166+
for (int i = 0; i < ascii.Length; i++)
167+
{
168+
ascii[i] += 0b10000000;
169+
170+
unsafe
171+
{
172+
fixed (byte* pAscii = ascii)
173+
{
174+
nuint result = Ascii.GetIndexOfFirstNonAsciiByte(pAscii, (nuint)ascii.Length);
175+
if (result != (nuint)i)
176+
{
177+
throw new Exception($"Expected non-ASCII character at index {i}, but found at index {result}");
178+
}
179+
}
180+
}
181+
182+
ascii[i] -= 0b10000000;
183+
}
184+
}
185+
}
186+
126187

127188
}

0 commit comments

Comments
 (0)