From 26bdfa9c844f057797455feaeb533f555b528417 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sun, 3 Nov 2019 18:37:46 +0100 Subject: [PATCH] cgo: add bare-bones test --- .circleci/config.yml | 2 +- Makefile | 2 +- cgo/cgo_test.go | 62 +++++++++++++++++++++++++++++++++++++++ cgo/testdata/basic.go | 3 ++ cgo/testdata/basic.out.go | 24 +++++++++++++++ 5 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 cgo/cgo_test.go create mode 100644 cgo/testdata/basic.go create mode 100644 cgo/testdata/basic.out.go diff --git a/.circleci/config.yml b/.circleci/config.yml index dda829b0..10210446 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -65,7 +65,7 @@ commands: - go-cache-v2-{{ checksum "go.mod" }} - llvm-source-linux - run: go install . - - run: go test -v ./interp ./transform . + - run: go test -v ./cgo ./interp ./transform . - run: make gen-device -j4 - run: make smoketest RISCV=0 - save_cache: diff --git a/Makefile b/Makefile index d8daeeba..3a869c39 100644 --- a/Makefile +++ b/Makefile @@ -139,7 +139,7 @@ tinygo: CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build -o build/tinygo$(EXE) -tags byollvm . test: - CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test -v -tags byollvm ./interp ./transform . + CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test -v -tags byollvm ./cgo ./interp ./transform . tinygo-test: cd tests/tinygotest && tinygo test diff --git a/cgo/cgo_test.go b/cgo/cgo_test.go new file mode 100644 index 00000000..52279629 --- /dev/null +++ b/cgo/cgo_test.go @@ -0,0 +1,62 @@ +package cgo + +import ( + "bytes" + "go/ast" + "go/format" + "go/parser" + "go/token" + "io/ioutil" + "path/filepath" + "strings" + "testing" +) + +func TestCGo(t *testing.T) { + var cflags = []string{"--target=armv6m-none-eabi"} + + for _, name := range []string{"basic"} { + name := name // avoid a race condition + t.Run(name, func(t *testing.T) { + t.Parallel() + + // Read the AST in memory. + path := filepath.Join("testdata", name+".go") + fset := token.NewFileSet() + f, err := parser.ParseFile(fset, path, nil, parser.ParseComments) + if err != nil { + t.Fatal("could not parse Go source file:", err) + } + + // Process the AST with CGo. + cgoAST, errs := Process([]*ast.File{f}, "testdata", fset, cflags) + for _, err := range errs { + t.Errorf("error during CGo processing: %v", err) + } + + // Store the (formatted) output in a buffer. Format it, so it + // becomes easier to read (and will hopefully change less with CGo + // changes). + buf := &bytes.Buffer{} + err = format.Node(buf, fset, cgoAST) + if err != nil { + t.Errorf("could not write out CGo AST: %v", err) + } + actual := strings.Replace(string(buf.Bytes()), "\r\n", "\n", -1) + + // Read the file with the expected output, to compare against. + outfile := filepath.Join("testdata", name+".out.go") + expectedBytes, err := ioutil.ReadFile(outfile) + if err != nil { + t.Fatalf("could not read expected output: %v", err) + } + expected := strings.Replace(string(expectedBytes), "\r\n", "\n", -1) + + // Check whether the output is as expected. + if expected != actual { + // It is not. Test failed. + t.Errorf("output did not match:\n%s", string(actual)) + } + }) + } +} diff --git a/cgo/testdata/basic.go b/cgo/testdata/basic.go new file mode 100644 index 00000000..706397e0 --- /dev/null +++ b/cgo/testdata/basic.go @@ -0,0 +1,3 @@ +package main + +import "C" diff --git a/cgo/testdata/basic.out.go b/cgo/testdata/basic.out.go new file mode 100644 index 00000000..26126a16 --- /dev/null +++ b/cgo/testdata/basic.out.go @@ -0,0 +1,24 @@ +package main + +import "unsafe" + +type C.int16_t = int16 +type C.int32_t = int32 +type C.int64_t = int64 +type C.int8_t = int8 +type C.uint16_t = uint16 +type C.uint32_t = uint32 +type C.uint64_t = uint64 +type C.uint8_t = uint8 +type C.uintptr_t = uintptr +type C.char uint8 +type C.int int32 +type C.long int32 +type C.longlong int64 +type C.schar int8 +type C.short int16 +type C.uchar uint8 +type C.uint uint32 +type C.ulong uint32 +type C.ulonglong uint64 +type C.ushort uint16