github.com/annchain/OG@v0.0.9/trie/node_gen_test.go (about)

     1  package trie
     2  
     3  // Code generated by github.com/tinylib/msgp DO NOT EDIT.
     4  
     5  import (
     6  	"bytes"
     7  	"math/rand"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/annchain/OG/types"
    12  	"github.com/tinylib/msgp/msgp"
    13  )
    14  
    15  var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
    16  
    17  func newFullNode() *FullNode {
    18  	rand.Seed(time.Now().UnixNano())
    19  
    20  	fn := FullNode{}
    21  	for i := range fn.Children {
    22  		flag := rand.Intn(3)
    23  		if flag == 0 {
    24  			continue
    25  		} else if flag == 1 {
    26  			fn.Children[i] = newHashNode()
    27  		} else {
    28  			fn.Children[i] = newValueNode()
    29  		}
    30  	}
    31  	return &fn
    32  }
    33  
    34  func newShortNode() *ShortNode {
    35  	rand.Seed(time.Now().UnixNano())
    36  
    37  	sn := ShortNode{}
    38  	flag := rand.Intn(3)
    39  	if flag == 0 {
    40  		return &sn
    41  	} else if flag == 1 {
    42  		sn.Val = newHashNode()
    43  	} else {
    44  		sn.Val = newValueNode()
    45  	}
    46  	sn.Key = []byte("key")
    47  	return &sn
    48  }
    49  
    50  func newHashNode() HashNode {
    51  	rand.Seed(time.Now().UnixNano())
    52  
    53  	sr := make([]rune, 10)
    54  	for i := range sr {
    55  		sr[i] = letterRunes[rand.Intn(len(letterRunes))]
    56  	}
    57  	seedstr := string(sr)
    58  	return HashNode(common.HexToHash(seedstr).ToBytes())
    59  }
    60  
    61  func newValueNode() ValueNode {
    62  	rand.Seed(time.Now().UnixNano())
    63  
    64  	sr := make([]rune, rand.Intn(50))
    65  	for i := range sr {
    66  		sr[i] = letterRunes[rand.Intn(len(letterRunes))]
    67  	}
    68  	seedstr := string(sr)
    69  	return ValueNode([]byte(seedstr))
    70  }
    71  
    72  func TestMarshalUnmarshalFullNode(t *testing.T) {
    73  	v := FullNode{}
    74  	bts, err := v.MarshalMsg(nil)
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  	left, err := v.UnmarshalMsg(bts)
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  	if len(left) > 0 {
    83  		t.Errorf("%d bytes left over after UnmarshalMsg(): %q", len(left), left)
    84  	}
    85  
    86  	left, err = msgp.Skip(bts)
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  	if len(left) > 0 {
    91  		t.Errorf("%d bytes left over after Skip(): %q", len(left), left)
    92  	}
    93  
    94  	vnew := newFullNode()
    95  	bts, err = vnew.MarshalMsg(nil)
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	}
    99  	var fn FullNode
   100  	left, err = fn.UnmarshalMsg(bts)
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  	if len(left) > 0 {
   105  		t.Errorf("%d bytes left over after UnmarshalMsg(): %q", len(left), left)
   106  	}
   107  
   108  }
   109  
   110  func BenchmarkMarshalMsgFullNode(b *testing.B) {
   111  	v := FullNode{}
   112  	b.ReportAllocs()
   113  	b.ResetTimer()
   114  	for i := 0; i < b.N; i++ {
   115  		v.MarshalMsg(nil)
   116  	}
   117  }
   118  
   119  func BenchmarkAppendMsgFullNode(b *testing.B) {
   120  	v := FullNode{}
   121  	bts := make([]byte, 0, v.Msgsize())
   122  	bts, _ = v.MarshalMsg(bts[0:0])
   123  	b.SetBytes(int64(len(bts)))
   124  	b.ReportAllocs()
   125  	b.ResetTimer()
   126  	for i := 0; i < b.N; i++ {
   127  		bts, _ = v.MarshalMsg(bts[0:0])
   128  	}
   129  }
   130  
   131  func BenchmarkUnmarshalFullNode(b *testing.B) {
   132  	v := FullNode{}
   133  	bts, _ := v.MarshalMsg(nil)
   134  	b.ReportAllocs()
   135  	b.SetBytes(int64(len(bts)))
   136  	b.ResetTimer()
   137  	for i := 0; i < b.N; i++ {
   138  		_, err := v.UnmarshalMsg(bts)
   139  		if err != nil {
   140  			b.Fatal(err)
   141  		}
   142  	}
   143  }
   144  
   145  func TestEncodeDecodeFullNode(t *testing.T) {
   146  	v := FullNode{}
   147  	var buf bytes.Buffer
   148  	msgp.Encode(&buf, &v)
   149  
   150  	m := v.Msgsize()
   151  	if buf.Len() > m {
   152  		t.Logf("WARNING: Msgsize() for %v is inaccurate", v)
   153  	}
   154  
   155  	vn := FullNode{}
   156  	err := msgp.Decode(&buf, &vn)
   157  	if err != nil {
   158  		t.Error(err)
   159  	}
   160  
   161  	buf.Reset()
   162  	msgp.Encode(&buf, &v)
   163  	err = msgp.NewReader(&buf).Skip()
   164  	if err != nil {
   165  		t.Error(err)
   166  	}
   167  }
   168  
   169  func BenchmarkEncodeFullNode(b *testing.B) {
   170  	v := FullNode{}
   171  	var buf bytes.Buffer
   172  	msgp.Encode(&buf, &v)
   173  	b.SetBytes(int64(buf.Len()))
   174  	en := msgp.NewWriter(msgp.Nowhere)
   175  	b.ReportAllocs()
   176  	b.ResetTimer()
   177  	for i := 0; i < b.N; i++ {
   178  		v.EncodeMsg(en)
   179  	}
   180  	en.Flush()
   181  }
   182  
   183  func BenchmarkDecodeFullNode(b *testing.B) {
   184  	v := FullNode{}
   185  	var buf bytes.Buffer
   186  	msgp.Encode(&buf, &v)
   187  	b.SetBytes(int64(buf.Len()))
   188  	rd := msgp.NewEndlessReader(buf.Bytes(), b)
   189  	dc := msgp.NewReader(rd)
   190  	b.ReportAllocs()
   191  	b.ResetTimer()
   192  	for i := 0; i < b.N; i++ {
   193  		err := v.DecodeMsg(dc)
   194  		if err != nil {
   195  			b.Fatal(err)
   196  		}
   197  	}
   198  }
   199  
   200  func TestMarshalUnmarshalShortNode(t *testing.T) {
   201  	v := ShortNode{}
   202  	bts, err := v.MarshalMsg(nil)
   203  	if err != nil {
   204  		t.Fatal(err)
   205  	}
   206  	left, err := v.UnmarshalMsg(bts)
   207  	if err != nil {
   208  		t.Fatal(err)
   209  	}
   210  	if len(left) > 0 {
   211  		t.Errorf("%d bytes left over after UnmarshalMsg(): %q", len(left), left)
   212  	}
   213  
   214  	left, err = msgp.Skip(bts)
   215  	if err != nil {
   216  		t.Fatal(err)
   217  	}
   218  	if len(left) > 0 {
   219  		t.Errorf("%d bytes left over after Skip(): %q", len(left), left)
   220  	}
   221  
   222  	vnew := newShortNode()
   223  	bts, err = vnew.MarshalMsg(nil)
   224  	if err != nil {
   225  		t.Fatal(err)
   226  	}
   227  	var sn ShortNode
   228  	left, err = sn.UnmarshalMsg(bts)
   229  	if err != nil {
   230  		t.Fatal(err)
   231  	}
   232  	if len(left) > 0 {
   233  		t.Errorf("%d bytes left over after UnmarshalMsg(): %q", len(left), left)
   234  	}
   235  	if string(vnew.Key) != string(sn.Key) {
   236  		t.Errorf("vnew and sn not the same, vnew key: %s, sn key: %s", string(vnew.Key), string(sn.Key))
   237  	}
   238  
   239  }
   240  
   241  func BenchmarkMarshalMsgShortNode(b *testing.B) {
   242  	v := ShortNode{}
   243  	b.ReportAllocs()
   244  	b.ResetTimer()
   245  	for i := 0; i < b.N; i++ {
   246  		v.MarshalMsg(nil)
   247  	}
   248  }
   249  
   250  func BenchmarkAppendMsgShortNode(b *testing.B) {
   251  	v := ShortNode{}
   252  	bts := make([]byte, 0, v.Msgsize())
   253  	bts, _ = v.MarshalMsg(bts[0:0])
   254  	b.SetBytes(int64(len(bts)))
   255  	b.ReportAllocs()
   256  	b.ResetTimer()
   257  	for i := 0; i < b.N; i++ {
   258  		bts, _ = v.MarshalMsg(bts[0:0])
   259  	}
   260  }
   261  
   262  func BenchmarkUnmarshalShortNode(b *testing.B) {
   263  	v := ShortNode{}
   264  	bts, _ := v.MarshalMsg(nil)
   265  	b.ReportAllocs()
   266  	b.SetBytes(int64(len(bts)))
   267  	b.ResetTimer()
   268  	for i := 0; i < b.N; i++ {
   269  		_, err := v.UnmarshalMsg(bts)
   270  		if err != nil {
   271  			b.Fatal(err)
   272  		}
   273  	}
   274  }
   275  
   276  func TestEncodeDecodeShortNode(t *testing.T) {
   277  	v := ShortNode{}
   278  	var buf bytes.Buffer
   279  	msgp.Encode(&buf, &v)
   280  
   281  	m := v.Msgsize()
   282  	if buf.Len() > m {
   283  		t.Logf("WARNING: Msgsize() for %v is inaccurate", v)
   284  	}
   285  
   286  	vn := ShortNode{}
   287  	err := msgp.Decode(&buf, &vn)
   288  	if err != nil {
   289  		t.Error(err)
   290  	}
   291  
   292  	buf.Reset()
   293  	msgp.Encode(&buf, &v)
   294  	err = msgp.NewReader(&buf).Skip()
   295  	if err != nil {
   296  		t.Error(err)
   297  	}
   298  }
   299  
   300  func BenchmarkEncodeShortNode(b *testing.B) {
   301  	v := ShortNode{}
   302  	var buf bytes.Buffer
   303  	msgp.Encode(&buf, &v)
   304  	b.SetBytes(int64(buf.Len()))
   305  	en := msgp.NewWriter(msgp.Nowhere)
   306  	b.ReportAllocs()
   307  	b.ResetTimer()
   308  	for i := 0; i < b.N; i++ {
   309  		v.EncodeMsg(en)
   310  	}
   311  	en.Flush()
   312  }
   313  
   314  func BenchmarkDecodeShortNode(b *testing.B) {
   315  	v := ShortNode{}
   316  	var buf bytes.Buffer
   317  	msgp.Encode(&buf, &v)
   318  	b.SetBytes(int64(buf.Len()))
   319  	rd := msgp.NewEndlessReader(buf.Bytes(), b)
   320  	dc := msgp.NewReader(rd)
   321  	b.ReportAllocs()
   322  	b.ResetTimer()
   323  	for i := 0; i < b.N; i++ {
   324  		err := v.DecodeMsg(dc)
   325  		if err != nil {
   326  			b.Fatal(err)
   327  		}
   328  	}
   329  }