github.com/dogestan/eth@v1.9.7/core/state/state_object_test.go (about)

     1  // Copyright 2019 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package state
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"math/rand"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/ethereum/go-ethereum/common"
    27  )
    28  
    29  func BenchmarkCutOriginal(b *testing.B) {
    30  	value := common.HexToHash("0x01")
    31  	for i := 0; i < b.N; i++ {
    32  		bytes.TrimLeft(value[:], "\x00")
    33  	}
    34  }
    35  
    36  func BenchmarkCutsetterFn(b *testing.B) {
    37  	value := common.HexToHash("0x01")
    38  	cutSetFn := func(r rune) bool {
    39  		return int32(r) == int32(0)
    40  	}
    41  	for i := 0; i < b.N; i++ {
    42  		bytes.TrimLeftFunc(value[:], cutSetFn)
    43  	}
    44  }
    45  
    46  func BenchmarkCutCustomTrim(b *testing.B) {
    47  	value := common.HexToHash("0x01")
    48  	for i := 0; i < b.N; i++ {
    49  		common.TrimLeftZeroes(value[:])
    50  	}
    51  }
    52  
    53  func xTestFuzzCutter(t *testing.T) {
    54  	rand.Seed(time.Now().Unix())
    55  	for {
    56  		v := make([]byte, 20)
    57  		zeroes := rand.Intn(21)
    58  		rand.Read(v[zeroes:])
    59  		exp := bytes.TrimLeft(v[:], "\x00")
    60  		got := common.TrimLeftZeroes(v)
    61  		if !bytes.Equal(exp, got) {
    62  
    63  			fmt.Printf("Input %x\n", v)
    64  			fmt.Printf("Exp %x\n", exp)
    65  			fmt.Printf("Got %x\n", got)
    66  			t.Fatalf("Error")
    67  		}
    68  		//break
    69  	}
    70  }