Skip to content

Commit 7cf135d

Browse files
committed
test(extensions): 添加字节序转换测试
为 byte[]、Span<byte> 和 ReadOnlySpan<byte> 的字节序转换方法添加单元测试
1 parent e3c9033 commit 7cf135d

3 files changed

Lines changed: 229 additions & 2 deletions

File tree

GameFrameX.Foundation.Tests/Extensions/ByteExtensionTests.cs

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2428,4 +2428,105 @@ public void RoundTrip_WriteStringWithoutLengthLittleEndian_ReadStringLittleEndia
24282428
}
24292429

24302430
#endregion
2431-
}
2431+
2432+
#region Endian Conversion Tests
2433+
2434+
[Fact]
2435+
public void ConvertEndianInPlace_ValidInput_ShouldConvertCorrectly()
2436+
{
2437+
// Arrange
2438+
byte[] buffer = { 0x12, 0x34, 0x56, 0x78 };
2439+
2440+
// Act
2441+
buffer.ConvertEndianInPlace(2);
2442+
2443+
// Assert
2444+
Assert.Equal(new byte[] { 0x34, 0x12, 0x78, 0x56 }, buffer);
2445+
}
2446+
2447+
[Fact]
2448+
public void ConvertEndian_ValidInput_ShouldReturnConvertedCopy()
2449+
{
2450+
// Arrange
2451+
byte[] source = { 0x01, 0x02, 0x03, 0x04 };
2452+
2453+
// Act
2454+
var result = source.ConvertEndian(2);
2455+
2456+
// Assert
2457+
Assert.Equal(new byte[] { 0x02, 0x01, 0x04, 0x03 }, result);
2458+
Assert.Equal(new byte[] { 0x01, 0x02, 0x03, 0x04 }, source);
2459+
}
2460+
2461+
[Fact]
2462+
public void BigEndianToLittleEndian_ValidInput_ShouldConvertCorrectly()
2463+
{
2464+
// Arrange
2465+
byte[] source = { 0x11, 0x22, 0x33, 0x44 };
2466+
2467+
// Act
2468+
var result = source.BigEndianToLittleEndian(2);
2469+
2470+
// Assert
2471+
Assert.Equal(new byte[] { 0x22, 0x11, 0x44, 0x33 }, result);
2472+
}
2473+
2474+
[Fact]
2475+
public void ConvertEndianInPlace_InvalidElementSize_ShouldThrowArgumentOutOfRangeException()
2476+
{
2477+
// Arrange
2478+
byte[] buffer = { 0x12, 0x34 };
2479+
2480+
// Act & Assert
2481+
Assert.Throws<ArgumentOutOfRangeException>(() => buffer.ConvertEndianInPlace(0));
2482+
}
2483+
2484+
[Fact]
2485+
public void ConvertEndianInPlace_LengthNotAligned_ShouldThrowArgumentException()
2486+
{
2487+
// Arrange
2488+
byte[] buffer = { 0x01, 0x02, 0x03 };
2489+
2490+
// Act & Assert
2491+
Assert.Throws<ArgumentException>(() => buffer.ConvertEndianInPlace(2));
2492+
}
2493+
2494+
[Fact]
2495+
public void ConvertEndianByInt16InPlace_ValidInput_ShouldConvertCorrectly()
2496+
{
2497+
// Arrange
2498+
byte[] buffer = { 0x12, 0x34, 0x56, 0x78 };
2499+
2500+
// Act
2501+
buffer.ConvertEndianByInt16InPlace();
2502+
2503+
// Assert
2504+
Assert.Equal(new byte[] { 0x34, 0x12, 0x78, 0x56 }, buffer);
2505+
}
2506+
2507+
[Fact]
2508+
public void ConvertEndianByInt32_ValidInput_ShouldReturnConvertedCopy()
2509+
{
2510+
// Arrange
2511+
byte[] source = { 0x01, 0x02, 0x03, 0x04 };
2512+
2513+
// Act
2514+
var result = source.ConvertEndianByInt32();
2515+
2516+
// Assert
2517+
Assert.Equal(new byte[] { 0x04, 0x03, 0x02, 0x01 }, result);
2518+
Assert.Equal(new byte[] { 0x01, 0x02, 0x03, 0x04 }, source);
2519+
}
2520+
2521+
[Fact]
2522+
public void ConvertEndianByInt64_LengthNotAligned_ShouldThrowArgumentException()
2523+
{
2524+
// Arrange
2525+
byte[] source = { 0x01, 0x02, 0x03, 0x04 };
2526+
2527+
// Act & Assert
2528+
Assert.Throws<ArgumentException>(() => source.ConvertEndianByInt64());
2529+
}
2530+
2531+
#endregion
2532+
}

GameFrameX.Foundation.Tests/Extensions/ReadOnlySpanExtensionsTests.cs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,5 +705,68 @@ public void ReadDoubleBigEndianValue_NegativeOffset_ShouldThrowException()
705705

706706
#endregion
707707

