feat(runner): compile Go and check answers per test group
This commit is contained in:
1 parent
e95c681165
commit
afee564bf4
61 files changed
+2592
-143
No files matched your search
@@ -142,10 +142,14 @@ func (h *Handler) toJudgeRequest(req *api.RunRequest) (judge.Request, []api.Prob
|
||||
fieldErrors = append(fieldErrors, invalidField(api.ProblemFieldLocationBody, groupPointer+"/id"))
|
||||
}
|
||||
groupIDs[group.ID] = true
|
||||
spec, valid := h.toCheckerSpec(group.Checker)
|
||||
if !valid {
|
||||
fieldErrors = append(fieldErrors, invalidField(api.ProblemFieldLocationBody, groupPointer+"/checker"))
|
||||
}
|
||||
converted := judge.Group{
|
||||
ID: string(group.ID),
|
||||
Visibility: judge.Visibility(group.Visibility),
|
||||
Checker: checker.Spec{Kind: checker.Kind(group.Checker.Kind)},
|
||||
Checker: spec,
|
||||
Tests: make([]judge.Test, 0, len(group.Tests)),
|
||||
}
|
||||
testIDs := make(map[api.TestKey]bool, len(group.Tests))
|
||||
@@ -165,6 +169,31 @@ func (h *Handler) toJudgeRequest(req *api.RunRequest) (judge.Request, []api.Prob
|
||||
return request, fieldErrors
|
||||
}
|
||||
|
||||
// toCheckerSpec converts a checker and reports whether its parameters fit
|
||||
// its kind: numbers needs a positive tolerance, custom needs a program in a
|
||||
// supported language, and other kinds take no parameter.
|
||||
func (h *Handler) toCheckerSpec(source api.Checker) (checker.Spec, bool) {
|
||||
spec := checker.Spec{
|
||||
Kind: checker.Kind(source.Kind),
|
||||
AbsTolerance: source.AbsTolerance.Or(0),
|
||||
RelTolerance: source.RelTolerance.Or(0),
|
||||
}
|
||||
hasTolerance := source.AbsTolerance.IsSet() || source.RelTolerance.IsSet()
|
||||
if program, ok := source.Program.Get(); ok {
|
||||
spec.Program = &checker.Program{Language: string(program.Language), Source: program.Source}
|
||||
}
|
||||
switch spec.Kind {
|
||||
case checker.Numbers:
|
||||
return spec, spec.Program == nil && spec.AbsTolerance+spec.RelTolerance > 0
|
||||
case checker.Custom:
|
||||
return spec, !hasTolerance && spec.Program != nil && h.supports(spec.Program.Language)
|
||||
case checker.Exact, checker.Tokens:
|
||||
return spec, !hasTolerance && spec.Program == nil
|
||||
default:
|
||||
return spec, false
|
||||
}
|
||||
}
|
||||
|
||||
func toRunStatus(snapshot jobs.Snapshot) api.RunStatus {
|
||||
status := api.RunStatus{RunID: snapshot.ID, State: api.RunStateRunning}
|
||||
if snapshot.State != jobs.Finished || snapshot.Result == nil {
|
||||
|
||||
@@ -82,7 +82,7 @@ func startServer(t *testing.T) *testServer {
|
||||
if err != nil {
|
||||
t.Fatalf("NewManager: %v", err)
|
||||
}
|
||||
supports := func(language string) bool { return language == "python3" }
|
||||
supports := func(language string) bool { return language == "python3" || language == "go" }
|
||||
handler := NewHandler(manager, supports, func() bool { return !manager.Draining() }, 3*time.Second, logger)
|
||||
httpHandler, err := New(handler, testToken, maxBody, logger)
|
||||
if err != nil {
|
||||
@@ -233,6 +233,29 @@ func TestSubmitRunIsIdempotentAndAnswersBusy(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubmitRunAcceptsParameterizedCheckers(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
server := startServer(t)
|
||||
body := runBody(secretSource)
|
||||
groups := body["groups"].([]any)
|
||||
numbers := map[string]any{
|
||||
"id": "hidden-1", "visibility": "hidden", "checker": map[string]any{"kind": "numbers", "abs_tolerance": 1e-6},
|
||||
"tests": []any{map[string]any{"id": "t1", "input": "1", "expected_output": "0.5"}},
|
||||
}
|
||||
custom := map[string]any{
|
||||
"id": "hidden-2", "visibility": "hidden",
|
||||
"checker": map[string]any{"kind": "custom", "program": map[string]any{"language": "python3", "source": "raise SystemExit(0)"}},
|
||||
"tests": []any{map[string]any{"id": "t1", "input": "1", "expected_output": "1"}},
|
||||
}
|
||||
body["groups"] = append(groups, numbers, custom)
|
||||
body["language"] = "go"
|
||||
response, decoded := server.do(t, http.MethodPut, runPath(uuid.New()), testToken, body)
|
||||
if response.StatusCode != http.StatusAccepted {
|
||||
t.Fatalf("PUT = %d %v, want 202", response.StatusCode, decoded)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubmitRunValidatesTheRequest(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -267,6 +290,20 @@ func TestSubmitRunValidatesTheRequest(t *testing.T) {
|
||||
t.Fatalf("invalid run ID = %d %v, want 400", response.StatusCode, body)
|
||||
}
|
||||
|
||||
for name, checkerValue := range map[string]map[string]any{
|
||||
"numbers without tolerance": {"kind": "numbers"},
|
||||
"exact with tolerance": {"kind": "exact", "abs_tolerance": 0.1},
|
||||
"custom without program": {"kind": "custom"},
|
||||
"tokens with program": {"kind": "tokens", "program": map[string]any{"language": "python3", "source": "x"}},
|
||||
} {
|
||||
invalid := runBody(secretSource)
|
||||
invalid["groups"].([]any)[0].(map[string]any)["checker"] = checkerValue
|
||||
response, body = server.do(t, http.MethodPut, runPath(uuid.New()), testToken, invalid)
|
||||
if response.StatusCode != http.StatusBadRequest || body["errors"].([]any)[0].(map[string]any)["pointer"] != "/groups/0/checker" {
|
||||
t.Fatalf("%s = %d %v, want 400 at /groups/0/checker", name, response.StatusCode, body)
|
||||
}
|
||||
}
|
||||
|
||||
response, body = server.do(t, http.MethodGet, runPath(uuid.New()), testToken, nil)
|
||||
if response.StatusCode != http.StatusNotFound || body["code"] != "not_found" {
|
||||
t.Fatalf("unknown run = %d %v, want 404", response.StatusCode, body)
|
||||
|
||||
Reference in new issue
Block a user