WPF HexEditor - Unit Tests
Comprehensive unit tests for WPF HexEditor performance optimizations using xUnit.
π Test Coverage
SpanSearchExtensionsTests (18 tests)
Tests for high-performance Span
Coverage:
FindIndexOf()- Pattern matching in Span- Single occurrence
- Multiple occurrences
- Overlapping matches
- Edge cases (empty data, empty pattern, pattern longer than data)
- Base offset application
FindFirstIndexOf()- First-match optimization- Early termination
- Base offset
- No-match scenarios
CountOccurrences()- Zero-allocation counting- Correct counting
- Empty patterns
- No-match scenarios
Status: β All 18 tests passing
ByteProviderOptimizedSearchTests (17 tests)
Tests for ByteProvider optimized search methods using Span
Coverage:
FindIndexOfOptimized()- Chunked search with ArrayPool- Multiple occurrences across file
- Start position support
- No-match handling
- Small/large chunk sizes
- Patterns at chunk boundaries
- Edge cases (null, empty, beyond end)
FindFirstOptimized()- Early-exit search- First occurrence finding
- Start position support
- No-match scenarios
CountOccurrencesOptimized()- Optimized counting- Accurate counting
- Start position support
- Frequent patterns
Status: β All 17 tests passing
π Running Tests
All Tests
cd Sources/Tests/WpfHexEditor.Tests
dotnet test
Specific Test Class
dotnet test --filter "FullyQualifiedName~SpanSearchExtensionsTests"
dotnet test --filter "FullyQualifiedName~ByteProviderOptimizedSearchTests"
Single Test Method
dotnet test --filter "FullyQualifiedName~FindIndexOfOptimized_FindsAllOccurrences"
With Verbose Output
dotnet test --verbosity normal
Generate Code Coverage
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura
π οΈ Requirements
- .NET 8.0 SDK for Windows
- Windows 10/11 (WPF dependency for ByteProvider tests)
- xUnit 2.6.6
- Microsoft.NET.Test.Sdk 17.8.0
Note: Tests target net8.0-windows with UseWPF=true because ByteProvider depends on PresentationFramework.
π Test Structure
Tests/
βββ WpfHexEditor.Tests/
βββ WpfHexEditor.Tests.csproj
βββ SpanSearchExtensionsTests.cs (Pure Span<byte> tests, no file I/O)
βββ ByteProviderOptimizedSearchTests.cs (ByteProvider integration tests)
βββ README.md
π― Test Philosophy
Unit Tests (Current)
- Fast execution (< 2 seconds total)
- No external dependencies (temp files created/cleaned automatically)
- Deterministic (same input = same output)
- Isolated (each test independent)
Integration Tests (Future)
- Large file handling (GB-sized files)
- Performance regression detection
- Multi-threaded scenarios
- Memory profiling
π Test Results Example
Test Run Successful.
Total tests: 35
Passed: 35
Total time: 1.8423 Seconds
π§ͺ Adding New Tests
For Span Extensions
[Fact]
public void MyNewTest()
{
// Arrange
byte[] data = { /* test data */ };
byte[] pattern = { /* search pattern */ };
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(data);
// Act
var results = span.FindIndexOf(pattern, baseOffset: 0);
// Assert
Assert.Equal(expectedCount, results.Count);
Assert.Equal(expectedPosition, results[0]);
}
For ByteProvider Tests
public class MyNewTests : IDisposable
{
private readonly string _testFile;
private readonly ByteProvider _provider;
public MyNewTests()
{
_testFile = Path.GetTempFileName();
// Create test data
File.WriteAllBytes(_testFile, testData);
_provider = new ByteProvider(_testFile);
}
public void Dispose()
{
_provider?.Dispose();
if (File.Exists(_testFile))
try { File.Delete(_testFile); } catch { }
}
[Fact]
public void MyTest()
{
// Test using _provider
}
}
π Troubleshooting
Tests Fail with βCould not load PresentationFrameworkβ
Solution: Ensure project targets net8.0-windows with <UseWPF>true</UseWPF>
Tests Fail with File Access Errors
Solution: Check that test cleanup (Dispose) is working correctly
Slow Test Execution
Solution:
- Reduce test file sizes if possible
- Use smaller chunk sizes for ByteProvider tests
- Run specific test classes instead of all tests
π Related
- Benchmarks - Performance benchmarking with BenchmarkDotNet
- Performance Sample - Interactive performance demo
- Performance Extensions - API documentation
π License
Apache 2.0 - Same as WPF HexEditor parent project
β All tests green!