708+
#region Endian Conversion Tests
709+
710+
[Fact]
711+
public void ConvertEndian_ValidInput_ShouldReturnConvertedArray()
712+
{
713+
// Arrange
714+
var buffer = new byte[] { 0x01, 0x02, 0x03, 0x04 };
715+
716+
// Act
717+
var result = ((ReadOnlySpan<byte>)buffer).ConvertEndian(2);
718+
719+
// Assert
720+
Assert.Equal(new byte[] { 0x02, 0x01, 0x04, 0x03 }, result);
721+
}
722+
723+
[Fact]
724+
public void LittleEndianToBigEndian_ValidInput_ShouldReturnConvertedArray()
725+
{
726+
// Arrange
727+
var buffer = new byte[] { 0x78, 0x56, 0x34, 0x12 };
728+
729+
// Act
730+
var result = ((ReadOnlySpan<byte>)buffer).LittleEndianToBigEndian(2);
731+
732+
// Assert
733+
Assert.Equal(new byte[] { 0x56, 0x78, 0x12, 0x34 }, result);
734+
}
735+
736+
[Fact]
737+
public void ConvertEndian_LengthNotAligned_ShouldThrowArgumentException()
738+
{
739+
// Arrange
740+
var buffer = new byte[] { 0x01, 0x02, 0x03 };
741+
742+
// Act & Assert
743+
Assert.Throws<ArgumentException>(() => ((ReadOnlySpan<byte>)buffer).ConvertEndian(2));
744+
}
745+
746+
[Fact]
747+
public void ConvertEndianByInt16_ValidInput_ShouldReturnConvertedArray()
748+
{
749+
// Arrange
750+
var buffer = new byte[] { 0x12, 0x34, 0x56, 0x78 };
751+
752+
// Act
753+
var result = ((ReadOnlySpan<byte>)buffer).ConvertEndianByInt16();
754+
755+
// Assert
756+
Assert.Equal(new byte[] { 0x34, 0x12, 0x78, 0x56 }, result);
757+
}
758+
759+
[Fact]
760+
public void ConvertEndianByInt64_LengthNotAligned_ShouldThrowArgumentException()
761+
{
762+
// Arrange
763+
var buffer = new byte[] { 0x01, 0x02, 0x03, 0x04 };
764+
765+
// Act & Assert
766+
Assert.Throws<ArgumentException>(() => ((ReadOnlySpan<byte>)buffer).ConvertEndianByInt64());
767+
}
768+
769+
#endregion
770+
708771
#endregion
709-
}
772+
}

GameFrameX.Foundation.Tests/Extensions/SpanExtensionTests.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,4 +904,67 @@ public void RoundTrip_WriteStringLittleEndianValue_ReadStringLittleEndianValue_S
904904
}
905905

906906
#endregion
907+
908+
#region Endian Conversion Tests
909+
910+
[Fact]
911+
public void ConvertEndianInPlace_ValidInput_ShouldConvertCorrectly()
912+
{
913+
// Arrange
914+
byte[] buffer = { 0x10, 0x20, 0x30, 0x40 };
915+
916+
// Act
917+
buffer.AsSpan().ConvertEndianInPlace(2);
918+
919+
// Assert
920+
Assert.Equal(new byte[] { 0x20, 0x10, 0x40, 0x30 }, buffer);
921+
}
922+
923+
[Fact]
924+
public void BigEndianToLittleEndianInPlace_ValidInput_ShouldConvertCorrectly()
925+
{
926+
// Arrange
927+
byte[] buffer = { 0xAA, 0xBB, 0xCC, 0xDD };
928+
929+
// Act
930+
buffer.AsSpan().BigEndianToLittleEndianInPlace(2);
931+
932+
// Assert
933+
Assert.Equal(new byte[] { 0xBB, 0xAA, 0xDD, 0xCC }, buffer);
934+
}
935+
936+
[Fact]
937+
public void ConvertEndianInPlace_LengthNotAligned_ShouldThrowArgumentException()
938+
{
939+
// Arrange
940+
byte[] buffer = { 0x01, 0x02, 0x03 };
941+
942+
// Act & Assert
943+
Assert.Throws<ArgumentException>(() => buffer.AsSpan().ConvertEndianInPlace(2));
944+
}
945+
946+
[Fact]
947+
public void ConvertEndianByInt16InPlace_ValidInput_ShouldConvertCorrectly()
948+
{
949+
// Arrange
950+
byte[] buffer = { 0x10, 0x20, 0x30, 0x40 };
951+
952+
// Act
953+
buffer.AsSpan().ConvertEndianByInt16InPlace();
954+
955+
// Assert
956+
Assert.Equal(new byte[] { 0x20, 0x10, 0x40, 0x30 }, buffer);
957+
}
958+
959+
[Fact]
960+
public void ConvertEndianByInt64InPlace_LengthNotAligned_ShouldThrowArgumentException()
961+
{
962+
// Arrange
963+
byte[] buffer = { 0x01, 0x02, 0x03, 0x04 };
964+
965+
// Act & Assert
966+
Assert.Throws<ArgumentException>(() => buffer.AsSpan().ConvertEndianByInt64InPlace());
967+
}
968+
969+
#endregion
907970
}

0 commit comments

Comments
 (0)