Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Float APIs - Part1 #664

Merged
merged 7 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions vm/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,157 @@ var builtinFloatInstanceMethods = []*BuiltinMethodObject{
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
r := receiver.(*FloatObject)
return t.vm.initGoObject(&r.value)
},
},
{
// Returns the Float as a positive value.
//
// ```Ruby
// -34.56.abs # => 34.56
// 34.56.abs # => 34.56
// ```
// @return [Float]
Name: "abs",
st0012 marked this conversation as resolved.
Show resolved Hide resolved
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
if len(args) != 0 {
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, "Expect 0 argument. got=%v", strconv.Itoa(len(args)))
}
r := receiver.(*FloatObject)
result := math.Abs(r.value)
return t.vm.initFloatObject(result)
},
},
{
// Returns the smallest Integer greater than or equal to self.
//
// ```Ruby
// 1.2.ceil # => 2
// 2.ceil # => 2
// -1.2.ceil # => -1
// -2.ceil # => -2
// ```
// @return [Integer]
Name: "ceil",
st0012 marked this conversation as resolved.
Show resolved Hide resolved
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
// TODO: Make ceil accept arguments
if len(args) != 0 {
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, "Expect 0 argument. got=%v", strconv.Itoa(len(args)))
}
r := receiver.(*FloatObject)
result := math.Ceil(r.value)
newInt := t.vm.InitIntegerObject(int(result))
newInt.flag = i
return newInt
},
},
{
// Returns the largest Integer less than or equal to self.
//
// ```Ruby
// 1.2.floor # => 1
// 2.0.floor # => 2
// -1.2.floor # => -2
// -2.0.floor # => -2
// ```
// @return [Integer]
Name: "floor",
st0012 marked this conversation as resolved.
Show resolved Hide resolved
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
// TODO: Make floor accept arguments
if len(args) != 0 {
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, "Expect 0 argument. got=%v", strconv.Itoa(len(args)))
}
r := receiver.(*FloatObject)
result := math.Floor(r.value)
newInt := t.vm.InitIntegerObject(int(result))
newInt.flag = i
return newInt
},
},
{
// Returns true if Float is equal to 0.0
//
// ```Ruby
// 0.0.zero? # => true
// 1.0.zero? # => false
// ```
// @return [Boolean]
Name: "zero?",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
if len(args) != 0 {
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, "Expect 0 argument. got=%v", strconv.Itoa(len(args)))
}
r := receiver.(*FloatObject)
return toBooleanObject(r.value == 0.0)
},
},
{
// Returns true if Float is larger than 0.0
//
// ```Ruby
// -1.0.positive? # => false
// 0.0.positive? # => false
// 1.0.positive? # => true
// ```
// @return [Boolean]
Name: "positive?",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
if len(args) != 0 {
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, "Expect 0 argument. got=%v", strconv.Itoa(len(args)))
}
r := receiver.(*FloatObject)
return toBooleanObject(r.value > 0.0)
},
},
{
// Returns true if Float is less than 0.0
//
// ```Ruby
// -1.0.negative? # => true
// 0.0.negative? # => false
// 1.0.negative? # => false
// ```
// @return [Boolean]
Name: "negative?",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
if len(args) != 0 {
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, "Expect 0 argument. got=%v", strconv.Itoa(len(args)))
}
r := receiver.(*FloatObject)
return toBooleanObject(r.value < 0.0)
},
},
{
// Rounds float to a given precision in decimal digits (default 0 digits)
//
// ```Ruby
// 1.115.round # => 1
// 1.115.round(1) # => 1.1
// 1.115.round(2) # => 1.12
// -1.115.round # => -1
// -1.115.round(1) # => -1.1
// -1.115.round(2) # => -1.12
// ```
// @return [Integer]
Name: "round",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
var precision int

if len(args) > 1 {
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, "Expect 0 or 1 argument. got=%v", strconv.Itoa(len(args)))
} else if len(args) == 1 {
int, ok := args[0].(*IntegerObject)

if !ok {
return t.vm.InitErrorObject(errors.TypeError, sourceLine, errors.WrongArgumentTypeFormat, classes.IntegerClass, args[0].Class().Name)
}

precision = int.value
}

f := receiver.(*FloatObject).floatValue()
n := math.Pow10(precision)

