github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/core/state/state_object_test.go (about)

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