mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-26 19:43:48 +00:00
fixed8: add some testcases from dev, split Value functions
Add FloatValue() and rename Value() to Int64Value() for consistency.
This commit is contained in:
parent
a861f53733
commit
20a4ad99b3
4 changed files with 36 additions and 151 deletions
|
@ -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
|
||||
}
|
|
@ -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)
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue