Add additional helper functions to pairs package

This commit is contained in:
Adrian Gallagher
2018-10-05 15:21:06 +10:00
parent 9dd0474c7c
commit f6bce891c4
3 changed files with 70 additions and 1 deletions

View File

@@ -497,7 +497,7 @@ func (c *Config) CheckPairConsistency(exchName string) error {
}
if len(pairs) == 0 {
exchCfg.EnabledPairs = availPairs[0].Pair().String()
exchCfg.EnabledPairs = pair.RandomPairFromPairs(availPairs).Pair().String()
} else {
exchCfg.EnabledPairs = common.JoinStrings(pair.PairsToStringArray(pairs), ",")
}

View File

@@ -1,6 +1,7 @@
package pair
import (
"math/rand"
"strings"
"github.com/thrasher-/gocryptotrader/common"
@@ -80,6 +81,14 @@ func (c CurrencyPair) Swap() CurrencyPair {
return p
}
// Empty returns whether or not the pair is empty
func (c CurrencyPair) Empty() bool {
if c.FirstCurrency == "" || c.SecondCurrency == "" {
return true
}
return false
}
// NewCurrencyPairDelimiter splits the desired currency string at delimeter,
// the returns a CurrencyPair struct
func NewCurrencyPairDelimiter(currency, delimiter string) CurrencyPair {
@@ -216,3 +225,14 @@ func PairsToStringArray(pairs []CurrencyPair) []string {
}
return p
}
// RandomPairFromPairs returns a random pair from a list of pairs
func RandomPairFromPairs(pairs []CurrencyPair) CurrencyPair {
pairsLen := len(pairs)
if pairsLen == 0 {
return CurrencyPair{}
}
return pairs[rand.Intn(pairsLen)]
}

View File

@@ -154,6 +154,19 @@ func TestSwap(t *testing.T) {
}
}
func TestEmpty(t *testing.T) {
t.Parallel()
pair := NewCurrencyPair("BTC", "USD")
if pair.Empty() {
t.Error("Test failed. Empty() returned true when the pair was initialised")
}
var p CurrencyPair
if !p.Empty() {
t.Error("Test failed. Empty() returned true when the pair wasn't initialised")
}
}
func TestNewCurrencyPair(t *testing.T) {
t.Parallel()
pair := NewCurrencyPair("BTC", "USD")
@@ -370,3 +383,39 @@ func TestPairsToStringArray(t *testing.T) {
t.Error("Test failed. TestPairsToStringArray: Unexpected values")
}
}
func TestRandomPairFromPairs(t *testing.T) {
// Test that an empty pairs array returns an empty currency pair
result := RandomPairFromPairs([]CurrencyPair{})
if !result.Empty() {
t.Error("Test failed. TestRandomPairFromPairs: Unexpected values")
}
// Test that a populated pairs array returns a non-empty currency pair
var pairs []CurrencyPair
pairs = append(pairs, NewCurrencyPair("BTC", "USD"))
result = RandomPairFromPairs(pairs)
if result.Empty() {
t.Error("Test failed. TestRandomPairFromPairs: Unexpected values")
}
// Test that a populated pairs array over a number of attempts returns ALL
// currency pairs
pairs = append(pairs, NewCurrencyPair("ETH", "USD"))
expectedResults := make(map[string]bool)
for i := 0; i < 50; i++ {
p := RandomPairFromPairs(pairs).Pair().String()
_, ok := expectedResults[p]
if !ok {
expectedResults[p] = true
}
}
for x := range pairs {
_, ok := expectedResults[pairs[x].Pair().String()]
if !ok {
t.Error("Test failed. TestRandomPairFromPairs: Unexpected values")
}
}
}