-
Notifications
You must be signed in to change notification settings - Fork 6
/
run.go
57 lines (45 loc) · 1.02 KB
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package tool
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"github.com/bclicn/color"
"github.com/petuhovskiy/acos/tool/def"
"github.com/sergi/go-diff/diffmatchpatch"
)
func TestRun(exe string, test FileTest) {
conf := def.LoadConfig()
args := append([]string{}, conf.Defaults.RunArgs...)
for i, v := range args {
if v == "$exe" {
args[i] = exe
}
}
cmd := exec.Command(args[0], args[1:]...)
out := bytes.Buffer{}
cmd.Stdin = strings.NewReader(test.In)
cmd.Stdout = &out
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Println(color.BRed("ERR:"), "Solution failed.", err)
return
}
outString := string(out.Bytes())
if !strings.HasSuffix(outString, "\n") {
FWarn("Missing \\n")
}
if bytes.ContainsRune(out.Bytes(), 0) {
FWarn("Contains \\0")
}
if outString == test.Ans {
fmt.Println(color.BGreen("OK"))
return
}
fmt.Println(color.BRed("WA"), "Ans != Out")
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(outString, test.Ans, false)
fmt.Println(dmp.DiffPrettyText(diffs))
}