2017-08-20 02:21:36 +02:00
|
|
|
// Copyright (c) 2014-2017 The btcsuite developers
|
2014-03-12 03:33:56 +01:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2017-08-20 02:21:36 +02:00
|
|
|
package blockchain
|
2014-03-12 03:33:56 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2017-08-20 02:21:36 +02:00
|
|
|
// TestBigToCompact ensures BigToCompact converts big integers to the expected
|
|
|
|
// compact representation.
|
2014-03-12 03:33:56 +01:00
|
|
|
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)
|
2017-08-20 02:21:36 +02:00
|
|
|
r := BigToCompact(n)
|
2014-03-12 03:33:56 +01:00
|
|
|
if r != test.out {
|
|
|
|
t.Errorf("TestBigToCompact test #%d failed: got %d want %d\n",
|
|
|
|
x, r, test.out)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-20 02:21:36 +02:00
|
|
|
// TestCompactToBig ensures CompactToBig converts numbers using the compact
|
|
|
|
// representation to the expected big intergers.
|
2014-03-12 03:33:56 +01:00
|
|
|
func TestCompactToBig(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
in uint32
|
|
|
|
out int64
|
|
|
|
}{
|
|
|
|
{10000000, 0},
|
|
|
|
}
|
|
|
|
|
|
|
|
for x, test := range tests {
|
2017-08-20 02:21:36 +02:00
|
|
|
n := CompactToBig(test.in)
|
2014-03-12 03:33:56 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-20 02:21:36 +02:00
|
|
|
// TestCalcWork ensures CalcWork calculates the expected work value from values
|
|
|
|
// in compact representation.
|
2014-03-12 03:33:56 +01:00
|
|
|
func TestCalcWork(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
in uint32
|
|
|
|
out int64
|
|
|
|
}{
|
|
|
|
{10000000, 0},
|
|
|
|
}
|
|
|
|
|
|
|
|
for x, test := range tests {
|
2020-05-13 14:58:39 +02:00
|
|
|
bits := test.in
|
2014-03-12 03:33:56 +01:00
|
|
|
|
2017-08-20 02:21:36 +02:00
|
|
|
r := CalcWork(bits)
|
2014-03-12 03:33:56 +01:00
|
|
|
if r.Int64() != test.out {
|
|
|
|
t.Errorf("TestCalcWork test #%d failed: got %v want %d\n",
|
|
|
|
x, r.Int64(), test.out)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|