Skip to content

Commit

Permalink
Merge pull request #288 from thanhnguyennguyen/unit-test-settle-balance
Browse files Browse the repository at this point in the history
settleBalance: unit tests
  • Loading branch information
thanhson1085 committed Jun 19, 2020
2 parents f6f9605 + 4da4aa3 commit 5e9e783
Show file tree
Hide file tree
Showing 5 changed files with 1,067 additions and 3 deletions.
6 changes: 4 additions & 2 deletions build/ci.go
Expand Up @@ -221,11 +221,13 @@ func doTest(cmdline []string) {
flag.CommandLine.Parse(cmdline)
env := build.Env()

packages := []string{"./..."}
packages := []string{"./..."} // if a package has no test files, the lines in that package are not added to the total line count
if len(flag.CommandLine.Args()) > 0 {
packages = flag.CommandLine.Args()
} else {
// added all files in all packages (except vendor) to coverage report files count, even there is no test file in the package
packages = build.ExpandPackagesNoVendor(packages)
}
// packages = build.ExpandPackagesNoVendor(packages)

// Run analysis tools before the tests.
// build.MustRun(goTool("vet", packages...))
Expand Down
232 changes: 231 additions & 1 deletion tomox/order_processor_test.go
Expand Up @@ -80,7 +80,237 @@ func Test_getCancelFee(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getCancelFee(tt.args.baseTokenDecimal, tt.args.feeRate, tt.args.order); !reflect.DeepEqual(got, tt.want) {
t.Errorf("getCancelFee() = %v, want %v", got, tt.want)
t.Errorf("getCancelFee() = %v, quantity %v", got, tt.want)
}
})
}
}

