-
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
Showing
29 changed files
with
344 additions
and
218 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
75 changes: 75 additions & 0 deletions
75
ByteSerialization.Tests/Integration/SimpleIntegrationTest.cs
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,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)); | ||
} | ||
} | ||
} |
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
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,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 | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |
8 changes: 3 additions & 5 deletions
8
...Tests/IO/Utils/StructArrayComparerTest.cs → ....Tests/Unit/IO/StructArrayComparerTest.cs
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
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
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
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
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
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
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
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
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
Oops, something went wrong.