return t.vm.initFloatObject(math.Round(f*n) / n)
},
},
}
Expand Down
139 changes: 139 additions & 0 deletions vm/float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,26 @@ func TestFloatNumberOfDigit(t *testing.T) {
}
}

// API tests

func TestFloatAbs(t *testing.T) {
tests := []struct {
input string
expected interface{}
}{
{"34.56.abs", 34.56},
{"-34.56.abs", 34.56},
}

for i, tt := range tests {
v := initTestVM()
evaluated := v.testEval(t, tt.input, getFilename())
VerifyExpected(t, i, evaluated, tt.expected)
v.checkCFP(t, i, 0)
v.checkSP(t, i, 1)
}
}

func TestFloatMinusZero(t *testing.T) {
tests := []struct {
input string
Expand Down Expand Up @@ -369,6 +389,26 @@ func TestFloatMinusZero(t *testing.T) {
}
}

func TestFloatCeil(t *testing.T) {
tests := []struct {
input string
expected interface{}
}{
{"1.2.ceil", 2},
{"2.0.ceil", 2},
{"-1.2.ceil", -1},
{"-2.0.ceil", -2},
}

for i, tt := range tests {
v := initTestVM()
evaluated := v.testEval(t, tt.input, getFilename())
VerifyExpected(t, i, evaluated, tt.expected)
v.checkCFP(t, i, 0)
v.checkSP(t, i, 1)
}
}

func TestFloatDupMethod(t *testing.T) {
tests := []struct {
input string
Expand All @@ -385,3 +425,102 @@ func TestFloatDupMethod(t *testing.T) {
v.checkSP(t, i, 1)
}
}
func TestFloatFloor(t *testing.T) {
tests := []struct {
input string
expected interface{}
}{
{"1.2.floor", 1},
{"2.0.floor", 2},
{"-1.2.floor", -2},
{"-2.0.floor", -2},
}

for i, tt := range tests {
v := initTestVM()
evaluated := v.testEval(t, tt.input, getFilename())
VerifyExpected(t, i, evaluated, tt.expected)
v.checkCFP(t, i, 0)
v.checkSP(t, i, 1)
}
}

func TestFloatNegative(t *testing.T) {
tests := []struct {
input string
expected interface{}
}{
{"-1.0.negative?", true},
{"0.0.negative?", false},
{"1.0.negative?", false},
}

for i, tt := range tests {
v := initTestVM()
evaluated := v.testEval(t, tt.input, getFilename())
VerifyExpected(t, i, evaluated, tt.expected)
v.checkCFP(t, i, 0)
v.checkSP(t, i, 1)
}
}

func TestFloatPositive(t *testing.T) {
tests := []struct {
input string
expected interface{}
}{
{"-1.0.positive?", false},
{"0.0.positive?", false},
{"1.0.positive?", true},
}

for i, tt := range tests {
v := initTestVM()
evaluated := v.testEval(t, tt.input, getFilename())
VerifyExpected(t, i, evaluated, tt.expected)
v.checkCFP(t, i, 0)
v.checkSP(t, i, 1)
}
}

func TestFloatRound(t *testing.T) {
tests := []struct {
input string
expected interface{}
}{
{"1.115.round", 1.0},
{"1.115.round(1)", 1.1},
{"1.115.round(2)", 1.12},
{"-1.115.round", -1.0},
{"-1.115.round(1)", -1.1},
{"-1.115.round(2)", -1.12},
{"1.115.round(-1)", 0.0},
{"-1.115.round(-1)", 0.0},
}

for i, tt := range tests {
v := initTestVM()
evaluated := v.testEval(t, tt.input, getFilename())
VerifyExpected(t, i, evaluated, tt.expected)
v.checkCFP(t, i, 0)
v.checkSP(t, i, 1)
}
}

func TestFloatZero(t *testing.T) {
tests := []struct {
input string
expected interface{}
}{
{"0.0.zero?", true},
{"1.0.zero?", false},
}

for i, tt := range tests {
v := initTestVM()
evaluated := v.testEval(t, tt.input, getFilename())
VerifyExpected(t, i, evaluated, tt.expected)
v.checkCFP(t, i, 0)
v.checkSP(t, i, 1)
}
}