-
Notifications
You must be signed in to change notification settings - Fork 0
/
judging.go
171 lines (138 loc) · 4.13 KB
/
judging.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package polygon
import (
"fmt"
"path/filepath"
"time"
"github.com/mraron/njudge/pkg/problems"
"github.com/mraron/njudge/pkg/problems/checker"
)
type Test struct {
Method string `xml:"method,attr"`
Cmd string `xml:"cmd,attr"`
Sample bool `xml:"sample,attr"`
Points float64 `xml:"points,attr"`
Group string `xml:"group,attr"`
Input string
Answer string
Index int
}
func (tc Test) Testcase() problems.Testcase {
return problems.Testcase{
InputPath: tc.Input,
AnswerPath: tc.Answer,
VerdictName: problems.VerdictDR,
MaxScore: tc.Points,
}
}
type Dependency struct {
Group string `xml:"group,attr"`
}
type Group struct {
Name string `xml:"name,attr"`
PointsPolicy string `xml:"points-policy,attr"`
Points float64 `xml:"points,attr"`
Dependencies []Dependency `xml:"dependencies>dependency"`
}
type Testset struct {
Name string `xml:"name,attr"`
TimeLimit int `xml:"time-limit"`
MemoryLimit int `xml:"memory-limit"`
TestCount int `xml:"test-count"`
InputPathPattern string `xml:"input-path-pattern"`
AnswerPathPattern string `xml:"answer-path-pattern"`
Tests []Test `xml:"tests>test"`
Groups []Group `xml:"groups>group"`
}
func (ts Testset) Testset(path string) problems.Testset {
testset := problems.Testset{Name: ts.Name}
testcases := make(map[string][]Test)
for ind, tc := range ts.Tests {
tc.Input, tc.Answer = fmt.Sprintf(filepath.Join(path, ts.InputPathPattern), ind+1), fmt.Sprintf(filepath.Join(path, ts.AnswerPathPattern), ind+1)
tc.Index = ind + 1
if len(testcases[tc.Group]) == 0 {
testcases[tc.Group] = make([]Test, 0)
}
testcases[tc.Group] = append(testcases[tc.Group], tc)
}
if len(ts.Groups) == 0 {
ts.Groups = append(ts.Groups, Group{Name: "", PointsPolicy: "sum", Points: -1.0})
}
idx := 1
for _, grp := range ts.Groups {
testset.Groups = append(testset.Groups, problems.Group{})
group := &testset.Groups[len(testset.Groups)-1]
group.Name = grp.Name
if grp.PointsPolicy == "complete-group" {
group.Scoring = problems.ScoringGroup
} else if grp.PointsPolicy == "min-group" {
group.Scoring = problems.ScoringMin
} else {
group.Scoring = problems.ScoringSum
}
group.Dependencies = make([]string, 0, len(grp.Dependencies))
for _, dep := range grp.Dependencies {
group.Dependencies = append(group.Dependencies, dep.Group)
}
for _, tc := range testcases[grp.Name] {
testcase := tc.Testcase()
testcase.Index = idx
testcase.Testset = ts.Name
testcase.Group = group.Name
testcase.TimeLimit = time.Duration(ts.TimeLimit) * time.Millisecond
testcase.MemoryLimit = ts.MemoryLimit
if group.Scoring == problems.ScoringMin {
testcase.MaxScore = grp.Points
}
group.Testcases = append(group.Testcases, testcase)
idx++
}
}
return testset
}
func (ts Testset) Group(name string) (*Group, error) {
for ind, g := range ts.Groups {
if g.Name == name {
return &ts.Groups[ind], nil
}
}
return nil, fmt.Errorf("can't find group %s", name)
}
type Judging struct {
CpuName string `xml:"cpu-name,attr"`
CpuSpeed int `xml:"cpu-speed,attr"`
InputFile string `xml:"input-file,attr"`
OutputFile string `xml:"output-file,attr"`
Testsets []Testset `xml:"testset"`
}
func (j Judging) GetTestset(name string) (*Testset, error) {
for _, ts := range j.Testsets {
if name == ts.Name {
return &ts, nil
}
}
return nil, fmt.Errorf("no such testset %q", name)
}
func (p Problem) StatusSkeleton(name string) (*problems.Status, error) {
if name == "" {
name = "tests"
}
testset, err := p.Judging.GetTestset(name)
if err != nil {
return nil, err
}
feedback := []problems.Testset{testset.Testset(p.Path)}
if p.TaskType == "outputonly" {
for _, tc := range feedback[0].Testcases() {
tc.OutputPath = filepath.Base(tc.AnswerPath)
}
}
return &problems.Status{
Compiled: false,
CompilerOutput: "status skeleton",
FeedbackType: problems.FeedbackFromString(p.FeedbackType),
Feedback: feedback,
}, nil
}
func (p Problem) Checker() problems.Checker {
return checker.NewTestlib(filepath.Join(p.Path, "check"))
}