package objects_test import ( "testing" "github.com/d5/tengo/compiler/token" "github.com/d5/tengo/objects" ) func TestFloat_BinaryOp(t *testing.T) { // float + float for l := float64(-2); l <= 2.1; l += 0.4 { for r := float64(-2); r <= 2.1; r += 0.4 { testBinaryOp(t, &objects.Float{Value: l}, token.Add, &objects.Float{Value: r}, &objects.Float{Value: l + r}) } } // float - float for l := float64(-2); l <= 2.1; l += 0.4 { for r := float64(-2); r <= 2.1; r += 0.4 { testBinaryOp(t, &objects.Float{Value: l}, token.Sub, &objects.Float{Value: r}, &objects.Float{Value: l - r}) } } // float * float for l := float64(-2); l <= 2.1; l += 0.4 { for r := float64(-2); r <= 2.1; r += 0.4 { testBinaryOp(t, &objects.Float{Value: l}, token.Mul, &objects.Float{Value: r}, &objects.Float{Value: l * r}) } } // float / float for l := float64(-2); l <= 2.1; l += 0.4 { for r := float64(-2); r <= 2.1; r += 0.4 { if r != 0 { testBinaryOp(t, &objects.Float{Value: l}, token.Quo, &objects.Float{Value: r}, &objects.Float{Value: l / r}) } } } // float < float for l := float64(-2); l <= 2.1; l += 0.4 { for r := float64(-2); r <= 2.1; r += 0.4 { testBinaryOp(t, &objects.Float{Value: l}, token.Less, &objects.Float{Value: r}, boolValue(l < r)) } } // float > float for l := float64(-2); l <= 2.1; l += 0.4 { for r := float64(-2); r <= 2.1; r += 0.4 { testBinaryOp(t, &objects.Float{Value: l}, token.Greater, &objects.Float{Value: r}, boolValue(l > r)) } } // float <= float for l := float64(-2); l <= 2.1; l += 0.4 { for r := float64(-2); r <= 2.1; r += 0.4 { testBinaryOp(t, &objects.Float{Value: l}, token.LessEq, &objects.Float{Value: r}, boolValue(l <= r)) } } // float >= float for l := float64(-2); l <= 2.1; l += 0.4 { for r := float64(-2); r <= 2.1; r += 0.4 { testBinaryOp(t, &objects.Float{Value: l}, token.GreaterEq, &objects.Float{Value: r}, boolValue(l >= r)) } } // float + int for l := float64(-2); l <= 2.1; l += 0.4 { for r := int64(-2); r <= 2; r++ { testBinaryOp(t, &objects.Float{Value: l}, token.Add, &objects.Int{Value: r}, &objects.Float{Value: l + float64(r)}) } } // float - int for l := float64(-2); l <= 2.1; l += 0.4 { for r := int64(-2); r <= 2; r++ { testBinaryOp(t, &objects.Float{Value: l}, token.Sub, &objects.Int{Value: r}, &objects.Float{Value: l - float64(r)}) } } // float * int for l := float64(-2); l <= 2.1; l += 0.4 { for r := int64(-2); r <= 2; r++ { testBinaryOp(t, &objects.Float{Value: l}, token.Mul, &objects.Int{Value: r}, &objects.Float{Value: l * float64(r)}) } } // float / int for l := float64(-2); l <= 2.1; l += 0.4 { for r := int64(-2); r <= 2; r++ { if r != 0 { testBinaryOp(t, &objects.Float{Value: l}, token.Quo, &objects.Int{Value: r}, &objects.Float{Value: l / float64(r)}) } } } // float < int for l := float64(-2); l <= 2.1; l += 0.4 { for r := int64(-2); r <= 2; r++ { testBinaryOp(t, &objects.Float{Value: l}, token.Less, &objects.Int{Value: r}, boolValue(l < float64(r))) } } // float > int for l := float64(-2); l <= 2.1; l += 0.4 { for r := int64(-2); r <= 2; r++ { testBinaryOp(t, &objects.Float{Value: l}, token.Greater, &objects.Int{Value: r}, boolValue(l > float64(r))) } } // float <= int for l := float64(-2); l <= 2.1; l += 0.4 { for r := int64(-2); r <= 2; r++ { testBinaryOp(t, &objects.Float{Value: l}, token.LessEq, &objects.Int{Value: r}, boolValue(l <= float64(r))) } } // float >= int for l := float64(-2); l <= 2.1; l += 0.4 { for r := int64(-2); r <= 2; r++ { testBinaryOp(t, &objects.Float{Value: l}, token.GreaterEq, &objects.Int{Value: r}, boolValue(l >= float64(r))) } } }