mirror of
https://github.com/tuneinsight/lattigo.git
synced 2025-09-13 03:27:14 +00:00
77 lines
3.1 KiB
Go
77 lines
3.1 KiB
Go
package buffer
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding"
|
|
"fmt"
|
|
"io"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// binarySerializer is a testing interface for byte encoding and decoding.
|
|
type binarySerializer interface {
|
|
BinarySize() int
|
|
io.WriterTo
|
|
io.ReaderFrom
|
|
encoding.BinaryMarshaler
|
|
encoding.BinaryUnmarshaler
|
|
}
|
|
|
|
// RequireSerializerCorrect tests that:
|
|
// - input and output implement TestInterface
|
|
// - input.WriteTo(io.Writer) writes a number of bytes on the writer equal to the number of bytes generated by input.MarshalBinary()
|
|
// - input.WriteTo buffered bytes are equal to the bytes generated by input.MarshalBinary()
|
|
// - output.ReadFrom(io.Reader) reads a number of bytes on the reader equal to the number of bytes written using input.WriteTo(io.Writer)
|
|
// - applies require.Equalf between the original and reconstructed object for
|
|
// - all the above WriteTo, ReadFrom, MarhsalBinary and UnmarshalBinary do not return an error
|
|
func RequireSerializerCorrect(t *testing.T, input binarySerializer) {
|
|
|
|
// Allocates a new object of the underlying type of input
|
|
output := reflect.New(reflect.TypeOf(input).Elem()).Elem().Addr().Interface().(binarySerializer)
|
|
|
|
data := []byte{}
|
|
|
|
buf := bytes.NewBuffer(data) // Compliant to io.Writer and io.Reader
|
|
|
|
// Check io.Writer
|
|
bytesWritten, err := input.WriteTo(buf)
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int(bytesWritten), input.BinarySize(), fmt.Errorf("invalid size: %T.WriteTo #bytes written = %d != %T.BinarySize = %d", input, bytesWritten, input, input.BinarySize()))
|
|
|
|
// Checks that #bytes written = len(buffer)
|
|
require.Equal(t, len(buf.Bytes()), int(bytesWritten), fmt.Errorf("invalid size: %T.WriteTo len(buf.Bytes()) = %d != %T.WriteTo #bytes written = %d", input, len(buf.Bytes()), input, bytesWritten))
|
|
|
|
// Check encoding.BinaryMarshaler
|
|
data2, err := input.MarshalBinary()
|
|
require.NoError(t, err)
|
|
|
|
// Check that #bytes written with io.Writer = #bytes generates by encoding.BinaryMarshaler
|
|
require.Equal(t, len(data2), int(bytesWritten), fmt.Errorf("invalid size: %T.MarshalBinary #bytes generated = %d != %T.WriteTo #bytes written = %d", input, len(data2), input, bytesWritten))
|
|
|
|
// Check that bytes written with io.Writer = bytes generates by encoding.BinaryMarshaler
|
|
require.True(t, bytes.Equal(buf.Bytes(), data2), fmt.Errorf("invalid encoding: %T.WriteTo buf.Bytes() != %T.MarshalBinary bytes generated", input, input))
|
|
|
|
// Check io.Reader
|
|
bytesRead, err := output.ReadFrom(buf)
|
|
require.NoError(t, err)
|
|
|
|
// Check that #bytes read with io.Reader = #bytes written with io.Writer
|
|
require.Equal(t, bytesRead, bytesWritten, fmt.Errorf("invalid encoding: %T.ReadFrom #bytes read = %d != %T.WriteTo #bytes written = %d", input, bytesRead, input, bytesWritten))
|
|
|
|
// Deep equal output = input
|
|
require.True(t, cmp.Equal(input, output))
|
|
|
|
// Check encoding.BinaryUnmarshaler
|
|
output = reflect.New(reflect.TypeOf(input).Elem()).Elem().Addr().Interface().(binarySerializer)
|
|
|
|
require.NoError(t, output.UnmarshalBinary(data2))
|
|
|
|
// Deep equal output = input
|
|
require.True(t, cmp.Equal(input, output))
|
|
}
|