Skip to content

Commit f355838

Browse files
committed
Add Equals method for BlockHash with test coverage
1 parent ce60b4e commit f355838

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

kernel/block_hash.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,13 @@ func (bh *BlockHash) Bytes() [32]byte {
4848
func (bh *BlockHash) Copy() *BlockHash {
4949
return newBlockHash((*C.btck_BlockHash)(bh.ptr), false)
5050
}
51+
52+
// Equals checks if two block hashes are equal.
53+
//
54+
// Parameters:
55+
// - other: Block hash to compare against
56+
//
57+
// Returns true if the block hashes are equal.
58+
func (bh *BlockHash) Equals(other *BlockHash) bool {
59+
return C.btck_block_hash_equals((*C.btck_BlockHash)(bh.ptr), (*C.btck_BlockHash)(other.ptr)) != 0
60+
}

kernel/block_hash_test.go

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,40 @@
11
package kernel
22

33
import (
4-
"encoding/hex"
54
"testing"
65
)
76

87
func TestBlockHash(t *testing.T) {
9-
genesisHex := "0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000"
10-
genesisBytes, err := hex.DecodeString(genesisHex)
11-
if err != nil {
12-
t.Fatalf("Failed to decode genesis hex: %v", err)
13-
}
14-
15-
block, err := NewBlock(genesisBytes)
16-
if err != nil {
17-
t.Fatalf("NewBlock() error = %v", err)
18-
}
19-
defer block.Destroy()
8+
hashBytes := [32]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32}
209

21-
hash := block.Hash()
10+
hash := NewBlockHash(hashBytes)
2211
defer hash.Destroy()
2312

24-
hashBytes := hash.Bytes()
25-
26-
newHash := NewBlockHash(hashBytes)
13+
// Test Bytes()
14+
newHash := NewBlockHash(hash.Bytes())
2715
defer newHash.Destroy()
2816

29-
newHashBytes := newHash.Bytes()
30-
31-
if hashBytes != newHashBytes {
32-
t.Errorf("Hash bytes differ: %x != %x", hashBytes, newHashBytes)
17+
if hash.Bytes() != newHash.Bytes() {
18+
t.Errorf("Hash bytes differ: %x != %x", hash.Bytes(), newHash.Bytes())
3319
}
3420

21+
// Test Copy()
3522
copiedHash := hash.Copy()
3623
defer copiedHash.Destroy()
3724

38-
copiedHashBytes := copiedHash.Bytes()
25+
if hash.Bytes() != copiedHash.Bytes() {
26+
t.Errorf("Copied hash bytes differ: %x != %x", hash.Bytes(), copiedHash.Bytes())
27+
}
28+
29+
// Test Equals()
30+
if !hash.Equals(copiedHash) {
31+
t.Errorf("hash.Equals(copiedHash) = false, want true")
32+
}
33+
34+
differentHash := NewBlockHash([32]byte{0xFF})
35+
defer differentHash.Destroy()
3936

40-
if hashBytes != copiedHashBytes {
41-
t.Errorf("Copied hash bytes differ: %x != %x", hashBytes, copiedHashBytes)
37+
if hash.Equals(differentHash) {
38+
t.Errorf("hash.Equals(differentHash) = true, want false")
4239
}
4340
}

0 commit comments

Comments
 (0)