Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ludete committed Apr 9, 2020
1 parent b9ae332 commit 4496170
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
5 changes: 5 additions & 0 deletions modules/market/internal/types/order.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/coinexchain/cet-sdk/types"
Expand Down Expand Up @@ -49,15 +51,18 @@ func (or *Order) CalActualOrderCommissionInt64(feeForZeroDeal int64) int64 {

func (or *Order) CalActualOrderFeatureFeeInt64(ctx sdk.Context, freeTimeBlocks int64) int64 {
if or.ExistBlocks <= freeTimeBlocks {
fmt.Println("======")
return 0
}
existTime := ctx.BlockHeight() - or.Height + 1
if existTime < freeTimeBlocks {
fmt.Println("---------")
return 0
}
chargeBlocks := existTime - freeTimeBlocks
fee := sdk.NewDec(chargeBlocks).MulInt64(or.FrozenFeatureFee).QuoInt64(or.ExistBlocks - freeTimeBlocks).TruncateInt64()
if fee > or.FrozenFeatureFee {
fmt.Println("***********")
fee = or.FrozenFeatureFee
}
return fee
Expand Down
56 changes: 56 additions & 0 deletions modules/market/internal/types/order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"math"
"math/rand"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -96,3 +97,58 @@ func TestOrder_CalActualOrderFeatureFeeInt64(t *testing.T) {
fee = order.CalActualOrderFeatureFeeInt64(ctx, 200)
require.EqualValues(t, 0, fee)
}

func TestFuzz_CalActualOrderCommissionInt64(t *testing.T) {
param := DefaultParams()
r := rand.New(rand.NewSource(2))
addr, _ := sdk.AccAddressFromHex("0123456789012345678901234567890123423456")
for i := 0; i < 500; i++ {
or := getRandOrder(r, addr, &param)
or.CalActualOrderCommissionInt64(param.FeeForZeroDeal)
}
}

func TestFuzz_CalActualOrderFeatureFeeInt64(t *testing.T) {
ctx := sdk.Context{}
param := DefaultParams()
r := rand.New(rand.NewSource(2))
addr, _ := sdk.AccAddressFromHex("0123456789012345678901234567890123423456")

for i := 0; i < 500; i++ {
ctx = ctx.WithBlockHeight(r.Int63n(10000))
or := getRandOrder(r, addr, &param)
or.CalActualOrderFeatureFeeInt64(ctx, param.GTEOrderLifetime)
}
}

func getRandOrder(r *rand.Rand, addr sdk.AccAddress, param *Params) *Order {
side := SELL
if r.Int31n(2) == 1 {
side = BUY
}
tif := GTE
if r.Int31n(4)%3 == 0 {
tif = IOC
}
or := Order{
Sender: addr,
Sequence: r.Uint64(),
Identify: 0,
TradingPair: "abc/cet",
OrderType: LimitOrder,
Price: sdk.NewDec(r.Int63n(1000000)),
Quantity: r.Int63n(100000),
Side: byte(side),
TimeInForce: int64(tif),
Height: r.Int63n(10000),
FrozenCommission: r.Int63n(param.FeeForZeroDeal * 1000),
ExistBlocks: r.Int63n(param.GTEOrderLifetime * 100),
FrozenFeatureFee: r.Int63n(param.FeeForZeroDeal * 100),
FrozenFee: r.Int63n(10000),
LeftStock: 0,
Freeze: 0,
DealStock: r.Int63n(10000),
DealMoney: r.Int63n(10000),
}
return &or
}

0 comments on commit 4496170

Please sign in to comment.