Skip to content

Commit

Permalink
Test: Add test coverage for ReadOnlySpan<char> Parse and TryParse met…
Browse files Browse the repository at this point in the history
…hods

Add comprehensive test coverage for the new ReadOnlySpan<char> parsing methods:

- Add Parse_WithSpan_ShouldHandleValidInput
- Add Parse_WithSpan_ShouldThrowOnInvalidInput
- Add Parse_WithSpan_ShouldHandleStackAllocatedSpan
- Add TryParse_WithSpan_ShouldHandleValidInput
- Add TryParse_WithSpan_ShouldHandleInvalidInput
- Add TryParse_WithSpan_ShouldHandleStackAllocatedSpan

Tests cover normal usage, error cases, and stack allocation scenarios.
  • Loading branch information
Taiizor committed Jan 15, 2025
1 parent 94706bf commit 5cad5b3
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions test/UUID.Tests/UUIDConstructorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,48 @@ public void Parse_ShouldHandleValidInput()
Assert.Equal(uuid, parsed);
}

[Fact]
public void Parse_WithSpan_ShouldHandleValidInput()
{
// Arrange
UUID uuid = UUID.New();
ReadOnlySpan<char> span = uuid.ToString().AsSpan();

// Act
UUID parsed = UUID.Parse(span);

// Assert
Assert.Equal(uuid, parsed);
}

[Fact]
public void Parse_WithSpan_ShouldThrowOnInvalidInput()
{
// Invalid length
Assert.Throws<FormatException>(() => UUID.Parse("invalid".AsSpan()));
Assert.Throws<FormatException>(() => UUID.Parse("1234567890123456789012345678901".AsSpan())); // 31 chars
Assert.Throws<FormatException>(() => UUID.Parse("123456789012345678901234567890123".AsSpan())); // 33 chars

// Invalid hex characters
Assert.Throws<FormatException>(() => UUID.Parse("GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG".AsSpan())); // Invalid hex
}

[Fact]
public void Parse_WithSpan_ShouldHandleStackAllocatedSpan()
{
// Arrange
UUID original = UUID.New();
string str = original.ToString();
Span<char> stackSpan = stackalloc char[32];
str.AsSpan().CopyTo(stackSpan);

// Act
UUID parsed = UUID.Parse(stackSpan);

// Assert
Assert.Equal(original, parsed);
}

[Fact]
public void Parse_ShouldThrowOnInvalidInput()
{
Expand All @@ -87,6 +129,50 @@ public void TryParse_ShouldHandleValidAndInvalidInput()
Assert.False(UUID.TryParse("123456789012345678901234567890123", out _)); // 33 chars
}

[Fact]
public void TryParse_WithSpan_ShouldHandleValidInput()
{
// Arrange
UUID uuid = UUID.New();
ReadOnlySpan<char> span = uuid.ToString().AsSpan();

// Act
bool success = UUID.TryParse(span, out UUID parsed);

// Assert
Assert.True(success);
Assert.Equal(uuid, parsed);
}

[Fact]
public void TryParse_WithSpan_ShouldHandleInvalidInput()
{
// Invalid length
Assert.False(UUID.TryParse("invalid".AsSpan(), out _));
Assert.False(UUID.TryParse("1234567890123456789012345678901".AsSpan(), out _)); // 31 chars
Assert.False(UUID.TryParse("123456789012345678901234567890123".AsSpan(), out _)); // 33 chars

// Invalid hex characters
Assert.False(UUID.TryParse("GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG".AsSpan(), out _));
}

[Fact]
public void TryParse_WithSpan_ShouldHandleStackAllocatedSpan()
{
// Arrange
UUID original = UUID.New();
string str = original.ToString();
Span<char> stackSpan = stackalloc char[32];
str.AsSpan().CopyTo(stackSpan);

// Act
bool success = UUID.TryParse(stackSpan, out UUID parsed);

// Assert
Assert.True(success);
Assert.Equal(original, parsed);
}

[Fact]
public void Base32_ShouldBeValid()
{
Expand Down

0 comments on commit 5cad5b3

Please sign in to comment.