-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial commit of RangeMapped<T> functionality. * Cleaned up code, improved test coverage. * Remove extraneous null suppression. * Removed extraneous Delegate casts. * Changed RangeMapped<> to readonly struct. * Expanded RangeMapped API. * Started migration to range mapped reads. * Added range mapping to MetadataTablesStream. * Rolled back RangeMapped<> use. * Applied code review suggestions.
- Loading branch information
1 parent
86d51bd
commit 66ced57
Showing
9 changed files
with
304 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
namespace Reemit.Common.UnitTests; | ||
|
||
public sealed class RangeMappedTests | ||
{ | ||
[Fact] | ||
public void With_Called_ConstructsWithNewValue() | ||
{ | ||
// Arrange | ||
var rangeMapped = new RangeMapped<int>(0x52, 0x30, 0x1592); | ||
|
||
// Act | ||
var actualRangeMapped = rangeMapped.With(0xdeadbeef); | ||
|
||
// Assert | ||
AssertLengthAndPosition(rangeMapped, actualRangeMapped); | ||
Assert.Equal(0xdeadbeef, actualRangeMapped.Value); | ||
Assert.IsType<uint>(actualRangeMapped.Value); | ||
Assert.IsType<RangeMapped<uint>>(actualRangeMapped); | ||
} | ||
|
||
[Fact] | ||
public void Select_Called_ConstructsWithNewValue() | ||
{ | ||
// Arrange | ||
var rangeMapped = new RangeMapped<int>(0x52, 0x30, 0x1000); | ||
|
||
// Act | ||
var actualRangeMapped = rangeMapped.Select(x => x * 2); | ||
|
||
// Assert | ||
AssertLengthAndPosition(rangeMapped, actualRangeMapped); | ||
Assert.Equal(0x2000, actualRangeMapped.Value); | ||
} | ||
|
||
[Fact] | ||
public void Cast_Called_ConstructsWithNewValue() | ||
{ | ||
// Arrange | ||
var rangeMapped = new RangeMapped<int>(0x52, 0x30, 0x1592); | ||
|
||
// Act | ||
var actualRangeMapped = rangeMapped.Cast<uint>(); | ||
|
||
// Assert | ||
AssertLengthAndPosition(rangeMapped, actualRangeMapped); | ||
Assert.Equal((uint)0x1592, actualRangeMapped.Value); | ||
Assert.IsType<uint>(actualRangeMapped.Value); | ||
Assert.IsType<RangeMapped<uint>>(actualRangeMapped); | ||
} | ||
|
||
private void AssertLengthAndPosition(IRangeMapped expected, IRangeMapped actual) | ||
{ | ||
Assert.Equal(expected.Length, actual.Length); | ||
Assert.Equal(expected.Position, actual.Position); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
namespace Reemit.Common.UnitTests; | ||
|
||
public sealed class SharedReaderScopeTests | ||
{ | ||
[Fact] | ||
public void CreateScope_Called_TracksRange() | ||
{ | ||
// Arrange | ||
byte[] bytes = [0x0, 0x1, 0x2, 0x3, 0x4, 0x5]; | ||
using var stream = new MemoryStream(bytes); | ||
using var binaryReader = new BinaryReader(stream); | ||
const int sharedReaderOffset = 1; | ||
using var sharedReader = new SharedReader(sharedReaderOffset, binaryReader, new object()); | ||
const int readBytesCount = 2; | ||
byte[] actualBytes; | ||
RangeMapped<byte[]> actualRangeMappedBytes; | ||
|
||
// Act | ||
using (var scope = sharedReader.CreateRangeScope()) | ||
{ | ||
actualBytes = sharedReader.ReadBytes(readBytesCount); | ||
actualRangeMappedBytes = scope.ToRangeMapped(actualBytes); | ||
} | ||
|
||
// Assert | ||
Assert.Equal(sharedReaderOffset + readBytesCount, sharedReader.Offset); | ||
Assert.Equal(readBytesCount, sharedReader.RelativeOffset); | ||
Assert.Equal(readBytesCount, actualBytes.Length); | ||
Assert.Equal([0x1, 0x2], actualBytes); | ||
Assert.Equal(1, actualRangeMappedBytes.Position); | ||
Assert.Equal(2, actualRangeMappedBytes.Length); | ||
Assert.Equal(readBytesCount, actualRangeMappedBytes.Value.Length); | ||
Assert.Equal([0x1, 0x2], actualRangeMappedBytes.Value); | ||
} | ||
|
||
[Fact] | ||
public void CreateNestedScope_Called_TracksRange() | ||
{ | ||
// Arrange | ||
byte[] bytes = [0x0, 0x1, 0x2, 0x3, 0x4, 0x5]; | ||
using var stream = new MemoryStream(bytes); | ||
using var binaryReader = new BinaryReader(stream); | ||
const int sharedReaderOffset = 1; | ||
using var sharedReader = new SharedReader(sharedReaderOffset, binaryReader, new object()); | ||
const int readBytesCount = 2; | ||
byte[] actualOuterBytes = new byte[readBytesCount]; | ||
RangeMapped<byte[]> actualInnerBytes, actualRangeMappedOuterBytes, actualRangeMappedInnerBytes; | ||
|
||
// Act | ||
using (var outerScope = sharedReader.CreateRangeScope()) | ||
{ | ||
actualOuterBytes[0] = sharedReader.ReadByte(); | ||
|
||
using (var innerScope = sharedReader.CreateRangeScope()) | ||
{ | ||
actualInnerBytes = sharedReader.ReadMappedBytes(readBytesCount); | ||
actualRangeMappedInnerBytes = innerScope.ToRangeMapped(actualInnerBytes); | ||
} | ||
|
||
actualOuterBytes[1] = sharedReader.ReadByte(); | ||
|
||
actualRangeMappedOuterBytes = outerScope.ToRangeMapped(actualOuterBytes); | ||
} | ||
|
||
// Assert | ||
Assert.Equal(sharedReaderOffset + readBytesCount * 2, sharedReader.Offset); | ||
Assert.Equal(readBytesCount * 2, sharedReader.RelativeOffset); | ||
|
||
Assert.Equal([0x1, 0x4], actualOuterBytes); | ||
Assert.Equal([0x1, 0x4], actualRangeMappedOuterBytes.Value); | ||
Assert.Equal(1, actualRangeMappedOuterBytes.Position); | ||
Assert.Equal(readBytesCount * 2, actualRangeMappedOuterBytes.Length); | ||
|
||
Assert.Equal([0x2, 0x3], actualInnerBytes.Value); | ||
Assert.Equal([0x2, 0x3], actualRangeMappedInnerBytes.Value); | ||
Assert.Equal(2, actualRangeMappedInnerBytes.Position); | ||
Assert.Equal(readBytesCount, actualRangeMappedInnerBytes.Length); | ||
|
||
Assert.Equal(2, actualInnerBytes.Position); | ||
Assert.Equal(readBytesCount, actualInnerBytes.Length); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Reemit.Common; | ||
|
||
public interface IRangeMapped | ||
{ | ||
int Length { get; } | ||
int Position { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Reemit.Common; | ||
|
||
public readonly struct RangeMapped<TValue>(int position, int length, TValue value) : IRangeMapped | ||
{ | ||
public int Position { get; } = position; | ||
public int Length { get; } = length; | ||
public TValue Value { get; } = value; | ||
|
||
public static implicit operator TValue(RangeMapped<TValue> rangeMapped) => rangeMapped.Value; | ||
|
||
public RangeMapped<TResult> With<TResult>(TResult otherValue) => new(Position, Length, otherValue); | ||
|
||
public RangeMapped<TResult> Cast<TResult>() | ||
{ | ||
var v = Value; | ||
|
||
return With(Unsafe.As<TValue, TResult>(ref v)); | ||
} | ||
|
||
public RangeMapped<TResult> Select<TResult>(Func<TValue, TResult> selector) => With(selector(Value)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.