From 20a4ad99b3d92b9946f10ad2784eaa9cff146435 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 23 Aug 2019 13:35:34 +0300 Subject: [PATCH] fixed8: add some testcases from dev, split Value functions Add FloatValue() and rename Value() to Int64Value() for consistency. --- _pkg.dev/wire/util/fixed8/fixed8.go | 62 ----------------- _pkg.dev/wire/util/fixed8/fixed8_test.go | 85 ------------------------ pkg/util/fixed8.go | 9 ++- pkg/util/fixed8_test.go | 31 ++++++++- 4 files changed, 36 insertions(+), 151 deletions(-) delete mode 100644 _pkg.dev/wire/util/fixed8/fixed8.go delete mode 100644 _pkg.dev/wire/util/fixed8/fixed8_test.go diff --git a/_pkg.dev/wire/util/fixed8/fixed8.go b/_pkg.dev/wire/util/fixed8/fixed8.go deleted file mode 100644 index 9b7f04e4a..000000000 --- a/_pkg.dev/wire/util/fixed8/fixed8.go +++ /dev/null @@ -1,62 +0,0 @@ -package fixed8 - -import ( - "errors" - "fmt" - "strconv" -) - -const ( - decimals = 100000000 -) - -var errInvalidString = errors.New("Fixed8 must satisfy following regex \\d+(\\.\\d{1,8})?") - -// Fixed8 represents a fixed-point number with precision 10^-8. -type Fixed8 int64 - -// String implements the Stringer interface. -func (f Fixed8) String() string { - val := f.Value() - return strconv.FormatFloat(val, 'f', -1, 64) -} - -// Value returns the original value representing the Fixed8. -func (f Fixed8) Value() float64 { - return float64(f) / float64(decimals) -} - -// Add adds two Fixed8 values together -func (f Fixed8) Add(val Fixed8) Fixed8 { - a := int64(f.Value()) - b := int64(val.Value()) - c := a + b - return FromInt(c) -} - -//Sub subtracts two fixed values from each other -func (f Fixed8) Sub(val Fixed8) Fixed8 { - a := int64(f.Value()) - b := int64(val.Value()) - c := a - b - return FromInt(c) -} - -//FromInt returns a Fixed8 objects from an int64 -func FromInt(val int64) Fixed8 { - return Fixed8(val * decimals) -} - -// FromFloat returns a Fixed8 object from a float64 -func FromFloat(val float64) Fixed8 { - return Fixed8(val * decimals) -} - -// FromString returns a Fixed8 object from a string -func FromString(val string) (Fixed8, error) { - res, err := strconv.ParseFloat(val, 64) - if err != nil { - return 0, fmt.Errorf("failed at parsing string %s", val) - } - return FromFloat(res), nil -} diff --git a/_pkg.dev/wire/util/fixed8/fixed8_test.go b/_pkg.dev/wire/util/fixed8/fixed8_test.go deleted file mode 100644 index 8ee5ab37f..000000000 --- a/_pkg.dev/wire/util/fixed8/fixed8_test.go +++ /dev/null @@ -1,85 +0,0 @@ -package fixed8 - -import ( - "strconv" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestFixed8Value(t *testing.T) { - - input := int64(12) - assert.Equal(t, float64(input), FromInt(input).Value()) - -} -func TestFixed8Add(t *testing.T) { - - a := FromInt(1) - b := FromInt(2) - - c := a.Add(b) - expected := float64(3) - assert.Equal(t, expected, c.Value()) - -} -func TestFixed8AddRecursive(t *testing.T) { - - a := FromInt(1) - sum := int64(1) - - for i := int64(2); i <= 10; i++ { - - sum += i - b := FromInt(i) - c := a.Add(b) - a = c // 1 + 2 + 3 ... + 10 - } - assert.Equal(t, float64(sum), a.Value()) - -} - -func TestFromInt(t *testing.T) { - - inputs := []int64{12, 23, 100, 456789} - - for _, val := range inputs { - assert.Equal(t, Fixed8(val*decimals), FromInt(val)) - assert.Equal(t, float64(val), FromInt(val).Value()) - } - - for _, val := range inputs { - valString := strconv.FormatInt(val, 10) - assert.Equal(t, valString, FromInt(val).String()) - } - -} -func TestFromFloat(t *testing.T) { - inputs := []float64{12.98, 23.87654333, 100.654322, 456789.12345665} - - for _, val := range inputs { - assert.Equal(t, Fixed8(val*decimals), FromFloat(val)) - assert.Equal(t, float64(val), FromFloat(val).Value()) - } -} -func TestFromString(t *testing.T) { - inputs := []string{"9000", "100000000", "5", "10945", "20.45", "0.00000001"} - - for _, val := range inputs { - - n, err := FromString(val) - assert.Nil(t, err) - assert.Equal(t, val, n.String()) - - } - - val := "123456789.12345678" - n, err := FromString(val) - assert.Nil(t, err) - assert.Equal(t, Fixed8(12345678912345678), n) - - val = "901.2341" - n, err = FromString(val) - assert.Nil(t, err) - assert.Equal(t, Fixed8(90123410000), n) -} diff --git a/pkg/util/fixed8.go b/pkg/util/fixed8.go index ad9f13beb..80216edef 100644 --- a/pkg/util/fixed8.go +++ b/pkg/util/fixed8.go @@ -39,8 +39,13 @@ func (f Fixed8) String() string { return buf.String() } -// Value returns the original value representing the Fixed8. -func (f Fixed8) Value() int64 { +// FloatValue returns the original value representing Fixed8 as float64. +func (f Fixed8) FloatValue() float64 { + return float64(f) / decimals +} + +// Int64Value returns the original value representing Fixed8 as int64. +func (f Fixed8) Int64Value() int64 { return int64(f) / decimals } diff --git a/pkg/util/fixed8_test.go b/pkg/util/fixed8_test.go index 53546d783..7bb4aa9d0 100644 --- a/pkg/util/fixed8_test.go +++ b/pkg/util/fixed8_test.go @@ -9,11 +9,38 @@ import ( ) func TestNewFixed8(t *testing.T) { - values := []int64{9000, 100000000, 5, 10945} + values := []int64{9000, 100000000, 5, 10945, -42} for _, val := range values { assert.Equal(t, Fixed8(val*decimals), NewFixed8(val)) - assert.Equal(t, val, NewFixed8(val).Value()) + assert.Equal(t, val, NewFixed8(val).Int64Value()) + } +} + +func TestFixed8Add(t *testing.T) { + a := NewFixed8(1) + b := NewFixed8(2) + + c := a.Add(b) + expected := int64(3) + assert.Equal(t, strconv.FormatInt(expected, 10), c.String()) +} + +func TestFixed8Sub(t *testing.T) { + + a := NewFixed8(42) + b := NewFixed8(34) + + c := a.Sub(b) + assert.Equal(t, int64(8), c.Int64Value()) +} + +func TestFixed8FromFloat(t *testing.T) { + inputs := []float64{12.98, 23.87654333, 100.654322, 456789.12345665, -3.14159265} + + for _, val := range inputs { + assert.Equal(t, Fixed8(val*decimals), NewFixed8FromFloat(val)) + assert.Equal(t, val, NewFixed8FromFloat(val).FloatValue()) } }