-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request c9s#1125 from c9s/bhwu/grid2/using-dnum
FEATURE: [grid2] using dnum
- Loading branch information
Showing
5 changed files
with
325 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,124 @@ | ||
package fixedpoint | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_FormatString(t *testing.T) { | ||
assert := assert.New(t) | ||
cases := []struct { | ||
input string | ||
prec int | ||
expected string | ||
}{ | ||
{input: "0.57", prec: 5, expected: "0.57000"}, | ||
{input: "-0.57", prec: 5, expected: "-0.57000"}, | ||
{input: "0.57123456", prec: 8, expected: "0.57123456"}, | ||
{input: "-0.57123456", prec: 8, expected: "-0.57123456"}, | ||
{input: "0.57123456", prec: 5, expected: "0.57123"}, | ||
{input: "-0.57123456", prec: 5, expected: "-0.57123"}, | ||
{input: "0.57123456", prec: 0, expected: "0"}, | ||
{input: "-0.57123456", prec: 0, expected: "0"}, | ||
{input: "0.57123456", prec: -1, expected: "0"}, | ||
{input: "-0.57123456", prec: -1, expected: "0"}, | ||
{input: "0.57123456", prec: -5, expected: "0"}, | ||
{input: "-0.57123456", prec: -5, expected: "0"}, | ||
{input: "0.57123456", prec: -9, expected: "0"}, | ||
{input: "-0.57123456", prec: -9, expected: "0"}, | ||
|
||
t.Run("0.57 with prec = 5, expected 0.57", func(t *testing.T) { | ||
v := MustNewFromString("0.57") | ||
s := v.FormatString(5) | ||
assert.Equal("0.57000", s) | ||
}) | ||
{input: "1.23456789", prec: 9, expected: "1.234567890"}, | ||
{input: "-1.23456789", prec: 9, expected: "-1.234567890"}, | ||
{input: "1.02345678", prec: 9, expected: "1.023456780"}, | ||
{input: "-1.02345678", prec: 9, expected: "-1.023456780"}, | ||
{input: "1.02345678", prec: 2, expected: "1.02"}, | ||
{input: "-1.02345678", prec: 2, expected: "-1.02"}, | ||
{input: "1.02345678", prec: 0, expected: "1"}, | ||
{input: "-1.02345678", prec: 0, expected: "-1"}, | ||
{input: "1.02345678", prec: -1, expected: "0"}, | ||
{input: "-1.02345678", prec: -1, expected: "0"}, | ||
{input: "1.02345678", prec: -10, expected: "0"}, | ||
{input: "-1.02345678", prec: -10, expected: "0"}, | ||
|
||
t.Run("0.57123456 with prec = 5, expected 0.57123", func(t *testing.T) { | ||
v := MustNewFromString("0.57123456") | ||
s := v.FormatString(5) | ||
assert.Equal("0.57123", s) | ||
}) | ||
{input: "0.0001234", prec: 9, expected: "0.000123400"}, | ||
{input: "-0.0001234", prec: 9, expected: "-0.000123400"}, | ||
{input: "0.0001234", prec: 7, expected: "0.0001234"}, | ||
{input: "-0.0001234", prec: 7, expected: "-0.0001234"}, | ||
{input: "0.0001234", prec: 5, expected: "0.00012"}, | ||
{input: "-0.0001234", prec: 5, expected: "-0.00012"}, | ||
{input: "0.0001234", prec: 3, expected: "0.000"}, | ||
{input: "-0.0001234", prec: 3, expected: "0.000"}, | ||
{input: "0.0001234", prec: 2, expected: "0.00"}, | ||
{input: "-0.0001234", prec: 2, expected: "0.00"}, | ||
{input: "0.0001234", prec: 0, expected: "0"}, | ||
{input: "-0.0001234", prec: 0, expected: "0"}, | ||
{input: "0.00001234", prec: -1, expected: "0"}, | ||
{input: "-0.00001234", prec: -1, expected: "0"}, | ||
{input: "0.00001234", prec: -5, expected: "0"}, | ||
{input: "-0.00001234", prec: -5, expected: "0"}, | ||
{input: "0.00001234", prec: -9, expected: "0"}, | ||
{input: "-0.00001234", prec: -9, expected: "0"}, | ||
|
||
t.Run("1.23456789 with prec = 9, expected 1.23456789", func(t *testing.T) { | ||
v := MustNewFromString("1.23456789") | ||
s := v.FormatString(9) | ||
assert.Equal("1.234567890", s) | ||
}) | ||
{input: "12.3456789", prec: 10, expected: "12.3456789000"}, | ||
{input: "-12.3456789", prec: 10, expected: "-12.3456789000"}, | ||
{input: "12.3456789", prec: 9, expected: "12.345678900"}, | ||
{input: "-12.3456789", prec: 9, expected: "-12.345678900"}, | ||
{input: "12.3456789", prec: 7, expected: "12.3456789"}, | ||
{input: "-12.3456789", prec: 7, expected: "-12.3456789"}, | ||
{input: "12.3456789", prec: 5, expected: "12.34567"}, | ||
{input: "-12.3456789", prec: 5, expected: "-12.34567"}, | ||
{input: "12.3456789", prec: 1, expected: "12.3"}, | ||
{input: "-12.3456789", prec: 1, expected: "-12.3"}, | ||
{input: "12.3456789", prec: 0, expected: "12"}, | ||
{input: "-12.3456789", prec: 0, expected: "-12"}, | ||
{input: "12.3456789", prec: -1, expected: "10"}, | ||
{input: "-12.3456789", prec: -1, expected: "-10"}, | ||
{input: "12.3456789", prec: -2, expected: "0"}, | ||
{input: "-12.3456789", prec: -2, expected: "0"}, | ||
{input: "12.3456789", prec: -3, expected: "0"}, | ||
{input: "-12.3456789", prec: -3, expected: "0"}, | ||
|
||
t.Run("1.02345678 with prec = 9, expected 1.02345678", func(t *testing.T) { | ||
v := MustNewFromString("1.02345678") | ||
s := v.FormatString(9) | ||
assert.Equal("1.023456780", s) | ||
}) | ||
{input: "12345678.9", prec: 10, expected: "12345678.9000000000"}, | ||
{input: "-12345678.9", prec: 10, expected: "-12345678.9000000000"}, | ||
{input: "12345678.9", prec: 3, expected: "12345678.900"}, | ||
{input: "-12345678.9", prec: 3, expected: "-12345678.900"}, | ||
{input: "12345678.9", prec: 1, expected: "12345678.9"}, | ||
{input: "-12345678.9", prec: 1, expected: "-12345678.9"}, | ||
{input: "12345678.9", prec: 0, expected: "12345678"}, | ||
{input: "-12345678.9", prec: 0, expected: "-12345678"}, | ||
{input: "12345678.9", prec: -2, expected: "12345600"}, | ||
{input: "-12345678.9", prec: -2, expected: "-12345600"}, | ||
{input: "12345678.9", prec: -5, expected: "12300000"}, | ||
{input: "-12345678.9", prec: -5, expected: "-12300000"}, | ||
{input: "12345678.9", prec: -7, expected: "10000000"}, | ||
{input: "-12345678.9", prec: -7, expected: "-10000000"}, | ||
{input: "12345678.9", prec: -8, expected: "0"}, | ||
{input: "-12345678.9", prec: -8, expected: "0"}, | ||
{input: "12345678.9", prec: -10, expected: "0"}, | ||
{input: "-12345678.9", prec: -10, expected: "0"}, | ||
|
||
t.Run("-0.57 with prec = 5, expected -0.57", func(t *testing.T) { | ||
v := MustNewFromString("-0.57") | ||
s := v.FormatString(5) | ||
assert.Equal("-0.57000", s) | ||
}) | ||
{input: "123000", prec: 7, expected: "123000.0000000"}, | ||
{input: "-123000", prec: 7, expected: "-123000.0000000"}, | ||
{input: "123000", prec: 2, expected: "123000.00"}, | ||
{input: "-123000", prec: 2, expected: "-123000.00"}, | ||
{input: "123000", prec: 0, expected: "123000"}, | ||
{input: "-123000", prec: 0, expected: "-123000"}, | ||
{input: "123000", prec: -1, expected: "123000"}, | ||
{input: "-123000", prec: -1, expected: "-123000"}, | ||
{input: "123000", prec: -5, expected: "100000"}, | ||
{input: "-123000", prec: -5, expected: "-100000"}, | ||
{input: "123000", prec: -6, expected: "0"}, | ||
{input: "-123000", prec: -6, expected: "0"}, | ||
{input: "123000", prec: -8, expected: "0"}, | ||
{input: "-123000", prec: -8, expected: "0"}, | ||
} | ||
|
||
t.Run("-1.23456789 with prec = 9, expected 1.23456789", func(t *testing.T) { | ||
v := MustNewFromString("-1.23456789") | ||
s := v.FormatString(9) | ||
assert.Equal("-1.234567890", s) | ||
}) | ||
|
||
t.Run("-0.00001234 with prec = 3, expected = 0.000", func(t *testing.T) { | ||
v := MustNewFromString("-0.0001234") | ||
s := v.FormatString(3) | ||
assert.Equal("0.000", s) | ||
}) | ||
|
||
// comment out negative precision for dnum testing | ||
/* | ||
t.Run("12.3456789 with prec = -1, expected 10", func(t *testing.T) { | ||
v := MustNewFromString("12.3456789") | ||
s := v.FormatString(-1) | ||
assert.Equal("10", s) | ||
}) | ||
t.Run("12.3456789 with prec = -3, expected = 0", func(t *testing.T) { | ||
v := MustNewFromString("12.3456789") | ||
s := v.FormatString(-2) | ||
assert.Equal("0", s) | ||
for _, c := range cases { | ||
t.Run(fmt.Sprintf("%s with prec = %d, expected %s", c.input, c.prec, c.expected), func(t *testing.T) { | ||
v := MustNewFromString(c.input) | ||
s := v.FormatString(c.prec) | ||
assert.Equal(t, c.expected, s) | ||
}) | ||
*/ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
//go:build dnum | ||
|
||
package grid2 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGrid_HasPrice_Dnum(t *testing.T) { | ||
t.Run("case1", func(t *testing.T) { | ||
upper := number(500.0) | ||
lower := number(100.0) | ||
size := number(5.0) | ||
grid := NewGrid(lower, upper, size, number(0.01)) | ||
grid.CalculateArithmeticPins() | ||
|
||
assert.True(t, grid.HasPrice(number(500.0)), "upper price") | ||
assert.True(t, grid.HasPrice(number(100.0)), "lower price") | ||
assert.True(t, grid.HasPrice(number(200.0)), "found 200 price ok") | ||
assert.True(t, grid.HasPrice(number(300.0)), "found 300 price ok") | ||
}) | ||
|
||
t.Run("case2", func(t *testing.T) { | ||
upper := number(0.9) | ||
lower := number(0.1) | ||
size := number(7.0) | ||
grid := NewGrid(lower, upper, size, number(0.00000001)) | ||
grid.CalculateArithmeticPins() | ||
|
||
assert.Equal(t, []Pin{ | ||
Pin(number("0.1")), | ||
Pin(number("0.23333333")), | ||
Pin(number("0.36666666")), | ||
Pin(number("0.50000000")), | ||
Pin(number("0.63333333")), | ||
Pin(number("0.76666666")), | ||
Pin(number("0.9")), | ||
}, grid.Pins) | ||
|
||
assert.False(t, grid.HasPrice(number(200.0)), "out of range") | ||
assert.True(t, grid.HasPrice(number(0.9)), "upper price") | ||
assert.True(t, grid.HasPrice(number(0.1)), "lower price") | ||
assert.True(t, grid.HasPrice(number(0.5)), "found 0.49999999 price ok") | ||
}) | ||
|
||
t.Run("case3", func(t *testing.T) { | ||
upper := number(0.9) | ||
lower := number(0.1) | ||
size := number(7.0) | ||
grid := NewGrid(lower, upper, size, number(0.0001)) | ||
grid.CalculateArithmeticPins() | ||
|
||
assert.Equal(t, []Pin{ | ||
Pin(number(0.1)), | ||
Pin(number(0.2333)), | ||
Pin(number(0.3666)), | ||
Pin(number(0.5000)), | ||
Pin(number(0.6333)), | ||
Pin(number(0.7666)), | ||
Pin(number(0.9)), | ||
}, grid.Pins) | ||
|
||
assert.False(t, grid.HasPrice(number(200.0)), "out of range") | ||
assert.True(t, grid.HasPrice(number(0.9)), "upper price") | ||
assert.True(t, grid.HasPrice(number(0.1)), "lower price") | ||
assert.True(t, grid.HasPrice(number(0.5)), "found 0.5 price ok") | ||
assert.True(t, grid.HasPrice(number(0.2333)), "found 0.2333 price ok") | ||
}) | ||
|
||
t.Run("case4", func(t *testing.T) { | ||
upper := number(90.0) | ||
lower := number(10.0) | ||
size := number(7.0) | ||
grid := NewGrid(lower, upper, size, number(0.001)) | ||
grid.CalculateArithmeticPins() | ||
|
||
assert.Equal(t, []Pin{ | ||
Pin(number("10.0")), | ||
Pin(number("23.333")), | ||
Pin(number("36.666")), | ||
Pin(number("50.00")), | ||
Pin(number("63.333")), | ||
Pin(number("76.666")), | ||
Pin(number("90.0")), | ||
}, grid.Pins) | ||
|
||
assert.False(t, grid.HasPrice(number(200.0)), "out of range") | ||
assert.True(t, grid.HasPrice(number("36.666")), "found 36.666 price ok") | ||
}) | ||
|
||
} |
Oops, something went wrong.