Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions BInaryKit-Test/BInaryKit-Test.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<RootNamespace>BinaryKit_Test</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<AssemblyName>BinaryKit-Test</AssemblyName>
<LangVersion>14</LangVersion>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand All @@ -22,7 +23,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BinaryToolKit\BinaryToolKit.csproj" />
<ProjectReference Include="..\BinaryToolKit\BinaryToolKit.csproj"/>
</ItemGroup>

</Project>
18 changes: 5 additions & 13 deletions BInaryKit-Test/EndianTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,22 @@ public class EndianTest(ITestOutputHelper output)
public void Read_int()
{
const int value = 1000;
var expected = BitConverter.IsLittleEndian ? value : System.Buffers.Binary.BinaryPrimitives.ReverseEndianness(value);
var expected = BitConverter.IsLittleEndian ? value : BinaryPrimitives.ReverseEndianness(value);
var actual = value;
actual = EndianToolkit.Convert(actual, Endianness.Local, Endianness.Little);
output.WriteLine("expected: " + expected);
output.WriteLine("actual: " + actual);
Assert.Equal(expected, actual);
}

[Fact]
public unsafe void Read_Struct()
{
UnionStruct value = new();
var expected = value;
var actual = value;
// to big endian
if (BitConverter.IsLittleEndian)
{
new Span<byte>(&expected, sizeof(UnionStruct)).Reverse();
}
if (BitConverter.IsLittleEndian) new Span<byte>(&expected, sizeof(UnionStruct)).Reverse();
// to big endian
actual = EndianToolkit.Convert(actual, Endianness.Local, Endianness.Big);
output.WriteLine("expected: " + expected);
Expand All @@ -41,7 +38,7 @@ public unsafe void Read_Struct()
[Fact]
public void ReverseManyTest()
{
Span<int> old = [10,20,30,40,50,60,70,80];
Span<int> old = [10, 20, 30, 40, 50, 60, 70, 80];
Span<int> expected = stackalloc int[old.Length];
for (var index = 0; index < old.Length; index++)
{
Expand All @@ -50,15 +47,10 @@ public void ReverseManyTest()
}

EndianToolkit.ReverseMany(old);
for (int i = 0; i < old.Length; i++)
{
for (var i = 0; i < old.Length; i++)
if (old[i] != expected[i])
{
Assert.Fail();
}
}
}


#endregion
}
48 changes: 24 additions & 24 deletions BInaryKit-Test/StreamExTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ namespace BinaryKit_Test;

public class StreamExTest(ITestOutputHelper output)
{
#region WriteFrom Tests

[Fact]
public unsafe void Write()
{
UnionStruct expected = new();
UnionStruct value = default;
using var stream = new MemoryStream();
stream.WriteFrom(expected);
stream.Position = 0;
stream.GetBuffer().AsSpan(0, sizeof(UnionStruct)).CopyTo(new Span<byte>(&expected, sizeof(UnionStruct)));
output.WriteLine("left: " + expected);
output.WriteLine("right: " + value);
Assert.Equal(expected, value);
}

#endregion

#region ReadAs Tests

[Fact]
Expand Down Expand Up @@ -65,29 +83,12 @@ public unsafe void Read_UnionStruct()

#endregion

#region WriteFrom Tests

[Fact]
public unsafe void Write()
{
UnionStruct expected = new();
UnionStruct value = default;
using var stream = new MemoryStream();
stream.WriteFrom(expected);
stream.Position = 0;
stream.GetBuffer().AsSpan(0, sizeof(UnionStruct)).CopyTo(new Span<byte>(&expected, sizeof(UnionStruct)));
output.WriteLine("left: " + expected);
output.WriteLine("right: " + value);
Assert.Equal(expected, value);
}
#endregion

#region Multiple-Test

[Fact]
public unsafe void MultipleRead()
{
byte[] ints = [10,20,30,40];
byte[] ints = [10, 20, 30, 40];
Span<int> span = stackalloc int[1];
var ptr = (int*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ints));
var expected = *ptr;
Expand All @@ -99,17 +100,16 @@ public unsafe void MultipleRead()
stream.ReadManyAs(span2);
if (span2[0] != *(short*)ptr) Assert.Fail();
if (span2[1] != ((short*)ptr)[1]) Assert.Fail();
}
}

[Fact]
public void MultipleWrite()
{
byte[] ints = [10,20,30,40];
byte[] ints = [10, 20, 30, 40];
using var stream = new MemoryStream();
stream.WriteManyFrom(ints);
if (stream.GetBuffer() is not [10,20,30,40,..]) Assert.Fail();
}

