Skip to content

Commit

Permalink
save game
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick-Nuon committed Mar 17, 2024
1 parent 8975942 commit cf7c79f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
42 changes: 42 additions & 0 deletions test/UTF8ValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -773,4 +773,46 @@ private bool ValidateUtf8(byte[] utf8, Range range = default)
var length = end - start;
return (start, length);
}


[Fact]
public void ExtraArgsTest()
{
int utf16Adjustment, scalarCountAdjustment;
// Generate a UTF-8 sequence with 3 units, each 2 bytes long, presumed to be valid.
byte[] utf8 = generator.Generate(howManyUnits: 3, byteCountInUnit: 2).ToArray();
PrintHexAndBinary(utf8);
var (offset, length) = (0, utf8.Length);

unsafe
{
fixed (byte* pInput = utf8)
{
byte* startPtr = pInput + offset;
// Invoke the method under test.
byte* result = DotnetRuntime.Utf8Utility.GetPointerToFirstInvalidByte(pInput, length, out utf16Adjustment, out scalarCountAdjustment);

// Since we are generating presumably valid 2-byte sequences, and depending on the specifics
// of the generator and Utf8Utility implementation, we need to assert expectations for adjustments.
// These assertions need to match your understanding of how utf16CodeUnitCountAdjustment and
// scalarCountAdjustment are supposed to be calculated based on the input data.

// Example: For simple 2-byte characters that map 1:1 from UTF-8 to UTF-16,
// utf16CodeUnitCountAdjustment might be 0 if the utility directly translates byte count.
// Assert.Equal(0, utf16Adjustment); // Placeholder, adjust based on actual logic.
// Assert.Equal(0, scalarCountAdjustment); // Placeholder, adjust based on actual logic.

Console.WriteLine("Scalar:" + scalarCountAdjustment);

Console.WriteLine("utf16:" + utf16Adjustment);

// If your generator creates specific patterns or the utility calculates these adjustments differently,
// you'll need to adjust the expected values accordingly.
}
}
}




}
29 changes: 26 additions & 3 deletions test/helpers/randomutf8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,47 @@ public RandomUtf8(uint seed, int prob_1byte, int prob_2bytes, int prob_3bytes, i
// break;
// }
// return result.ToArray();
// }

// public List<byte> Generate(int howManyUnits, int? byteCountInUnit = null)
// {
// var result = new List<byte>();
// while (result.Count < howManyUnits)
// {
// int count = byteCountInUnit ?? PickRandomByteCount();
// int codePoint = GenerateCodePoint(count);
// byte[] utf8Bytes = Encoding.UTF8.GetBytes(char.ConvertFromUtf32(codePoint));

// result.AddRange(utf8Bytes);
// if (result.Count + utf8Bytes.Length > howManyUnits)
// break;
// }
// return result;
// }

public List<byte> Generate(int howManyUnits, int? byteCountInUnit = null)
{
var result = new List<byte>();
while (result.Count < howManyUnits)
var unitsAdded = 0; // Track the number of characters added.

while (unitsAdded < howManyUnits)
{
int count = byteCountInUnit ?? PickRandomByteCount();
int codePoint = GenerateCodePoint(count);
byte[] utf8Bytes = Encoding.UTF8.GetBytes(char.ConvertFromUtf32(codePoint));

result.AddRange(utf8Bytes);
if (result.Count + utf8Bytes.Length > howManyUnits)
// Ensure adding the new character won't exceed the howManyUnits limit.
if (unitsAdded + 1 > howManyUnits)
break;

result.AddRange(utf8Bytes);
unitsAdded++; // Increment the units (characters) count.
}

return result;
}


// public object Generate(int howManyUnits, int? byteCountInUnit = null, bool returnAsList = false)
// {
// var result = new List<byte>();
Expand Down

0 comments on commit cf7c79f

Please sign in to comment.