From 5c351725062fd3df4fee9f00734b04ff34f97cd2 Mon Sep 17 00:00:00 2001 From: Diego Joss Date: Mon, 16 Dec 2024 22:17:05 +0100 Subject: [PATCH 1/3] testing: use luac5.2 instead of luac executable in tests At least on Debian (and derived distributions), the luac compiler for version 5.2 is explicitly named luac5.2. Otherwise, today, version 5.4 is used instead. --- dump_test.go | 2 +- parser_test.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/dump_test.go b/dump_test.go index 5a65a2a..cf01be8 100644 --- a/dump_test.go +++ b/dump_test.go @@ -11,7 +11,7 @@ import ( ) func TestUndumpThenDumpReturnsTheSameFunction(t *testing.T) { - _, err := exec.LookPath("luac") + _, err := exec.LookPath("luac5.2") if err != nil { t.Skipf("testing dump requires luac: %s", err) } diff --git a/parser_test.go b/parser_test.go index 36171e3..dff1f26 100644 --- a/parser_test.go +++ b/parser_test.go @@ -46,9 +46,9 @@ func TestEmptyString(t *testing.T) { } func TestParserExhaustively(t *testing.T) { - _, err := exec.LookPath("luac") + _, err := exec.LookPath("luac5.2") if err != nil { - t.Skipf("exhaustively testing the parser requires luac: %s", err) + t.Skipf("exhaustively testing the parser requires luac5.2: %s", err) } l := NewState() matches, err := filepath.Glob(filepath.Join("lua-tests", "*.lua")) @@ -73,8 +73,8 @@ func protectedTestParser(l *State, t *testing.T, source string) { }() t.Log("Compiling " + source) binary := strings.TrimSuffix(source, ".lua") + ".bin" - if err := exec.Command("luac", "-o", binary, source).Run(); err != nil { - t.Fatalf("luac failed to compile %s: %s", source, err) + if err := exec.Command("luac5.2", "-o", binary, source).Run(); err != nil { + t.Fatalf("luac5.2 failed to compile %s: %s", source, err) } t.Log("Parsing " + source) bin := load(l, t, binary) From 466c3751f38e369a809eacc2bd25020d87fee3c8 Mon Sep 17 00:00:00 2001 From: Diego Joss Date: Mon, 16 Dec 2024 22:19:08 +0100 Subject: [PATCH 2/3] testing: use t.Helper in expectEqual and expectDeepEqual --- parser_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/parser_test.go b/parser_test.go index dff1f26..7f47122 100644 --- a/parser_test.go +++ b/parser_test.go @@ -86,12 +86,14 @@ func protectedTestParser(l *State, t *testing.T, source string) { } func expectEqual(t *testing.T, x, y interface{}, m string) { + t.Helper() if x != y { t.Errorf("%s doesn't match: %v, %v\n", m, x, y) } } func expectDeepEqual(t *testing.T, x, y interface{}, m string) bool { + t.Helper() if reflect.DeepEqual(x, y) { return true } From 89460325f03172d4a16cafa3437505dca0bc8a29 Mon Sep 17 00:00:00 2001 From: Diego Joss Date: Mon, 16 Dec 2024 22:55:09 +0100 Subject: [PATCH 3/3] testing: accept small floating-point differences between C & Go lua It is known that the Go stdlib math functions may give different results than the C equivalents. See https://github.com/golang/go/issues/25270 --- parser_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/parser_test.go b/parser_test.go index 7f47122..d555d63 100644 --- a/parser_test.go +++ b/parser_test.go @@ -1,6 +1,7 @@ package lua import ( + "math" "os/exec" "path/filepath" "reflect" @@ -100,10 +101,34 @@ func expectDeepEqual(t *testing.T, x, y interface{}, m string) bool { if reflect.TypeOf(x).Kind() == reflect.Slice && reflect.ValueOf(y).Len() == 0 && reflect.ValueOf(x).Len() == 0 { return true } + // The following exception is necessary because Go stdlib math + // functions may be less precise than the C equivalents. In + // particular constants[153] in lua-tests/attrib.lua:360 + // which is the result of "10^33" gives different results. See + // https://github.com/golang/go/issues/25270 for discussion. + if m == "constants" { + xc, yc := x.([]value), y.([]value) + if len(xc) != len(yc) { + return false + } + for i := range xc { + if fx, ok := xc[i].(float64); ok { + fy, ok2 := yc[i].(float64) + if !ok2 || !similarFloat64(fx, fy) { + return false + } + } + } + return true + } t.Errorf("%s doesn't match: %v, %v\n", m, x, y) return false } +func similarFloat64(x, y float64) bool { + return x == y || math.Nextafter(x, y) == y +} + func compareClosures(t *testing.T, a, b *luaClosure) { expectEqual(t, a.upValueCount(), b.upValueCount(), "upvalue count") comparePrototypes(t, a.prototype, b.prototype)