github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/base/hash_test.go (about)

     1  package base
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/ethereum/go-ethereum/common"
     8  )
     9  
    10  func TestHash_Hex(t *testing.T) {
    11  	hex := "0xF1AA581F353005BA3765B81BF52D6B1C488C2101"
    12  	var hash Hash
    13  	hash.SetHex(hex)
    14  
    15  	expected := "0x000000000000000000000000f1aa581f353005ba3765b81bf52d6b1c488c2101"
    16  	if result := hash.Hex(); result != expected {
    17  		t.Fatal("wrong Hex() return value:", result)
    18  	}
    19  
    20  	zero := "0x000000000000"
    21  	expected = "0x0000000000000000000000000000000000000000000000000000000000000000"
    22  	hash = HexToHash(zero)
    23  	if result := hash.Hex(); result != expected {
    24  		t.Fatal("wrong Hex() return value for zero hash:", result)
    25  	}
    26  
    27  	var zero2 Hash
    28  	hashStr := fmt.Sprint(zero2)
    29  	if hashStr != expected {
    30  		t.Fatal("wrong Hex() return value for zero hash:", hashStr)
    31  	}
    32  }
    33  
    34  func TestHash_Stringer(t *testing.T) {
    35  	hex := "0xF1AA581F353005BA3765B81BF52D6B1C488C2101"
    36  	var hash Hash
    37  	hash.SetHex(hex)
    38  
    39  	expected := "0x000000000000000000000000f1aa581f353005ba3765b81bf52d6b1c488c2101"
    40  	if result := fmt.Sprint(hash); result != expected {
    41  		t.Fatal("wrong Hex() return value:", result)
    42  	}
    43  }
    44  
    45  func TestHash_IsZero(t *testing.T) {
    46  	var zeroValue Hash
    47  	if result := zeroValue.IsZero(); result != true {
    48  		t.Fatal("wrong result for zero value")
    49  	}
    50  
    51  	zeroHash := HexToHash("0x0")
    52  	if result := zeroHash.IsZero(); result != true {
    53  		t.Fatal("wrong result for zero hash")
    54  	}
    55  }
    56  
    57  func TestHexToHash(t *testing.T) {
    58  	hex := "0xF1AA581F353005BA3765B81BF52D6B1C488C2101"
    59  	hash := HexToHash(hex)
    60  
    61  	expected := "0x000000000000000000000000f1aa581f353005ba3765b81bf52d6b1c488c2101"
    62  	if result := hash.Hex(); result != expected {
    63  		t.Fatal("HexToHash: wrong Hex() return value:", result)
    64  	}
    65  }
    66  
    67  func TestHashCompareToCommon(t *testing.T) {
    68  	c := common.HexToHash("0x00000123456789abcde")
    69  	b := HexToHash("0x00000123456789abcde")
    70  	if c != b.Common() {
    71  		t.Fatal("base.Hash.toCommon() does not match")
    72  	}
    73  	if b != new(Hash).SetCommon(&c) {
    74  		t.Fatal("fromCommon(c) does not match Hash")
    75  	}
    76  }