github.com/consensys/gnark-crypto@v0.14.0/utils/testutils/testing.go (about) 1 package testutils 2 3 import ( 4 "bytes" 5 "io" 6 "reflect" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 type Serializable interface { 13 io.ReaderFrom 14 io.WriterTo 15 } 16 17 type RawSerializable interface { 18 WriteRawTo(io.Writer) (int64, error) 19 } 20 21 type BinaryDumper interface { 22 WriteDump(w io.Writer, maxPkPoints ...int) error 23 ReadDump(r io.Reader, maxPkPoints ...int) error 24 } 25 26 func SerializationRoundTrip(o Serializable) func(*testing.T) { 27 return func(t *testing.T) { 28 // serialize it... 29 var buf bytes.Buffer 30 _, err := o.WriteTo(&buf) 31 assert.NoError(t, err) 32 33 // reconstruct the object 34 _o := reflect.New(reflect.TypeOf(o).Elem()).Interface().(Serializable) 35 _, err = _o.ReadFrom(&buf) 36 assert.NoError(t, err) 37 38 // compare 39 assert.Equal(t, o, _o) 40 } 41 } 42 43 func SerializationRoundTripRaw(o RawSerializable) func(*testing.T) { 44 return func(t *testing.T) { 45 // serialize it... 46 var buf bytes.Buffer 47 _, err := o.WriteRawTo(&buf) 48 assert.NoError(t, err) 49 50 // reconstruct the object 51 _o := reflect.New(reflect.TypeOf(o).Elem()).Interface().(Serializable) 52 _, err = _o.ReadFrom(&buf) 53 assert.NoError(t, err) 54 55 // compare 56 assert.Equal(t, o, _o) 57 } 58 } 59 60 func UnsafeBinaryMarshalerRoundTrip(o BinaryDumper) func(*testing.T) { 61 return func(t *testing.T) { 62 // serialize it... 63 var buf bytes.Buffer 64 err := o.WriteDump(&buf) 65 assert.NoError(t, err) 66 67 // reconstruct the object 68 _o := reflect.New(reflect.TypeOf(o).Elem()).Interface().(BinaryDumper) 69 err = _o.ReadDump(&buf) 70 assert.NoError(t, err) 71 72 // compare 73 assert.Equal(t, o, _o) 74 } 75 }