grid2: improve number func

This commit is contained in:
c9s 2022-11-08 20:23:25 +08:00
parent f46fc7ee80
commit 4fb2230e5d
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -9,19 +9,25 @@ import (
) )
func number(a interface{}) fixedpoint.Value { func number(a interface{}) fixedpoint.Value {
if s, ok := a.(string); ok { switch v := a.(type) {
return fixedpoint.MustNewFromString(s) case string:
return fixedpoint.MustNewFromString(v)
case int:
return fixedpoint.NewFromInt(int64(v))
case int64:
return fixedpoint.NewFromInt(int64(v))
case float64:
return fixedpoint.NewFromFloat(v)
} }
f := a.(float64) return fixedpoint.Zero
return fixedpoint.NewFromFloat(f)
} }
func TestNewGrid(t *testing.T) { func TestNewGrid(t *testing.T) {
upper := fixedpoint.NewFromFloat(500.0) upper := fixedpoint.NewFromFloat(500.0)
lower := fixedpoint.NewFromFloat(100.0) lower := fixedpoint.NewFromFloat(100.0)
size := fixedpoint.NewFromFloat(100.0) size := fixedpoint.NewFromFloat(100.0)
grid := NewGrid(lower, upper, size, number(2.0)) grid := NewGrid(lower, upper, size, number(0.01))
assert.Equal(t, upper, grid.UpperPrice) assert.Equal(t, upper, grid.UpperPrice)
assert.Equal(t, lower, grid.LowerPrice) assert.Equal(t, lower, grid.LowerPrice)
assert.Equal(t, fixedpoint.NewFromFloat(4), grid.Spread) assert.Equal(t, fixedpoint.NewFromFloat(4), grid.Spread)
@ -35,7 +41,7 @@ func TestGrid_HasPin(t *testing.T) {
upper := fixedpoint.NewFromFloat(500.0) upper := fixedpoint.NewFromFloat(500.0)
lower := fixedpoint.NewFromFloat(100.0) lower := fixedpoint.NewFromFloat(100.0)
size := fixedpoint.NewFromFloat(100.0) size := fixedpoint.NewFromFloat(100.0)
grid := NewGrid(lower, upper, size, number(2)) grid := NewGrid(lower, upper, size, number(0.01))
assert.True(t, grid.HasPin(Pin(number(100.0)))) assert.True(t, grid.HasPin(Pin(number(100.0))))
assert.True(t, grid.HasPin(Pin(number(500.0)))) assert.True(t, grid.HasPin(Pin(number(500.0))))