-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
310bae7
commit d3a4a22
Showing
4 changed files
with
119 additions
and
12 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,40 @@ | ||
using System; | ||
|
||
using NUnit.Framework; | ||
|
||
using schema.testing; | ||
|
||
|
||
namespace schema.binary { | ||
public partial class NewTests { | ||
[BinarySchema] | ||
private partial class BinarySchemaThatWillSucceed : IBinaryConvertible { | ||
public int Value { get; set; } | ||
} | ||
|
||
[Test] | ||
public void TestTryReadNewSucceeds() { | ||
using var br = SchemaMemoryStream.From([123456]).GetBinaryReader(); | ||
Assert.True( | ||
br.TryReadNew<BinarySchemaThatWillSucceed>(out var successful)); | ||
Assert.AreEqual(4, br.Position); | ||
Assert.AreEqual(123456, successful.Value); | ||
} | ||
|
||
private class BinarySchemaThatWillFail : IBinaryDeserializable { | ||
public void Read(IBinaryReader br) { | ||
br.ReadByte(); | ||
throw new Exception(); | ||
} | ||
} | ||
|
||
[Test] | ||
public void TestTryReadNewFails() { | ||
using var br = SchemaMemoryStream.From([123456]).GetBinaryReader(); | ||
Assert.False( | ||
br.TryReadNew<BinarySchemaThatWillFail>(out var successful)); | ||
Assert.AreEqual(0, br.Position); | ||
Assert.Null(successful); | ||
} | ||
} | ||
} |
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,45 @@ | ||
using System; | ||
using System.IO; | ||
|
||
using CommunityToolkit.HighPerformance; | ||
|
||
using schema.binary; | ||
using schema.util.streams; | ||
|
||
namespace schema.testing { | ||
public class SchemaMemoryStream(MemoryStream impl) | ||
: ISeekableReadableStream, ISeekableWritableStream { | ||
public static unsafe SchemaMemoryStream From<T>(T[] src) | ||
where T : unmanaged { | ||
var size = sizeof(T); | ||
var data = new byte[size * src.Length]; | ||
src.AsSpan().AsBytes().CopyTo(data); | ||
|
||
var ms = new MemoryStream(data); | ||
return new SchemaMemoryStream(ms); | ||
} | ||
|
||
public IBinaryReader GetBinaryReader() | ||
=> new SchemaBinaryReader(this, this.Endianness); | ||
|
||
public void Dispose() => impl.Dispose(); | ||
|
||
public byte ReadByte() => (byte) impl.ReadByte(); | ||
public int Read(Span<byte> dst) => impl.Read(dst); | ||
|
||
public long Position { | ||
get => impl.Position; | ||
set => impl.Position = value; | ||
} | ||
|
||
public long Length => impl.Length; | ||
|
||
public void WriteByte(byte b) => impl.WriteByte(b); | ||
public void Write(ReadOnlySpan<byte> src) => impl.Write(src); | ||
|
||
public void Write(IReadableStream readableStream) | ||
=> impl.WriteImpl(readableStream); | ||
|
||
public Endianness Endianness => EndiannessUtil.SystemEndianness; | ||
} | ||
} |
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,32 @@ | ||
using System; | ||
using System.IO; | ||
using System.Runtime.CompilerServices; | ||
|
||
using CommunityToolkit.HighPerformance; | ||
|
||
namespace schema.util.streams { | ||
public static class BasicStreamImpls { | ||
/// <summary> | ||
/// (Straight-up copied from the implementation of Stream.CopyTo()) | ||
/// We pick a value that is the largest multiple of 4096 that is still smaller than the large object heap threshold (85K). | ||
/// The CopyTo/CopyToAsync buffer is short-lived and is likely to be collected at Gen0, and it offers a significant | ||
/// improvement in Copy performance. | ||
/// </summary> | ||
private const int DEFAULT_COPY_BUFFER_SIZE = 81920; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal static void WriteImpl(this Stream impl, | ||
IReadableStream readableStream) { | ||
if (readableStream is ReadableStream readableStreamImpl) { | ||
readableStreamImpl.Impl.CopyTo(impl); | ||
return; | ||
} | ||
|
||
Span<byte> buffer = stackalloc byte[DEFAULT_COPY_BUFFER_SIZE]; | ||
int bytesRead; | ||
while ((bytesRead = readableStream.Read(buffer)) != 0) { | ||
impl.Write(buffer.Slice(0, bytesRead)); | ||
} | ||
} | ||
} | ||
} |
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