github.com/annchain/OG@v0.0.9/consensus/vrf/model_gen_test.go (about)

     1  package vrf
     2  
     3  import (
     4  	"bytes"
     5  	"github.com/tinylib/msgp/msgp"
     6  	"testing"
     7  )
     8  
     9  func TestMarshalUnmarshalVrfInfo(t *testing.T) {
    10  	v := VrfInfo{}
    11  	bts, err := v.MarshalMsg(nil)
    12  	if err != nil {
    13  		t.Fatal(err)
    14  	}
    15  	left, err := v.UnmarshalMsg(bts)
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  	if len(left) > 0 {
    20  		t.Errorf("%d bytes left over after UnmarshalMsg(): %q", len(left), left)
    21  	}
    22  
    23  	left, err = msgp.Skip(bts)
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  	if len(left) > 0 {
    28  		t.Errorf("%d bytes left over after Skip(): %q", len(left), left)
    29  	}
    30  }
    31  
    32  func BenchmarkMarshalMsgVrfInfo(b *testing.B) {
    33  	v := VrfInfo{}
    34  	b.ReportAllocs()
    35  	b.ResetTimer()
    36  	for i := 0; i < b.N; i++ {
    37  		v.MarshalMsg(nil)
    38  	}
    39  }
    40  
    41  func BenchmarkAppendMsgVrfInfo(b *testing.B) {
    42  	v := VrfInfo{}
    43  	bts := make([]byte, 0, v.Msgsize())
    44  	bts, _ = v.MarshalMsg(bts[0:0])
    45  	b.SetBytes(int64(len(bts)))
    46  	b.ReportAllocs()
    47  	b.ResetTimer()
    48  	for i := 0; i < b.N; i++ {
    49  		bts, _ = v.MarshalMsg(bts[0:0])
    50  	}
    51  }
    52  
    53  func BenchmarkUnmarshalVrfInfo(b *testing.B) {
    54  	v := VrfInfo{}
    55  	bts, _ := v.MarshalMsg(nil)
    56  	b.ReportAllocs()
    57  	b.SetBytes(int64(len(bts)))
    58  	b.ResetTimer()
    59  	for i := 0; i < b.N; i++ {
    60  		_, err := v.UnmarshalMsg(bts)
    61  		if err != nil {
    62  			b.Fatal(err)
    63  		}
    64  	}
    65  }
    66  
    67  func TestEncodeDecodeVrfInfo(t *testing.T) {
    68  	v := VrfInfo{}
    69  	var buf bytes.Buffer
    70  	msgp.Encode(&buf, &v)
    71  
    72  	m := v.Msgsize()
    73  	if buf.Len() > m {
    74  		t.Logf("WARNING: Msgsize() for %v is inaccurate", v)
    75  	}
    76  
    77  	vn := VrfInfo{}
    78  	err := msgp.Decode(&buf, &vn)
    79  	if err != nil {
    80  		t.Error(err)
    81  	}
    82  
    83  	buf.Reset()
    84  	msgp.Encode(&buf, &v)
    85  	err = msgp.NewReader(&buf).Skip()
    86  	if err != nil {
    87  		t.Error(err)
    88  	}
    89  }
    90  
    91  func BenchmarkEncodeVrfInfo(b *testing.B) {
    92  	v := VrfInfo{}
    93  	var buf bytes.Buffer
    94  	msgp.Encode(&buf, &v)
    95  	b.SetBytes(int64(buf.Len()))
    96  	en := msgp.NewWriter(msgp.Nowhere)
    97  	b.ReportAllocs()
    98  	b.ResetTimer()
    99  	for i := 0; i < b.N; i++ {
   100  		v.EncodeMsg(en)
   101  	}
   102  	en.Flush()
   103  }
   104  
   105  func BenchmarkDecodeVrfInfo(b *testing.B) {
   106  	v := VrfInfo{}
   107  	var buf bytes.Buffer
   108  	msgp.Encode(&buf, &v)
   109  	b.SetBytes(int64(buf.Len()))
   110  	rd := msgp.NewEndlessReader(buf.Bytes(), b)
   111  	dc := msgp.NewReader(rd)
   112  	b.ReportAllocs()
   113  	b.ResetTimer()
   114  	for i := 0; i < b.N; i++ {
   115  		err := v.DecodeMsg(dc)
   116  		if err != nil {
   117  			b.Fatal(err)
   118  		}
   119  	}
   120  }