Added unit tests, Improved literals stuff, Added flags

This commit is contained in:
2026-10-19 23:36:20 +02:00
parent 4d0e3a0e78
commit 8ff0cddecb
9 changed files with 258 additions and 6 deletions
@@ -0,0 +1,62 @@
EnableExplicit
XIncludeFile "../_UnitTesting.pbi"
XIncludeFile "../../Includes/MessagePack.pbi"
; Note: The checks were done on the table in the spec.
; I didn't go with my own constants to avoid missing a discrepency.
Define *MyBuffer
Define *MsgPack.MsgPackData
; Null MsgPack pointers
; FIXME: It returns #False, should this be normalized ?
;AssertEquals(MsgPackReadBoolean(#Null), #MsgPack_Error_NullPointerGiven, "MsgPackReadBoolean is given #Null")ssertEquals(MsgPackReadBoolean(#Null), #MsgPack_Error_NullPointerGiven, "MsgPackReadBoolean is given #Null")
AssertEquals(MsgPackWriteNull(#Null), #MsgPack_Error_NullPointerGiven, "MsgPackWriteNull is given #Null")
AssertEquals(MsgPackWriteFalse(#Null), #MsgPack_Error_NullPointerGiven, "MsgPackWriteFalse is given #Null")
AssertEquals(MsgPackWriteTrue(#Null), #MsgPack_Error_NullPointerGiven, "MsgPackWriteTrue is given #Null")
; Buffer at end
*MyBuffer = AllocateMemory(8)
FillMemory(*MyBuffer, MemorySize(*MyBuffer), #MsgPack_FormatCode_True, #PB_Ascii) ; Filling with `#True`
*MsgPack = MsgPackWrapMsgBuffer(*MyBuffer, MemorySize(*MyBuffer))
*MsgPack\BufferOffset = MemorySize(*MsgPack\Buffer)
AssertIsFalse(MsgPackReadBoolean(*MsgPack), "Reading at the end returns #False")
; FIXME: Implement the non-growth flag.
;AssertEquals(MsgPackWriteNull(*MsgPack), #MsgPack_Error_NullPointerGiven, "MsgPackWriteNull is given #Null")
;AssertEquals(MsgPackWriteFalse(*MsgPack), #MsgPack_Error_NullPointerGiven, "MsgPackWriteNull is given #Null")
;AssertEquals(MsgPackWriteTrue(*MsgPack), #MsgPack_Error_NullPointerGiven, "MsgPackWriteNull is given #Null")
MsgPackFree(*MsgPack, #False)
FreeMemory(*MyBuffer)
; Offset out-of-bounds
;; Setup
;*MyBuffer = AllocateMemory(8)
;FillMemory(*MyBuffer, MemorySize(*MyBuffer), $C1, #PB_Ascii)
;*MsgPack = MsgPackWrapMsgBuffer(*MyBuffer, MemorySize(*MyBuffer))
;
;
;; Writing in place
;*MsgPack\DataFlags = *MsgPack\DataFlags & (~#MsgPack_DataFlags_MoveOffsetAfterWrite)
;AssertIsFalse(Bool(_MsgPackHasFlag(*MsgPack, #MsgPack_DataFlags_MoveOffsetAfterWrite)), "Move after flag is unset")
;
;AssertEquals(MsgPackWriteNull(*MsgPack), #MsgPack_Error_Success, "Wrote null using helper")
;AssertEqualsA(PeekA(*MsgPack\Buffer), #MsgPack_FormatCode_Null, "Format code is changed and correct")
;AssertEqualsA(*MsgPack\BufferOffset, 0, "Offset was left at 0")
;
;
;; Ending
;MsgPackFree(*MsgPack, #False)
;FreeMemory(*MyBuffer)
@@ -0,0 +1,78 @@
EnableExplicit
XIncludeFile "../_UnitTesting.pbi"
XIncludeFile "../../Includes/MessagePack.pbi"
; Note: The checks were done on the table in the spec.
; I didn't go with my own constants to avoid missing a discrepency.
Define *MyBuffer
Define *MsgPack.MsgPackData
; Null
*MyBuffer = AllocateMemory(2)
PokeA(*MyBuffer + 0, $C0) ; Null
PokeA(*MyBuffer + 1, $C1) ; Invalid
*MsgPack = MsgPackWrapMsgBuffer(*MyBuffer, MemorySize(*MyBuffer))
AssertEqualsA(MsgPackGetFormatCode(*MsgPack), #MsgPack_FormatCode_Null, "[Null] First format code is null")
; FIXME: Implement a way to move forward for those !!!
;AssertEqualsA(MsgPackGetFormatCode(*MsgPack), , "Second is invalid, we moved to the 2nd byte")
MsgPackFree(*MsgPack, #False)
FreeMemory(*MyBuffer)
; False
*MyBuffer = AllocateMemory(2)
PokeA(*MyBuffer + 0, $C2) ; False
PokeA(*MyBuffer + 1, $C1) ; Invalid
*MsgPack = MsgPackWrapMsgBuffer(*MyBuffer, MemorySize(*MyBuffer))
AssertEqualsA(*MsgPack\BufferOffset, 0, "[False] Offset is 0")
AssertEqualsA(MsgPackGetFormatCode(*MsgPack), #MsgPack_FormatCode_False, "[False] First format code is false")
AssertEqualsA(MsgPackReadBoolean(*MsgPack), #False, "[False] Boolean literal getter returns #False")
AssertEqualsA(*MsgPack\BufferOffset, 0, "[False] Offset is still 0, we'll change the flag")
*MsgPack\DataFlags = *MsgPack\DataFlags | #MsgPack_DataFlags_MoveOffsetAfterRead
AssertEqualsA(MsgPackReadBoolean(*MsgPack), #False, "[False] Boolean literal getter returns #False")
AssertEqualsA(*MsgPack\BufferOffset, 1, "[False] Offset is 1 after setting flag and reading")
AssertEqualsA(MsgPackGetFormatCode(*MsgPack), #MsgPack_FormatCode_Invalid, "[False] Second format code is invalid as expected")
MsgPackFree(*MsgPack, #False)
FreeMemory(*MyBuffer)
; True
*MyBuffer = AllocateMemory(2)
PokeA(*MyBuffer + 0, $C3) ; True
PokeA(*MyBuffer + 1, $C1) ; Invalid
*MsgPack = MsgPackWrapMsgBuffer(*MyBuffer, MemorySize(*MyBuffer))
AssertEqualsA(*MsgPack\BufferOffset, 0, "[True] Offset is 0")
AssertEqualsA(MsgPackGetFormatCode(*MsgPack), #MsgPack_FormatCode_True, "[True] First format code is false")
AssertEqualsA(MsgPackReadBoolean(*MsgPack), #True, "[True] Boolean literal getter returns #True")
AssertEqualsA(*MsgPack\BufferOffset, 0, "[True] Offset is still 0, we'll change the flag")
*MsgPack\DataFlags = *MsgPack\DataFlags | #MsgPack_DataFlags_MoveOffsetAfterRead
AssertEqualsA(MsgPackReadBoolean(*MsgPack), #True, "[True] Boolean literal getter returns #True")
AssertEqualsA(*MsgPack\BufferOffset, 1, "[True] Offset is 1 after setting flag and reading")
AssertEqualsA(MsgPackGetFormatCode(*MsgPack), #MsgPack_FormatCode_Invalid, "[True] Second format code is invalid as expected")
MsgPackFree(*MsgPack, #False)
FreeMemory(*MyBuffer)
@@ -0,0 +1,57 @@
EnableExplicit
XIncludeFile "../_UnitTesting.pbi"
XIncludeFile "../../Includes/MessagePack.pbi"
; Note: The checks were done on the table in the spec.
; I didn't go with my own constants to avoid missing a discrepency.
Define *MyBuffer
Define *MsgPack.MsgPackData
; Setup
*MyBuffer = AllocateMemory(8)
FillMemory(*MyBuffer, MemorySize(*MyBuffer), $C1, #PB_Ascii)
*MsgPack = MsgPackWrapMsgBuffer(*MyBuffer, MemorySize(*MyBuffer))
; Writing in place
*MsgPack\DataFlags = *MsgPack\DataFlags & (~#MsgPack_DataFlags_MoveOffsetAfterWrite)
AssertIsFalse(Bool(_MsgPackHasFlag(*MsgPack, #MsgPack_DataFlags_MoveOffsetAfterWrite)), "Move after flag is unset")
AssertEquals(MsgPackWriteNull(*MsgPack), #MsgPack_Error_Success, "Wrote null using helper")
AssertEqualsA(PeekA(*MsgPack\Buffer), #MsgPack_FormatCode_Null, "Format code is changed and correct")
AssertEqualsA(*MsgPack\BufferOffset, 0, "Offset was left at 0")
AssertEquals(MsgPackWriteFalse(*MsgPack), #MsgPack_Error_Success, "Wrote false using helper")
AssertEqualsA(PeekA(*MsgPack\Buffer), #MsgPack_FormatCode_False, "Format code is changed and correct")
AssertEqualsA(*MsgPack\BufferOffset, 0, "Offset was left at 0")
AssertEquals(MsgPackWriteTrue(*MsgPack), #MsgPack_Error_Success, "Wrote true using helper")
AssertEqualsA(PeekA(*MsgPack\Buffer), #MsgPack_FormatCode_True, "Format code is changed and correct")
AssertEqualsA(*MsgPack\BufferOffset, 0, "Offset was left at 0")
; Writing sequentially
*MsgPack\DataFlags = *MsgPack\DataFlags | #MsgPack_DataFlags_MoveOffsetAfterWrite
AssertIsTrue(Bool(_MsgPackHasFlag(*MsgPack, #MsgPack_DataFlags_MoveOffsetAfterWrite)), "Move after flag is set")
AssertEquals(MsgPackWriteNull(*MsgPack), #MsgPack_Error_Success, "Wrote null using helper")
AssertEqualsA(PeekA(*MsgPack\Buffer + 0), #MsgPack_FormatCode_Null, "Format code is changed and correct")
AssertEqualsA(*MsgPack\BufferOffset, 1, "Offset is now 1")
AssertEquals(MsgPackWriteFalse(*MsgPack), #MsgPack_Error_Success, "Wrote false using helper")
AssertEqualsA(PeekA(*MsgPack\Buffer + 1), #MsgPack_FormatCode_False, "Format code is changed and correct")
AssertEqualsA(*MsgPack\BufferOffset, 2, "Offset is now 2")
AssertEquals(MsgPackWriteTrue(*MsgPack), #MsgPack_Error_Success, "Wrote true using helper")
AssertEqualsA(PeekA(*MsgPack\Buffer + 2), #MsgPack_FormatCode_True, "Format code is changed and correct")
AssertEqualsA(*MsgPack\BufferOffset, 3, "Offset is now 3")
; Ending
MsgPackFree(*MsgPack, #False)
FreeMemory(*MyBuffer)