func TestGetTradeQuantity(t *testing.T) {
type GetTradeQuantityArg struct {
takerSide string
takerFeeRate *big.Int
takerBalance *big.Int
makerPrice *big.Int
makerFeeRate *big.Int
makerBalance *big.Int
baseTokenDecimal *big.Int
quantityToTrade *big.Int
}
tests := []struct {
name string
args GetTradeQuantityArg
quantity *big.Int
rejectMaker bool
}{
{
"BUY: feeRate = 0, price 1, quantity 1000, taker balance 1000, maker balance 1000",
GetTradeQuantityArg{
takerSide: tradingstate.Bid,
takerFeeRate: common.Big0,
takerBalance: new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
makerPrice: common.BasePrice,
makerFeeRate: common.Big0,
makerBalance: new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
baseTokenDecimal: common.BasePrice,
quantityToTrade: new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
},
new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
false,
},
{
"BUY: feeRate = 0, price 1, quantity 1000, taker balance 1000, maker balance 900 -> reject maker",
GetTradeQuantityArg{
takerSide: tradingstate.Bid,
takerFeeRate: common.Big0,
takerBalance: new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
makerPrice: common.BasePrice,
makerFeeRate: common.Big0,
makerBalance: new(big.Int).Mul(big.NewInt(900), common.BasePrice),
baseTokenDecimal: common.BasePrice,
quantityToTrade: new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
},
new(big.Int).Mul(big.NewInt(900), common.BasePrice),
true,
},
{
"BUY: feeRate = 0, price 1, quantity 1000, taker balance 900, maker balance 1000 -> reject taker",
GetTradeQuantityArg{
takerSide: tradingstate.Bid,
takerFeeRate: common.Big0,
takerBalance: new(big.Int).Mul(big.NewInt(900), common.BasePrice),
makerPrice: common.BasePrice,
makerFeeRate: common.Big0,
makerBalance: new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
baseTokenDecimal: common.BasePrice,
quantityToTrade: new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
},
new(big.Int).Mul(big.NewInt(900), common.BasePrice),
false,
},
{
"BUY: feeRate = 0, price 1, quantity 1000, taker balance 0, maker balance 1000 -> reject taker",
GetTradeQuantityArg{
takerSide: tradingstate.Bid,
takerFeeRate: common.Big0,
takerBalance: common.Big0,
makerPrice: common.BasePrice,
makerFeeRate: common.Big0,
makerBalance: new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
baseTokenDecimal: common.BasePrice,
quantityToTrade: new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
},
common.Big0,
false,
},
{
"BUY: feeRate = 0, price 1, quantity 1000, taker balance 0, maker balance 0 -> reject both taker",
GetTradeQuantityArg{
takerSide: tradingstate.Bid,
takerFeeRate: common.Big0,
takerBalance: common.Big0,
makerPrice: common.BasePrice,
makerFeeRate: common.Big0,
makerBalance: common.Big0,
baseTokenDecimal: common.BasePrice,
quantityToTrade: new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
},
common.Big0,
false,
},
{
"BUY: feeRate = 0, price 1, quantity 1000, taker balance 500, maker balance 100 -> reject both taker, maker",
GetTradeQuantityArg{
takerSide: tradingstate.Bid,
takerFeeRate: common.Big0,
takerBalance: new(big.Int).Mul(big.NewInt(500), common.BasePrice),
makerPrice: common.BasePrice,
makerFeeRate: common.Big0,
makerBalance: new(big.Int).Mul(big.NewInt(100), common.BasePrice),
baseTokenDecimal: common.BasePrice,
quantityToTrade: new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
},
new(big.Int).Mul(big.NewInt(100), common.BasePrice),
true,
},




{
"SELL: feeRate = 0, price 1, quantity 1000, taker balance 1000, maker balance 1000",
GetTradeQuantityArg{
takerSide: tradingstate.Ask,
takerFeeRate: common.Big0,
takerBalance: new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
makerPrice: common.BasePrice,
makerFeeRate: common.Big0,
makerBalance: new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
baseTokenDecimal: common.BasePrice,
quantityToTrade: new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
},
new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
false,
},
{
"SELL: feeRate = 0, price 1, quantity 1000, taker balance 1000, maker balance 900 -> reject maker",
GetTradeQuantityArg{
takerSide: tradingstate.Ask,
takerFeeRate: common.Big0,
takerBalance: new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
makerPrice: common.BasePrice,
makerFeeRate: common.Big0,
makerBalance: new(big.Int).Mul(big.NewInt(900), common.BasePrice),
baseTokenDecimal: common.BasePrice,
quantityToTrade: new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
},
new(big.Int).Mul(big.NewInt(900), common.BasePrice),
true,
},
{
"SELL: feeRate = 0, price 1, quantity 1000, taker balance 900, maker balance 1000 -> reject taker",
GetTradeQuantityArg{
takerSide: tradingstate.Ask,
takerFeeRate: common.Big0,
takerBalance: new(big.Int).Mul(big.NewInt(900), common.BasePrice),
makerPrice: common.BasePrice,
makerFeeRate: common.Big0,
makerBalance: new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
baseTokenDecimal: common.BasePrice,
quantityToTrade: new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
},
new(big.Int).Mul(big.NewInt(900), common.BasePrice),
false,
},
{
"SELL: feeRate = 0, price 1, quantity 1000, taker balance 0, maker balance 1000 -> reject taker",
GetTradeQuantityArg{
takerSide: tradingstate.Ask,
takerFeeRate: common.Big0,
takerBalance: common.Big0,
makerPrice: common.BasePrice,
makerFeeRate: common.Big0,
makerBalance: new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
baseTokenDecimal: common.BasePrice,
quantityToTrade: new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
},
common.Big0,
false,
},
{
"SELL: feeRate = 0, price 1, quantity 1000, taker balance 0, maker balance 0 -> reject maker",
GetTradeQuantityArg{
takerSide: tradingstate.Ask,
takerFeeRate: common.Big0,
takerBalance: common.Big0,
makerPrice: common.BasePrice,
makerFeeRate: common.Big0,
makerBalance: common.Big0,
baseTokenDecimal: common.BasePrice,
quantityToTrade: new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
},
common.Big0,
true,
},
{
"SELL: feeRate = 0, price 1, quantity 1000, taker balance 500, maker balance 100 -> reject both taker, maker",
GetTradeQuantityArg{
takerSide: tradingstate.Ask,
takerFeeRate: common.Big0,
takerBalance: new(big.Int).Mul(big.NewInt(500), common.BasePrice),
makerPrice: common.BasePrice,
makerFeeRate: common.Big0,
makerBalance: new(big.Int).Mul(big.NewInt(100), common.BasePrice),
baseTokenDecimal: common.BasePrice,
quantityToTrade: new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
},
new(big.Int).Mul(big.NewInt(100), common.BasePrice),
true,
},
{
"SELL: feeRate = 0, price 1, quantity 1000, taker balance 0, maker balance 100 -> reject both taker, maker",
GetTradeQuantityArg{
takerSide: tradingstate.Ask,
takerFeeRate: common.Big0,
takerBalance: common.Big0,
makerPrice: common.BasePrice,
makerFeeRate: common.Big0,
makerBalance: new(big.Int).Mul(big.NewInt(100), common.BasePrice),
baseTokenDecimal: common.BasePrice,
quantityToTrade: new(big.Int).Mul(big.NewInt(1000), common.BasePrice),
},
common.Big0,
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1 := GetTradeQuantity(tt.args.takerSide, tt.args.takerFeeRate, tt.args.takerBalance, tt.args.makerPrice, tt.args.makerFeeRate, tt.args.makerBalance, tt.args.baseTokenDecimal, tt.args.quantityToTrade)
if !reflect.DeepEqual(got, tt.quantity) {
t.Errorf("GetTradeQuantity() got = %v, quantity %v", got, tt.quantity)
}
if got1 != tt.rejectMaker {
t.Errorf("GetTradeQuantity() got1 = %v, quantity %v", got1, tt.rejectMaker)
}
})
}
Expand Down

0 comments on commit 5e9e783

Please sign in to comment.