diff --git a/internal/asthelper/asthelper.go b/internal/asthelper/asthelper.go index c7e4b7b..c14ecba 100644 --- a/internal/asthelper/asthelper.go +++ b/internal/asthelper/asthelper.go @@ -28,6 +28,22 @@ func IntLit(value int) *ast.BasicLit { } } +// Float32Lit returns an ast.BasicLit of kind FLOAT, 32 bit +func Float32Lit(value float32) *ast.BasicLit { + return &ast.BasicLit{ + Kind: token.FLOAT, + Value: strconv.FormatFloat(float64(value), 'f', -1, 32), + } +} + +// Float64Lit returns an ast.BasicLit of kind FLOAT, 64 bit +func Float64Lit(value float64) *ast.BasicLit { + return &ast.BasicLit{ + Kind: token.FLOAT, + Value: strconv.FormatFloat(value, 'f', -1, 64), + } +} + // IndexExpr "name[index]" func IndexExpr(name string, index ast.Expr) *ast.IndexExpr { return &ast.IndexExpr{ diff --git a/main_test.go b/main_test.go index 2f468f8..2b59f1c 100644 --- a/main_test.go +++ b/main_test.go @@ -263,7 +263,7 @@ func bincmp(ts *testscript.TestScript, neg bool, args []string) { var literalGenerators = []func() *ast.BasicLit{ func() *ast.BasicLit { - buffer := make([]byte, 1+mathrand.Intn(math.MaxUint8)) + buffer := make([]byte, 1+mathrand.Intn(255)) _, err := mathrand.Read(buffer) if err != nil { panic(err) @@ -276,24 +276,19 @@ var literalGenerators = []func() *ast.BasicLit{ return ah.IntLit(i) }, func() *ast.BasicLit { - f := fmt.Sprint(mathrand.NormFloat64()) - return &ast.BasicLit{ - Kind: token.FLOAT, - Value: fmt.Sprint(f), - } + return ah.Float64Lit(mathrand.NormFloat64()) }, func() *ast.BasicLit { - f := fmt.Sprint(mathrand.Float32()) - return &ast.BasicLit{ - Kind: token.FLOAT, - Value: fmt.Sprint(f), - } + return ah.Float32Lit(mathrand.Float32()) }, } func generateLiterals(ts *testscript.TestScript, neg bool, args []string) { + if neg { + ts.Fatalf("unsupported: ! generate-literals") + } if len(args) != 3 { - ts.Fatalf("usage: generate-literals file.go literalCount funcName") + ts.Fatalf("usage: generate-literals file literalCount funcName") } codePath, funcName := args[0], args[2] @@ -306,9 +301,7 @@ func generateLiterals(ts *testscript.TestScript, neg bool, args []string) { var statements []ast.Stmt for i := 0; i < literalCount; i++ { literal := literalGenerators[i%len(literalGenerators)]() - statements = append(statements, &ast.ExprStmt{ - X: ah.CallExpr(ah.Ident("println"), literal), - }) + statements = append(statements, ah.ExprStmt(ah.CallExpr(ah.Ident("println"), literal))) } file := &ast.File{ @@ -319,9 +312,7 @@ func generateLiterals(ts *testscript.TestScript, neg bool, args []string) { Type: &ast.FuncType{ Params: &ast.FieldList{}, }, - Body: &ast.BlockStmt{ - List: statements, - }, + Body: ah.BlockStmt(statements...), }, }, } diff --git a/testdata/scripts/literals.txt b/testdata/scripts/literals.txt index 4e9f0b1..55cb251 100644 --- a/testdata/scripts/literals.txt +++ b/testdata/scripts/literals.txt @@ -1,3 +1,4 @@ +# Generate and write random literals into a separate file generate-literals extraLiterals.go 500 printExtraLiterals go build @@ -25,7 +26,7 @@ rm main$exe garble -literals build bincmp main$exe main_old$exe -# Also check that the binary is true random. +# Also check that the binary is different from previous builds. rm main$exe garble -literals -debugdir=.obf-src -seed=8J+Ri/Cfh6fwn4e+ build ! bincmp main$exe main_old$exe @@ -117,7 +118,6 @@ func main() { byteTest() numTest() boolTest() - printExtraLiterals() }