Skip to content

Commit

Permalink
Added more test coverage for streams and some extension methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Oct 20, 2023
1 parent b8f0432 commit a513626
Show file tree
Hide file tree
Showing 26 changed files with 166 additions and 9 deletions.
1 change: 1 addition & 0 deletions Schema Tests/binary/io/DelayedContentOutputStreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Text;

using schema.util;
using schema.util.asserts;
using schema.util.streams;

namespace schema.binary.io {
Expand Down
1 change: 1 addition & 0 deletions Schema Tests/binary/io/NestedListsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Text;
using schema.util;
using schema.util.asserts;

namespace schema.binary.io {
public class NestedListsTests {
Expand Down
1 change: 1 addition & 0 deletions Schema Tests/text/reader/TextReaderNumberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using NUnit.Framework;

using schema.util;
using schema.util.asserts;


namespace schema.text.reader {
Expand Down
1 change: 1 addition & 0 deletions Schema Tests/text/reader/TextReaderNumbersTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using NUnit.Framework;

using schema.util;
using schema.util.asserts;

namespace schema.text.reader {
internal class TextReaderNumbersTests {
Expand Down
24 changes: 24 additions & 0 deletions Schema Tests/util/enumerables/EnumerableExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using NUnit.Framework;

namespace schema.util.enumerables {
public class EnumerableExtensionsTests {
[Test]
public void TestResizedOriginallyNull() {
CollectionAssert.AreEqual(
new[] { 0, 0, 0 },
((int[]?) null).Resized(3));
}

[Test]
public void TestResizeArrayGrowing() {
var inputList = new[] { 1, 2, 3 };
CollectionAssert.AreEqual(new[] { 1, 2, 3, 0 }, inputList.Resized(4));
}

[Test]
public void TestResizeArrayShrinking() {
var inputList = new[] { 1, 2, 3 };
CollectionAssert.AreEqual(new[] { 1, 2 }, inputList.Resized(2));
}
}
}
37 changes: 37 additions & 0 deletions Schema Tests/util/sequences/SequencesUtilTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using NUnit.Framework;

namespace schema.util.sequences {
public class SequencesUtilTests {
[Test]
public void TestResizeArrayOriginallyNull() {
CollectionAssert.AreEqual(
new[] { 0, 0, 0 },
SequencesUtil.CloneAndResizeSequence((int[]?) null, 3));
}

[Test]
public void TestResizeArrayGrowing() {
var inputList = new[] { 1, 2, 3 };
CollectionAssert.AreEqual(
new[] { 1, 2, 3, 0 },
SequencesUtil.CloneAndResizeSequence(inputList, 4));
}

[Test]
public void TestResizeArrayShrinking() {
var inputList = new[] { 1, 2, 3 };
CollectionAssert.AreEqual(
new[] { 1, 2, },
SequencesUtil.CloneAndResizeSequence(inputList, 2));
}

[Test]
public void TestResizingArrayReturnsSameWhenLengthIsSame() {
var inputList = new[] { 1, 2, 3 };
Assert.AreSame(inputList,
SequencesUtil.CloneAndResizeSequence(
inputList,
inputList.Length));
}
}
}
43 changes: 36 additions & 7 deletions Schema Tests/util/streams/RangedReadableSubstreamTests.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,46 @@
using NUnit.Framework;
using System;

using NUnit.Framework;

using schema.util.asserts;

using static System.Runtime.InteropServices.JavaScript.JSType;

namespace schema.util.streams {
public class RangedReadableSubstreamTests {
[Test]
public void TestFullSubstream() {
var s = new ReadableStream(new byte[] { 1, 2, 3, 4, 5, 6, 7 });
var ss = new RangedReadableSubstream(s, 0, s.Length);
var data = new byte[] { 1, 2, 3, 4, 5, 6, 7 };
var rs = new ReadableStream(data);
var rrs = new RangedReadableSubstream(rs, 0, rs.Length);

Assert.AreEqual(0, rs.Position);
Assert.AreEqual(0, rrs.Position);
Assert.AreEqual(7, rs.Length);
Assert.AreEqual(7, rrs.Length);

CollectionAssert.AreEqual(data, rrs.ReadAllBytes());
Assert.AreEqual(7, rs.Position);
Assert.AreEqual(7, rrs.Position);
}

[Test]
public void TestPartialSubstream() {
var data = new byte[] { 1, 2, 3, 4, 5, 6, 7 };
var rs = new ReadableStream(data);

var rrs = new RangedReadableSubstream(rs, 1, rs.Length - 2);
rrs.Position = 1;

Assert.AreEqual(0, s.Position);
Assert.AreEqual(0, ss.Position);
Assert.AreEqual(1, rs.Position);
Assert.AreEqual(1, rrs.Position);
Assert.AreEqual(7, rs.Length);
Assert.AreEqual(6, rrs.Length);

Assert.AreEqual(7, ss.Length);
Assert.AreEqual(7, ss.Length);
CollectionAssert.AreEqual(data.AsSpan(1, data.Length - 2).ToArray(),
rrs.ReadAllBytes());
Assert.AreEqual(6, rs.Position);
Assert.AreEqual(6, rrs.Position);
}
}
}
39 changes: 39 additions & 0 deletions Schema/Schema - Backup.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<IsRoslynComponent>true</IsRoslynComponent>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>preview</LangVersion>
<RootNamespace>schema</RootNamespace>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<PropertyGroup>
<PackageProjectUrl>https://github.com/MeltyPlayer/Schema</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Description>Library for converting classes to and from binary. Provides a C# Roslyn generator that automatically implements conversion logic for simple classes.</Description>
<PackageId>schema</PackageId>
<Title>schema</Title>
<Version>0.3.5</Version>
<Authors>MeltyPlayer</Authors>
</PropertyGroup>

<ItemGroup>
<None Include="..\README.md" Pack="true" PackagePath="\" />
<!-- Include the netstandard2.0 DLL into the package -->
<None Include="$(OutputPath)$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.HighPerformance" Version="8.2.0" />
<PackageReference Include="Encoding.SpanExtensions" Version="1.0.0" />
<PackageReference Include="Half" Version="1.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.0.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.0.1" />
<PackageReference Include="System.Memory" Version="4.5.5" Condition="'$(TargetFramework)'=='netstandard2.0'" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions Schema/src/binary/BinarySchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using schema.binary.attributes;
using schema.binary.text;
using schema.util;
using schema.util.asserts;
using schema.util.syntax;
using schema.util.types;

Expand Down
1 change: 1 addition & 0 deletions Schema/src/binary/LocalPositionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Runtime.CompilerServices;

using schema.util;
using schema.util.asserts;
using schema.util.streams;

namespace schema.binary {
Expand Down
1 change: 1 addition & 0 deletions Schema/src/binary/attributes/BMemberAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using schema.binary.parser;
using schema.util;
using schema.util.asserts;
using schema.util.diagnostics;
using schema.util.symbols;
using schema.util.types;
Expand Down
1 change: 1 addition & 0 deletions Schema/src/binary/io/DelayedContentOutputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using CommunityToolkit.HighPerformance;

using schema.util;
using schema.util.asserts;
using schema.util.streams;

namespace schema.binary.io {
Expand Down
1 change: 1 addition & 0 deletions Schema/src/binary/io/OutOfOrderDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;

using schema.util;
using schema.util.asserts;

namespace schema.binary.io {
public interface IOutOfOrderDictionary<in TKey, TValue>
Expand Down
1 change: 1 addition & 0 deletions Schema/src/binary/parser/MemberReferenceUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using schema.binary.attributes;
using schema.util;
using schema.util.asserts;
using schema.util.types;

namespace schema.binary.parser {
Expand Down
1 change: 1 addition & 0 deletions Schema/src/binary/parser/TypeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.CodeAnalysis;

using schema.util;
using schema.util.asserts;
using schema.util.symbols;
using schema.util.types;

Expand Down
1 change: 1 addition & 0 deletions Schema/src/binary/text/BinarySchemaReaderGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using schema.binary.attributes;
using schema.binary.dependencies;
using schema.util;
using schema.util.asserts;
using schema.util.symbols;
using schema.util.text;
using schema.util.types;
Expand Down
1 change: 1 addition & 0 deletions Schema/src/binary/text/BinarySchemaWriterGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using schema.binary.dependencies;
using schema.binary.parser;
using schema.util;
using schema.util.asserts;
using schema.util.symbols;
using schema.util.text;

Expand Down
2 changes: 2 additions & 0 deletions Schema/src/binary/writer/EndianBinaryWriter_Position.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Text;
using System.Threading.Tasks;

using schema.util.asserts;


namespace schema.binary {
public sealed partial class EndianBinaryWriter {
Expand Down
1 change: 1 addition & 0 deletions Schema/src/testing/BinarySchemaAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections;

using schema.util;
using schema.util.asserts;

namespace schema.binary.testing {
public static class BinarySchemaAssert {
Expand Down
1 change: 1 addition & 0 deletions Schema/src/text/reader/TextReader_Number.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;

using schema.util;
using schema.util.asserts;

namespace schema.text.reader {
public sealed partial class TextReader {
Expand Down
1 change: 1 addition & 0 deletions Schema/src/text/reader/TextReader_NumberHex.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Linq;

using schema.util;
using schema.util.asserts;

namespace schema.text.reader {
public sealed partial class TextReader {
Expand Down
1 change: 1 addition & 0 deletions Schema/src/text/reader/TextReader_String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text;

using schema.util;
using schema.util.asserts;

namespace schema.text.reader {
public sealed partial class TextReader {
Expand Down
2 changes: 1 addition & 1 deletion Schema/src/util/AccessChainUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using schema.binary.attributes;
using schema.binary.io;
using schema.binary.parser;
using schema.util;
using schema.util.asserts;
using schema.util.diagnostics;
using schema.util.symbols;
using schema.util.types;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Linq;
using System.Text.RegularExpressions;

namespace schema.util {
namespace schema.util.asserts {
public sealed class Asserts {
/**
* NOTE: Using $"" to define messages allocates strings, and can be expensive!
Expand Down
2 changes: 2 additions & 0 deletions Schema/src/util/streams/RangedReadableSubstream.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.IO;

using schema.util.asserts;

namespace schema.util.streams {
/// <summary>
/// Represents a substream of an underlying <see cref="Stream" />.
Expand Down
7 changes: 7 additions & 0 deletions Schema/src/util/streams/StreamExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ public static byte ReadByte(this IReadableStream stream) {
return value;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] ReadAllBytes(this ISizedReadableStream stream) {
var bytes = new byte[stream.Length - stream.Position];
stream.Read(bytes);
return bytes;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteByte(this IWritableStream stream, byte value)
=> stream.Write(UnsafeUtil.AsSpan(ref value));
Expand Down

0 comments on commit a513626

Please sign in to comment.