github.com/elastos/Elastos.ELA.SideChain.ETH@v0.2.2/common/bytes_test.go (about)

     1  // Copyright 2014 The Elastos.ELA.SideChain.ESC Authors
     2  // This file is part of the Elastos.ELA.SideChain.ESC library.
     3  //
     4  // The Elastos.ELA.SideChain.ESC 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 Elastos.ELA.SideChain.ESC 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 Elastos.ELA.SideChain.ESC 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  }