@ 3608a1b3f346ed04e58a598e4f092b09f515c3fd | history
package eval
import "testing"
func TestComparisons(t *testing.T) {
runEvalCases(t, []evalCase{
{"(< 1 2)", "T"},
{"(< 2 1)", "F"},
{"(< 1 1)", "F"},
{"(<= 1 1)", "T"},
{"(> 2 1)", "T"},
{"(>= 1 2)", "F"},
{"(>= 2 2)", "T"},
{"(< -1.5 -1)", "T"},
{"(< 1 1.5)", "T"},
{"(> 2.0 1)", "T"},
{"(<= 1.0 1)", "T"},
// Exact across Int and Float, even where float64 rounds.
// 9007199254740993 is 2^53 + 1, which as a float64 is 2^53.
{"(> 9007199254740993 9007199254740992.0)", "T"},
{"(== 9007199254740993 9007199254740992.0)", "F"},
{"(== 9007199254740992 9007199254740992.0)", "T"},
{"(< 9223372036854775807 9223372036854775808.0)", "T"},
{"(> -9223372036854775808 -9223372036854777856.0)", "T"},
{"(== -9223372036854775808 -9223372036854775808.0)", "T"},
{"(< 1 1.0000000000000002)", "T"},
{"(> -1 -1.0000000000000002)", "T"},
{"(< -0.5 0)", "T"},
{"(> 0.5 0)", "T"},
{"(== 1 1)", "T"},
{"(== 1 2)", "F"},
{"(== 1 1.0)", "T"},
{"(== 1.0 1)", "T"},
{"(!= 1 1.0)", "F"},
{`(== "a" "a")`, "T"},
{`(== "a" "b")`, "F"},
{`(== "" "")`, "T"},
{"(== 'a 'a)", "T"},
{"(== 'a 'b)", "F"},
{"(== T T)", "T"},
{"(== T F)", "F"},
{"(== () ())", "T"},
{"(== '() ())", "T"},
// Different kinds are never equal.
{`(== 1 "1")`, "F"},
{"(== 'T T)", "T"},
{`(== 'a "a")`, "F"},
{"(== F ())", "F"},
{"(== 0 F)", "F"},
{"(!= 0 ())", "T"},
// Pairs compare structurally.
{"(== '(1 2 3) '(1 2 3))", "T"},
{"(== '(1 2 3) '(1 2))", "F"},
{"(== '(1 2) '(1 2 3))", "F"},
{"(== '(1 (2 3)) '(1 (2 3)))", "T"},
{"(== '(1 (2 3)) '(1 (2 4)))", "F"},
{"(== '(1 . 2) '(1 . 2))", "T"},
{"(== '(1 . 2) '(1 2))", "F"},
{"(== '(1 2.0) '(1.0 2))", "T"},
{"(== (cons 1 2) '(1 . 2))", "T"},
{"(== '(a) 'a)", "F"},
{"(!= '(1 2) '(1 2))", "F"},
// Only F is false.
{"(not F)", "T"},
{"(not T)", "F"},
{"(not 0)", "F"},
{"(not ())", "F"},
{`(not "")`, "F"},
{"(not (not F))", "F"},
{"(not (< 2 1))", "T"},
})
}
func TestComparisonErrors(t *testing.T) {
runErrorCases(t, []errorCase{
{"(< 1)", ErrArity, "< takes 2 arguments, got 1"},
{"(< 1 2 3)", ErrArity, "< takes 2 arguments, got 3"},
{"(==)", ErrArity, "== takes 2 arguments, got 0"},
{"(not)", ErrArity, "not takes 1 argument, got 0"},
{"(not T F)", ErrArity, "not takes 1 argument, got 2"},
{`(< "a" "b")`, ErrType, `argument 1 of < must be a number, got "a"`},
{"(>= 1 'x)", ErrType, "argument 2 of >= must be a number, got x"},
{"(< 1 ())", ErrType, "argument 2 of < must be a number, got ()"},
{"(== + +)", ErrType, "cannot compare functions"},
{"(== + 1)", ErrType, "cannot compare functions"},
{"(!= 1 car)", ErrType, "cannot compare functions"},
{"(== (cons + 1) (cons + 1))", ErrType, "cannot compare functions"},
})
}
func TestEqualityOnLongLists(t *testing.T) {
src := "(== '(" + repeat("1 ", 100_000) + ") '(" + repeat("1 ", 100_000) + "))"
runEvalCases(t, []evalCase{{src, "T"}})
}
func repeat(s string, n int) string {
b := make([]byte, 0, len(s)*n)
for i := 0; i < n; i++ {
b = append(b, s...)
}
return string(b)
}