Skip to content

Commit

Permalink
refactor: use slices.Equal to simplify code (#23384)
Browse files Browse the repository at this point in the history
Signed-off-by: fudancoder <[email protected].>
  • Loading branch information
fudancoder authored Jan 14, 2025
1 parent 80552a6 commit 64e8280
Showing 1 changed file with 4 additions and 15 deletions.
19 changes: 4 additions & 15 deletions core/header/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package header

import (
"crypto/sha256"
"slices"
"testing"
"time"
)
Expand Down Expand Up @@ -29,7 +30,7 @@ func TestInfo_Bytes(t *testing.T) {
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !bytesEqual(expectedBytes, bytes) {
if !slices.Equal(expectedBytes, bytes) {
t.Errorf("expected bytes %v, got %v", expectedBytes, bytes)
}
}
Expand All @@ -54,28 +55,16 @@ func TestInfo_FromBytes(t *testing.T) {
if info.Height != 12345 {
t.Errorf("expected Height %d, got %d", 12345, info.Height)
}
if !bytesEqual([]byte{0x26, 0xb0, 0xb8, 0x3e, 0x72, 0x81, 0xbe, 0x3b, 0x11, 0x76, 0x58, 0xb6, 0xf2, 0x63, 0x6d, 0x3, 0x68, 0xca, 0xd3, 0xd7, 0x4f, 0x22, 0x24, 0x34, 0x28, 0xf5, 0x40, 0x1a, 0x4b, 0x70, 0x89, 0x7e}, info.Hash) {
if !slices.Equal([]byte{0x26, 0xb0, 0xb8, 0x3e, 0x72, 0x81, 0xbe, 0x3b, 0x11, 0x76, 0x58, 0xb6, 0xf2, 0x63, 0x6d, 0x3, 0x68, 0xca, 0xd3, 0xd7, 0x4f, 0x22, 0x24, 0x34, 0x28, 0xf5, 0x40, 0x1a, 0x4b, 0x70, 0x89, 0x7e}, info.Hash) {
t.Errorf("expected Hash %v, got %v", []byte{0x26, 0xb0, 0xb8, 0x3e, 0x72, 0x81, 0xbe, 0x3b, 0x11, 0x76, 0x58, 0xb6, 0xf2, 0x63, 0x6d, 0x3, 0x68, 0xca, 0xd3, 0xd7, 0x4f, 0x22, 0x24, 0x34, 0x28, 0xf5, 0x40, 0x1a, 0x4b, 0x70, 0x89, 0x7e}, info.Hash)
}
if info.Time != time.Date(2024, time.January, 1, 0, 0, 0, 0, time.UTC) {
t.Errorf("expected Time %v, got %v", time.Date(2024, time.January, 1, 0, 0, 0, 0, time.UTC), info.Time)
}
if !bytesEqual([]byte{0x26, 0xb0, 0xb8, 0x3e, 0x72, 0x81, 0xbe, 0x3b, 0x11, 0x76, 0x58, 0xb6, 0xf2, 0x63, 0x6d, 0x3, 0x68, 0xca, 0xd3, 0xd7, 0x4f, 0x22, 0x24, 0x34, 0x28, 0xf5, 0x40, 0x1a, 0x4b, 0x70, 0x89, 0x7e}, info.AppHash) {
if !slices.Equal([]byte{0x26, 0xb0, 0xb8, 0x3e, 0x72, 0x81, 0xbe, 0x3b, 0x11, 0x76, 0x58, 0xb6, 0xf2, 0x63, 0x6d, 0x3, 0x68, 0xca, 0xd3, 0xd7, 0x4f, 0x22, 0x24, 0x34, 0x28, 0xf5, 0x40, 0x1a, 0x4b, 0x70, 0x89, 0x7e}, info.AppHash) {
t.Errorf("expected AppHash %v, got %v", []byte{0x26, 0xb0, 0xb8, 0x3e, 0x72, 0x81, 0xbe, 0x3b, 0x11, 0x76, 0x58, 0xb6, 0xf2, 0x63, 0x6d, 0x3, 0x68, 0xca, 0xd3, 0xd7, 0x4f, 0x22, 0x24, 0x34, 0x28, 0xf5, 0x40, 0x1a, 0x4b, 0x70, 0x89, 0x7e}, info.AppHash)
}
if info.ChainID != "test-chain" {
t.Errorf("expected ChainID %s, got %s", "test-chain", info.ChainID)
}
}

func bytesEqual(a, b []byte) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}

0 comments on commit 64e8280

Please sign in to comment.