mirror of
https://github.com/aziascreations/PB-MessagePack.git
synced 2026-11-03 17:01:24 +01:00
Added unit tests for binary and strings
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
EnableExplicit
|
||||
|
||||
XIncludeFile "../_UnitTesting.pbi"
|
||||
XIncludeFile "../../Includes/MessagePack.pbi"
|
||||
XIncludeFile "../_TestBuffers.pbi"
|
||||
|
||||
|
||||
; Note: Only covers errors that don't call `DebuggerError()`.
|
||||
; See "TestBinaryFatalErrors.pb" for the others.
|
||||
|
||||
|
||||
; Setup
|
||||
Define *MsgPack.MsgPackData
|
||||
Define *Source = AllocatePattern(16)
|
||||
Define *Output = AllocateMemory(16)
|
||||
|
||||
|
||||
; Sizes past each format code's limit, checked before anything is allocated
|
||||
*MsgPack = MsgPackCreate(8)
|
||||
|
||||
AssertEquals(MsgPackWriteBin8(*MsgPack, *Source, 256), #MsgPack_Error_BinaryDataSizeIsTooBig, "Bin8 rejects 256 bytes")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_BinaryDataSizeIsTooBig, "Bin8 too big error is set")
|
||||
AssertEquals(MsgPackWriteBin16(*MsgPack, *Source, 65536), #MsgPack_Error_BinaryDataSizeIsTooBig, "Bin16 rejects 65536 bytes")
|
||||
AssertEquals(MsgPackWriteBin32(*MsgPack, *Source, 4294967296), #MsgPack_Error_BinaryDataSizeIsTooBig, "Bin32 rejects 2^32 bytes")
|
||||
AssertEquals(MsgPackWriteBinary(*MsgPack, *Source, 4294967296), #MsgPack_Error_BinaryDataSizeIsTooBig, "Binary rejects 2^32 bytes")
|
||||
|
||||
AssertEquals(MsgPackWriteBin8(*MsgPack, *Source, -1), #MsgPack_Error_BinaryDataSizeIsNegative, "Bin8 rejects negative sizes")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_BinaryDataSizeIsNegative, "Negative size error is set")
|
||||
AssertEquals(MsgPackWriteBin16(*MsgPack, *Source, -1), #MsgPack_Error_BinaryDataSizeIsNegative, "Bin16 rejects negative sizes")
|
||||
AssertEquals(MsgPackWriteBin32(*MsgPack, *Source, -1), #MsgPack_Error_BinaryDataSizeIsNegative, "Bin32 rejects negative sizes")
|
||||
AssertEquals(MsgPackWriteBinary(*MsgPack, *Source, -1), #MsgPack_Error_BinaryDataSizeIsNegative, "Binary rejects negative sizes")
|
||||
|
||||
AssertEquals(*MsgPack\BufferOffset, 0, "Offset is untouched by rejected writes")
|
||||
AssertEquals(*MsgPack\BufferSize, 8, "Buffer wasn't grown by rejected writes")
|
||||
|
||||
AssertEquals(MsgPackWriteBin8(*MsgPack, *Source, 4), #MsgPack_Error_Success, "Wrote bin8 after rejected writes")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_Success, "Successful write cleared the error")
|
||||
|
||||
MsgPackFree(*MsgPack, #True)
|
||||
|
||||
|
||||
; Buffers that can't grow
|
||||
*MsgPack = WrapHex("0000000000")
|
||||
|
||||
AssertEquals(MsgPackWriteBin8(*MsgPack, *Source, 4), #MsgPack_Error_CannotGrowBufferDueToConfig, "6 bytes don't fit in 5")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_CannotGrowBufferDueToConfig, "Growth error is set")
|
||||
AssertEquals(*MsgPack\BufferOffset, 0, "Offset is untouched by the failed write")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer), $00, "Nothing was written by the failed write")
|
||||
|
||||
AssertEquals(MsgPackWriteBin8(*MsgPack, *Source, 3), #MsgPack_Error_Success, "5 bytes fit exactly")
|
||||
AssertEquals(*MsgPack\BufferOffset, 5, "Offset is at the end")
|
||||
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
|
||||
; Reading at the end
|
||||
*MsgPack = WrapHex("C40100")
|
||||
*MsgPack\BufferOffset = 3
|
||||
|
||||
AssertEqualsQ(MsgPackReadBinary(*MsgPack, *Output, 16), #MsgPack_InvalidSize, "Read at the end fails")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_AtOrPastTheEndOfBuffer, "Read at the end error is set")
|
||||
AssertEquals(MsgPackCopyBinary(*MsgPack), #Null, "Copy at the end fails")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_AtOrPastTheEndOfBuffer, "Copy at the end error is set")
|
||||
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
|
||||
; Wrong format codes
|
||||
*MsgPack = WrapHex("A3616263")
|
||||
|
||||
AssertEqualsQ(MsgPackReadBinary(*MsgPack, *Output, 16), #MsgPack_InvalidSize, "Reading a str as a bin fails")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_InvalidFormatCode, "Str as bin read error is set")
|
||||
AssertEquals(MsgPackCopyBinary(*MsgPack), #Null, "Copying a str as a bin fails")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_InvalidFormatCode, "Str as bin copy error is set")
|
||||
AssertEquals(*MsgPack\BufferOffset, 0, "Offset is untouched by the wrong format code")
|
||||
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
*MsgPack = WrapHex("C1")
|
||||
AssertEqualsQ(MsgPackReadBinary(*MsgPack, *Output, 16), #MsgPack_InvalidSize, "Reading the 0xC1 never-used code fails")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_InvalidFormatCode, "0xC1 error is set")
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
|
||||
; Truncated size fields
|
||||
*MsgPack = WrapHex("C4")
|
||||
AssertEqualsQ(MsgPackReadBinary(*MsgPack, *Output, 16), #MsgPack_InvalidSize, "Bin8 without its size fails")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_BufferTooSmall, "Bin8 without its size error is set")
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
*MsgPack = WrapHex("C500")
|
||||
AssertEqualsQ(MsgPackReadBinary(*MsgPack, *Output, 16), #MsgPack_InvalidSize, "Bin16 with half its size fails")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_BufferTooSmall, "Bin16 with half its size error is set")
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
*MsgPack = WrapHex("C6000000")
|
||||
AssertEqualsQ(MsgPackReadBinary(*MsgPack, *Output, 16), #MsgPack_InvalidSize, "Bin32 with 3/4 of its size fails")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_BufferTooSmall, "Bin32 with 3/4 of its size error is set")
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
|
||||
; Truncated data
|
||||
*MsgPack = WrapHex("C4056162")
|
||||
AssertEqualsQ(MsgPackReadBinary(*MsgPack, *Output, 16), #MsgPack_InvalidSize, "Bin8 missing data fails")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_BufferTooSmall, "Bin8 missing data error is set")
|
||||
AssertEquals(MsgPackCopyBinary(*MsgPack), #Null, "Copying a bin8 missing data fails")
|
||||
AssertEquals(*MsgPack\BufferOffset, 0, "Offset is untouched by the truncated data")
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
; The bin32 size is unsigned, `0xFFFFFFFF` must not be read as `-1`.
|
||||
*MsgPack = WrapHex("C6FFFFFFFF00")
|
||||
AssertEqualsQ(MsgPackReadBinary(*MsgPack, *Output, 16), #MsgPack_InvalidSize, "Bin32 with a 0xFFFFFFFF size fails")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_BufferTooSmall, "Bin32 size is treated as unsigned")
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
|
||||
; Output buffer too small, then retried
|
||||
*MsgPack = WrapHex("C403010203")
|
||||
|
||||
AssertEqualsQ(MsgPackReadBinary(*MsgPack, *Output, 2), #MsgPack_InvalidSize, "3 bytes don't fit in 2")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_OutputBufferTooSmall, "Output too small error is set")
|
||||
AssertEquals(*MsgPack\BufferOffset, 0, "Offset is untouched so the read can be retried")
|
||||
|
||||
AssertEqualsQ(MsgPackReadBinary(*MsgPack, *Output, 3), 3, "Retried with an exactly sized buffer")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_Success, "Retry cleared the error")
|
||||
AssertEqualsA(PeekA(*Output), $01, "Retried data byte 1")
|
||||
AssertEqualsA(PeekA(*Output + 1), $02, "Retried data byte 2")
|
||||
AssertEqualsA(PeekA(*Output + 2), $03, "Retried data byte 3")
|
||||
AssertEquals(*MsgPack\BufferOffset, 5, "Offset is at the end")
|
||||
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
|
||||
; Ending
|
||||
FreeMemory(*Source)
|
||||
FreeMemory(*Output)
|
||||
|
||||
If FailedUnitTests
|
||||
End 1
|
||||
EndIf
|
||||
|
||||
Debug "Done !"
|
||||
|
||||
End 0
|
||||
@@ -0,0 +1,47 @@
|
||||
EnableExplicit
|
||||
|
||||
XIncludeFile "../_UnitTesting.pbi"
|
||||
XIncludeFile "../../Includes/MessagePack.pbi"
|
||||
XIncludeFile "../_TestBuffers.pbi"
|
||||
|
||||
|
||||
; /!\ Every case here calls `DebuggerError()`, which halts the program under the debugger.
|
||||
; Run it without the debugger and check the exit code.
|
||||
|
||||
|
||||
; Setup
|
||||
Define *MsgPack.MsgPackData
|
||||
Define *Source = AllocatePattern(16)
|
||||
Define *Output = AllocateMemory(16)
|
||||
|
||||
|
||||
; Null MsgPack pointers
|
||||
AssertEquals(MsgPackWriteBin8(#Null, *Source, 1), #MsgPack_Error_NullPointerGiven, "MsgPackWriteBin8 is given #Null")
|
||||
AssertEquals(MsgPackWriteBin16(#Null, *Source, 1), #MsgPack_Error_NullPointerGiven, "MsgPackWriteBin16 is given #Null")
|
||||
AssertEquals(MsgPackWriteBin32(#Null, *Source, 1), #MsgPack_Error_NullPointerGiven, "MsgPackWriteBin32 is given #Null")
|
||||
AssertEquals(MsgPackWriteBinary(#Null, *Source, 1), #MsgPack_Error_NullPointerGiven, "MsgPackWriteBinary is given #Null")
|
||||
AssertEqualsQ(MsgPackReadBinary(#Null, *Output, 16), #MsgPack_InvalidSize, "MsgPackReadBinary is given #Null")
|
||||
AssertEquals(MsgPackCopyBinary(#Null), #Null, "MsgPackCopyBinary is given #Null")
|
||||
|
||||
|
||||
; Null output buffer with data to copy
|
||||
*MsgPack = WrapHex("C4020102")
|
||||
|
||||
AssertEqualsQ(MsgPackReadBinary(*MsgPack, #Null, 2), #MsgPack_InvalidSize, "Reading into a #Null buffer fails")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_NullPointerGiven, "Null output buffer error is set")
|
||||
AssertEquals(*MsgPack\BufferOffset, 0, "Offset is untouched by the failed read")
|
||||
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
|
||||
; Ending
|
||||
FreeMemory(*Source)
|
||||
FreeMemory(*Output)
|
||||
|
||||
If FailedUnitTests
|
||||
End 1
|
||||
EndIf
|
||||
|
||||
Debug "Done !"
|
||||
|
||||
End 0
|
||||
@@ -0,0 +1,149 @@
|
||||
EnableExplicit
|
||||
|
||||
XIncludeFile "../_UnitTesting.pbi"
|
||||
XIncludeFile "../../Includes/MessagePack.pbi"
|
||||
XIncludeFile "../_TestBuffers.pbi"
|
||||
|
||||
|
||||
; Setup
|
||||
; Starting with 1 byte so every write has to grow the buffer.
|
||||
Define *MsgPack.MsgPackData = MsgPackCreate(1)
|
||||
Define *Source = AllocatePattern(65536)
|
||||
Define *Output = AllocateMemory(65536)
|
||||
Define *Copy
|
||||
|
||||
Define OffsetEmpty.i, Offset255.i, Offset256.i, Offset65535.i, Offset65536.i
|
||||
Define OffsetBin16Empty.i, OffsetBin32Small.i, OffsetBin16Small.i
|
||||
Define OffsetEnd.i
|
||||
|
||||
|
||||
; Automatic format code selection, on each boundary
|
||||
OffsetEmpty = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteBinary(*MsgPack, #Null, 0), #MsgPack_Error_Success, "Wrote 0 bytes from a #Null buffer")
|
||||
Offset255 = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteBinary(*MsgPack, *Source, 255), #MsgPack_Error_Success, "Wrote 255 bytes")
|
||||
Offset256 = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteBinary(*MsgPack, *Source, 256), #MsgPack_Error_Success, "Wrote 256 bytes")
|
||||
Offset65535 = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteBinary(*MsgPack, *Source, 65535), #MsgPack_Error_Success, "Wrote 65535 bytes")
|
||||
Offset65536 = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteBinary(*MsgPack, *Source, 65536), #MsgPack_Error_Success, "Wrote 65536 bytes")
|
||||
|
||||
|
||||
; Forced format codes, even when a smaller one fits
|
||||
OffsetBin16Empty = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteBin16(*MsgPack, #Null, 0), #MsgPack_Error_Success, "Wrote 0 bytes as bin16")
|
||||
OffsetBin32Small = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteBin32(*MsgPack, *Source, 5), #MsgPack_Error_Success, "Wrote 5 bytes as bin32")
|
||||
OffsetBin16Small = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteBin16(*MsgPack, *Source, 1), #MsgPack_Error_Success, "Wrote 1 byte as bin16")
|
||||
|
||||
|
||||
; Something right after, to catch offset drifts
|
||||
AssertEquals(MsgPackWriteUInt8(*MsgPack, 42), #MsgPack_Error_Success, "Wrote trailing UInt8")
|
||||
OffsetEnd = *MsgPack\BufferOffset
|
||||
|
||||
|
||||
; Offsets
|
||||
AssertEquals(Offset255, OffsetEmpty + 2, "Empty bin8 takes 2 bytes")
|
||||
AssertEquals(Offset256, Offset255 + 2 + 255, "255 bytes bin8 takes 257 bytes")
|
||||
AssertEquals(Offset65535, Offset256 + 3 + 256, "256 bytes bin16 takes 259 bytes")
|
||||
AssertEquals(Offset65536, Offset65535 + 3 + 65535, "65535 bytes bin16 takes 65538 bytes")
|
||||
AssertEquals(OffsetBin16Empty, Offset65536 + 5 + 65536, "65536 bytes bin32 takes 65541 bytes")
|
||||
AssertEquals(OffsetBin32Small, OffsetBin16Empty + 3, "Empty bin16 takes 3 bytes")
|
||||
AssertEquals(OffsetBin16Small, OffsetBin32Small + 5 + 5, "5 bytes bin32 takes 10 bytes")
|
||||
AssertEquals(OffsetEnd, OffsetBin16Small + 3 + 1 + 2, "1 byte bin16 and UInt8 take 6 bytes")
|
||||
AssertEquals(*MsgPack\BufferSize, OffsetEnd, "Buffer grew to exactly what was written")
|
||||
|
||||
|
||||
; Big-endian layouts
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetEmpty), #MsgPack_FormatCode_Bin8, "Empty uses bin8")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetEmpty + 1), $00, "Empty bin8 size")
|
||||
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset255), #MsgPack_FormatCode_Bin8, "255 bytes uses bin8")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset255 + 1), $FF, "255 bytes bin8 size")
|
||||
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset256), #MsgPack_FormatCode_Bin16, "256 bytes uses bin16")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset256 + 1), $01, "256 bytes bin16 size high byte")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset256 + 2), $00, "256 bytes bin16 size low byte")
|
||||
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset65535), #MsgPack_FormatCode_Bin16, "65535 bytes uses bin16")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset65535 + 1), $FF, "65535 bytes bin16 size high byte")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset65535 + 2), $FF, "65535 bytes bin16 size low byte")
|
||||
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset65536), #MsgPack_FormatCode_Bin32, "65536 bytes uses bin32")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset65536 + 1), $00, "65536 bytes bin32 size byte 1")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset65536 + 2), $01, "65536 bytes bin32 size byte 2")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset65536 + 3), $00, "65536 bytes bin32 size byte 3")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset65536 + 4), $00, "65536 bytes bin32 size byte 4")
|
||||
Assert(CompareMemory(*MsgPack\Buffer + Offset65536 + 5, *Source, 65536), "65536 bytes bin32 data")
|
||||
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetBin16Empty), #MsgPack_FormatCode_Bin16, "Forced empty bin16")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetBin16Empty + 1), $00, "Forced empty bin16 size high byte")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetBin16Empty + 2), $00, "Forced empty bin16 size low byte")
|
||||
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetBin32Small), #MsgPack_FormatCode_Bin32, "Forced 5 bytes bin32")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetBin32Small + 1), $00, "Forced 5 bytes bin32 size byte 1")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetBin32Small + 2), $00, "Forced 5 bytes bin32 size byte 2")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetBin32Small + 3), $00, "Forced 5 bytes bin32 size byte 3")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetBin32Small + 4), $05, "Forced 5 bytes bin32 size byte 4")
|
||||
Assert(CompareMemory(*MsgPack\Buffer + OffsetBin32Small + 5, *Source, 5), "Forced 5 bytes bin32 data")
|
||||
|
||||
|
||||
; Reading them back
|
||||
*MsgPack\BufferOffset = 0
|
||||
|
||||
AssertEqualsQ(MsgPackReadBinary(*MsgPack, #Null, 0), 0, "Read empty bin8 into a #Null buffer")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_Success, "Empty read is a success")
|
||||
|
||||
FillMemory(*Output, 65536, $AA)
|
||||
AssertEqualsQ(MsgPackReadBinary(*MsgPack, *Output, 255), 255, "Read 255 bytes into an exactly sized buffer")
|
||||
Assert(CompareMemory(*Output, *Source, 255), "255 bytes data matches")
|
||||
AssertEqualsA(PeekA(*Output + 255), $AA, "Nothing written past the data")
|
||||
|
||||
*Copy = MsgPackCopyBinary(*MsgPack)
|
||||
Assert(Bool(*Copy <> #Null), "Copied 256 bytes")
|
||||
If *Copy
|
||||
AssertEquals(MemorySize(*Copy), 256, "Copy is exactly 256 bytes")
|
||||
Assert(CompareMemory(*Copy, *Source, 256), "256 bytes data matches")
|
||||
FreeMemory(*Copy)
|
||||
EndIf
|
||||
|
||||
AssertEqualsQ(MsgPackReadBinary(*MsgPack, *Output, 65536), 65535, "Read 65535 bytes into a bigger buffer")
|
||||
Assert(CompareMemory(*Output, *Source, 65535), "65535 bytes data matches")
|
||||
|
||||
*Copy = MsgPackCopyBinary(*MsgPack)
|
||||
Assert(Bool(*Copy <> #Null), "Copied 65536 bytes")
|
||||
If *Copy
|
||||
AssertEquals(MemorySize(*Copy), 65536, "Copy is exactly 65536 bytes")
|
||||
Assert(CompareMemory(*Copy, *Source, 65536), "65536 bytes data matches")
|
||||
FreeMemory(*Copy)
|
||||
EndIf
|
||||
|
||||
AssertEquals(MsgPackCopyBinary(*MsgPack), #Null, "Copying an empty bin16 returns #Null")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_Success, "Empty copy is a success")
|
||||
AssertEquals(*MsgPack\BufferOffset, OffsetBin32Small, "Empty copy still moved the offset")
|
||||
|
||||
AssertEqualsQ(MsgPackReadBinary(*MsgPack, *Output, 65536), 5, "Read forced 5 bytes bin32")
|
||||
Assert(CompareMemory(*Output, *Source, 5), "Forced 5 bytes bin32 data matches")
|
||||
|
||||
AssertEqualsQ(MsgPackReadBinary(*MsgPack, *Output, 65536), 1, "Read forced 1 byte bin16")
|
||||
AssertEqualsA(PeekA(*Output), PeekA(*Source), "Forced 1 byte bin16 data matches")
|
||||
|
||||
AssertEqualsA(MsgPackReadUInt8(*MsgPack), 42, "Read trailing UInt8")
|
||||
AssertEquals(*MsgPack\BufferOffset, OffsetEnd, "Offset is at the end")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_Success, "No error after reading")
|
||||
|
||||
|
||||
; Ending
|
||||
MsgPackFree(*MsgPack, #True)
|
||||
FreeMemory(*Source)
|
||||
FreeMemory(*Output)
|
||||
|
||||
If FailedUnitTests
|
||||
End 1
|
||||
EndIf
|
||||
|
||||
Debug "Done !"
|
||||
|
||||
End 0
|
||||
@@ -0,0 +1,129 @@
|
||||
EnableExplicit
|
||||
|
||||
XIncludeFile "../_UnitTesting.pbi"
|
||||
XIncludeFile "../../Includes/MessagePack.pbi"
|
||||
XIncludeFile "../_TestBuffers.pbi"
|
||||
|
||||
|
||||
; Note: Only covers errors that don't call `DebuggerError()`.
|
||||
; See "TestStringsFatalErrors.pb" for the others.
|
||||
|
||||
|
||||
; Setup
|
||||
Define *MsgPack.MsgPackData
|
||||
|
||||
|
||||
; Strings too big for a forced format code
|
||||
*MsgPack = MsgPackCreate(8)
|
||||
|
||||
AssertEquals(MsgPackWriteFixStr(*MsgPack, LSet("", 32, "a")), #MsgPack_Error_BinaryDataSizeIsTooBig, "Fixstr rejects 32 bytes")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_BinaryDataSizeIsTooBig, "Fixstr too big error is set")
|
||||
AssertEquals(MsgPackWriteFixStr(*MsgPack, LSet("", 16, Chr($E9))), #MsgPack_Error_BinaryDataSizeIsTooBig, "Fixstr rejects 16 accents (32 bytes)")
|
||||
AssertEquals(MsgPackWriteFixStr(*MsgPack, LSet("", 16, "a"), #PB_Unicode), #MsgPack_Error_BinaryDataSizeIsTooBig, "Fixstr rejects 16 UTF-16 characters (32 bytes)")
|
||||
AssertEquals(MsgPackWriteStr8(*MsgPack, LSet("", 256, "a")), #MsgPack_Error_BinaryDataSizeIsTooBig, "Str8 rejects 256 bytes")
|
||||
AssertEquals(MsgPackWriteStr16(*MsgPack, LSet("", 65536, "a")), #MsgPack_Error_BinaryDataSizeIsTooBig, "Str16 rejects 65536 bytes")
|
||||
|
||||
AssertEquals(*MsgPack\BufferOffset, 0, "Offset is untouched by rejected writes")
|
||||
AssertEquals(*MsgPack\BufferSize, 8, "Buffer wasn't grown by rejected writes")
|
||||
|
||||
AssertEquals(MsgPackWriteFixStr(*MsgPack, LSet("", 31, "a")), #MsgPack_Error_Success, "Fixstr accepts 31 bytes")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_Success, "Successful write cleared the error")
|
||||
|
||||
MsgPackFree(*MsgPack, #True)
|
||||
|
||||
|
||||
; Buffers that can't grow
|
||||
*MsgPack = WrapHex("0000")
|
||||
|
||||
AssertEquals(MsgPackWriteString(*MsgPack, "ab"), #MsgPack_Error_CannotGrowBufferDueToConfig, "3 bytes don't fit in 2")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_CannotGrowBufferDueToConfig, "Growth error is set")
|
||||
AssertEquals(*MsgPack\BufferOffset, 0, "Offset is untouched by the failed write")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer), $00, "Nothing was written by the failed write")
|
||||
|
||||
AssertEquals(MsgPackWriteString(*MsgPack, "a"), #MsgPack_Error_Success, "2 bytes fit exactly")
|
||||
AssertEquals(*MsgPack\BufferOffset, 2, "Offset is at the end")
|
||||
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
|
||||
; Reading at the end
|
||||
*MsgPack = WrapHex("A161")
|
||||
*MsgPack\BufferOffset = 2
|
||||
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = ""), "Read at the end returns an empty string")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_AtOrPastTheEndOfBuffer, "Read at the end error is set")
|
||||
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
|
||||
; Wrong format codes
|
||||
*MsgPack = WrapHex("C40161")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = ""), "Reading a bin as a str fails")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_InvalidFormatCode, "Bin as str error is set")
|
||||
AssertEquals(*MsgPack\BufferOffset, 0, "Offset is untouched by the wrong format code")
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
*MsgPack = WrapHex("C1")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = ""), "Reading the 0xC1 never-used code fails")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_InvalidFormatCode, "0xC1 error is set")
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
|
||||
; Truncated size fields
|
||||
*MsgPack = WrapHex("D9")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = ""), "Str8 without its size fails")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_BufferTooSmall, "Str8 without its size error is set")
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
*MsgPack = WrapHex("DA00")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = ""), "Str16 with half its size fails")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_BufferTooSmall, "Str16 with half its size error is set")
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
*MsgPack = WrapHex("DB000000")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = ""), "Str32 with 3/4 of its size fails")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_BufferTooSmall, "Str32 with 3/4 of its size error is set")
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
|
||||
; Truncated data
|
||||
*MsgPack = WrapHex("A36162")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = ""), "Fixstr missing data fails")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_BufferTooSmall, "Fixstr missing data error is set")
|
||||
AssertEquals(*MsgPack\BufferOffset, 0, "Offset is untouched by the truncated data")
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
*MsgPack = WrapHex("D90561")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = ""), "Str8 missing data fails")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_BufferTooSmall, "Str8 missing data error is set")
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
; The str32 size is unsigned, `0xFFFFFFFF` must not be read as `-1`.
|
||||
*MsgPack = WrapHex("DBFFFFFFFF61")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = ""), "Str32 with a 0xFFFFFFFF size fails")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_BufferTooSmall, "Str32 size is treated as unsigned")
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
|
||||
; UTF-16 with an odd byte count, then read as UTF-8 instead
|
||||
*MsgPack = WrapHex("A3414243")
|
||||
|
||||
Assert(Bool(MsgPackReadString(*MsgPack, #PB_Unicode) = ""), "Odd sized UTF-16 fails")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_InvalidStringSize, "Odd sized UTF-16 error is set")
|
||||
AssertEquals(*MsgPack\BufferOffset, 0, "Offset is untouched so the read can be retried")
|
||||
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = "ABC"), "Retried as UTF-8")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_Success, "Retry cleared the error")
|
||||
AssertEquals(*MsgPack\BufferOffset, 4, "Offset is at the end")
|
||||
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
|
||||
; Ending
|
||||
If FailedUnitTests
|
||||
End 1
|
||||
EndIf
|
||||
|
||||
Debug "Done !"
|
||||
|
||||
End 0
|
||||
@@ -0,0 +1,61 @@
|
||||
EnableExplicit
|
||||
|
||||
XIncludeFile "../_UnitTesting.pbi"
|
||||
XIncludeFile "../../Includes/MessagePack.pbi"
|
||||
XIncludeFile "../_TestBuffers.pbi"
|
||||
|
||||
|
||||
; /!\ Every case here calls `DebuggerError()`, which halts the program under the debugger.
|
||||
; Run it without the debugger and check the exit code.
|
||||
|
||||
|
||||
; Setup
|
||||
#UnsupportedEncoding = 999
|
||||
|
||||
Define *MsgPack.MsgPackData
|
||||
|
||||
|
||||
; Null MsgPack pointers
|
||||
AssertEquals(MsgPackWriteString(#Null, "a"), #MsgPack_Error_NullPointerGiven, "MsgPackWriteString is given #Null")
|
||||
AssertEquals(MsgPackWriteFixStr(#Null, "a"), #MsgPack_Error_NullPointerGiven, "MsgPackWriteFixStr is given #Null")
|
||||
AssertEquals(MsgPackWriteStr8(#Null, "a"), #MsgPack_Error_NullPointerGiven, "MsgPackWriteStr8 is given #Null")
|
||||
AssertEquals(MsgPackWriteStr16(#Null, "a"), #MsgPack_Error_NullPointerGiven, "MsgPackWriteStr16 is given #Null")
|
||||
AssertEquals(MsgPackWriteStr32(#Null, "a"), #MsgPack_Error_NullPointerGiven, "MsgPackWriteStr32 is given #Null")
|
||||
Assert(Bool(MsgPackReadString(#Null) = ""), "MsgPackReadString is given #Null")
|
||||
|
||||
|
||||
; Unsupported encodings when writing
|
||||
*MsgPack = MsgPackCreate(8)
|
||||
|
||||
AssertEquals(MsgPackWriteString(*MsgPack, "a", #UnsupportedEncoding), #MsgPack_Error_UnsupportedStringEncoding, "MsgPackWriteString rejects the encoding")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_UnsupportedStringEncoding, "Unsupported encoding error is set")
|
||||
AssertEquals(MsgPackWriteStr8(*MsgPack, "a", #UnsupportedEncoding), #MsgPack_Error_UnsupportedStringEncoding, "MsgPackWriteStr8 rejects the encoding")
|
||||
AssertEquals(MsgPackWriteString(*MsgPack, "", #UnsupportedEncoding), #MsgPack_Error_UnsupportedStringEncoding, "Empty strings are rejected too")
|
||||
AssertEquals(*MsgPack\BufferOffset, 0, "Offset is untouched by rejected writes")
|
||||
|
||||
MsgPackFree(*MsgPack, #True)
|
||||
|
||||
|
||||
; Unsupported encodings when reading
|
||||
*MsgPack = WrapHex("A161")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack, #UnsupportedEncoding) = ""), "MsgPackReadString rejects the encoding")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_UnsupportedStringEncoding, "Unsupported encoding error is set")
|
||||
AssertEquals(*MsgPack\BufferOffset, 0, "Offset is untouched by the rejected read")
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
; Nothing to decode, but the encoding is still checked.
|
||||
*MsgPack = WrapHex("A0")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack, #UnsupportedEncoding) = ""), "Empty string read rejects the encoding")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_UnsupportedStringEncoding, "Empty string encoding error is set")
|
||||
AssertEquals(*MsgPack\BufferOffset, 0, "Offset is untouched by the rejected empty read")
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
|
||||
; Ending
|
||||
If FailedUnitTests
|
||||
End 1
|
||||
EndIf
|
||||
|
||||
Debug "Done !"
|
||||
|
||||
End 0
|
||||
@@ -0,0 +1,211 @@
|
||||
EnableExplicit
|
||||
|
||||
XIncludeFile "../_UnitTesting.pbi"
|
||||
XIncludeFile "../../Includes/MessagePack.pbi"
|
||||
XIncludeFile "../_TestBuffers.pbi"
|
||||
|
||||
|
||||
; Note: Non-ASCII characters are built with `Chr()` to not depend on the source file's encoding.
|
||||
|
||||
|
||||
; Setup
|
||||
; Starting with 1 byte so every write has to grow the buffer.
|
||||
Define *MsgPack.MsgPackData = MsgPackCreate(1)
|
||||
|
||||
Define String31.s = LSet("", 31, "a")
|
||||
Define String32.s = LSet("", 32, "b")
|
||||
Define String255.s = LSet("", 255, "c")
|
||||
Define String256.s = LSet("", 256, "d")
|
||||
Define String65535.s = LSet("", 65535, "e")
|
||||
Define String65536.s = LSet("", 65536, "f")
|
||||
|
||||
; U+00E9 is 2 bytes in UTF-8, so the format code depends on bytes, not characters.
|
||||
Define Accents15.s = LSet("", 15, Chr($E9)) ; 30 bytes
|
||||
Define Accents16.s = LSet("", 16, Chr($E9)) ; 32 bytes
|
||||
|
||||
; U+20AC is 3 bytes in UTF-8.
|
||||
Define Euro.s = "Price: 5" + Chr($20AC) ; 11 bytes
|
||||
|
||||
Define OffsetEmpty.i, Offset31.i, Offset32.i, Offset255.i, Offset256.i, Offset65535.i, Offset65536.i
|
||||
Define OffsetAccents15.i, OffsetAccents16.i, OffsetEuro.i
|
||||
Define OffsetStr32.i, OffsetStr8Empty.i, OffsetFixStr.i, OffsetStr16.i
|
||||
Define OffsetAscii.i, OffsetUnicode.i, OffsetEnd.i
|
||||
|
||||
|
||||
; Automatic format code selection, on each boundary
|
||||
OffsetEmpty = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteString(*MsgPack, ""), #MsgPack_Error_Success, "Wrote empty string")
|
||||
Offset31 = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteString(*MsgPack, String31), #MsgPack_Error_Success, "Wrote 31 bytes")
|
||||
Offset32 = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteString(*MsgPack, String32), #MsgPack_Error_Success, "Wrote 32 bytes")
|
||||
Offset255 = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteString(*MsgPack, String255), #MsgPack_Error_Success, "Wrote 255 bytes")
|
||||
Offset256 = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteString(*MsgPack, String256), #MsgPack_Error_Success, "Wrote 256 bytes")
|
||||
Offset65535 = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteString(*MsgPack, String65535), #MsgPack_Error_Success, "Wrote 65535 bytes")
|
||||
Offset65536 = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteString(*MsgPack, String65536), #MsgPack_Error_Success, "Wrote 65536 bytes")
|
||||
|
||||
|
||||
; Multi-byte UTF-8 characters
|
||||
OffsetAccents15 = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteString(*MsgPack, Accents15), #MsgPack_Error_Success, "Wrote 15 accents")
|
||||
OffsetAccents16 = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteString(*MsgPack, Accents16), #MsgPack_Error_Success, "Wrote 16 accents")
|
||||
OffsetEuro = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteString(*MsgPack, Euro), #MsgPack_Error_Success, "Wrote euro string")
|
||||
|
||||
|
||||
; Forced format codes, even when a smaller one fits
|
||||
OffsetStr32 = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteStr32(*MsgPack, "abc"), #MsgPack_Error_Success, "Wrote 3 bytes as str32")
|
||||
OffsetStr8Empty = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteStr8(*MsgPack, ""), #MsgPack_Error_Success, "Wrote empty string as str8")
|
||||
OffsetFixStr = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteFixStr(*MsgPack, "xyz"), #MsgPack_Error_Success, "Wrote 3 bytes as fixstr")
|
||||
OffsetStr16 = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteStr16(*MsgPack, "q"), #MsgPack_Error_Success, "Wrote 1 byte as str16")
|
||||
|
||||
|
||||
; Other encodings
|
||||
OffsetAscii = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteString(*MsgPack, "Hello", #PB_Ascii), #MsgPack_Error_Success, "Wrote ASCII string")
|
||||
OffsetUnicode = *MsgPack\BufferOffset
|
||||
AssertEquals(MsgPackWriteString(*MsgPack, "Hi" + Chr($20AC), #PB_Unicode), #MsgPack_Error_Success, "Wrote UTF-16 string")
|
||||
|
||||
|
||||
; Something right after, to catch offset drifts
|
||||
AssertEquals(MsgPackWriteUInt8(*MsgPack, 42), #MsgPack_Error_Success, "Wrote trailing UInt8")
|
||||
OffsetEnd = *MsgPack\BufferOffset
|
||||
|
||||
|
||||
; Offsets
|
||||
AssertEquals(Offset31, OffsetEmpty + 1, "Empty fixstr takes 1 byte")
|
||||
AssertEquals(Offset32, Offset31 + 1 + 31, "31 bytes fixstr takes 32 bytes")
|
||||
AssertEquals(Offset255, Offset32 + 2 + 32, "32 bytes str8 takes 34 bytes")
|
||||
AssertEquals(Offset256, Offset255 + 2 + 255, "255 bytes str8 takes 257 bytes")
|
||||
AssertEquals(Offset65535, Offset256 + 3 + 256, "256 bytes str16 takes 259 bytes")
|
||||
AssertEquals(Offset65536, Offset65535 + 3 + 65535, "65535 bytes str16 takes 65538 bytes")
|
||||
AssertEquals(OffsetAccents15, Offset65536 + 5 + 65536, "65536 bytes str32 takes 65541 bytes")
|
||||
AssertEquals(OffsetAccents16, OffsetAccents15 + 1 + 30, "15 accents fixstr takes 31 bytes")
|
||||
AssertEquals(OffsetEuro, OffsetAccents16 + 2 + 32, "16 accents str8 takes 34 bytes")
|
||||
AssertEquals(OffsetStr32, OffsetEuro + 1 + 11, "Euro fixstr takes 12 bytes")
|
||||
AssertEquals(OffsetStr8Empty, OffsetStr32 + 5 + 3, "3 bytes str32 takes 8 bytes")
|
||||
AssertEquals(OffsetFixStr, OffsetStr8Empty + 2, "Empty str8 takes 2 bytes")
|
||||
AssertEquals(OffsetStr16, OffsetFixStr + 1 + 3, "3 bytes fixstr takes 4 bytes")
|
||||
AssertEquals(OffsetAscii, OffsetStr16 + 3 + 1, "1 byte str16 takes 4 bytes")
|
||||
AssertEquals(OffsetUnicode, OffsetAscii + 1 + 5, "5 bytes ASCII fixstr takes 6 bytes")
|
||||
AssertEquals(OffsetEnd, OffsetUnicode + 1 + 6 + 2, "6 bytes UTF-16 fixstr and UInt8 take 9 bytes")
|
||||
AssertEquals(*MsgPack\BufferSize, OffsetEnd, "Buffer grew to exactly what was written")
|
||||
|
||||
|
||||
; Layouts
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetEmpty), $A0, "Empty uses fixstr")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset31), $BF, "31 bytes uses fixstr")
|
||||
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset32), #MsgPack_FormatCode_Str8, "32 bytes uses str8")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset32 + 1), 32, "32 bytes str8 size")
|
||||
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset255), #MsgPack_FormatCode_Str8, "255 bytes uses str8")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset255 + 1), $FF, "255 bytes str8 size")
|
||||
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset256), #MsgPack_FormatCode_Str16, "256 bytes uses str16")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset256 + 1), $01, "256 bytes str16 size high byte")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset256 + 2), $00, "256 bytes str16 size low byte")
|
||||
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset65535), #MsgPack_FormatCode_Str16, "65535 bytes uses str16")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset65535 + 1), $FF, "65535 bytes str16 size high byte")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset65535 + 2), $FF, "65535 bytes str16 size low byte")
|
||||
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset65536), #MsgPack_FormatCode_Str32, "65536 bytes uses str32")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset65536 + 1), $00, "65536 bytes str32 size byte 1")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset65536 + 2), $01, "65536 bytes str32 size byte 2")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset65536 + 3), $00, "65536 bytes str32 size byte 3")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + Offset65536 + 4), $00, "65536 bytes str32 size byte 4")
|
||||
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetAccents15), $BE, "15 accents use a 30 bytes fixstr")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetAccents15 + 1), $C3, "Accent UTF-8 byte 1")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetAccents15 + 2), $A9, "Accent UTF-8 byte 2")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetAccents16), #MsgPack_FormatCode_Str8, "16 accents use str8, not fixstr")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetAccents16 + 1), 32, "16 accents str8 size is in bytes")
|
||||
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetEuro), $AB, "Euro uses a 11 bytes fixstr")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetEuro + 9), $E2, "Euro UTF-8 byte 1")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetEuro + 10), $82, "Euro UTF-8 byte 2")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetEuro + 11), $AC, "Euro UTF-8 byte 3")
|
||||
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetStr32), #MsgPack_FormatCode_Str32, "Forced str32")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetStr32 + 4), $03, "Forced str32 size low byte")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetStr32 + 5), 'a', "Forced str32 data")
|
||||
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetStr8Empty), #MsgPack_FormatCode_Str8, "Forced empty str8")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetStr8Empty + 1), $00, "Forced empty str8 size")
|
||||
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetFixStr), $A3, "Forced fixstr")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetStr16), #MsgPack_FormatCode_Str16, "Forced str16")
|
||||
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetAscii), $A5, "ASCII uses a 5 bytes fixstr")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetAscii + 1), 'H', "ASCII data")
|
||||
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetUnicode), $A6, "UTF-16 uses a 6 bytes fixstr")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetUnicode + 1), 'H', "UTF-16 is little-endian (1)")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetUnicode + 2), $00, "UTF-16 is little-endian (2)")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetUnicode + 5), $AC, "UTF-16 euro low byte")
|
||||
AssertEqualsA(PeekA(*MsgPack\Buffer + OffsetUnicode + 6), $20, "UTF-16 euro high byte")
|
||||
|
||||
|
||||
; Reading them back
|
||||
*MsgPack\BufferOffset = 0
|
||||
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = ""), "Read empty string")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_Success, "Empty read is a success")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = String31), "Read 31 bytes")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = String32), "Read 32 bytes")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = String255), "Read 255 bytes")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = String256), "Read 256 bytes")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = String65535), "Read 65535 bytes")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = String65536), "Read 65536 bytes")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = Accents15), "Read 15 accents")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = Accents16), "Read 16 accents")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = Euro), "Read euro string")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = "abc"), "Read forced str32")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = ""), "Read forced empty str8")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_Success, "Forced empty str8 read is a success")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = "xyz"), "Read forced fixstr")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = "q"), "Read forced str16")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack, #PB_Ascii) = "Hello"), "Read ASCII string")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack, #PB_Unicode) = "Hi" + Chr($20AC)), "Read UTF-16 string")
|
||||
|
||||
AssertEqualsA(MsgPackReadUInt8(*MsgPack), 42, "Read trailing UInt8")
|
||||
AssertEquals(*MsgPack\BufferOffset, OffsetEnd, "Offset is at the end")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_Success, "No error after reading")
|
||||
|
||||
MsgPackFree(*MsgPack, #True)
|
||||
|
||||
|
||||
; Raw strings from other writers
|
||||
|
||||
; A null character cuts the string, but the whole field is still skipped.
|
||||
*MsgPack = WrapHex("A46100626301")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = "a"), "Null character cuts the string")
|
||||
AssertEquals(MsgPackLastError(*MsgPack), #MsgPack_Error_Success, "Null character isn't an error")
|
||||
AssertEquals(*MsgPack\BufferOffset, 5, "Whole field was skipped")
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
; A string ending exactly at the end of the buffer.
|
||||
*MsgPack = WrapHex("D903616263")
|
||||
Assert(Bool(MsgPackReadString(*MsgPack) = "abc"), "Read a str8 ending with the buffer")
|
||||
AssertEquals(*MsgPack\BufferOffset, 5, "Offset is at the end")
|
||||
FreeWrappedHex(*MsgPack)
|
||||
|
||||
|
||||
; Ending
|
||||
If FailedUnitTests
|
||||
End 1
|
||||
EndIf
|
||||
|
||||
Debug "Done !"
|
||||
|
||||
End 0
|
||||
@@ -0,0 +1,38 @@
|
||||
; Test-only helpers for building raw MessagePack buffers.
|
||||
; Must be included after "MessagePack.pbi".
|
||||
|
||||
|
||||
; Wraps a new buffer filled from a hex string like "C50001" into a non-growable `MsgPackData`.
|
||||
; Free it with `FreeWrappedHex()`.
|
||||
Procedure.i WrapHex(Hex.s)
|
||||
Protected Size.i = Len(Hex) / 2
|
||||
Protected *Buffer = AllocateMemory(Size)
|
||||
Protected i.i
|
||||
|
||||
For i = 0 To Size - 1
|
||||
PokeA(*Buffer + i, Val("$" + Mid(Hex, (i * 2) + 1, 2)))
|
||||
Next
|
||||
|
||||
ProcedureReturn MsgPackWrapMsgBuffer(*Buffer, Size)
|
||||
EndProcedure
|
||||
|
||||
|
||||
Procedure FreeWrappedHex(*MsgPack.MsgPackData)
|
||||
FreeMemory(*MsgPack\Buffer)
|
||||
MsgPackFree(*MsgPack, #False)
|
||||
EndProcedure
|
||||
|
||||
|
||||
; Returns a new buffer of `Size` bytes holding `(Index * 7 + 3 + Index / 256) & $FF`.
|
||||
; The pattern shifts every 256 bytes so a misplaced offset can't hide behind a repeat,
|
||||
; and it contains both `$00` and `$FF` bytes.
|
||||
Procedure.i AllocatePattern(Size.i)
|
||||
Protected *Buffer = AllocateMemory(Size)
|
||||
Protected i.i
|
||||
|
||||
For i = 0 To Size - 1
|
||||
PokeA(*Buffer + i, ((i * 7) + 3 + (i / 256)) & $FF)
|
||||
Next
|
||||
|
||||
ProcedureReturn *Buffer
|
||||
EndProcedure
|
||||
Reference in New Issue
Block a user