-
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.
Add support for trivial natural encoding (#44)
- Loading branch information
Showing
11 changed files
with
221 additions
and
38 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,8 +1,14 @@ | ||
package codec | ||
|
||
type Codec interface { | ||
type Uint interface { | ||
uint8 | uint16 | uint32 | uint64 | ||
} | ||
|
||
type Codec[T Uint] interface { | ||
Marshal(v interface{}) ([]byte, error) | ||
MarshalGeneral(x uint64) ([]byte, error) | ||
MarshalTrivialUint(x T, l uint8) ([]byte, error) | ||
Unmarshal(data []byte, v interface{}) error | ||
UnmarshalGeneral(data []byte, v *uint64) error | ||
UnmarshalTrivialUint(data []byte, v *T) error | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package jam | ||
|
||
import ( | ||
"math" | ||
) | ||
|
||
func SerializeTrivialNatural[T ~uint8 | ~uint16 | ~uint32 | ~uint64](x T, l uint8) []byte { | ||
bytes := make([]byte, l) | ||
for i := uint8(0); i < l; i++ { | ||
bytes[i] = byte((x >> (8 * i)) & T(math.MaxUint8)) | ||
} | ||
return bytes | ||
} | ||
|
||
func DeserializeTrivialNatural[T ~uint8 | ~uint16 | ~uint32 | ~uint64](serialized []byte, u *T) { | ||
*u = 0 | ||
|
||
// Iterate over each byte in the serialized array | ||
for i := 0; i < len(serialized); i++ { | ||
*u |= T(serialized[i]) << (8 * i) | ||
} | ||
} |
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,83 @@ | ||
package jam | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSerializationTrivialNatural(t *testing.T) { | ||
testCases := []struct { | ||
x any | ||
l uint8 | ||
expected []byte | ||
}{ | ||
{uint8(0), 0, []byte{}}, | ||
{uint8(0), 1, []byte{0}}, | ||
{uint8(1), 3, []byte{1, 0, 0}}, | ||
{uint8(math.MaxInt8), 4, []byte{127, 0, 0, 0}}, | ||
{uint8(math.MaxUint8), 2, []byte{255, 0}}, | ||
{uint16(0), 0, []byte{}}, | ||
{uint16(0), 1, []byte{0}}, | ||
{uint16(math.MaxUint16), 2, []byte{255, 255}}, | ||
{uint32(0), 0, []byte{}}, | ||
{uint32(0), 1, []byte{0}}, | ||
{uint32(1), 3, []byte{1, 0, 0}}, | ||
{uint32(math.MaxInt8), 4, []byte{127, 0, 0, 0}}, | ||
{uint32(128), 1, []byte{128}}, | ||
{uint32(math.MaxUint8), 3, []byte{255, 0, 0}}, | ||
{uint32(256), 2, []byte{0, 1}}, | ||
{uint32(1023), 3, []byte{255, 3, 0}}, | ||
{uint32(1024), 2, []byte{0, 4}}, | ||
{uint32(16383), 4, []byte{255, 63, 0, 0}}, | ||
{uint32(math.MaxUint16), 3, []byte{255, 255, 0}}, | ||
{uint32(math.MaxUint32), 4, []byte{255, 255, 255, 255}}, | ||
{uint64(0), 0, []byte{}}, | ||
{uint64(0), 4, []byte{0, 0, 0, 0}}, | ||
{uint64(1), 3, []byte{1, 0, 0}}, | ||
{uint64(math.MaxUint16), 3, []byte{255, 255, 0}}, | ||
{uint64(math.MaxUint32), 6, []byte{255, 255, 255, 255, 0, 0}}, | ||
{uint64(math.MaxUint64), 8, []byte{255, 255, 255, 255, 255, 255, 255, 255}}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
testName := fmt.Sprintf("%s_%v", reflect.TypeOf(tc.x).Name(), tc.x) | ||
t.Run(testName, func(t *testing.T) { | ||
var serialized []byte | ||
switch v := tc.x.(type) { | ||
case uint8: | ||
serialized = SerializeTrivialNatural(v, tc.l) | ||
case uint16: | ||
serialized = SerializeTrivialNatural(v, tc.l) | ||
case uint32: | ||
serialized = SerializeTrivialNatural(v, tc.l) | ||
case uint64: | ||
serialized = SerializeTrivialNatural(v, tc.l) | ||
} | ||
|
||
assert.Equal(t, tc.expected, serialized, "serialized output mismatch") | ||
|
||
switch v := tc.x.(type) { | ||
case uint8: | ||
var deserialized uint8 | ||
DeserializeTrivialNatural(serialized, &deserialized) | ||
assert.Equal(t, v, deserialized, "deserialized value mismatch") | ||
case uint16: | ||
var deserialized uint16 | ||
DeserializeTrivialNatural(serialized, &deserialized) | ||
assert.Equal(t, v, deserialized, "deserialized value mismatch") | ||
case uint32: | ||
var deserialized uint32 | ||
DeserializeTrivialNatural(serialized, &deserialized) | ||
assert.Equal(t, v, deserialized, "deserialized value mismatch") | ||
case uint64: | ||
var deserialized uint64 | ||
DeserializeTrivialNatural(serialized, &deserialized) | ||
assert.Equal(t, v, deserialized, "deserialized value mismatch") | ||
} | ||
}) | ||
} | ||
} |
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.