mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-10-14 00:59:33 +00:00
[DEV-330] dagconfig coverage (#142)
* [DEV-314] Added tests for DisasmPC and DisasmScript * [DEV-314] Re-wrote TestCheckErrorCondition to cover the whole method * [DEV-314] Fixed error message * [DEV-330] Covered all methods in daghash with tests * [DEV-330] Added tests for NewHashFromString * [DEV-330] Added test case for TestNewHashFromStr that is not the default daghash.Hash value
This commit is contained in:
parent
c0aafdf7e1
commit
53e8e5a10a
@ -7,6 +7,7 @@ package daghash
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"math/big"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@ -271,3 +272,155 @@ func TestAreEqual(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHashToBig(t *testing.T) {
|
||||||
|
hash0, _ := NewHashFromStr("0000000000000000000000000000000000000000000000000000000000000000")
|
||||||
|
big0 := big.NewInt(0)
|
||||||
|
hash1, _ := NewHashFromStr("1111111111111111111111111111111111111111111111111111111111111111")
|
||||||
|
big1 := big.NewInt(0)
|
||||||
|
big1.SetString("1111111111111111111111111111111111111111111111111111111111111111", 16)
|
||||||
|
hash2, _ := NewHashFromStr("2222222222222222222222222222222222222222222222222222222222222222")
|
||||||
|
big2 := big.NewInt(0)
|
||||||
|
big2.SetString("2222222222222222222222222222222222222222222222222222222222222222", 16)
|
||||||
|
hash3, _ := NewHashFromStr("3333333333333333333333333333333333333333333333333333333333333333")
|
||||||
|
big3 := big.NewInt(0)
|
||||||
|
big3.SetString("3333333333333333333333333333333333333333333333333333333333333333", 16)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
hash *Hash
|
||||||
|
expected *big.Int
|
||||||
|
}{
|
||||||
|
{hash0, big0},
|
||||||
|
{hash1, big1},
|
||||||
|
{hash2, big2},
|
||||||
|
{hash3, big3},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
result := HashToBig(test.hash)
|
||||||
|
|
||||||
|
if result.Cmp(test.expected) != 0 {
|
||||||
|
t.Errorf("unexpected HashToBig result for"+
|
||||||
|
" test \"%s\". Expected: %s, got: %s.", test.hash, test.expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHashCmp(t *testing.T) {
|
||||||
|
hash0, _ := NewHashFromStr("0000000000000000000000000000000000000000000000000000000000000000")
|
||||||
|
hash1, _ := NewHashFromStr("1111111111111111111111111111111111111111111111111111111111111111")
|
||||||
|
hash2, _ := NewHashFromStr("2222222222222222222222222222222222222222222222222222222222222222")
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
first *Hash
|
||||||
|
second *Hash
|
||||||
|
expected int
|
||||||
|
}{
|
||||||
|
{"equal 0", hash0, hash0, 0},
|
||||||
|
{"equal 2", hash2, hash2, 0},
|
||||||
|
{"1 vs 0", hash1, hash0, 1},
|
||||||
|
{"0 vs 1", hash0, hash1, -1},
|
||||||
|
{"2 vs 1", hash2, hash1, 1},
|
||||||
|
{"2 vs 0", hash2, hash0, 1},
|
||||||
|
{"0 vs 2", hash0, hash2, -1},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
result := test.first.Cmp(test.second)
|
||||||
|
|
||||||
|
if result != test.expected {
|
||||||
|
t.Errorf("unexpected Hash.Cmp result for"+
|
||||||
|
" test \"%s\". Expected: %d, got: %d.", test.name, test.expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHashLess(t *testing.T) {
|
||||||
|
hash0, _ := NewHashFromStr("0000000000000000000000000000000000000000000000000000000000000000")
|
||||||
|
hash1, _ := NewHashFromStr("1111111111111111111111111111111111111111111111111111111111111111")
|
||||||
|
hash2, _ := NewHashFromStr("2222222222222222222222222222222222222222222222222222222222222222")
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
first *Hash
|
||||||
|
second *Hash
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
{"equal 0", hash0, hash0, false},
|
||||||
|
{"equal 2", hash2, hash2, false},
|
||||||
|
{"1 vs 0", hash1, hash0, false},
|
||||||
|
{"0 vs 1", hash0, hash1, true},
|
||||||
|
{"2 vs 1", hash2, hash1, false},
|
||||||
|
{"2 vs 0", hash2, hash0, false},
|
||||||
|
{"0 vs 2", hash0, hash2, true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
result := Less(test.first, test.second)
|
||||||
|
|
||||||
|
if result != test.expected {
|
||||||
|
t.Errorf("unexpected Hash.Less result for"+
|
||||||
|
" test \"%s\". Expected: %t, got: %t.", test.name, test.expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestJoinHashesStrings(t *testing.T) {
|
||||||
|
hash0, _ := NewHashFromStr("0000000000000000000000000000000000000000000000000000000000000000")
|
||||||
|
hash1, _ := NewHashFromStr("1111111111111111111111111111111111111111111111111111111111111111")
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
hashes []Hash
|
||||||
|
separator string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{"no separator", []Hash{*hash0, *hash1}, "",
|
||||||
|
"00000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111"},
|
||||||
|
{", separator", []Hash{*hash0, *hash1}, ",",
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000000,1111111111111111111111111111111111111111111111111111111111111111"},
|
||||||
|
{"blabla separator", []Hash{*hash0, *hash1}, "blabla",
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000000blabla1111111111111111111111111111111111111111111111111111111111111111"},
|
||||||
|
{"1 hash", []Hash{*hash0}, ",", "0000000000000000000000000000000000000000000000000000000000000000"},
|
||||||
|
{"0 hashes", []Hash{}, ",", ""},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
result := JoinHashesStrings(test.hashes, test.separator)
|
||||||
|
|
||||||
|
if result != test.expected {
|
||||||
|
t.Errorf("unexpected JoinHashesStrings result for"+
|
||||||
|
" test \"%s\". Expected: %s, got: %s.", test.name, test.expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSort(t *testing.T) {
|
||||||
|
hash0, _ := NewHashFromStr("0000000000000000000000000000000000000000000000000000000000000000")
|
||||||
|
hash1, _ := NewHashFromStr("1111111111111111111111111111111111111111111111111111111111111111")
|
||||||
|
hash2, _ := NewHashFromStr("2222222222222222222222222222222222222222222222222222222222222222")
|
||||||
|
hash3, _ := NewHashFromStr("3333333333333333333333333333333333333333333333333333333333333333")
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
hashes []Hash
|
||||||
|
expected []Hash
|
||||||
|
}{
|
||||||
|
{"empty", []Hash{}, []Hash{}},
|
||||||
|
{"single item", []Hash{*hash0}, []Hash{*hash0}},
|
||||||
|
{"already sorted", []Hash{*hash0, *hash1, *hash2, *hash3}, []Hash{*hash0, *hash1, *hash2, *hash3}},
|
||||||
|
{"inverted", []Hash{*hash3, *hash2, *hash1, *hash0}, []Hash{*hash0, *hash1, *hash2, *hash3}},
|
||||||
|
{"shuffled", []Hash{*hash2, *hash3, *hash0, *hash1}, []Hash{*hash0, *hash1, *hash2, *hash3}},
|
||||||
|
{"with duplicates", []Hash{*hash2, *hash3, *hash0, *hash1, *hash1}, []Hash{*hash0, *hash1, *hash1, *hash2, *hash3}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
Sort(test.hashes)
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(test.hashes, test.expected) {
|
||||||
|
t.Errorf("unexpected Sort result for"+
|
||||||
|
" test \"%s\". Expected: %v, got: %v.", test.name, test.expected, test.hashes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -4,17 +4,41 @@
|
|||||||
|
|
||||||
package dagconfig
|
package dagconfig
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
// TestInvalidHashStr ensures the newShaHashFromStr function panics when used to
|
"github.com/daglabs/btcd/dagconfig/daghash"
|
||||||
// with an invalid hash string.
|
)
|
||||||
func TestInvalidHashStr(t *testing.T) {
|
|
||||||
defer func() {
|
func TestNewHashFromStr(t *testing.T) {
|
||||||
if r := recover(); r == nil {
|
tests := []struct {
|
||||||
t.Errorf("Expected panic for invalid hash, got nil")
|
hexStr string
|
||||||
}
|
expectedHash *daghash.Hash
|
||||||
}()
|
expectedPanic bool
|
||||||
newHashFromStr("banana")
|
}{
|
||||||
|
{"banana", nil, true},
|
||||||
|
{"0000000000000000000000000000000000000000000000000000000000000000",
|
||||||
|
&daghash.Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
|
||||||
|
{"0101010101010101010101010101010101010101010101010101010101010101",
|
||||||
|
&daghash.Hash{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
func() {
|
||||||
|
defer func() {
|
||||||
|
err := recover()
|
||||||
|
if (err != nil) != test.expectedPanic {
|
||||||
|
t.Errorf("%s: Expected panic: %t for invalid hash, got %t", test.hexStr, test.expectedPanic, err != nil)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
result := newHashFromStr(test.hexStr)
|
||||||
|
|
||||||
|
if result.Cmp(test.expectedHash) != 0 {
|
||||||
|
t.Errorf("%s: Expected hash: %s, but got %s", test.hexStr, test.expectedHash, result)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestMustRegisterPanic ensures the mustRegister function panics when used to
|
// TestMustRegisterPanic ensures the mustRegister function panics when used to
|
||||||
|
Loading…
x
Reference in New Issue
Block a user