github.com/Elemental-core/elementalcore@v0.0.0-20191206075037-63891242267a/common/bytes_test.go (about)

     1  // Copyright 2014 The elementalcore Authors
     2  // This file is part of the elementalcore library.
     3  //
     4  // The elementalcore 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 elementalcore 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 elementalcore 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) TestIsHex(c *checker.C) {
    38  	data1 := "a9e67e"
    39  	exp1 := false
    40  	res1 := IsHex(data1)
    41  	c.Assert(res1, checker.DeepEquals, exp1)
    42  
    43  	data2 := "0xa9e67e00"
    44  	exp2 := true
    45  	res2 := IsHex(data2)
    46  	c.Assert(res2, checker.DeepEquals, exp2)
    47  
    48  }
    49  
    50  func (s *BytesSuite) TestLeftPadBytes(c *checker.C) {
    51  	val1 := []byte{1, 2, 3, 4}
    52  	exp1 := []byte{0, 0, 0, 0, 1, 2, 3, 4}
    53  
    54  	res1 := LeftPadBytes(val1, 8)
    55  	res2 := LeftPadBytes(val1, 2)
    56  
    57  	c.Assert(res1, checker.DeepEquals, exp1)
    58  	c.Assert(res2, checker.DeepEquals, val1)
    59  }
    60  
    61  func (s *BytesSuite) TestRightPadBytes(c *checker.C) {
    62  	val := []byte{1, 2, 3, 4}
    63  	exp := []byte{1, 2, 3, 4, 0, 0, 0, 0}
    64  
    65  	resstd := RightPadBytes(val, 8)
    66  	resshrt := RightPadBytes(val, 2)
    67  
    68  	c.Assert(resstd, checker.DeepEquals, exp)
    69  	c.Assert(resshrt, checker.DeepEquals, val)
    70  }
    71  
    72  func TestFromHex(t *testing.T) {
    73  	input := "0x01"
    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  }
    80  
    81  func TestFromHexOddLength(t *testing.T) {
    82  	input := "0x1"
    83  	expected := []byte{1}
    84  	result := FromHex(input)
    85  	if !bytes.Equal(expected, result) {
    86  		t.Errorf("Expected % x got % x", expected, result)
    87  	}
    88  }