bytes_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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) TestIsHex(c *checker.C) {
  31. data1 := "a9e67e"
  32. exp1 := false
  33. res1 := IsHex(data1)
  34. c.Assert(res1, checker.DeepEquals, exp1)
  35. data2 := "0xa9e67e00"
  36. exp2 := true
  37. res2 := IsHex(data2)
  38. c.Assert(res2, checker.DeepEquals, exp2)
  39. }
  40. func (s *BytesSuite) TestLeftPadBytes(c *checker.C) {
  41. val1 := []byte{1, 2, 3, 4}
  42. exp1 := []byte{0, 0, 0, 0, 1, 2, 3, 4}
  43. res1 := LeftPadBytes(val1, 8)
  44. res2 := LeftPadBytes(val1, 2)
  45. c.Assert(res1, checker.DeepEquals, exp1)
  46. c.Assert(res2, checker.DeepEquals, val1)
  47. }
  48. func (s *BytesSuite) TestRightPadBytes(c *checker.C) {
  49. val := []byte{1, 2, 3, 4}
  50. exp := []byte{1, 2, 3, 4, 0, 0, 0, 0}
  51. resstd := RightPadBytes(val, 8)
  52. resshrt := RightPadBytes(val, 2)
  53. c.Assert(resstd, checker.DeepEquals, exp)
  54. c.Assert(resshrt, checker.DeepEquals, val)
  55. }
  56. func TestFromHex(t *testing.T) {
  57. input := "0x01"
  58. expected := []byte{1}
  59. result := FromHex(input)
  60. if !bytes.Equal(expected, result) {
  61. t.Errorf("Expected %x got %x", expected, result)
  62. }
  63. }
  64. func TestFromHexOddLength(t *testing.T) {
  65. input := "0x1"
  66. expected := []byte{1}
  67. result := FromHex(input)
  68. if !bytes.Equal(expected, result) {
  69. t.Errorf("Expected %x got %x", expected, result)
  70. }
  71. }
  72. func TestNoPrefixShortHexOddLength(t *testing.T) {
  73. input := "1"
  74. expected := []byte{1}
  75. result := FromHex(input)
  76. if !bytes.Equal(expected, result) {
  77. t.Errorf("Expected %x got %x", expected, result)
  78. }
  79. }