github.com/consensys/gnark-crypto@v0.14.0/ecc/bls12-378/fp/vector_test.go (about)

     1  // Copyright 2020 ConsenSys Software Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Code generated by consensys/gnark-crypto DO NOT EDIT
    16  
    17  package fp
    18  
    19  import (
    20  	"bytes"
    21  	"github.com/stretchr/testify/require"
    22  	"reflect"
    23  	"sort"
    24  	"testing"
    25  )
    26  
    27  func TestVectorSort(t *testing.T) {
    28  	assert := require.New(t)
    29  
    30  	v := make(Vector, 3)
    31  	v[0].SetUint64(2)
    32  	v[1].SetUint64(3)
    33  	v[2].SetUint64(1)
    34  
    35  	sort.Sort(v)
    36  
    37  	assert.Equal("[1,2,3]", v.String())
    38  }
    39  
    40  func TestVectorRoundTrip(t *testing.T) {
    41  	assert := require.New(t)
    42  
    43  	v1 := make(Vector, 3)
    44  	v1[0].SetUint64(2)
    45  	v1[1].SetUint64(3)
    46  	v1[2].SetUint64(1)
    47  
    48  	b, err := v1.MarshalBinary()
    49  	assert.NoError(err)
    50  
    51  	var v2, v3 Vector
    52  
    53  	err = v2.UnmarshalBinary(b)
    54  	assert.NoError(err)
    55  
    56  	err = v3.unmarshalBinaryAsync(b)
    57  	assert.NoError(err)
    58  
    59  	assert.True(reflect.DeepEqual(v1, v2))
    60  	assert.True(reflect.DeepEqual(v3, v2))
    61  }
    62  
    63  func TestVectorEmptyRoundTrip(t *testing.T) {
    64  	assert := require.New(t)
    65  
    66  	v1 := make(Vector, 0)
    67  
    68  	b, err := v1.MarshalBinary()
    69  	assert.NoError(err)
    70  
    71  	var v2, v3 Vector
    72  
    73  	err = v2.UnmarshalBinary(b)
    74  	assert.NoError(err)
    75  
    76  	err = v3.unmarshalBinaryAsync(b)
    77  	assert.NoError(err)
    78  
    79  	assert.True(reflect.DeepEqual(v1, v2))
    80  	assert.True(reflect.DeepEqual(v3, v2))
    81  }
    82  
    83  func (vector *Vector) unmarshalBinaryAsync(data []byte) error {
    84  	r := bytes.NewReader(data)
    85  	_, err, chErr := vector.AsyncReadFrom(r)
    86  	if err != nil {
    87  		return err
    88  	}
    89  	return <-chErr
    90  }