mirror of
https://github.com/btcsuite/btcd.git
synced 2026-08-13 12:32:51 +02:00
Some of the functions in difficulty.go are not dependent on any external functions and they are needed to introduce testing code for the invalidateblock and reconsiderblock methods that are to be added on in later commits. Having the workmath package let's us reuse the code and avoid dependency cycles. The existing functions that were exported already (HashToBig, CompactToBig, BigToCompact, CalcWork) are still kept in difficulty.go to avoid breaking external code that depends on those exported functions.
75 lines
1.5 KiB
Go
75 lines
1.5 KiB
Go
// Copyright (c) 2014-2017 The btcsuite developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package workmath
|
|
|
|
import (
|
|
"math/big"
|
|
"testing"
|
|
)
|
|
|
|
// TestBigToCompact ensures BigToCompact converts big integers to the expected
|
|
// compact representation.
|
|
func TestBigToCompact(t *testing.T) {
|
|
tests := []struct {
|
|
in int64
|
|
out uint32
|
|
}{
|
|
{0, 0},
|
|
{-1, 25231360},
|
|
}
|
|
|
|
for x, test := range tests {
|
|
n := big.NewInt(test.in)
|
|
r := BigToCompact(n)
|
|
if r != test.out {
|
|
t.Errorf("TestBigToCompact test #%d failed: got %d want %d\n",
|
|
x, r, test.out)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestCompactToBig ensures CompactToBig converts numbers using the compact
|
|
// representation to the expected big integers.
|
|
func TestCompactToBig(t *testing.T) {
|
|
tests := []struct {
|
|
in uint32
|
|
out int64
|
|
}{
|
|
{10000000, 0},
|
|
}
|
|
|
|
for x, test := range tests {
|
|
n := CompactToBig(test.in)
|
|
want := big.NewInt(test.out)
|
|
if n.Cmp(want) != 0 {
|
|
t.Errorf("TestCompactToBig test #%d failed: got %d want %d\n",
|
|
x, n.Int64(), want.Int64())
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestCalcWork ensures CalcWork calculates the expected work value from values
|
|
// in compact representation.
|
|
func TestCalcWork(t *testing.T) {
|
|
tests := []struct {
|
|
in uint32
|
|
out int64
|
|
}{
|
|
{10000000, 0},
|
|
}
|
|
|
|
for x, test := range tests {
|
|
bits := test.in
|
|
|
|
r := CalcWork(bits)
|
|
if r.Int64() != test.out {
|
|
t.Errorf("TestCalcWork test #%d failed: got %v want %d\n",
|
|
x, r.Int64(), test.out)
|
|
return
|
|
}
|
|
}
|
|
}
|