Skip to content

Accept small differences in floating-point results between C & Go lua implementations #138

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
35 changes: 31 additions & 4 deletions parser_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lua

import (
"math"
"os/exec"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -46,9 +47,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"))
Expand All @@ -73,8 +74,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)
Expand All @@ -86,22 +87,48 @@ 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
}
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)
Expand Down
Loading