if (stream.GetBuffer() is not [10, 20, 30, 40, ..]) Assert.Fail();
}

#endregion
}
69 changes: 69 additions & 0 deletions BInaryKit-Test/UnsafeToolkit/Pin-Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Iks.BinaryToolkit.UnsafeToolkit;

namespace BinaryKit_Test.UnsafeToolkit;

public class PinTest
{
// pin must be not null
[Fact]
public void NotNullPin()
{
try
{
// set a null ref
string str = null!;
using var pin = str.AsPinned();
}
// throws is right,null cannot be pinned
catch
{
return;
}

Assert.Fail();
}

[Fact]
public void NotUseAfterRelease()
{
var pin = new object().AsPinned();
pin.Dispose();
// use after release
try
{
var pinTarget = pin.Target;
}
catch
{
return;
}

Assert.Fail();
}

[Fact]
public void NotMultipleRelease()
{
var pin = new object().AsPinned();
pin.Dispose();
// release again
try
{
pin.Dispose();
}
catch
{
return;
}

Assert.Fail();
}

[Fact]
public void CopyRelease()
{
var pin_old = new object().AsPinned();
var pin_new = pin_old;
pin_new.Dispose();
}
}
30 changes: 15 additions & 15 deletions BinaryToolKit.slnx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Solution>
<Configurations>
<BuildType Name="Debug" />
<BuildType Name="PackNuget" />
<BuildType Name="Release" />
</Configurations>
<Folder Name="/Docs/">
<File Path="Feature.md" />
<File Path="icon.png" />
<File Path="LICENSE.md" />
<File Path="README.md" />
</Folder>
<Project Path="BInaryKit-Test/BInaryKit-Test.csproj">
<BuildType Solution="PackNuget|*" Project="Debug" />
</Project>
<Project Path="BinaryToolKit/BinaryToolKit.csproj" />
<Configurations>
<BuildType Name="Debug"/>
<BuildType Name="PackNuget"/>
<BuildType Name="Release"/>
</Configurations>
<Folder Name="/Docs/">
<File Path="Feature.md"/>
<File Path="icon.png"/>
<File Path="LICENSE.md"/>
<File Path="README.md"/>
</Folder>
<Project Path="BInaryKit-Test/BInaryKit-Test.csproj">
<BuildType Solution="PackNuget|*" Project="Debug"/>
</Project>
<Project Path="BinaryToolKit/BinaryToolKit.csproj"/>
</Solution>
13 changes: 7 additions & 6 deletions BinaryToolKit/BinaryStreamToolkit.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Iks.BinaryToolKit.Message;

namespace Iks.BinaryToolkit;

Expand Down Expand Up @@ -62,7 +63,7 @@ allows ref struct
}

/// <summary>
/// write an unmanaged type(value) to the stream.
/// write an unmanaged type(value) to the stream.
/// </summary>
/// <typeparam name="T">target type</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -75,9 +76,9 @@ allows ref struct
{
stream.Write(new ReadOnlySpan<byte>(&value, sizeof(T)));
}

/// <summary>
/// write an unmanaged type(value) to the stream.
/// write an unmanaged type(value) to the stream.
/// </summary>
/// <typeparam name="T">target type</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -106,7 +107,7 @@ allows ref struct
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void ReadManyAs<T>(Span<T> targetSpan) where T : unmanaged
{
fixed(T* ptrT = &MemoryMarshal.GetReference(targetSpan))
fixed (T* ptrT = &MemoryMarshal.GetReference(targetSpan))
{
ReadManyAs(stream, ptrT, targetSpan.Length);
}
Expand Down Expand Up @@ -154,9 +155,9 @@ allows ref struct
/// <param name="values">target position</param>
public unsafe void WriteManyFrom<T>(ReadOnlySpan<T> values) where T : unmanaged
{
fixed(T* ptrT = &MemoryMarshal.GetReference(values))
fixed (T* ptrT = &MemoryMarshal.GetReference(values))
{
WriteManyFrom(stream,ptrT, values.Length);
WriteManyFrom(stream, ptrT, values.Length);
}
}

Expand Down
2 changes: 1 addition & 1 deletion BinaryToolKit/BinaryToolKit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<DefineConstants>TRACE RELEASE</DefineConstants>
<Optimize>true</Optimize>
<DebugSymbols>true</DebugSymbols>
<DocumentationFile>bin\PackNuget\BinaryToolKit.xml</DocumentationFile>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<DebugType>portable</DebugType>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
Expand Down
Loading