github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/common/types_test.go (about)

     1  // Copyright 2015 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  	//"math/big"
    21  	"testing"
    22  )
    23  
    24  func TestBytesConversion(t *testing.T) {
    25  	bytes := []byte{5}
    26  	hash := BytesToHash(bytes)
    27  
    28  	var exp Hash
    29  	exp[31] = 5
    30  
    31  	if hash != exp {
    32  		t.Errorf("expected %x got %x", exp, hash)
    33  	}
    34  }
    35  
    36  func TestHashJsonValidation(t *testing.T) {
    37  	var h Hash
    38  	var tests = []struct {
    39  		Prefix string
    40  		Size   int
    41  		Error  error
    42  	}{
    43  		{"", 2, hashJsonLengthErr},
    44  		{"", 62, hashJsonLengthErr},
    45  		{"", 66, hashJsonLengthErr},
    46  		{"", 65, hashJsonLengthErr},
    47  		{"0X", 64, nil},
    48  		{"0x", 64, nil},
    49  		{"0x", 62, hashJsonLengthErr},
    50  	}
    51  	for i, test := range tests {
    52  		if err := h.UnmarshalJSON(append([]byte(test.Prefix), make([]byte, test.Size)...)); err != test.Error {
    53  			t.Errorf("test #%d: error mismatch: have %v, want %v", i, err, test.Error)
    54  		}
    55  	}
    56  }
    57  
    58  /*
    59  func TestAddressUnmarshalJSON(t *testing.T) {
    60  	var a Address
    61  	var tests = []struct {
    62  		Input     string
    63  		ShouldErr bool
    64  		Output    *big.Int
    65  	}{
    66  		{"", true, nil},
    67  		{`""`, true, nil},
    68  		{`"0x"`, true, nil},
    69  		{`"0x00"`, true, nil},
    70  		{`"0xG000000000000000000000000000000000000000"`, true, nil},
    71  		{`"0x0000000000000000000000000000000000000000"`, false, big.NewInt(0)},
    72  		{`"0x0000000000000000000000000000000000000010"`, false, big.NewInt(16)},
    73  	}
    74  	for i, test := range tests {
    75  		err := a.UnmarshalJSON([]byte(test.Input))
    76  		if err != nil && !test.ShouldErr {
    77  			t.Errorf("test #%d: unexpected error: %v", i, err)
    78  		}
    79  		if err == nil {
    80  			if test.ShouldErr {
    81  				t.Errorf("test #%d: expected error, got none", i)
    82  			}
    83  			if a.Big().Cmp(test.Output) != 0 {
    84  				t.Errorf("test #%d: address mismatch: have %v, want %v", i, a.Big(), test.Output)
    85  			}
    86  		}
    87  	}
    88  }*/