github.com/consensys/gnark-crypto@v0.14.0/field/generator/internal/templates/element/tests_vector.go (about)

     1  package element
     2  
     3  const TestVector = `
     4  
     5  
     6  import (
     7  	"testing"
     8  	"github.com/stretchr/testify/require"
     9  	"sort"
    10  	"reflect"
    11  	"bytes"
    12  )
    13  
    14  
    15  
    16  func TestVectorSort(t *testing.T) {
    17  	assert := require.New(t)
    18  
    19  	v := make(Vector, 3)
    20  	v[0].SetUint64(2)
    21  	v[1].SetUint64(3)
    22  	v[2].SetUint64(1)
    23  
    24  	sort.Sort(v)
    25  
    26  	assert.Equal("[1,2,3]", v.String())
    27  }
    28  
    29  func TestVectorRoundTrip(t *testing.T) {
    30  	assert := require.New(t)
    31  
    32  	v1 := make(Vector, 3)
    33  	v1[0].SetUint64(2)
    34  	v1[1].SetUint64(3)
    35  	v1[2].SetUint64(1)
    36  
    37  	b, err := v1.MarshalBinary()
    38  	assert.NoError(err)
    39  
    40  	var v2,v3 Vector
    41  
    42  	err = v2.UnmarshalBinary(b)
    43  	assert.NoError(err)
    44  
    45  	err = v3.unmarshalBinaryAsync(b)
    46  	assert.NoError(err)
    47  
    48  	assert.True(reflect.DeepEqual(v1,v2))
    49  	assert.True(reflect.DeepEqual(v3,v2))
    50  }
    51  
    52  func TestVectorEmptyRoundTrip(t *testing.T) {
    53  	assert := require.New(t)
    54  
    55  	v1 := make(Vector, 0)
    56  
    57  	b, err := v1.MarshalBinary()
    58  	assert.NoError(err)
    59  
    60  	var v2, v3 Vector
    61  
    62  	err = v2.UnmarshalBinary(b)
    63  	assert.NoError(err)
    64  
    65  	err = v3.unmarshalBinaryAsync(b)
    66  	assert.NoError(err)
    67  
    68  	assert.True(reflect.DeepEqual(v1,v2))
    69  	assert.True(reflect.DeepEqual(v3,v2))
    70  }
    71  
    72  
    73  
    74  func (vector *Vector) unmarshalBinaryAsync(data []byte) error {
    75  	r := bytes.NewReader(data)
    76  	_, err, chErr := vector.AsyncReadFrom(r)
    77  	if err != nil {
    78  		return err
    79  	}
    80  	return <-chErr
    81  }
    82  
    83  `