message-size-increase #3
@ -9,7 +9,7 @@ import "iter"
|
||||
func DecodePairs(data []byte) (iter.Seq2[uint16, []byte], error) {
|
||||
// determine section bounds
|
||||
if len(data) < 2 { return nil, ErrDataTooLarge }
|
||||
length16, _ := DecodeI16[uint16](data[0:2])
|
||||
length16, _, _ := DecodeI16[uint16](data[0:2])
|
||||
data = data[2:]
|
||||
length := int(length16)
|
||||
headerSize := length * 4
|
||||
@ -20,7 +20,7 @@ func DecodePairs(data []byte) (iter.Seq2[uint16, []byte], error) {
|
||||
var valuesSize int
|
||||
for index := range length {
|
||||
offset := index * 4
|
||||
end, _ := DecodeI16[uint16](data[offset + 2:offset + 4])
|
||||
end, _, _ := DecodeI16[uint16](data[offset + 2:offset + 4])
|
||||
valuesSize = int(end)
|
||||
}
|
||||
if valuesSize > len(valuesData) {
|
||||
@ -32,8 +32,8 @@ func DecodePairs(data []byte) (iter.Seq2[uint16, []byte], error) {
|
||||
start := uint16(0)
|
||||
for index := range length {
|
||||
offset := index * 4
|
||||
key , _ := DecodeI16[uint16](data[offset + 0:offset + 2])
|
||||
end, _ := DecodeI16[uint16](data[offset + 2:offset + 4])
|
||||
key, _, _ := DecodeI16[uint16](data[offset + 0:offset + 2])
|
||||
end, _, _ := DecodeI16[uint16](data[offset + 2:offset + 4])
|
||||
// if nextValuesOffset < len(valuesData) {
|
||||
if !yield(key, valuesData[start:end]) {
|
||||
return
|
||||
|
146
tape/types.go
146
tape/types.go
@ -34,54 +34,54 @@ type UInt interface { ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 }
|
||||
type String interface { ~string }
|
||||
|
||||
// DecodeI8 decodes an 8 bit integer from the given data.
|
||||
func DecodeI8[T Int8](data []byte) (T, error) {
|
||||
if len(data) != 1 { return 0, fmt.Errorf("decoding int8: %w", ErrWrongBufferLength) }
|
||||
return T(data[0]), nil
|
||||
func DecodeI8[T Int8](data []byte) (value T, n int, err error) {
|
||||
if len(data) != 1 { return 0, 0, fmt.Errorf("decoding int8: %w", ErrWrongBufferLength) }
|
||||
return T(data[0]), 1, nil
|
||||
}
|
||||
|
||||
// EncodeI8 encodes an 8 bit integer into the given buffer.
|
||||
func EncodeI8[T Int8](buffer []byte, value T) error {
|
||||
if len(buffer) != 1 { return fmt.Errorf("encoding int8: %w", ErrWrongBufferLength) }
|
||||
func EncodeI8[T Int8](buffer []byte, value T) (n int, err error) {
|
||||
if len(buffer) != 1 { return 0, fmt.Errorf("encoding int8: %w", ErrWrongBufferLength) }
|
||||
buffer[0] = byte(value)
|
||||
return nil
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
// DecodeI16 decodes a 16 bit integer from the given data.
|
||||
func DecodeI16[T Int16](data []byte) (T, error) {
|
||||
if len(data) != 2 { return 0, fmt.Errorf("decoding int16: %w", ErrWrongBufferLength) }
|
||||
return T(data[0]) << 8 | T(data[1]), nil
|
||||
func DecodeI16[T Int16](data []byte) (value T, n int, err error) {
|
||||
if len(data) != 2 { return 0, 0, fmt.Errorf("decoding int16: %w", ErrWrongBufferLength) }
|
||||
return T(data[0]) << 8 | T(data[1]), 2, nil
|
||||
}
|
||||
|
||||
// EncodeI16 encodes a 16 bit integer into the given buffer.
|
||||
func EncodeI16[T Int16](buffer []byte, value T) error {
|
||||
if len(buffer) != 2 { return fmt.Errorf("encoding int16: %w", ErrWrongBufferLength) }
|
||||
func EncodeI16[T Int16](buffer []byte, value T) (n int, err error) {
|
||||
if len(buffer) != 2 { return 0, fmt.Errorf("encoding int16: %w", ErrWrongBufferLength) }
|
||||
buffer[0] = byte(value >> 8)
|
||||
buffer[1] = byte(value)
|
||||
return nil
|
||||
return 2, nil
|
||||
}
|
||||
|
||||
// DecodeI32 decodes a 32 bit integer from the given data.
|
||||
func DecodeI32[T Int32](data []byte) (T, error) {
|
||||
if len(data) != 4 { return 0, fmt.Errorf("decoding int32: %w", ErrWrongBufferLength) }
|
||||
func DecodeI32[T Int32](data []byte) (value T, n int, err error) {
|
||||
if len(data) != 4 { return 0, 0, fmt.Errorf("decoding int32: %w", ErrWrongBufferLength) }
|
||||
return T(data[0]) << 24 |
|
||||
T(data[1]) << 16 |
|
||||
T(data[2]) << 8 |
|
||||
T(data[3]), nil
|
||||
T(data[3]), 4, nil
|
||||
}
|
||||
|
||||
// EncodeI32 encodes a 32 bit integer into the given buffer.
|
||||
func EncodeI32[T Int32](buffer []byte, value T) error {
|
||||
if len(buffer) != 4 { return fmt.Errorf("encoding int32: %w", ErrWrongBufferLength) }
|
||||
func EncodeI32[T Int32](buffer []byte, value T) (n int, err error) {
|
||||
if len(buffer) != 4 { return 0, fmt.Errorf("encoding int32: %w", ErrWrongBufferLength) }
|
||||
buffer[0] = byte(value >> 24)
|
||||
buffer[1] = byte(value >> 16)
|
||||
buffer[2] = byte(value >> 8)
|
||||
buffer[3] = byte(value)
|
||||
return nil
|
||||
return 4, nil
|
||||
}
|
||||
|
||||
// DecodeI64 decodes a 64 bit integer from the given data.
|
||||
func DecodeI64[T Int64](data []byte) (T, error) {
|
||||
if len(data) != 8 { return 0, fmt.Errorf("decoding int64: %w", ErrWrongBufferLength) }
|
||||
func DecodeI64[T Int64](data []byte) (value T, n int, err error) {
|
||||
if len(data) != 8 { return 0, 0, fmt.Errorf("decoding int64: %w", ErrWrongBufferLength) }
|
||||
return T(data[0]) << 56 |
|
||||
T(data[1]) << 48 |
|
||||
T(data[2]) << 40 |
|
||||
@ -89,12 +89,12 @@ func DecodeI64[T Int64](data []byte) (T, error) {
|
||||
T(data[4]) << 24 |
|
||||
T(data[5]) << 16 |
|
||||
T(data[6]) << 8 |
|
||||
T(data[7]), nil
|
||||
T(data[7]), 8, nil
|
||||
}
|
||||
|
||||
// EncodeI64 encodes a 64 bit integer into the given buffer.
|
||||
func EncodeI64[T Int64](buffer []byte, value T) error {
|
||||
if len(buffer) != 8 { return fmt.Errorf("encoding int64: %w", ErrWrongBufferLength) }
|
||||
func EncodeI64[T Int64](buffer []byte, value T) (n int, err error) {
|
||||
if len(buffer) != 8 { return 0, fmt.Errorf("encoding int64: %w", ErrWrongBufferLength) }
|
||||
buffer[0] = byte(value >> 56)
|
||||
buffer[1] = byte(value >> 48)
|
||||
buffer[2] = byte(value >> 40)
|
||||
@ -103,21 +103,22 @@ func EncodeI64[T Int64](buffer []byte, value T) error {
|
||||
buffer[5] = byte(value >> 16)
|
||||
buffer[6] = byte(value >> 8)
|
||||
buffer[7] = byte(value)
|
||||
return nil
|
||||
return 8, nil
|
||||
}
|
||||
|
||||
// DecodeGBEU decodes an 8 to 64 bit growing integer into the given buffer.
|
||||
func DecodeGBEU[T UInt](data []byte) (T, error) {
|
||||
var value uint64
|
||||
func DecodeGBEU[T UInt](data []byte) (value T, n int, err error) {
|
||||
var fullValue uint64
|
||||
for _, chunk := range data {
|
||||
value *= 0x80
|
||||
value += uint64(chunk & 0x7F)
|
||||
fullValue *= 0x80
|
||||
fullValue += uint64(chunk & 0x7F)
|
||||
ccb := chunk >> 7
|
||||
if ccb == 0 {
|
||||
return T(value), nil
|
||||
return T(fullValue), n, nil
|
||||
}
|
||||
n += 1
|
||||
}
|
||||
return 0, fmt.Errorf("decoding GBEU: %w", ErrGBEUNotTerminated)
|
||||
return 0, n, fmt.Errorf("decoding GBEU: %w", ErrGBEUNotTerminated)
|
||||
}
|
||||
|
||||
// EncodeGBEU encodes an 8 to 64 bit growing integer into a given buffer.
|
||||
@ -151,15 +152,14 @@ func GBEUSize[T UInt] (value T) int {
|
||||
}
|
||||
|
||||
// DecodeString decodes a string from the given data.
|
||||
func DecodeString[T String](data []byte) (T, error) {
|
||||
return T(data), nil
|
||||
func DecodeString[T String](data []byte) (value T, n int, err error) {
|
||||
return T(data), len(data), nil
|
||||
}
|
||||
|
||||
// EncodeString encodes a string into the given buffer.
|
||||
func EncodeString[T String](data []byte, value T) error {
|
||||
if len(data) != len(value) { return fmt.Errorf("encoding string: %w", ErrWrongBufferLength) }
|
||||
copy(data, value)
|
||||
return nil
|
||||
func EncodeString[T String](data []byte, value T) (n int, err error) {
|
||||
if len(data) != len(value) { return 0, fmt.Errorf("encoding string: %w", ErrWrongBufferLength) }
|
||||
return copy(data, value), nil
|
||||
}
|
||||
|
||||
// StringSize returns the size of a string. Returns 0 and an error if the size
|
||||
@ -170,33 +170,35 @@ func StringSize[T String](value T) (int, error) {
|
||||
}
|
||||
|
||||
// DecodeStringArray decodes a packed string array from the given data.
|
||||
func DecodeStringArray[T String](data []byte) ([]T, error) {
|
||||
result := []T { }
|
||||
func DecodeStringArray[T String](data []byte) (result []T, n int, err error) {
|
||||
for len(data) > 0 {
|
||||
if len(data) < 2 { return nil, fmt.Errorf("decoding []string: %w", ErrWrongBufferLength) }
|
||||
itemSize16, _ := DecodeI16[uint16](data[:2])
|
||||
if len(data) < 2 { return nil, n, fmt.Errorf("decoding []string: %w", ErrWrongBufferLength) }
|
||||
itemSize16, nn, _ := DecodeI16[uint16](data[:2])
|
||||
itemSize := int(itemSize16)
|
||||
data = data[2:]
|
||||
if len(data) < itemSize { return nil, fmt.Errorf("decoding []string: %w", ErrWrongBufferLength) }
|
||||
n += nn
|
||||
data = data[nn:]
|
||||
if len(data) < itemSize { return nil, n, fmt.Errorf("decoding []string: %w", ErrWrongBufferLength) }
|
||||
result = append(result, T(data[:itemSize]))
|
||||
data = data[itemSize:]
|
||||
n += itemSize
|
||||
}
|
||||
return result, nil
|
||||
return result, n, nil
|
||||
}
|
||||
|
||||
// EncodeStringArray encodes a packed string array into the given buffer.
|
||||
func EncodeStringArray[T String](buffer []byte, value []T) error {
|
||||
func EncodeStringArray[T String](buffer []byte, value []T) (n int, err error) {
|
||||
for _, item := range value {
|
||||
length, err := StringSize(item)
|
||||
if err != nil { return err }
|
||||
if len(buffer) < 2 + length { return fmt.Errorf("encoding []string: %w", ErrWrongBufferLength) }
|
||||
if err != nil { return n, err }
|
||||
if len(buffer) < 2 + length { return n, fmt.Errorf("encoding []string: %w", ErrWrongBufferLength) }
|
||||
EncodeI16(buffer[:2], uint16(length))
|
||||
buffer = buffer[2:]
|
||||
copy(buffer, item)
|
||||
buffer = buffer[length:]
|
||||
n += 2 + length
|
||||
}
|
||||
if len(buffer) > 0 { return fmt.Errorf("encoding []string: %w", ErrWrongBufferLength) }
|
||||
return nil
|
||||
if len(buffer) > 0 { return n, fmt.Errorf("encoding []string: %w", ErrWrongBufferLength) }
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// StringArraySize returns the size of a packed string array. Returns 0 and an
|
||||
@ -211,21 +213,21 @@ func StringArraySize[T String](value []T) (int, error) {
|
||||
}
|
||||
|
||||
// DecodeI8Array decodes a packed array of 8 bit integers from the given data.
|
||||
func DecodeI8Array[T Int8](data []byte) ([]T, error) {
|
||||
result := make([]T, len(data))
|
||||
func DecodeI8Array[T Int8](data []byte) (result []T, n int, err error) {
|
||||
result = make([]T, len(data))
|
||||
for index, item := range data {
|
||||
result[index] = T(item)
|
||||
}
|
||||
return result, nil
|
||||
return result, len(data), nil
|
||||
}
|
||||
|
||||
// EncodeI8Array encodes a packed array of 8 bit integers into the given buffer.
|
||||
func EncodeI8Array[T Int8](buffer []byte, value []T) error {
|
||||
if len(buffer) != len(value) { return fmt.Errorf("encoding []int8: %w", ErrWrongBufferLength) }
|
||||
func EncodeI8Array[T Int8](buffer []byte, value []T) (n int, err error) {
|
||||
if len(buffer) != len(value) { return 0, fmt.Errorf("encoding []int8: %w", ErrWrongBufferLength) }
|
||||
for index, item := range value {
|
||||
buffer[index] = byte(item)
|
||||
}
|
||||
return nil
|
||||
return len(buffer), nil
|
||||
}
|
||||
|
||||
// I8ArraySize returns the size of a packed 8 bit integer array. Returns 0 and
|
||||
@ -237,26 +239,26 @@ func I8ArraySize[T Int8](value []T) (int, error) {
|
||||
}
|
||||
|
||||
// DecodeI16Array decodes a packed array of 16 bit integers from the given data.
|
||||
func DecodeI16Array[T Int16](data []byte) ([]T, error) {
|
||||
if len(data) % 2 != 0 { return nil, fmt.Errorf("decoding []int16: %w", ErrWrongBufferLength) }
|
||||
func DecodeI16Array[T Int16](data []byte) (value []T, n int, err error) {
|
||||
if len(data) % 2 != 0 { return nil, 0, fmt.Errorf("decoding []int16: %w", ErrWrongBufferLength) }
|
||||
length := len(data) / 2
|
||||
result := make([]T, length)
|
||||
for index := range length {
|
||||
offset := index * 2
|
||||
result[index] = T(data[offset]) << 8 | T(data[offset + 1])
|
||||
}
|
||||
return result, nil
|
||||
return result, len(data) / 2, nil
|
||||
}
|
||||
|
||||
// EncodeI16Array encodes a packed array of 16 bit integers into the given buffer.
|
||||
func EncodeI16Array[T Int16](buffer []byte, value []T) error {
|
||||
if len(buffer) != len(value) * 2 { return fmt.Errorf("encoding []int16: %w", ErrWrongBufferLength) }
|
||||
func EncodeI16Array[T Int16](buffer []byte, value []T) (n int, err error) {
|
||||
if len(buffer) != len(value) * 2 { return 0, fmt.Errorf("encoding []int16: %w", ErrWrongBufferLength) }
|
||||
for _, item := range value {
|
||||
buffer[0] = byte(item >> 8)
|
||||
buffer[1] = byte(item)
|
||||
buffer = buffer[2:]
|
||||
}
|
||||
return nil
|
||||
return len(value) * 2, nil
|
||||
}
|
||||
|
||||
// I16ArraySize returns the size of a packed 16 bit integer array. Returns 0 and
|
||||
@ -268,8 +270,8 @@ func I16ArraySize[T Int16](value []T) (int, error) {
|
||||
}
|
||||
|
||||
// DecodeI32Array decodes a packed array of 32 bit integers from the given data.
|
||||
func DecodeI32Array[T Int32](data []byte) ([]T, error) {
|
||||
if len(data) % 4 != 0 { return nil, fmt.Errorf("decoding []int32: %w", ErrWrongBufferLength) }
|
||||
func DecodeI32Array[T Int32](data []byte) (value []T, n int, err error) {
|
||||
if len(data) % 4 != 0 { return nil, 0, fmt.Errorf("decoding []int32: %w", ErrWrongBufferLength) }
|
||||
length := len(data) / 4
|
||||
result := make([]T, length)
|
||||
for index := range length {
|
||||
@ -280,12 +282,12 @@ func DecodeI32Array[T Int32](data []byte) ([]T, error) {
|
||||
T(data[offset + 2]) << 8 |
|
||||
T(data[offset + 3])
|
||||
}
|
||||
return result, nil
|
||||
return result, len(data) / 4, nil
|
||||
}
|
||||
|
||||
// EncodeI32Array encodes a packed array of 32 bit integers into the given buffer.
|
||||
func EncodeI32Array[T Int32](buffer []byte, value []T) error {
|
||||
if len(buffer) != len(value) * 4 { return fmt.Errorf("encoding []int32: %w", ErrWrongBufferLength) }
|
||||
func EncodeI32Array[T Int32](buffer []byte, value []T) (n int, err error) {
|
||||
if len(buffer) != len(value) * 4 { return 0, fmt.Errorf("encoding []int32: %w", ErrWrongBufferLength) }
|
||||
for _, item := range value {
|
||||
buffer[0] = byte(item >> 24)
|
||||
buffer[1] = byte(item >> 16)
|
||||
@ -293,7 +295,7 @@ func EncodeI32Array[T Int32](buffer []byte, value []T) error {
|
||||
buffer[3] = byte(item)
|
||||
buffer = buffer[4:]
|
||||
}
|
||||
return nil
|
||||
return len(value) * 4, nil
|
||||
}
|
||||
|
||||
// I32ArraySize returns the size of a packed 32 bit integer array. Returns 0 and
|
||||
@ -305,8 +307,8 @@ func I32ArraySize[T Int32](value []T) (int, error) {
|
||||
}
|
||||
|
||||
// DecodeI64Array decodes a packed array of 32 bit integers from the given data.
|
||||
func DecodeI64Array[T Int64](data []byte) ([]T, error) {
|
||||
if len(data) % 8 != 0 { return nil, fmt.Errorf("decoding []int64: %w", ErrWrongBufferLength) }
|
||||
func DecodeI64Array[T Int64](data []byte) (value []T, n int, err error) {
|
||||
if len(data) % 8 != 0 { return nil, 0, fmt.Errorf("decoding []int64: %w", ErrWrongBufferLength) }
|
||||
length := len(data) / 8
|
||||
result := make([]T, length)
|
||||
for index := range length {
|
||||
@ -321,12 +323,12 @@ func DecodeI64Array[T Int64](data []byte) ([]T, error) {
|
||||
T(data[offset + 6]) << 8 |
|
||||
T(data[offset + 7])
|
||||
}
|
||||
return result, nil
|
||||
return result, len(data) / 8, nil
|
||||
}
|
||||
|
||||
// EncodeI64Array encodes a packed array of 64 bit integers into the given buffer.
|
||||
func EncodeI64Array[T Int64](buffer []byte, value []T) error {
|
||||
if len(buffer) != len(value) * 8 { return fmt.Errorf("encoding []int64: %w", ErrWrongBufferLength) }
|
||||
func EncodeI64Array[T Int64](buffer []byte, value []T) (n int, err error) {
|
||||
if len(buffer) != len(value) * 8 { return 0, fmt.Errorf("encoding []int64: %w", ErrWrongBufferLength) }
|
||||
for _, item := range value {
|
||||
buffer[0] = byte(item >> 56)
|
||||
buffer[1] = byte(item >> 48)
|
||||
@ -338,7 +340,7 @@ func EncodeI64Array[T Int64](buffer []byte, value []T) error {
|
||||
buffer[7] = byte(item)
|
||||
buffer = buffer[8:]
|
||||
}
|
||||
return nil
|
||||
return len(value) * 8, nil
|
||||
}
|
||||
|
||||
// I64ArraySize returns the size of a packed 64 bit integer array. Returns 0 and
|
||||
|
@ -11,19 +11,19 @@ const randStringBytes = "-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTU
|
||||
|
||||
func TestI8(test *testing.T) {
|
||||
var buffer [16]byte
|
||||
err := EncodeI8[uint8](buffer[:], 5)
|
||||
_, err := EncodeI8[uint8](buffer[:], 5)
|
||||
if err.Error() != "encoding int8: wrong buffer length" { test.Fatal(err) }
|
||||
err = EncodeI8[uint8](buffer[:0], 5)
|
||||
_, err = EncodeI8[uint8](buffer[:0], 5)
|
||||
if err.Error() != "encoding int8: wrong buffer length" { test.Fatal(err) }
|
||||
_, err = DecodeI8[uint8](buffer[:])
|
||||
_, _, err = DecodeI8[uint8](buffer[:])
|
||||
if err.Error() != "decoding int8: wrong buffer length" { test.Fatal(err) }
|
||||
_, err = DecodeI8[uint8](buffer[:0])
|
||||
_, _, err = DecodeI8[uint8](buffer[:0])
|
||||
if err.Error() != "decoding int8: wrong buffer length" { test.Fatal(err) }
|
||||
|
||||
for number := range uint8(255) {
|
||||
err := EncodeI8[uint8](buffer[:1], number)
|
||||
_, err := EncodeI8[uint8](buffer[:1], number)
|
||||
if err != nil { test.Fatal(err) }
|
||||
decoded, err := DecodeI8[uint8](buffer[:1])
|
||||
decoded, _, err := DecodeI8[uint8](buffer[:1])
|
||||
if err != nil { test.Fatal(err) }
|
||||
if decoded != number {
|
||||
test.Fatalf("%d != %d", decoded, number)
|
||||
@ -33,20 +33,20 @@ func TestI8(test *testing.T) {
|
||||
|
||||
func TestI16(test *testing.T) {
|
||||
var buffer [16]byte
|
||||
err := EncodeI16[uint16](buffer[:], 5)
|
||||
_, err := EncodeI16[uint16](buffer[:], 5)
|
||||
if err.Error() != "encoding int16: wrong buffer length" { test.Fatal(err) }
|
||||
err = EncodeI16[uint16](buffer[:0], 5)
|
||||
_, err = EncodeI16[uint16](buffer[:0], 5)
|
||||
if err.Error() != "encoding int16: wrong buffer length" { test.Fatal(err) }
|
||||
_, err = DecodeI16[uint16](buffer[:])
|
||||
_, _, err = DecodeI16[uint16](buffer[:])
|
||||
if err.Error() != "decoding int16: wrong buffer length" { test.Fatal(err) }
|
||||
_, err = DecodeI16[uint16](buffer[:0])
|
||||
_, _, err = DecodeI16[uint16](buffer[:0])
|
||||
if err.Error() != "decoding int16: wrong buffer length" { test.Fatal(err) }
|
||||
|
||||
for _ = range largeNumberNTestRounds {
|
||||
number := uint16(rand.Int())
|
||||
err := EncodeI16[uint16](buffer[:2], number)
|
||||
_, err := EncodeI16[uint16](buffer[:2], number)
|
||||
if err != nil { test.Fatal(err) }
|
||||
decoded, err := DecodeI16[uint16](buffer[:2])
|
||||
decoded, _, err := DecodeI16[uint16](buffer[:2])
|
||||
if err != nil { test.Fatal(err) }
|
||||
if decoded != number {
|
||||
test.Fatalf("%d != %d", decoded, number)
|
||||
@ -56,20 +56,20 @@ func TestI16(test *testing.T) {
|
||||
|
||||
func TestI32(test *testing.T) {
|
||||
var buffer [16]byte
|
||||
err := EncodeI32[uint32](buffer[:], 5)
|
||||
_, err := EncodeI32[uint32](buffer[:], 5)
|
||||
if err.Error() != "encoding int32: wrong buffer length" { test.Fatal(err) }
|
||||
err = EncodeI32[uint32](buffer[:0], 5)
|
||||
_, err = EncodeI32[uint32](buffer[:0], 5)
|
||||
if err.Error() != "encoding int32: wrong buffer length" { test.Fatal(err) }
|
||||
_, err = DecodeI32[uint32](buffer[:])
|
||||
_, _, err = DecodeI32[uint32](buffer[:])
|
||||
if err.Error() != "decoding int32: wrong buffer length" { test.Fatal(err) }
|
||||
_, err = DecodeI32[uint32](buffer[:0])
|
||||
_, _, err = DecodeI32[uint32](buffer[:0])
|
||||
if err.Error() != "decoding int32: wrong buffer length" { test.Fatal(err) }
|
||||
|
||||
for _ = range largeNumberNTestRounds {
|
||||
number := uint32(rand.Int())
|
||||
err := EncodeI32[uint32](buffer[:4], number)
|
||||
_, err := EncodeI32[uint32](buffer[:4], number)
|
||||
if err != nil { test.Fatal(err) }
|
||||
decoded, err := DecodeI32[uint32](buffer[:4])
|
||||
decoded, _, err := DecodeI32[uint32](buffer[:4])
|
||||
if err != nil { test.Fatal(err) }
|
||||
if decoded != number {
|
||||
test.Fatalf("%d != %d", decoded, number)
|
||||
@ -79,20 +79,20 @@ func TestI32(test *testing.T) {
|
||||
|
||||
func TestI64(test *testing.T) {
|
||||
var buffer [16]byte
|
||||
err := EncodeI64[uint64](buffer[:], 5)
|
||||
_, err := EncodeI64[uint64](buffer[:], 5)
|
||||
if err.Error() != "encoding int64: wrong buffer length" { test.Fatal(err) }
|
||||
err = EncodeI64[uint64](buffer[:0], 5)
|
||||
_, err = EncodeI64[uint64](buffer[:0], 5)
|
||||
if err.Error() != "encoding int64: wrong buffer length" { test.Fatal(err) }
|
||||
_, err = DecodeI64[uint64](buffer[:])
|
||||
_, _, err = DecodeI64[uint64](buffer[:])
|
||||
if err.Error() != "decoding int64: wrong buffer length" { test.Fatal(err) }
|
||||
_, err = DecodeI64[uint64](buffer[:0])
|
||||
_, _, err = DecodeI64[uint64](buffer[:0])
|
||||
if err.Error() != "decoding int64: wrong buffer length" { test.Fatal(err) }
|
||||
|
||||
for _ = range largeNumberNTestRounds {
|
||||
number := uint64(rand.Int())
|
||||
err := EncodeI64[uint64](buffer[:8], number)
|
||||
_, err := EncodeI64[uint64](buffer[:8], number)
|
||||
if err != nil { test.Fatal(err) }
|
||||
decoded, err := DecodeI64[uint64](buffer[:8])
|
||||
decoded, _, err := DecodeI64[uint64](buffer[:8])
|
||||
if err != nil { test.Fatal(err) }
|
||||
if decoded != number {
|
||||
test.Fatalf("%d != %d", decoded, number)
|
||||
@ -113,7 +113,7 @@ func TestGBEU(test *testing.T) {
|
||||
_, err = EncodeGBEU[uint64](buffer[:2], 5555555)
|
||||
if err == nil { test.Fatal("no error") }
|
||||
if err.Error() != "encoding GBEU: wrong buffer length" { test.Fatal(err) }
|
||||
_, err = DecodeGBEU[uint64](buffer[:0])
|
||||
_, _, err = DecodeGBEU[uint64](buffer[:0])
|
||||
if err == nil { test.Fatal("no error") }
|
||||
if err.Error() != "decoding GBEU: GBEU not terminated" { test.Fatal(err) }
|
||||
|
||||
@ -126,7 +126,7 @@ func TestGBEU(test *testing.T) {
|
||||
}
|
||||
test.Fatal(message)
|
||||
}
|
||||
decoded, err := DecodeGBEU[uint64](buffer[:])
|
||||
decoded, _, err := DecodeGBEU[uint64](buffer[:])
|
||||
if err != nil { test.Fatal(err) }
|
||||
if correct, got := uint64(0x97), decoded; correct != got {
|
||||
test.Fatalf("not equal: %x", got)
|
||||
@ -141,7 +141,7 @@ func TestGBEU(test *testing.T) {
|
||||
}
|
||||
test.Fatal(message)
|
||||
}
|
||||
decoded, err = DecodeGBEU[uint64](buffer[:])
|
||||
decoded, _, err = DecodeGBEU[uint64](buffer[:])
|
||||
if err != nil { test.Fatal(err) }
|
||||
if correct, got := uint64(0x123456), decoded; correct != got {
|
||||
test.Fatalf("not equal: %x", got)
|
||||
@ -157,7 +157,7 @@ func TestGBEU(test *testing.T) {
|
||||
}
|
||||
test.Fatal(message)
|
||||
}
|
||||
decoded, err = DecodeGBEU[uint64](buffer[:])
|
||||
decoded, _, err = DecodeGBEU[uint64](buffer[:])
|
||||
if err != nil { test.Fatal(err) }
|
||||
if correct, got := uint64(0xFFFFFFFFFFFFFFFF), decoded; correct != got {
|
||||
test.Fatalf("not equal: %x", got)
|
||||
@ -168,7 +168,7 @@ func TestGBEU(test *testing.T) {
|
||||
if correct, got := byte(0xb), buffer[0]; correct != got {
|
||||
test.Fatal("not equal:", got)
|
||||
}
|
||||
decoded, err = DecodeGBEU[uint64](buffer[:])
|
||||
decoded, _, err = DecodeGBEU[uint64](buffer[:])
|
||||
if err != nil { test.Fatal(err) }
|
||||
if correct, got := uint64(0xb), decoded; correct != got {
|
||||
test.Fatalf("not equal: %x", got)
|
||||
@ -179,7 +179,7 @@ func TestGBEU(test *testing.T) {
|
||||
number := uint64(rand.Int())
|
||||
_, err := EncodeGBEU[uint64](buffer[:], number)
|
||||
if err != nil { test.Fatal(err) }
|
||||
decoded, err := DecodeGBEU[uint64](buffer[:])
|
||||
decoded, _, err := DecodeGBEU[uint64](buffer[:])
|
||||
if err != nil { test.Fatal(err) }
|
||||
if decoded != number {
|
||||
test.Error("not equal:")
|
||||
@ -210,21 +210,21 @@ func TestGBEUSize(test *testing.T) {
|
||||
|
||||
func TestString(test *testing.T) {
|
||||
var buffer [16]byte
|
||||
err := EncodeString[string](buffer[:], "hello")
|
||||
_, err := EncodeString[string](buffer[:], "hello")
|
||||
if !errIs(err, ErrWrongBufferLength, "encoding string: wrong buffer length") { test.Fatal(err) }
|
||||
err = EncodeString[string](buffer[:0], "hello")
|
||||
_, err = EncodeString[string](buffer[:0], "hello")
|
||||
if !errIs(err, ErrWrongBufferLength, "encoding string: wrong buffer length") { test.Fatal(err) }
|
||||
_, err = DecodeString[string](buffer[:])
|
||||
_, _, err = DecodeString[string](buffer[:])
|
||||
if err != nil { test.Fatal(err) }
|
||||
_, err = DecodeString[string](buffer[:0])
|
||||
_, _, err = DecodeString[string](buffer[:0])
|
||||
if err != nil { test.Fatal(err) }
|
||||
|
||||
for _ = range largeNumberNTestRounds {
|
||||
length := rand.Intn(16)
|
||||
str := randString(length)
|
||||
err := EncodeString[string](buffer[:length], str)
|
||||
_, err := EncodeString[string](buffer[:length], str)
|
||||
if err != nil { test.Fatal(err) }
|
||||
decoded, err := DecodeString[string](buffer[:length])
|
||||
decoded, _, err := DecodeString[string](buffer[:length])
|
||||
if err != nil { test.Fatal(err) }
|
||||
if decoded != str {
|
||||
test.Fatalf("%s != %s", decoded, str)
|
||||
@ -234,22 +234,22 @@ func TestString(test *testing.T) {
|
||||
|
||||
func TestI8Array(test *testing.T) {
|
||||
var buffer [64]byte
|
||||
err := EncodeI8Array[uint8](buffer[:], []uint8 { 0, 4, 50, 19 })
|
||||
_, err := EncodeI8Array[uint8](buffer[:], []uint8 { 0, 4, 50, 19 })
|
||||
if !errIs(err, ErrWrongBufferLength, "encoding []int8: wrong buffer length") { test.Fatal(err) }
|
||||
err = EncodeI8Array[uint8](buffer[:0], []uint8 { 0, 4, 50, 19 })
|
||||
_, err = EncodeI8Array[uint8](buffer[:0], []uint8 { 0, 4, 50, 19 })
|
||||
if !errIs(err, ErrWrongBufferLength, "encoding []int8: wrong buffer length") { test.Fatal(err) }
|
||||
_, err = DecodeI8Array[uint8](buffer[:])
|
||||
_, _, err = DecodeI8Array[uint8](buffer[:])
|
||||
if err != nil { test.Fatal(err) }
|
||||
_, err = DecodeI8Array[uint8](buffer[:0])
|
||||
_, _, err = DecodeI8Array[uint8](buffer[:0])
|
||||
if err != nil { test.Fatal(err) }
|
||||
|
||||
for _ = range largeNumberNTestRounds {
|
||||
array := randInts[uint8](rand.Intn(16))
|
||||
length, _ := I8ArraySize(array)
|
||||
if length != len(array) { test.Fatalf("%d != %d", length, len(array)) }
|
||||
err := EncodeI8Array[uint8](buffer[:length], array)
|
||||
_, err := EncodeI8Array[uint8](buffer[:length], array)
|
||||
if err != nil { test.Fatal(err) }
|
||||
decoded, err := DecodeI8Array[uint8](buffer[:length])
|
||||
decoded, _, err := DecodeI8Array[uint8](buffer[:length])
|
||||
if err != nil { test.Fatal(err) }
|
||||
if !slices.Equal(decoded, array) {
|
||||
test.Fatalf("%v != %v", decoded, array)
|
||||
@ -259,22 +259,22 @@ func TestI8Array(test *testing.T) {
|
||||
|
||||
func TestI16Array(test *testing.T) {
|
||||
var buffer [128]byte
|
||||
err := EncodeI16Array[uint16](buffer[:], []uint16 { 0, 4, 50, 19 })
|
||||
_, err := EncodeI16Array[uint16](buffer[:], []uint16 { 0, 4, 50, 19 })
|
||||
if !errIs(err, ErrWrongBufferLength, "encoding []int16: wrong buffer length") { test.Fatal(err) }
|
||||
err = EncodeI16Array[uint16](buffer[:0], []uint16 { 0, 4, 50, 19 })
|
||||
_, err = EncodeI16Array[uint16](buffer[:0], []uint16 { 0, 4, 50, 19 })
|
||||
if !errIs(err, ErrWrongBufferLength, "encoding []int16: wrong buffer length") { test.Fatal(err) }
|
||||
_, err = DecodeI16Array[uint16](buffer[:])
|
||||
_, _, err = DecodeI16Array[uint16](buffer[:])
|
||||
if err != nil { test.Fatal(err) }
|
||||
_, err = DecodeI16Array[uint16](buffer[:0])
|
||||
_, _, err = DecodeI16Array[uint16](buffer[:0])
|
||||
if err != nil { test.Fatal(err) }
|
||||
|
||||
for _ = range largeNumberNTestRounds {
|
||||
array := randInts[uint16](rand.Intn(16))
|
||||
length, _ := I16ArraySize(array)
|
||||
if length != 2 * len(array) { test.Fatalf("%d != %d", length, 2 * len(array)) }
|
||||
err := EncodeI16Array[uint16](buffer[:length], array)
|
||||
_, err := EncodeI16Array[uint16](buffer[:length], array)
|
||||
if err != nil { test.Fatal(err) }
|
||||
decoded, err := DecodeI16Array[uint16](buffer[:length])
|
||||
decoded, _, err := DecodeI16Array[uint16](buffer[:length])
|
||||
if err != nil { test.Fatal(err) }
|
||||
if !slices.Equal(decoded, array) {
|
||||
test.Fatalf("%v != %v", decoded, array)
|
||||
@ -284,22 +284,22 @@ func TestI16Array(test *testing.T) {
|
||||
|
||||
func TestI32Array(test *testing.T) {
|
||||
var buffer [256]byte
|
||||
err := EncodeI32Array[uint32](buffer[:], []uint32 { 0, 4, 50, 19 })
|
||||
_, err := EncodeI32Array[uint32](buffer[:], []uint32 { 0, 4, 50, 19 })
|
||||
if !errIs(err, ErrWrongBufferLength, "encoding []int32: wrong buffer length") { test.Fatal(err) }
|
||||
err = EncodeI32Array[uint32](buffer[:0], []uint32 { 0, 4, 50, 19 })
|
||||
_, err = EncodeI32Array[uint32](buffer[:0], []uint32 { 0, 4, 50, 19 })
|
||||
if !errIs(err, ErrWrongBufferLength, "encoding []int32: wrong buffer length") { test.Fatal(err) }
|
||||
_, err = DecodeI32Array[uint32](buffer[:])
|
||||
_, _, err = DecodeI32Array[uint32](buffer[:])
|
||||
if err != nil { test.Fatal(err) }
|
||||
_, err = DecodeI32Array[uint32](buffer[:0])
|
||||
_, _, err = DecodeI32Array[uint32](buffer[:0])
|
||||
if err != nil { test.Fatal(err) }
|
||||
|
||||
for _ = range largeNumberNTestRounds {
|
||||
array := randInts[uint32](rand.Intn(16))
|
||||
length, _ := I32ArraySize(array)
|
||||
if length != 4 * len(array) { test.Fatalf("%d != %d", length, 4 * len(array)) }
|
||||
err := EncodeI32Array[uint32](buffer[:length], array)
|
||||
_, err := EncodeI32Array[uint32](buffer[:length], array)
|
||||
if err != nil { test.Fatal(err) }
|
||||
decoded, err := DecodeI32Array[uint32](buffer[:length])
|
||||
decoded, _, err := DecodeI32Array[uint32](buffer[:length])
|
||||
if err != nil { test.Fatal(err) }
|
||||
if !slices.Equal(decoded, array) {
|
||||
test.Fatalf("%v != %v", decoded, array)
|
||||
@ -309,22 +309,22 @@ func TestI32Array(test *testing.T) {
|
||||
|
||||
func TestI64Array(test *testing.T) {
|
||||
var buffer [512]byte
|
||||
err := EncodeI64Array[uint64](buffer[:], []uint64 { 0, 4, 50, 19 })
|
||||
_, err := EncodeI64Array[uint64](buffer[:], []uint64 { 0, 4, 50, 19 })
|
||||
if !errIs(err, ErrWrongBufferLength, "encoding []int64: wrong buffer length") { test.Fatal(err) }
|
||||
err = EncodeI64Array[uint64](buffer[:0], []uint64 { 0, 4, 50, 19 })
|
||||
_, err = EncodeI64Array[uint64](buffer[:0], []uint64 { 0, 4, 50, 19 })
|
||||
if !errIs(err, ErrWrongBufferLength, "encoding []int64: wrong buffer length") { test.Fatal(err) }
|
||||
_, err = DecodeI64Array[uint64](buffer[:])
|
||||
_, _, err = DecodeI64Array[uint64](buffer[:])
|
||||
if err != nil { test.Fatal(err) }
|
||||
_, err = DecodeI64Array[uint64](buffer[:0])
|
||||
_, _, err = DecodeI64Array[uint64](buffer[:0])
|
||||
if err != nil { test.Fatal(err) }
|
||||
|
||||
for _ = range largeNumberNTestRounds {
|
||||
array := randInts[uint64](rand.Intn(16))
|
||||
length, _ := I64ArraySize(array)
|
||||
if length != 8 * len(array) { test.Fatalf("%d != %d", length, 8 * len(array)) }
|
||||
err := EncodeI64Array[uint64](buffer[:length], array)
|
||||
_, err := EncodeI64Array[uint64](buffer[:length], array)
|
||||
if err != nil { test.Fatal(err) }
|
||||
decoded, err := DecodeI64Array[uint64](buffer[:length])
|
||||
decoded, _, err := DecodeI64Array[uint64](buffer[:length])
|
||||
if err != nil { test.Fatal(err) }
|
||||
if !slices.Equal(decoded, array) {
|
||||
test.Fatalf("%v != %v", decoded, array)
|
||||
@ -334,20 +334,20 @@ func TestI64Array(test *testing.T) {
|
||||
|
||||
func TestStringArray(test *testing.T) {
|
||||
var buffer [8192]byte
|
||||
err := EncodeStringArray[string](buffer[:], []string { "0", "4", "50", "19" })
|
||||
_, err := EncodeStringArray[string](buffer[:], []string { "0", "4", "50", "19" })
|
||||
if !errIs(err, ErrWrongBufferLength, "encoding []string: wrong buffer length") { test.Fatal(err) }
|
||||
err = EncodeStringArray[string](buffer[:0], []string { "0", "4", "50", "19" })
|
||||
_, err = EncodeStringArray[string](buffer[:0], []string { "0", "4", "50", "19" })
|
||||
if !errIs(err, ErrWrongBufferLength, "encoding []string: wrong buffer length") { test.Fatal(err) }
|
||||
_, err = DecodeStringArray[string](buffer[:0])
|
||||
_, _, err = DecodeStringArray[string](buffer[:0])
|
||||
if err != nil { test.Fatal(err) }
|
||||
|
||||
for _ = range largeNumberNTestRounds {
|
||||
array := randStrings[string](rand.Intn(16), 16)
|
||||
length, _ := StringArraySize(array)
|
||||
// TODO test length
|
||||
err := EncodeStringArray[string](buffer[:length], array)
|
||||
_, err := EncodeStringArray[string](buffer[:length], array)
|
||||
if err != nil { test.Fatal(err) }
|
||||
decoded, err := DecodeStringArray[string](buffer[:length])
|
||||
decoded, _, err := DecodeStringArray[string](buffer[:length])
|
||||
if err != nil { test.Fatal(err) }
|
||||
if !slices.Equal(decoded, array) {
|
||||
test.Fatalf("%v != %v", decoded, array)
|
||||
|
Loading…
Reference in New Issue
Block a user