@ 425eee5ae0d898d1e8c0558a56c41f58d7a05028 | history
package eval
import "testing"
func TestArithmetic(t *testing.T) {
runEvalCases(t, []evalCase{
{"(+)", "0"},
{"(+ 5)", "5"},
{"(+ 1 2)", "3"},
{"(+ 1 2 3 4)", "10"},
{"(*)", "1"},
{"(* 6)", "6"},
{"(* 2 3 4)", "24"},
{"(- 5)", "-5"},
{"(- -5)", "5"},
{"(- 10 3)", "7"},
{"(- 10 3 2)", "5"},
{"(/ 7 2)", "3"},
{"(/ -7 2)", "-3"},
{"(/ 7 -2)", "-3"},
{"(/ 100 2 5)", "10"},
{"(/ 2)", "0"},
{"(/ 1)", "1"},
// Any Float makes the result a Float.
{"(+ 1 2.0)", "3.0"},
{"(+ 1.5 1.5)", "3.0"},
{"(* 2 0.5)", "1.0"},
{"(- 1.5)", "-1.5"},
{"(/ 7.0 2)", "3.5"},
{"(/ 7 2.0)", "3.5"},
{"(/ 2.0)", "0.5"},
{"(+ 0.1 0.2)", "0.30000000000000004"},
// Promotion happens per step of the fold.
{"(+ 1 2 3.0)", "6.0"},
{"(/ 7 2 1.0)", "3.0"},
{"(/ 7.0 2 1)", "3.5"},
{"(+ (* 2 3) (- 10 4))", "12"},
{"(* (+ 1 2) (/ 9 3))", "9"},
{"(if (- 1 1) 'zero-is-true 'no)", "zero-is-true"},
// Builtins are values.
{"+", "#<builtin +>"},
{"((if T + *) 2 3)", "5"},
{"((if F + *) 2 3)", "6"},
{"(+ 9223372036854775806 1)", "9223372036854775807"},
{"(- -9223372036854775807 1)", "-9223372036854775808"},
{"(* -1 9223372036854775807)", "-9223372036854775807"},
{"(/ -9223372036854775808 1)", "-9223372036854775808"},
{"(* 1.0e308 1.0)", "1.0e+308"},
})
}
func TestArithmeticErrors(t *testing.T) {
runErrorCases(t, []errorCase{
{"(* 9223372036854775807 2)", ErrArith, "integer overflow in *"},
{"(+ 9223372036854775807 1)", ErrArith, "integer overflow in +"},
{"(- -9223372036854775808 1)", ErrArith, "integer overflow in -"},
{"(- -9223372036854775808)", ErrArith, "integer overflow in -"},
{"(* -1 -9223372036854775808)", ErrArith, "integer overflow in *"},
{"(* -9223372036854775808 -1)", ErrArith, "integer overflow in *"},
{"(/ -9223372036854775808 -1)", ErrArith, "integer overflow in /"},
{"(* 4294967296 4294967296)", ErrArith, "integer overflow in *"},
{"(/ 1 0)", ErrArith, "division by zero"},
{"(/ 0)", ErrArith, "division by zero"},
{"(/ 1.0 0)", ErrArith, "division by zero"},
{"(/ 1 0.0)", ErrArith, "division by zero"},
{"(/ 0.0 0.0)", ErrArith, "division by zero"},
{"(* 1.0e308 10)", ErrArith, "float overflow in *"},
{"(+ 1.0e308 1.0e308)", ErrArith, "float overflow in +"},
{"(- -1.0e308 1.0e308)", ErrArith, "float overflow in -"},
{"(/ 1.0e308 1.0e-308)", ErrArith, "float overflow in /"},
{"(-)", ErrArity, "- takes at least 1 argument, got 0"},
{"(/)", ErrArity, "/ takes at least 1 argument, got 0"},
{`(+ 1 "2")`, ErrType, `argument 2 of + must be a number, got "2"`},
{"(+ 'a)", ErrType, "argument 1 of + must be a number, got a"},
{"(- T 1)", ErrType, "argument 1 of - must be a number, got T"},
{"(- 1 2 ())", ErrType, "argument 3 of - must be a number, got ()"},
{"(- ())", ErrType, "argument 1 of - must be a number, got ()"},
{"(/ 1 +)", ErrType, "argument 2 of / must be a number, got #<builtin +>"},
// Arguments are evaluated before the call, left to right.
{"(+ boom (/ 1 0))", ErrUnbound, "no statute for symbol boom"},
{"(+ (/ 1 0) boom)", ErrArith, "division by zero"},
{"(+ 1 (if))", ErrSyntax, "if takes 2 or 3 operands, got 0"},
})
}