-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
84 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,87 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
// This test exists for code coverage and does not actually test anything. The | ||
// real tests are performed with run-tests.h. | ||
// | ||
// In the future it would be nice to combine them so that the files only have to | ||
// be compiled once and we don't need the extra bash script. | ||
var ( | ||
cPath = "build/a.out" | ||
goPath = "build/go.out" | ||
stdin = "7" | ||
args = []string{"some", "args"} | ||
) | ||
|
||
type programOut struct { | ||
stdout bytes.Buffer | ||
stderr bytes.Buffer | ||
isZero bool | ||
} | ||
|
||
// TestIntegrationScripts tests all programs in the tests directory | ||
func TestIntegrationScripts(t *testing.T) { | ||
files, err := filepath.Glob("tests/*/*.c") | ||
if err != nil { | ||
panic(err) | ||
t.Fatal(err) | ||
} | ||
|
||
for _, file := range files { | ||
Start([]string{file}) | ||
// Create build folder | ||
os.Mkdir("build/", os.ModePerm) | ||
|
||
t.Run(file, func(t *testing.T) { | ||
cProgram := programOut{} | ||
goProgram := programOut{} | ||
|
||
// Compile C | ||
err := exec.Command("clang", "-lm", "-o", cPath, file).Run() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Run C program | ||
cmd := exec.Command(cPath, args...) | ||
cmd.Stdin = strings.NewReader(stdin) | ||
cmd.Stdout = &cProgram.stdout | ||
cmd.Stderr = &cProgram.stderr | ||
err = cmd.Run() | ||
cProgram.isZero = err == nil | ||
|
||
// Compile Go | ||
goSrc := Start([]string{file}) | ||
ioutil.WriteFile("build/main.go", []byte(goSrc), os.ModePerm) | ||
err = exec.Command("go", "build", "-o", goPath, "build/main.go").Run() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Run Go program | ||
cmd = exec.Command(goPath, args...) | ||
cmd.Stdin = strings.NewReader(stdin) | ||
cmd.Stdout = &goProgram.stdout | ||
cmd.Stderr = &goProgram.stderr | ||
err = cmd.Run() | ||
goProgram.isZero = err == nil | ||
|
||
// Check if both exit codes are zero (or non-zero) | ||
if cProgram.isZero != goProgram.isZero { | ||
t.Fatalf("Expected: %t, Got: %t", cProgram.isZero, goProgram.isZero) | ||
} | ||
|
||
// Check stderr | ||
if cProgram.stderr.String() != goProgram.stderr.String() { | ||
t.Fatalf("Expected %q, Got: %q", cProgram.stderr.String(), goProgram.stderr.String()) | ||
} | ||
|
||
// Check stdout | ||
if cProgram.stdout.String() != goProgram.stdout.String() { | ||
t.Fatalf("Expected %q, Got: %q", cProgram.stdout.String(), goProgram.stdout.String()) | ||
} | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.