75 lines
1.3 KiB
Go
75 lines
1.3 KiB
Go
package math_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/matryer/is"
|
|
"go.sour.is/pkg/grug/math"
|
|
)
|
|
|
|
func TestNumber(t *testing.T) {
|
|
is := is.New(t)
|
|
|
|
n := math.NewNumber().FromInt(100)
|
|
is.Equal(n.String(), "100")
|
|
|
|
n = n.FromString("00001")
|
|
is.Equal(n.String(), "1")
|
|
|
|
n = n.FromString("1x0")
|
|
is.True(n==nil)
|
|
is.Equal(n.String(), "NaN")
|
|
|
|
n = n.FromString("200")
|
|
is.True(n!=nil)
|
|
is.Equal(n.String(), "200")
|
|
|
|
n = (&math.Number{}).FromString("300")
|
|
is.True(n!=nil)
|
|
is.Equal(n.String(), "300")
|
|
}
|
|
|
|
func TestAdd(t *testing.T) {
|
|
is := is.New(t)
|
|
|
|
n := math.NewNumber().FromString("100")
|
|
|
|
n = n.Add(nil)
|
|
is.Equal(n.String(), "NaN")
|
|
|
|
n = n.FromInt(100)
|
|
n = n.Add(n.FromString("900"))
|
|
is.Equal(n.String(), "1000")
|
|
|
|
n = n.Add(math.NewNumber())
|
|
is.Equal(n.String(), "1000")
|
|
|
|
n = n.Add(n.FromString("10"))
|
|
is.Equal(n.String(), "1010")
|
|
|
|
n = n.Add(n.FromString("10000"))
|
|
is.Equal(n.String(), "11010")
|
|
|
|
n = n.Add(n.FromString("9000"))
|
|
is.Equal(n.String(), "20010")
|
|
}
|
|
|
|
func TestSub(t *testing.T) {
|
|
is := is.New(t)
|
|
|
|
n := math.NewNumber()
|
|
|
|
// n = n.FromString("100")
|
|
// n = n.Sub(n.FromInt(100))
|
|
// is.Equal(n.String(), "0")
|
|
|
|
// n = n.FromString("200")
|
|
// n = n.Sub(n.FromInt(100))
|
|
// is.Equal(n.String(), "100")
|
|
|
|
n = n.FromString("100")
|
|
n = n.Sub(n.FromInt(50))
|
|
is.Equal(n.String(), "50")
|
|
|
|
}
|