github.com/palcoin-project/palcd@v1.0.0/blockchain/difficulty_test.go (about)

     1  // Copyright (c) 2014-2017 The btcsuite developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package blockchain
     6  
     7  import (
     8  	"math/big"
     9  	"testing"
    10  )
    11  
    12  // TestBigToCompact ensures BigToCompact converts big integers to the expected
    13  // compact representation.
    14  func TestBigToCompact(t *testing.T) {
    15  	tests := []struct {
    16  		in  int64
    17  		out uint32
    18  	}{
    19  		{0, 0},
    20  		{-1, 25231360},
    21  	}
    22  
    23  	for x, test := range tests {
    24  		n := big.NewInt(test.in)
    25  		r := BigToCompact(n)
    26  		if r != test.out {
    27  			t.Errorf("TestBigToCompact test #%d failed: got %d want %d\n",
    28  				x, r, test.out)
    29  			return
    30  		}
    31  	}
    32  }
    33  
    34  // TestCompactToBig ensures CompactToBig converts numbers using the compact
    35  // representation to the expected big intergers.
    36  func TestCompactToBig(t *testing.T) {
    37  	tests := []struct {
    38  		in  uint32
    39  		out int64
    40  	}{
    41  		{10000000, 0},
    42  	}
    43  
    44  	for x, test := range tests {
    45  		n := CompactToBig(test.in)
    46  		want := big.NewInt(test.out)
    47  		if n.Cmp(want) != 0 {
    48  			t.Errorf("TestCompactToBig test #%d failed: got %d want %d\n",
    49  				x, n.Int64(), want.Int64())
    50  			return
    51  		}
    52  	}
    53  }
    54  
    55  // TestCalcWork ensures CalcWork calculates the expected work value from values
    56  // in compact representation.
    57  func TestCalcWork(t *testing.T) {
    58  	tests := []struct {
    59  		in  uint32
    60  		out int64
    61  	}{
    62  		{10000000, 0},
    63  	}
    64  
    65  	for x, test := range tests {
    66  		bits := test.in
    67  
    68  		r := CalcWork(bits)
    69  		if r.Int64() != test.out {
    70  			t.Errorf("TestCalcWork test #%d failed: got %v want %d\n",
    71  				x, r.Int64(), test.out)
    72  			return
    73  		}
    74  	}
    75  }