bytes_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. checker "gopkg.in/check.v1"
  21. )
  22. type BytesSuite struct{}
  23. var _ = checker.Suite(&BytesSuite{})
  24. func (s *BytesSuite) TestCopyBytes(c *checker.C) {
  25. data1 := []byte{1, 2, 3, 4}
  26. exp1 := []byte{1, 2, 3, 4}
  27. res1 := CopyBytes(data1)
  28. c.Assert(res1, checker.DeepEquals, exp1)
  29. }
  30. func (s *BytesSuite) TestLeftPadBytes(c *checker.C) {
  31. val1 := []byte{1, 2, 3, 4}
  32. exp1 := []byte{0, 0, 0, 0, 1, 2, 3, 4}
  33. res1 := LeftPadBytes(val1, 8)
  34. res2 := LeftPadBytes(val1, 2)
  35. c.Assert(res1, checker.DeepEquals, exp1)
  36. c.Assert(res2, checker.DeepEquals, val1)
  37. }
  38. func (s *BytesSuite) TestRightPadBytes(c *checker.C) {
  39. val := []byte{1, 2, 3, 4}
  40. exp := []byte{1, 2, 3, 4, 0, 0, 0, 0}
  41. resstd := RightPadBytes(val, 8)
  42. resshrt := RightPadBytes(val, 2)
  43. c.Assert(resstd, checker.DeepEquals, exp)
  44. c.Assert(resshrt, checker.DeepEquals, val)
  45. }
  46. func TestFromHex(t *testing.T) {
  47. input := "0x01"
  48. expected := []byte{1}
  49. result := FromHex(input)
  50. if !bytes.Equal(expected, result) {
  51. t.Errorf("Expected %x got %x", expected, result)
  52. }
  53. }
  54. func TestIsHex(t *testing.T) {
  55. tests := []struct {
  56. input string
  57. ok bool
  58. }{
  59. {"", true},
  60. {"0", false},
  61. {"00", true},
  62. {"a9e67e", true},
  63. {"A9E67E", true},
  64. {"0xa9e67e", false},
  65. {"a9e67e001", false},
  66. {"0xHELLO_MY_NAME_IS_STEVEN_@#$^&*", false},
  67. }
  68. for _, test := range tests {
  69. if ok := isHex(test.input); ok != test.ok {
  70. t.Errorf("isHex(%q) = %v, want %v", test.input, ok, test.ok)
  71. }
  72. }
  73. }
  74. func TestFromHexOddLength(t *testing.T) {
  75. input := "0x1"
  76. expected := []byte{1}
  77. result := FromHex(input)
  78. if !bytes.Equal(expected, result) {
  79. t.Errorf("Expected %x got %x", expected, result)
  80. }
  81. }
  82. func TestNoPrefixShortHexOddLength(t *testing.T) {
  83. input := "1"
  84. expected := []byte{1}
  85. result := FromHex(input)
  86. if !bytes.Equal(expected, result) {
  87. t.Errorf("Expected %x got %x", expected, result)
  88. }
  89. }