From f6bce891c4f664418082d960bc0db6f8258e5eb5 Mon Sep 17 00:00:00 2001 From: Adrian Gallagher Date: Fri, 5 Oct 2018 15:21:06 +1000 Subject: [PATCH] Add additional helper functions to pairs package --- config/config.go | 2 +- currency/pair/pair.go | 20 ++++++++++++++++ currency/pair/pair_test.go | 49 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 6bd023d6..a849d82a 100644 --- a/config/config.go +++ b/config/config.go @@ -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), ",") } diff --git a/currency/pair/pair.go b/currency/pair/pair.go index 8eabacac..dd989c95 100644 --- a/currency/pair/pair.go +++ b/currency/pair/pair.go @@ -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)] +} diff --git a/currency/pair/pair_test.go b/currency/pair/pair_test.go index 798865d7..2ae662ff 100644 --- a/currency/pair/pair_test.go +++ b/currency/pair/pair_test.go @@ -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") + } + } +}