Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/nuget/test/xunit-2.8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o authored May 28, 2024
2 parents c7c7af3 + daab10f commit 7d18ba9
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/src/extension/PrimitiveExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ internal static byte[] ToPrimitive<T>(this T value, Type type)
}
else if (type.IsArray || value is T[])
{
// TODO: array
// primitive.Add.Array(value);
primitive.Add.Array(value);
}
else if (value is ICollection)
{
Expand Down
5 changes: 4 additions & 1 deletion src/src/primitive/interfances/IPrimitiveAdd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ public interface IPrimitiveAdd
void Class<T>(T value);
void Struct<T>(T value);
void Array<T>(T[] value);
void List(object value);
void List<T>(List<T> value);
void BigInteger(BigInteger value);
void Bytes(byte[] value);

// overhead
void Array(object value);
void List(object value);
}
}
35 changes: 35 additions & 0 deletions src/src/primitive/partials/PrimitiveAdd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,41 @@ public void Array<T>(T[] value)
}
}

public void Array(object value)
{
if (value == null) return;
var type = value.GetType();
if (!type.IsArray) return;
var childrenType = type.GetElementType();
if (childrenType == null) return;

var list = (IList)value;

Vault.Add(Prefix.Array);

var size = list.Count;

if (size > 0)
{
var collection = new List<byte>();

foreach (var x in list)
{
var result = x.ToPrimitive(childrenType);
collection.AddRange(result);
}

Vault.AddRange(BitConverter.GetBytes(size)); // objects count
Vault.AddRange(BitConverter.GetBytes(collection.Count)); // buffer size
Vault.AddRange(collection); // buffer
}
else
{
Vault.AddRange(BitConverter.GetBytes(0)); // objects count
Vault.AddRange(BitConverter.GetBytes(0)); // buffer
}
}

public void List(object value)
{
if (value == null || !(value is ICollection list)) return;
Expand Down
2 changes: 1 addition & 1 deletion test/ByterTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.0">
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
7 changes: 5 additions & 2 deletions test/primitive/ReadAndWrite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -591,15 +591,18 @@ public void TestArrayFromStruct2()

p.Add.Struct(real);

Assert.Equal(0, p.Position);


var clone = p.Get.Struct<ArrayFromStruct2>();

Assert.True(p.IsValid);
Assert.Equal(real.Array, clone.Array);
for (int i = 0; i < real.Array.Length; i++)
{
Assert.Equal(real.Array[i], clone.Array[i]);
}


Assert.True(p.IsValid);
Terminate(ref p);
}

Expand Down

0 comments on commit 7d18ba9

Please sign in to comment.