Files
casino/pkg/fixed/nofloat_test.go
2026-08-05 02:19:06 +00:00

53 lines
1.4 KiB
Go

package fixed_test
import (
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
"testing"
)
// Determinism depends on there being no floating-point arithmetic anywhere in
// the simulation path: floats drift between architectures and between native
// and WASM builds, which would silently break round verification. This test
// fails the build if a float type is ever introduced.
//
// Test files are exempt, since statistical assertions legitimately use floats.
func TestNoFloatingPointInDeterministicPackages(t *testing.T) {
for _, dir := range []string{".", "../sim"} {
if _, err := os.Stat(dir); os.IsNotExist(err) {
continue
}
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, dir, nil, 0)
if err != nil {
t.Fatalf("parse %s: %v", dir, err)
}
for _, pkg := range pkgs {
for name, file := range pkg.Files {
if strings.HasSuffix(name, "_test.go") {
continue
}
ast.Inspect(file, func(n ast.Node) bool {
switch node := n.(type) {
case *ast.Ident:
if node.Name == "float32" || node.Name == "float64" {
t.Errorf("%s: forbidden float type %q in deterministic package",
filepath.Base(name), node.Name)
}
case *ast.BasicLit:
if node.Kind == token.FLOAT {
t.Errorf("%s: forbidden float literal %s",
filepath.Base(name), node.Value)
}
}
return true
})
}
}
}
}