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
+11 -1
View File
@@ -157,12 +157,22 @@ EnumerationBinary _MsgPack_DataFlags
; Not implemented
#MsgPack_DataFlags_IsReadOnly = %0000000000000001
; Not implemented
; FIXME: Not implemented
; Prevents a `MsgPackData` structure from auto-growing its buffer when needed.
; It may make some functions return `#`
#MsgPack_DataFlags_CannotGrow = %0000000000000010
; Not implemented
#MsgPack_DataFlags_NoBufferFree = %0000000000000100
; Indicates a given `MsgPackData` should increment its buffer offset after
; reading a value using the standard procedures.
#MsgPack_DataFlags_MoveOffsetAfterRead = %0000000000001000
; Indicates a given `MsgPackData` should increment its buffer offset after
; writing a value using the standard procedures.
#MsgPack_DataFlags_MoveOffsetAfterWrite = %0000000000010000
; Indicates a given `MsgPackData` is made to be iterated on.
; And it implies it represents a Map or Array.
#MsgPack_DataFlags_Iterator = %0000000000001000
+33 -3
View File
@@ -32,6 +32,16 @@ XIncludeFile "./_Commons.pbi"
;-> Getters
;Procedure.i MsgPackReadNull(*MsgPackData.MsgPackData)
; CompilerIf Not #MsgPack_DisableNullChecks
; If Not *MsgPackData
; DebuggerError("A #Null MsgPackData pointer was passed !")
; ProcedureReturn #Null
; EndIf
; CompilerEndIf
;EndProcedure
; Reads a boolean from the given data.
; Returns: The boolean value `#True` or `#False` as a `.b`
; OnError: Returns `#False` and sets the `MsgPackData\LastError` field.
@@ -56,10 +66,16 @@ Procedure.b MsgPackReadBoolean(*MsgPackData.MsgPackData)
; We don't check if we're 1 byte off the end since the previous check implies it.
If PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset) = #MsgPack_FormatCode_False
*MsgPackData\BufferOffset + 1
If _MsgPackHasFlag(*MsgPackData, #MsgPack_DataFlags_MoveOffsetAfterRead)
*MsgPackData\BufferOffset = *MsgPackData\BufferOffset + 1
EndIf
ProcedureReturn #False
ElseIf PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset) = #MsgPack_FormatCode_True
*MsgPackData\BufferOffset + 1
If _MsgPackHasFlag(*MsgPackData, #MsgPack_DataFlags_MoveOffsetAfterRead)
*MsgPackData\BufferOffset = *MsgPackData\BufferOffset + 1
EndIf
ProcedureReturn #True
Else
DebuggerError("The format code isn't the one for a boolean ! (" +
@@ -100,7 +116,10 @@ Procedure.MsgPack_ErrorCode _MsgPackWriteLiteral(*MsgPackData.MsgPackData, Liter
EndIf
PokeA(*MsgPackData\Buffer + *MsgPackData\BufferOffset, LiteralFormatCode)
*MsgPackData\BufferOffset + 1
If _MsgPackHasFlag(*MsgPackData, #MsgPack_DataFlags_MoveOffsetAfterWrite)
*MsgPackData\BufferOffset = *MsgPackData\BufferOffset + 1
EndIf
ProcedureReturn #MsgPack_Error_Success
EndProcedure
@@ -119,6 +138,17 @@ EndProcedure
;-> Macro aliases
; @Function MsgPackWriteNull(*Ptr.MsgPackData).MsgPack_ErrorCode
; Writes a null field in the given MessagePack at its current offset. \
; The offset may be grown if required, and if ´MsgPackData\Flags´ has ´???´ unset.
; The offset may be incremented if the ´MsgPackData\Flags´ has ´???´ set.
; @Returns
; * `#MsgPack_Error_Success` on success.
; * `#MsgPack_Error_NullPointerGiven` if `MsgPackData` is `#Null`
; @SideEffects
; * ´MsgPackData\LastError´ will contains the returned code, except for the ´#MsgPack_Error_NullPointerGiven´ case.
; @SupportedOS Windows, Linux, MacOS
; @SupportedCPU x86, x64, arm64
Macro MsgPackWriteNull(MsgPackData) : _MsgPackWriteLiteral(MsgPackData, #MsgPack_FormatCode_Null) : EndMacro
Macro MsgPackWriteTrue(MsgPackData) : _MsgPackWriteLiteral(MsgPackData, #MsgPack_FormatCode_True) : EndMacro
+1 -1
View File
@@ -46,7 +46,7 @@ Procedure.MsgPack_FormatCode MsgPackGetFormatCode(*MsgPackData.MsgPackData)
ProcedureReturn #MsgPack_FormatCode_Invalid
EndIf
DataFormatCode = PeekA(*MsgPackData\Buffer)
DataFormatCode = PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset)
If DataFormatCode < $80
; 0x00 - 0x7F
@@ -0,0 +1,8 @@
EnableExplicit
XIncludeFile "../_UnitTesting.pbi"
XIncludeFile "../../Includes/MessagePack.pbi"
; ???
@@ -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)
+8
View File
@@ -34,6 +34,14 @@ Procedure AssertIsTrue(Bool.b=#True, TestName.s="", Details.s="")
EndIf
EndProcedure
Procedure AssertEquals(Val1, Val2, TestName.s="")
If Val1 = Val2
Pass(TestName)
Else
Fail(TestName, Str(Val1) + " <> " + Str(Val2))
EndIf
EndProcedure
Procedure AssertEqualsA(Val1.a, Val2.a, TestName.s="")
If Val1 = Val2
Pass(TestName)