Skip to content

Commit

Permalink
Merge pull request #393 from xushiwei/q
Browse files Browse the repository at this point in the history
llgo cmptest (#391)
  • Loading branch information
xushiwei committed Jun 22, 2024
2 parents 24d345a + cd6d402 commit 26312e0
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 44 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ Here are some examples related to Go syntax:

* [concat](_demo/concat/concat.go): define a variadic function
* [genints](_demo/genints/genints.go): various forms of closure usage (including C function, recv.method and anonymous function)
* [errors](_demo/errors/errors.go): demo to implement error interface
* [defer](_demo/defer/defer.go): defer demo
* [errors](_cmptest/errors/errors.go): demo to implement error interface
* [defer](_cmptest/defer/defer.go): defer demo
* [goroutine](_demo/goroutine/goroutine.go): goroutine demo


Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion _demo/errors/errors.go → _cmptest/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ func (e *errorString) Error() string {

func main() {
err := New("an error")
println(err)
println(err.Error())
}
File renamed without changes.
17 changes: 16 additions & 1 deletion cmd/internal/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,29 @@ var Cmd = &base.Command{
Short: "Compile and run Go program",
}

// llgo cmptest
var CmpTestCmd = &base.Command{
UsageLine: "llgo cmptest [build flags] package [arguments...]",
Short: "Compile programs by llgo and go, run them and do comparative tests (stdout/stderr/exitcode)",
}

func init() {
Cmd.Run = runCmd
CmpTestCmd.Run = runCmpTest
}

func runCmd(cmd *base.Command, args []string) {
runCmdEx(cmd, args, build.ModeRun)
}

func runCmpTest(cmd *base.Command, args []string) {
runCmdEx(cmd, args, build.ModeCmpTest)
}

func runCmdEx(cmd *base.Command, args []string, mode build.Mode) {
args, runArgs, err := parseRunArgs(args)
check(err)
conf := build.NewDefaultConf(build.ModeRun)
conf := build.NewDefaultConf(mode)
conf.RunArgs = runArgs
build.Do(args, conf)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/llgo/llgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func init() {
build.Cmd,
install.Cmd,
run.Cmd,
run.CmpTestCmd,
clean.Cmd,
}
}
Expand Down
38 changes: 5 additions & 33 deletions internal/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const (
ModeBuild Mode = iota
ModeInstall
ModeRun
ModeCmpTest
)

const (
Expand Down Expand Up @@ -380,7 +381,8 @@ func linkMainPkg(pkg *packages.Package, pkgs []*aPackage, llFiles []string, conf
err := clang.New("").Exec(args...)
check(err)

if mode == ModeRun {
switch mode {
case ModeRun:
cmd := exec.Command(app, conf.RunArgs...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
Expand All @@ -389,6 +391,8 @@ func linkMainPkg(pkg *packages.Package, pkgs []*aPackage, llFiles []string, conf
if s := cmd.ProcessState; s != nil {
os.Exit(s.ExitCode())
}
case ModeCmpTest:
cmpTest("", pkgPath, app, conf.RunArgs)
}
return
}
Expand Down Expand Up @@ -687,38 +691,6 @@ func isPkgInMod(pkgPath, modPath string) bool {
return false
}

/*
func llgoPkgLinkFile(pkgPath string) string {
// if kind == cl.PkgLinkBitCode {
// return filepath.Join(llgoRoot()+pkgPath[len(llgoModPath):], "llgo_autogen.bc")
// }
llFile := filepath.Join(llgoRoot()+pkgPath[len(llgoModPath):], "llgo_autogen.ll")
if _, err := os.Stat(llFile); os.IsNotExist(err) {
decodeLinkFile(llFile)
}
return llFile
}
// *.ll => *.lla
func decodeLinkFile(llFile string) {
zipFile := llFile + "a"
zipf, err := zip.OpenReader(zipFile)
if err != nil {
return
}
defer zipf.Close()
f, err := zipf.Open("llgo_autogen.ll")
if err != nil {
return
}
defer f.Close()
data, err := io.ReadAll(f)
if err == nil {
os.WriteFile(llFile, data, 0644)
}
}
*/

func decodeFile(outFile string, zipf *zip.File) (err error) {
f, err := zipf.Open()
if err != nil {
Expand Down
78 changes: 78 additions & 0 deletions internal/build/cmptest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package build

import (
"bytes"
"errors"
"fmt"
"io"
"log"
"os"
"os/exec"
)

func cmpTest(dir, pkgPath, llApp string, runArgs []string) {
var goOut, goErr bytes.Buffer
var llgoOut, llgoErr bytes.Buffer
var llgoRunErr = runApp(runArgs, dir, &llgoOut, &llgoErr, llApp)
var goRunErr = runApp(runArgs, dir, &goOut, &goErr, "go", "run", pkgPath)
checkEqual("output", llgoOut.Bytes(), goOut.Bytes())
checkEqual("stderr", llgoErr.Bytes(), goErr.Bytes())
checkEqualRunErr(llgoRunErr, goRunErr)
}

func checkEqualRunErr(llgoRunErr, goRunErr error) {
if llgoRunErr == goRunErr {
return
}
fmt.Fprintln(os.Stderr, "=> Exit:", llgoRunErr)
fmt.Fprintln(os.Stderr, "\n=> Expected Exit:", goRunErr)
}

func checkEqual(prompt string, a, expected []byte) {
if bytes.Equal(a, expected) {
return
}

fmt.Fprintln(os.Stderr, "=> Result of", prompt)
os.Stderr.Write(a)

fmt.Fprintln(os.Stderr, "\n=> Expected", prompt)
os.Stderr.Write(expected)

fatal(errors.New("checkEqual: unexpected " + prompt))
}

func runApp(runArgs []string, dir string, stdout, stderr io.Writer, app string, args ...string) error {
if len(runArgs) > 0 {
if len(args) > 0 {
args = append(args, runArgs...)
} else {
args = runArgs
}
}
cmd := exec.Command(app, args...)
cmd.Dir = dir
cmd.Stdout = stdout
cmd.Stderr = stderr
return cmd.Run()
}

func fatal(err error) {
log.Panicln(err)
}
17 changes: 10 additions & 7 deletions internal/runtime/z_print.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ import (
"github.com/goplus/llgo/c"
)

func PrintByte(v byte) {
c.Fputc(c.Int(v), c.Stderr)
func boolCStr(v bool) *c.Char {
if v {
return c.Str("true")
}
return c.Str("false")
}

func PrintBool(v bool) {
if v {
c.Fprintf(c.Stderr, c.Str("true"))
} else {
c.Fprintf(c.Stderr, c.Str("false"))
}
c.Fprintf(c.Stderr, boolCStr(v))
}

func PrintByte(v byte) {
c.Fputc(c.Int(v), c.Stderr)
}

func PrintFloat(v float64) {
Expand Down

0 comments on commit 26312e0

Please sign in to comment.