decred.org/dcrdex@v1.0.5/dex/market_test.go (about)

     1  // This code is available on the terms of the project LICENSE.md file,
     2  // also available online at https://blueoakcouncil.org/license/1.0.0.
     3  
     4  package dex
     5  
     6  import (
     7  	"os"
     8  	"testing"
     9  )
    10  
    11  const (
    12  	LotSize           = uint64(100_0000_0000) // 100
    13  	RateStep          = uint64(10_0000)       // 0.001
    14  	EpochDuration     = uint64(10_000)
    15  	MarketBuyBuffer   = 1.1
    16  	defaultParcelSize = 1
    17  )
    18  
    19  var (
    20  	AssetDCR uint32
    21  	AssetBTC uint32
    22  )
    23  
    24  func TestMain(m *testing.M) {
    25  	var ok bool
    26  	AssetDCR, ok = BipSymbolID("dcr")
    27  	if !ok {
    28  		os.Exit(1)
    29  	}
    30  	AssetBTC, ok = BipSymbolID("btc")
    31  	if !ok {
    32  		os.Exit(1)
    33  	}
    34  	os.Exit(m.Run())
    35  }
    36  
    37  func TestNewMarketName(t *testing.T) {
    38  	mkt, err := MarketName(AssetDCR, AssetBTC)
    39  	if err != nil {
    40  		t.Fatalf("MarketName(%d,%d) failed: %v", AssetDCR, AssetBTC, err)
    41  	}
    42  	if mkt != "dcr_btc" {
    43  		t.Errorf("Incorrect market name. Got %s, expected %s", mkt, "dcr_btc")
    44  	}
    45  }
    46  
    47  func TestNewMarketInfo(t *testing.T) {
    48  	// ok
    49  	mktInfo, err := NewMarketInfo(AssetDCR, AssetBTC, LotSize, RateStep, EpochDuration, MarketBuyBuffer)
    50  	if err != nil {
    51  		t.Errorf("NewMarketInfoFromSymbols failed: %v", err)
    52  	}
    53  	if mktInfo.Name != "dcr_btc" {
    54  		t.Errorf("market Name incorrect. got %s, expected %s", mktInfo.Name, "dcr_btc")
    55  	}
    56  
    57  	// bad base
    58  	_, err = NewMarketInfo(8765678, AssetBTC, LotSize, RateStep, EpochDuration, MarketBuyBuffer)
    59  	if err == nil {
    60  		t.Errorf("NewMarketInfoFromSymbols succeeded for non-existent base asset")
    61  	}
    62  
    63  	// bad quote
    64  	_, err = NewMarketInfo(AssetDCR, 8765678, LotSize, RateStep, EpochDuration, MarketBuyBuffer)
    65  	if err == nil {
    66  		t.Errorf("NewMarketInfoFromSymbols succeeded for non-existent quote asset")
    67  	}
    68  }
    69  
    70  func TestNewMarketInfoFromSymbols(t *testing.T) {
    71  	// ok
    72  	mktInfo, err := NewMarketInfoFromSymbols("dcr", "btc", LotSize, RateStep, EpochDuration, defaultParcelSize, MarketBuyBuffer)
    73  	if err != nil {
    74  		t.Errorf("NewMarketInfoFromSymbols failed: %v", err)
    75  	}
    76  	if mktInfo.Name != "dcr_btc" {
    77  		t.Errorf("market Name incorrect. got %s, expected %s", mktInfo.Name, "dcr_btc")
    78  	}
    79  
    80  	// bad base
    81  	_, err = NewMarketInfoFromSymbols("super fake asset", "btc", LotSize, RateStep, EpochDuration, defaultParcelSize, MarketBuyBuffer)
    82  	if err == nil {
    83  		t.Errorf("NewMarketInfoFromSymbols succeeded for non-existent base asset")
    84  	}
    85  
    86  	// bad quote
    87  	_, err = NewMarketInfoFromSymbols("dcr", "btc not BTC or bTC", LotSize, RateStep, EpochDuration, defaultParcelSize, MarketBuyBuffer)
    88  	if err == nil {
    89  		t.Errorf("NewMarketInfoFromSymbols succeeded for non-existent quote asset")
    90  	}
    91  }