/
test_case.go
97 lines (90 loc) · 1.85 KB
/
test_case.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"bytes"
"context"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"syscall"
"time"
)
type TestCase struct {
Title string `yaml:":title"`
Input string `yaml:":input"`
Output string `yaml:":output"`
}
func (tc *TestCase) save(dir string) error {
path := filepath.Join(dir, tc.Title+".in.txt")
err := ioutil.WriteFile(path, []byte(tc.Input), 0644)
if err != nil {
return err
}
path = filepath.Join(dir, tc.Title+".out.txt")
err = ioutil.WriteFile(path, []byte(tc.Output), 0644)
if err != nil {
return err
}
return nil
}
func (tc *TestCase) test(command string, args []string, timeLimit time.Duration) *TestCaseResult {
ctx, cancel := context.WithTimeout(context.Background(), timeLimit)
defer cancel()
res := TestCaseResult{TestCase: *tc}
cmd := exec.CommandContext(ctx, command, args...)
stdin, _ := cmd.StdinPipe()
stdout, _ := cmd.StdoutPipe()
stderr, _ := cmd.StderrPipe()
err := cmd.Start()
if err != nil {
res.Status = IE
res.Error = err
return &res
}
var wg sync.WaitGroup
wg.Add(3)
go func() {
_, err := io.Copy(stdin, strings.NewReader(tc.Input))
if e, ok := err.(*os.PathError); ok && e.Err == syscall.EPIPE {
// ignore EPIPE
} else if err != nil {
log.Println("failed to write to STDIN", err)
}
stdin.Close()
wg.Done()
}()
go func() {
io.Copy(&res.Stdout, stdout)
stdout.Close()
wg.Done()
}()
go func() {
io.Copy(&res.Stderr, stderr)
stderr.Close()
wg.Done()
}()
wg.Wait()
err = cmd.Wait()
if ctx.Err() == context.DeadlineExceeded {
res.Status = TLE
} else if err != nil {
res.Status = RE
res.Error = err
} else if res.Stdout.String() == tc.Output {
res.Status = AC
} else {
res.Status = WA
}
return &res
}
type TestCaseResult struct {
TestCase TestCase
Status Status
Stdout bytes.Buffer
Stderr bytes.Buffer
Error error
}