github.com/klaytn/klaytn@v1.12.1/common/bytes_test.go (about)

     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  
    17  package common
    18  
    19  import (
    20  	"bytes"
    21  	"testing"
    22  
    23  	checker "gopkg.in/check.v1"
    24  )
    25  
    26  type BytesSuite struct{}
    27  
    28  var _ = checker.Suite(&BytesSuite{})
    29  
    30  func (s *BytesSuite) TestCopyBytes(c *checker.C) {
    31  	data1 := []byte{1, 2, 3, 4}
    32  	exp1 := []byte{1, 2, 3, 4}
    33  	res1 := CopyBytes(data1)
    34  	c.Assert(res1, checker.DeepEquals, exp1)
    35  }
    36  
    37  func (s *BytesSuite) TestLeftPadBytes(c *checker.C) {
    38  	val1 := []byte{1, 2, 3, 4}
    39  	exp1 := []byte{0, 0, 0, 0, 1, 2, 3, 4}
    40  
    41  	res1 := LeftPadBytes(val1, 8)
    42  	res2 := LeftPadBytes(val1, 2)
    43  
    44  	c.Assert(res1, checker.DeepEquals, exp1)
    45  	c.Assert(res2, checker.DeepEquals, val1)
    46  }
    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  
    52  	resstd := RightPadBytes(val, 8)
    53  	resshrt := RightPadBytes(val, 2)
    54  
    55  	c.Assert(resstd, checker.DeepEquals, exp)
    56  	c.Assert(resshrt, checker.DeepEquals, val)
    57  }
    58  
    59  func TestFromHex(t *testing.T) {
    60  	input := "0x01"
    61  	expected := []byte{1}
    62  	result := FromHex(input)
    63  	if !bytes.Equal(expected, result) {
    64  		t.Errorf("Expected %x got %x", expected, result)
    65  	}
    66  }
    67  
    68  func TestIsHex(t *testing.T) {
    69  	tests := []struct {
    70  		input string
    71  		ok    bool
    72  	}{
    73  		{"", true},
    74  		{"0", false},
    75  		{"00", true},
    76  		{"a9e67e", true},
    77  		{"A9E67E", true},
    78  		{"0xa9e67e", false},
    79  		{"a9e67e001", false},
    80  		{"0xHELLO_MY_NAME_IS_STEVEN_@#$^&*", false},
    81  	}
    82  	for _, test := range tests {
    83  		if ok := isHex(test.input); ok != test.ok {
    84  			t.Errorf("isHex(%q) = %v, want %v", test.input, ok, test.ok)
    85  		}
    86  	}
    87  }
    88  
    89  func TestFromHexOddLength(t *testing.T) {
    90  	input := "0x1"
    91  	expected := []byte{1}
    92  	result := FromHex(input)
    93  	if !bytes.Equal(expected, result) {
    94  		t.Errorf("Expected %x got %x", expected, result)
    95  	}
    96  }
    97  
    98  func TestNoPrefixShortHexOddLength(t *testing.T) {
    99  	input := "1"
   100  	expected := []byte{1}
   101  	result := FromHex(input)
   102  	if !bytes.Equal(expected, result) {
   103  		t.Errorf("Expected %x got %x", expected, result)
   104  	}
   105  }
   106  
   107  func TestTrimLeftZeroes(t *testing.T) {
   108  	tests := []struct {
   109  		arr []byte
   110  		exp []byte
   111  	}{
   112  		{FromHex("0x0fffff00ff00"), FromHex("0x0fffff00ff00")},
   113  		{FromHex("0x00ffff00ff0000"), FromHex("0xffff00ff0000")},
   114  		{FromHex("0x00000000000000"), []byte{}},
   115  		{FromHex("0xff"), FromHex("0xff")},
   116  		{[]byte{}, []byte{}},
   117  		{FromHex("0xffffffffffff00"), FromHex("0xffffffffffff00")},
   118  	}
   119  	for i, test := range tests {
   120  		got := TrimLeftZeroes(test.arr)
   121  		if !bytes.Equal(got, test.exp) {
   122  			t.Errorf("test %d, got %x exp %x", i, got, test.exp)
   123  		}
   124  	}
   125  }
   126  
   127  func TestTrimRightZeroes(t *testing.T) {
   128  	tests := []struct {
   129  		arr []byte
   130  		exp []byte
   131  	}{
   132  		{FromHex("0x00ffff00ff0f"), FromHex("0x00ffff00ff0f")},
   133  		{FromHex("0x00ffff00ff0000"), FromHex("0x00ffff00ff")},
   134  		{FromHex("0x00000000000000"), []byte{}},
   135  		{FromHex("0xff"), FromHex("0xff")},
   136  		{[]byte{}, []byte{}},
   137  		{FromHex("0x00ffffffffffff"), FromHex("0x00ffffffffffff")},
   138  	}
   139  	for i, test := range tests {
   140  		got := TrimRightZeroes(test.arr)
   141  		if !bytes.Equal(got, test.exp) {
   142  			t.Errorf("test %d, got %x exp %x", i, got, test.exp)
   143  		}
   144  	}
   145  }