bytes_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2014 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 common
  17. import (
  18. "bytes"
  19. "testing"
  20. )
  21. func TestCopyBytes(t *testing.T) {
  22. input := []byte{1, 2, 3, 4}
  23. v := CopyBytes(input)
  24. if !bytes.Equal(v, []byte{1, 2, 3, 4}) {
  25. t.Fatal("not equal after copy")
  26. }
  27. v[0] = 99
  28. if bytes.Equal(v, input) {
  29. t.Fatal("result is not a copy")
  30. }
  31. }
  32. func TestLeftPadBytes(t *testing.T) {
  33. val := []byte{1, 2, 3, 4}
  34. padded := []byte{0, 0, 0, 0, 1, 2, 3, 4}
  35. if r := LeftPadBytes(val, 8); !bytes.Equal(r, padded) {
  36. t.Fatalf("LeftPadBytes(%v, 8) == %v", val, r)
  37. }
  38. if r := LeftPadBytes(val, 2); !bytes.Equal(r, val) {
  39. t.Fatalf("LeftPadBytes(%v, 2) == %v", val, r)
  40. }
  41. }
  42. func TestRightPadBytes(t *testing.T) {
  43. val := []byte{1, 2, 3, 4}
  44. padded := []byte{1, 2, 3, 4, 0, 0, 0, 0}
  45. if r := RightPadBytes(val, 8); !bytes.Equal(r, padded) {
  46. t.Fatalf("RightPadBytes(%v, 8) == %v", val, r)
  47. }
  48. if r := RightPadBytes(val, 2); !bytes.Equal(r, val) {
  49. t.Fatalf("RightPadBytes(%v, 2) == %v", val, r)
  50. }
  51. }
  52. func TestFromHex(t *testing.T) {
  53. input := "0x01"
  54. expected := []byte{1}
  55. result := FromHex(input)
  56. if !bytes.Equal(expected, result) {
  57. t.Errorf("Expected %x got %x", expected, result)
  58. }
  59. }
  60. func TestIsHex(t *testing.T) {
  61. tests := []struct {
  62. input string
  63. ok bool
  64. }{
  65. {"", true},
  66. {"0", false},
  67. {"00", true},
  68. {"a9e67e", true},
  69. {"A9E67E", true},
  70. {"0xa9e67e", false},
  71. {"a9e67e001", false},
  72. {"0xHELLO_MY_NAME_IS_STEVEN_@#$^&*", false},
  73. }
  74. for _, test := range tests {
  75. if ok := isHex(test.input); ok != test.ok {
  76. t.Errorf("isHex(%q) = %v, want %v", test.input, ok, test.ok)
  77. }
  78. }
  79. }
  80. func TestFromHexOddLength(t *testing.T) {
  81. input := "0x1"
  82. expected := []byte{1}
  83. result := FromHex(input)
  84. if !bytes.Equal(expected, result) {
  85. t.Errorf("Expected %x got %x", expected, result)
  86. }
  87. }
  88. func TestNoPrefixShortHexOddLength(t *testing.T) {
  89. input := "1"
  90. expected := []byte{1}
  91. result := FromHex(input)
  92. if !bytes.Equal(expected, result) {
  93. t.Errorf("Expected %x got %x", expected, result)
  94. }
  95. }
  96. func TestTrimRightZeroes(t *testing.T) {
  97. tests := []struct {
  98. arr []byte
  99. exp []byte
  100. }{
  101. {FromHex("0x00ffff00ff0000"), FromHex("0x00ffff00ff")},
  102. {FromHex("0x00000000000000"), []byte{}},
  103. {FromHex("0xff"), FromHex("0xff")},
  104. {[]byte{}, []byte{}},
  105. {FromHex("0x00ffffffffffff"), FromHex("0x00ffffffffffff")},
  106. }
  107. for i, test := range tests {
  108. got := TrimRightZeroes(test.arr)
  109. if !bytes.Equal(got, test.exp) {
  110. t.Errorf("test %d, got %x exp %x", i, got, test.exp)
  111. }
  112. }
  113. }