github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/renter/hostdb/hostweight_test.go (about)

     1  package hostdb
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/NebulousLabs/Sia/types"
     7  )
     8  
     9  func calculateWeightFromUInt64Price(price uint64) (weight types.Currency) {
    10  	var entry hostEntry
    11  	entry.ContractPrice = types.NewCurrency64(price)
    12  	return calculateHostWeight(entry)
    13  }
    14  
    15  func TestHostWeightDistinctPrices(t *testing.T) {
    16  	// Create two identical entries, except that one has a price that is 2x the
    17  	// other. The weight returned by hostWeight should be 1/8 for the more
    18  	// expensive host.
    19  	weight1 := calculateWeightFromUInt64Price(3)
    20  	weight2 := calculateWeightFromUInt64Price(6)
    21  	expectedWeight := weight1.Div64(32)
    22  	if weight2.Cmp(expectedWeight) != 0 {
    23  		t.Error("Weight of expensive host is not the correct value.")
    24  	}
    25  }
    26  
    27  func TestHostWeightIdenticalPrices(t *testing.T) {
    28  	weight1 := calculateWeightFromUInt64Price(42)
    29  	weight2 := calculateWeightFromUInt64Price(42)
    30  	if weight1.Cmp(weight2) != 0 {
    31  		t.Error("Weight of identically priced hosts should be equal.")
    32  	}
    33  }
    34  
    35  func TestHostWeightWithOnePricedZero(t *testing.T) {
    36  	weight1 := calculateWeightFromUInt64Price(5)
    37  	weight2 := calculateWeightFromUInt64Price(0)
    38  	if weight1.Cmp(weight2) >= 0 {
    39  		t.Error("Zero-priced host should have higher weight than nonzero-priced host.")
    40  	}
    41  }
    42  
    43  func TestHostWeightWithBothPricesZero(t *testing.T) {
    44  	weight1 := calculateWeightFromUInt64Price(0)
    45  	weight2 := calculateWeightFromUInt64Price(0)
    46  	if weight1.Cmp(weight2) != 0 {
    47  		t.Error("Weight of two zero-priced hosts should be equal.")
    48  	}
    49  }