state_object_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. package state
  17. import (
  18. "bytes"
  19. "fmt"
  20. "math/rand"
  21. "testing"
  22. "time"
  23. "github.com/ethereum/go-ethereum/common"
  24. )
  25. func BenchmarkCutOriginal(b *testing.B) {
  26. value := common.HexToHash("0x01")
  27. for i := 0; i < b.N; i++ {
  28. bytes.TrimLeft(value[:], "\x00")
  29. }
  30. }
  31. func BenchmarkCutsetterFn(b *testing.B) {
  32. value := common.HexToHash("0x01")
  33. cutSetFn := func(r rune) bool {
  34. return int32(r) == int32(0)
  35. }
  36. for i := 0; i < b.N; i++ {
  37. bytes.TrimLeftFunc(value[:], cutSetFn)
  38. }
  39. }
  40. func BenchmarkCutCustomTrim(b *testing.B) {
  41. value := common.HexToHash("0x01")
  42. for i := 0; i < b.N; i++ {
  43. common.TrimLeftZeroes(value[:])
  44. }
  45. }
  46. func xTestFuzzCutter(t *testing.T) {
  47. rand.Seed(time.Now().Unix())
  48. for {
  49. v := make([]byte, 20)
  50. zeroes := rand.Intn(21)
  51. rand.Read(v[zeroes:])
  52. exp := bytes.TrimLeft(v[:], "\x00")
  53. got := common.TrimLeftZeroes(v)
  54. if !bytes.Equal(exp, got) {
  55. fmt.Printf("Input %x\n", v)
  56. fmt.Printf("Exp %x\n", exp)
  57. fmt.Printf("Got %x\n", got)
  58. t.Fatalf("Error")
  59. }
  60. //break
  61. }
  62. }