|
1 | 1 | package kernel |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "encoding/hex" |
5 | 4 | "testing" |
6 | 5 | ) |
7 | 6 |
|
8 | 7 | 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} |
20 | 9 |
|
21 | | - hash := block.Hash() |
| 10 | + hash := NewBlockHash(hashBytes) |
22 | 11 | defer hash.Destroy() |
23 | 12 |
|
24 | | - hashBytes := hash.Bytes() |
25 | | - |
26 | | - newHash := NewBlockHash(hashBytes) |
| 13 | + // Test Bytes() |
| 14 | + newHash := NewBlockHash(hash.Bytes()) |
27 | 15 | defer newHash.Destroy() |
28 | 16 |
|
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()) |
33 | 19 | } |
34 | 20 |
|
| 21 | + // Test Copy() |
35 | 22 | copiedHash := hash.Copy() |
36 | 23 | defer copiedHash.Destroy() |
37 | 24 |
|
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() |
39 | 36 |
|
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") |
42 | 39 | } |
43 | 40 | } |
0 commit comments