Skip to content

Commit

Permalink
refactor / tests
Browse files Browse the repository at this point in the history
  • Loading branch information
akopetsch committed May 30, 2024
1 parent 27fec4e commit 6da6cbc
Show file tree
Hide file tree
Showing 29 changed files with 344 additions and 218 deletions.
22 changes: 0 additions & 22 deletions ByteSerialization.Tests/IO/Extensions/SwapBytesTest.cs

This file was deleted.

75 changes: 75 additions & 0 deletions ByteSerialization.Tests/Integration/SimpleIntegrationTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// SPDX-License-Identifier: MIT

using ByteSerialization.Attributes;
using ByteSerialization.IO;
using Xunit;

namespace ByteSerialization.Tests.Integration
{
public class SimpleIntegrationTest
{
public class LengthPrefixedString
{
[Order(0)]
public byte Length { get; set; }

[Order(1), Length(nameof(Length))]
public char[] CharArray { get; set; }

public LengthPrefixedString() { }

public LengthPrefixedString(string s)
{
Length = Convert.ToByte(s.Length);
CharArray = s.ToCharArray();
}
}

public class Manufacturer
{
[Order(0)]
public LengthPrefixedString Name { get; set; }

public Manufacturer() { }

public Manufacturer(string name)
{
Name = new LengthPrefixedString(name);
}
}

public class Car
{
[Order(0)]
public LengthPrefixedString Name { get; set; }

[Order(1), Reference]
public Manufacturer Manufacturer { get; set; }

public Car() { }

public Car(string name, Manufacturer manufacturer)
{
Name = new LengthPrefixedString(name);
Manufacturer = manufacturer;
}
}

[Fact]
public void Test()
{
// setup
var manufacturer = new Manufacturer("MF");
var car = new Car("Car1", manufacturer);

// serialize
using var ms = new MemoryStream();
new ByteSerializer().Serialize(ms, car, Endianness.BigEndian);

// compare
byte[] expected = HexStringConverter.ToByteArray("0443 6172 3100 0000 0902 4d46"); // .Car1.....MF
byte[] actual = ms.ToArray();
Assert.True(expected.SequenceEqual(actual));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
// Copyright 2024 Alexander Kopetsch
// Licensed under GPLv2 or any later version
// Refer to the included LICENSE.txt file.
// SPDX-License-Identifier: MIT

using ByteSerialization.IO;
using ByteSerialization.IO.Utils;
using Xunit;

namespace ByteSerialization.Tests.IO.Utils
namespace ByteSerialization.Tests.Unit.IO
{
public class BitsHelperTest
{
Expand Down Expand Up @@ -66,9 +63,10 @@ public void Test_GetBitMask_1_LsbFirst() =>
private static bool[] GetBitsArray(
bool value0, bool value1, bool value2, bool value3,
bool value4, bool value5, bool value6, bool value7) =>
new bool[] {
[
value0, value1, value2, value3,
value4, value5, value6, value7 };
value4, value5, value6, value7
];

private static bool[] GetAllZeroesBitsArray() =>
Enumerable.Repeat(false, BitsHelper.BitsPerByte).ToArray();
Expand Down
58 changes: 58 additions & 0 deletions ByteSerialization.Tests/Unit/IO/BytesSwapperTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SPDX-License-Identifier: MIT

using ByteSerialization.IO;
using Xunit;

namespace ByteSerialization.Tests.Unit.IO
{
public class BytesSwapperTest
{
#pragma warning disable IDE0004
// Disable 'Remove unnecessary cast (IDE0004)'
// because casts make the code more readable here.

[Fact]
public void Test_Swap_Int32() =>
Assert.Equal(
expected:
unchecked((int)0xDDCCBBAA),
actual: BytesSwapper.Swap(
unchecked((int)0xAABBCCDD)));

[Fact]
public void Test_Swap_UInt32() =>
Assert.Equal(
expected:
(uint)0xAABBCCDD,
actual: BytesSwapper.Swap(
(uint)0xDDCCBBAA));

[Fact]
public void Test_Swap_Int16() =>
Assert.Equal(
expected:
unchecked((short)0xC0A0),
actual: BytesSwapper.Swap(
unchecked((short)0xA0C0)));

[Fact]
public void Test_Swap_UInt16() =>
Assert.Equal(
expected:
(ushort)0xC0A0,
actual: BytesSwapper.Swap(
(ushort)0xA0C0));

// TODO: Test_Swap_Int64

[Fact]
public void Test_Swap_UInt64() =>
Assert.Equal(
expected:
(ulong)0x2211ffeeddccbbaa,
actual: BytesSwapper.Swap(
(ulong)0xaabbccddeeff1122));

#pragma warning restore IDE0004
}
}
26 changes: 26 additions & 0 deletions ByteSerialization.Tests/Unit/IO/HexStringConverterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: MIT

using ByteSerialization.IO;
using Xunit;

namespace ByteSerialization.Tests.Unit.IO
{
public class HexStringConverterTest
{
[Fact]
public void Test_ToByteArray()
{
byte[] expected = [0xDE, 0xAD, 0xBE, 0xEF];
byte[] actual = HexStringConverter.ToByteArray("deadbeef");
Assert.True(expected.SequenceEqual(actual));
}

[Fact]
public void Test_ToHexString()
{
string expected = "deadbeef";
string actual = HexStringConverter.ToHexString([0xDE, 0xAD, 0xBE, 0xEF]);
Assert.Equal(expected, actual);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// Copyright 2024 Alexander Kopetsch
// Licensed under GPLv2 or any later version
// Refer to the included LICENSE.txt file.
// SPDX-License-Identifier: MIT

using ByteSerialization.IO.Utils;
using ByteSerialization.IO;
using Xunit;

namespace ByteSerialization.Tests.IO.Utils
namespace ByteSerialization.Tests.Unit.IO
{
public class StructArrayComparerTest
{
Expand Down
7 changes: 4 additions & 3 deletions ByteSerialization/ByteSerializerContext.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// SPDX-License-Identifier: MIT

using ByteSerialization.IO.Extensions;
using ByteSerialization.IO;
using ByteSerialization.Nodes;
using ByteSerialization.Utils;
using System;
using System.Diagnostics;
using System.IO;
Expand All @@ -16,7 +17,7 @@ namespace ByteSerialization
public class ByteSerializerContext
{
private string DebuggerDisplay =>
$"0x{Position.ToHexString()} | {Mode}";
$"0x{HexStringConverter.ToCompactHexString(Position)} | {Mode}";

#region Properties

Expand Down Expand Up @@ -86,7 +87,7 @@ public void EnsureAlignment(int alignment)
if (alignment != 0)
{
long actual = Position;
long target = Position.Ceiling(alignment);
long target = CeilingHelper.Ceiling(Position, alignment);
ConsumeBytes(Convert.ToInt32(target - actual));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT

using ByteSerialization.Components.Values.Composites.Records;
using ByteSerialization.Utilities;
using ByteSerialization.Utils;
using System;
using System.Linq;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using ByteSerialization.Components.Attributes.Reference;
using ByteSerialization.Components.Values;
using ByteSerialization.Extensions;
using ByteSerialization.IO.Extensions;
using ByteSerialization.IO;
using ByteSerialization.Nodes;
using System;

Expand All @@ -19,7 +19,7 @@ public override string GetDebuggerDisplay()
if (Pointer.HasValue)
{
string type = Type.GetFriendlyName();
string pointer = HasNullPointer ? "NULL" : $"0x{Pointer.Value.ToHexString()}";
string pointer = HasNullPointer ? "NULL" : $"0x{HexStringConverter.ToCompactHexString(Pointer.Value)}";
return $"{type} @ {pointer}";
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using ByteSerialization.Attributes;
using ByteSerialization.Nodes;
using ByteSerialization.Pooling;
using ByteSerialization.Utilities;
using ByteSerialization.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
30 changes: 3 additions & 27 deletions ByteSerialization/Extensions/LinqExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,12 @@

namespace ByteSerialization.Extensions
{
public static class LinqExtensions
internal static class LinqExtensions
{
public static IEnumerable<T> Except<T>(this IEnumerable<T> first, T second) =>
internal static IEnumerable<T> Except<T>(this IEnumerable<T> first, T second) =>
first.Except(new T[] { second });

public static IEnumerable<T> OfType<T>(this IEnumerable<T> source, Type type) =>
internal static IEnumerable<T> OfType<T>(this IEnumerable<T> source, Type type) =>
source.Where(x => x?.GetType().Is(type) ?? false);

public static bool AnyDuplicate<T>(this IEnumerable<T> source)
{
var knownKeys = new HashSet<T>();
return source.Any(item => !knownKeys.Add(item));
}

public static bool AllUnique<T>(this IEnumerable<T> source) =>
!source.AnyDuplicate();

public static bool Are<T1, T2>(this IEnumerable values) =>
values.Cast<object>().Are(new Type[] { typeof(T1), typeof(T2) });

public static bool Are<T1, T2, T3>(this IEnumerable values) =>
values.Cast<object>().Are(new Type[] { typeof(T1), typeof(T2), typeof(T3) });

private static bool Are(this IEnumerable values, IEnumerable<Type> types) =>
types.Contains(values.Cast<object>().Select(n => n?.GetType()).Distinct());

private static bool Are(this IEnumerable values, params Type[] types) =>
values.Are(types as IEnumerable<Type>);

private static bool Contains<T>(this IEnumerable<T> a, IEnumerable<T> b) =>
!b.Any(x => !a.Contains(x));
}
}
4 changes: 2 additions & 2 deletions ByteSerialization/Extensions/ListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

namespace ByteSerialization.Extensions
{
public static class ListExtensions
internal static class ListExtensions
{
public static void AddRangeIfAny<T>(this List<T> list, IEnumerable<T> collection)
internal static void AddRangeIfAny<T>(this List<T> list, IEnumerable<T> collection)
{
if (collection.Any())
list.AddRange(collection);
Expand Down
14 changes: 7 additions & 7 deletions ByteSerialization/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@

namespace ByteSerialization.Extensions
{
public static class TypeExtensions
internal static class TypeExtensions
{
public static bool Is<T>(this Type type) =>
internal static bool Is<T>(this Type type) =>
typeof(T).IsAssignableFrom(type);

public static bool Is(this Type type, Type otherType) =>
internal static bool Is(this Type type, Type otherType) =>
otherType.IsAssignableFrom(type);

public static bool IsOneOf<T1, T2>(this Type type) =>
internal static bool IsOneOf<T1, T2>(this Type type) =>
type.IsOneOf(typeof(T1), typeof(T2));

public static bool IsOneOf(this Type type, params Type[] otherTypes) =>
internal static bool IsOneOf(this Type type, params Type[] otherTypes) =>
otherTypes.Contains(type);

public static bool IsBuiltinList(this Type type) =>
internal static bool IsBuiltinList(this Type type) =>
type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>);

public static string GetFriendlyName(this Type type)
internal static string GetFriendlyName(this Type type)
{
if (type.IsGenericType)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

using System;

namespace ByteSerialization.IO.Utils
namespace ByteSerialization.IO
{
public static class BitsHelper
{
Expand All @@ -26,8 +26,9 @@ public static byte GetBitMask(int bitIndex, BitOrder bitOrder)
throw new ArgumentOutOfRangeException(
nameof(bitIndex), $"Must be between {0} and {MaxBitIndex}");

return bitOrder switch {
BitOrder.MsbFirst => (byte)((1 << MaxBitIndex) >> bitIndex),
return bitOrder switch
{
BitOrder.MsbFirst => (byte)(1 << MaxBitIndex >> bitIndex),
BitOrder.LsbFirst => (byte)(1 << bitIndex),
_ => throw new ArgumentException("Must have a valid value.", nameof(bitOrder)),
};
Expand Down
Loading

0 comments on commit 6da6cbc

Please sign in to comment.