gitlab.com/jokerrs1/Sia@v1.3.2/types/target_test.go (about)

     1  package types
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	"github.com/NebulousLabs/Sia/crypto"
     8  )
     9  
    10  // TestTargetAdd probes the Add function of the target type.
    11  func TestTargetAdd(t *testing.T) {
    12  	var target3, target5, target10 Target
    13  	target3[crypto.HashSize-1] = 3
    14  	target5[crypto.HashSize-1] = 5
    15  	target10[crypto.HashSize-1] = 10
    16  
    17  	expect5 := target10.AddDifficulties(target10)
    18  	if expect5 != target5 {
    19  		t.Error("Target.Add not working as expected")
    20  	}
    21  	expect3 := target10.AddDifficulties(target5)
    22  	if expect3 != target3 {
    23  		t.Error("Target.Add not working as expected")
    24  	}
    25  }
    26  
    27  // TestTargetCmp probes the Cmp function of the target type.
    28  func TestTargetCmp(t *testing.T) {
    29  	var target1, target2 Target
    30  	target1[crypto.HashSize-1] = 1
    31  	target2[crypto.HashSize-1] = 2
    32  
    33  	if target1.Cmp(target2) != -1 {
    34  		t.Error("Target.Cmp not behaving as expected")
    35  	}
    36  	if target2.Cmp(target2) != 0 {
    37  		t.Error("Target.Cmp not behaving as expected")
    38  	}
    39  	if target2.Cmp(target1) != 1 {
    40  		t.Error("Target.Cmp not behaving as expected")
    41  	}
    42  }
    43  
    44  // TestTargetDifficulty probes the Difficulty function of the target type.
    45  func TestTargetDifficulty(t *testing.T) {
    46  	var target1, target2, target3 Target
    47  	target2[crypto.HashSize-1] = 1
    48  	target3[crypto.HashSize-1] = 2
    49  
    50  	expDifficulty1 := NewCurrency(RootDepth.Int())
    51  	expDifficulty2 := NewCurrency(RootDepth.Int())
    52  	expDifficulty3 := NewCurrency(RootDepth.Int()).Div(NewCurrency64(2))
    53  
    54  	if difficulty := target1.Difficulty(); difficulty.Cmp(expDifficulty1) != 0 {
    55  		t.Errorf("Expected difficulty %v, got %v", expDifficulty1, difficulty)
    56  	}
    57  	if difficulty := target2.Difficulty(); difficulty.Cmp(expDifficulty2) != 0 {
    58  		t.Errorf("Expected difficulty %v, got %v", expDifficulty2, difficulty)
    59  	}
    60  	if difficulty := target3.Difficulty(); difficulty.Cmp(expDifficulty3) != 0 {
    61  		t.Errorf("Expected difficulty %v, got %v", expDifficulty3, difficulty)
    62  	}
    63  }
    64  
    65  // TestTargetInt probes the Int function of the target type.
    66  func TestTargetInt(t *testing.T) {
    67  	var target Target
    68  	target[crypto.HashSize-1] = 1
    69  
    70  	b := target.Int()
    71  	if b.Cmp(big.NewInt(1)) != 0 {
    72  		t.Error("Target.Int did not work correctly")
    73  	}
    74  }
    75  
    76  // TestTargetIntToTarget probes the IntToTarget function of the target type.
    77  func TestTargetIntToTarget(t *testing.T) {
    78  	var target Target
    79  	target[crypto.HashSize-1] = 5
    80  	b := big.NewInt(5)
    81  	if IntToTarget(b) != target {
    82  		t.Error("IntToTarget not working as expected")
    83  	}
    84  }
    85  
    86  // TestTargetInverse probes the Inverse function of the target type.
    87  func TestTargetInverse(t *testing.T) {
    88  	var target Target
    89  	target[crypto.HashSize-1] = 2
    90  
    91  	r := target.Inverse()
    92  	if r.Num().Cmp(big.NewInt(1)) != 0 {
    93  		t.Error("Target.Rat did not work as expected")
    94  	}
    95  	if r.Denom().Cmp(big.NewInt(2)) != 0 {
    96  		t.Error("Target.Rat did not work as expected")
    97  	}
    98  }
    99  
   100  // TestTargetMul probes the Mul function of the target type.
   101  func TestTargetMul(t *testing.T) {
   102  	var target2, target6, target10, target14, target20 Target
   103  	target2[crypto.HashSize-1] = 2
   104  	target6[crypto.HashSize-1] = 6
   105  	target10[crypto.HashSize-1] = 10
   106  	target14[crypto.HashSize-1] = 14
   107  	target20[crypto.HashSize-1] = 20
   108  
   109  	// Multiplying the difficulty of a target at '10' by 5 will yield a target
   110  	// of '2'. Similar math follows for the remaining checks.
   111  	expect2 := target10.MulDifficulty(big.NewRat(5, 1))
   112  	if expect2 != target2 {
   113  		t.Error(expect2)
   114  		t.Error(target2)
   115  		t.Error("Target.Mul did not work as expected")
   116  	}
   117  	expect6 := target10.MulDifficulty(big.NewRat(3, 2))
   118  	if expect6 != target6 {
   119  		t.Error("Target.Mul did not work as expected")
   120  	}
   121  	expect14 := target10.MulDifficulty(big.NewRat(7, 10))
   122  	if expect14 != target14 {
   123  		t.Error("Target.Mul did not work as expected")
   124  	}
   125  	expect20 := target10.MulDifficulty(big.NewRat(1, 2))
   126  	if expect20 != target20 {
   127  		t.Error("Target.Mul did not work as expected")
   128  	}
   129  }
   130  
   131  // TestTargetRat probes the Rat function of the target type.
   132  func TestTargetRat(t *testing.T) {
   133  	var target Target
   134  	target[crypto.HashSize-1] = 3
   135  
   136  	r := target.Rat()
   137  	if r.Num().Cmp(big.NewInt(3)) != 0 {
   138  		t.Error("Target.Rat did not work as expected")
   139  	}
   140  	if r.Denom().Cmp(big.NewInt(1)) != 0 {
   141  		t.Error("Target.Rat did not work as expected")
   142  	}
   143  }
   144  
   145  // TestTargetOverflow checks that IntToTarget will return a maximum target if
   146  // there is an overflow.
   147  func TestTargetOverflow(t *testing.T) {
   148  	largeInt := new(big.Int).Lsh(big.NewInt(1), 260)
   149  	expectRoot := IntToTarget(largeInt)
   150  	if expectRoot != RootDepth {
   151  		t.Error("IntToTarget does not properly handle overflows")
   152  	}
   153  }
   154  
   155  // TestTargetNegativeIntToTarget tries to create a negative target using
   156  // IntToTarget.
   157  func TestTargetNegativeIntToTarget(t *testing.T) {
   158  	if testing.Short() {
   159  		t.SkipNow()
   160  	}
   161  
   162  	// In debug mode, attempting to create a negative target should trigger a
   163  	// panic.
   164  	defer func() {
   165  		r := recover()
   166  		if r != ErrNegativeTarget {
   167  			t.Error("no panic occurred when trying to create a negative target")
   168  		}
   169  	}()
   170  	b := big.NewInt(-3)
   171  	_ = IntToTarget(b)
   172  }
   173  
   174  // TestTargetNegativeRatToTarget tries to create a negative target using
   175  // RatToTarget.
   176  func TestTargetNegativeRatToTarget(t *testing.T) {
   177  	if testing.Short() {
   178  		t.SkipNow()
   179  	}
   180  
   181  	// In debug mode, attempting to create a negative target should trigger a
   182  	// panic.
   183  	defer func() {
   184  		r := recover()
   185  		if r != ErrNegativeTarget {
   186  			t.Error("no panic occurred when trying to create a negative target")
   187  		}
   188  	}()
   189  	r := big.NewRat(3, -5)
   190  	_ = RatToTarget(r)
   191  }