feat(runner): compile Go and check answers per test group

This commit is contained in:
shira committed 2026-10-11 08:59:29 +03:00
1 parent e95c681165
commit afee564bf4
61 files changed
+2592 -143

No files matched your search

+69
View File
@@ -10,7 +10,10 @@ package checker
import (
"bytes"
"context"
"errors"
"fmt"
"math"
"strconv"
)
// Kind names a comparison rule of the runner contract.
@@ -23,11 +26,32 @@ const (
// Tokens accepts texts with the same sequence of whitespace-separated
// tokens.
Tokens Kind = "tokens"
// Numbers compares tokens like Tokens, but numeric tokens are equal
// within an absolute or a relative tolerance.
Numbers Kind = "numbers"
// Custom runs a checker program of the task family in the sandbox.
Custom Kind = "custom"
)
// ErrNeedsSandbox reports a kind that the judge builds with its sandbox.
var ErrNeedsSandbox = errors.New("checker.New - this checker kind runs in the sandbox")
// Spec selects and parameterizes a checker.
type Spec struct {
Kind Kind `json:"kind"`
// AbsTolerance and RelTolerance parameterize Numbers.
AbsTolerance float64 `json:"abs_tolerance,omitempty"`
RelTolerance float64 `json:"rel_tolerance,omitempty"`
// Program is the checker program of Custom.
Program *Program `json:"program,omitempty"`
}
// Program is the source of a custom checker. It reads the files input,
// expected, and actual, whose paths are its arguments, and exits with 0 to
// accept, 1 to reject, and any other status when it fails itself.
type Program struct {
Language string `json:"language"`
Source string `json:"source"`
}
// Input is everything a checker may look at for one test.
@@ -51,6 +75,13 @@ func New(spec Spec) (Checker, error) {
return exactChecker{}, nil
case Tokens:
return tokensChecker{}, nil
case Numbers:
if spec.AbsTolerance < 0 || spec.RelTolerance < 0 || spec.AbsTolerance == 0 && spec.RelTolerance == 0 {
return nil, errors.New("checker.New - numbers needs a positive tolerance")
}
return numbersChecker{absolute: spec.AbsTolerance, relative: spec.RelTolerance}, nil
case Custom:
return nil, ErrNeedsSandbox
default:
return nil, fmt.Errorf("checker.New - unknown kind %q", spec.Kind)
}
@@ -78,6 +109,44 @@ func (tokensChecker) Check(_ context.Context, input Input) (bool, error) {
return true, nil
}
type numbersChecker struct {
absolute float64
relative float64
}
func (c numbersChecker) Check(_ context.Context, input Input) (bool, error) {
expected := bytes.Fields(input.Expected)
actual := bytes.Fields(input.Actual)
if len(expected) != len(actual) {
return false, nil
}
for index := range expected {
if !c.tokensMatch(expected[index], actual[index]) {
return false, nil
}
}
return true, nil
}
// tokensMatch compares two finite numbers within the tolerance and any
// other tokens exactly.
func (c numbersChecker) tokensMatch(expected, actual []byte) bool {
if bytes.Equal(expected, actual) {
return true
}
want, wantErr := strconv.ParseFloat(string(expected), 64)
got, gotErr := strconv.ParseFloat(string(actual), 64)
if wantErr != nil || gotErr != nil || !finite(want) || !finite(got) {
return false
}
difference := math.Abs(got - want)
return difference <= c.absolute || difference <= c.relative*math.Abs(want)
}
func finite(value float64) bool {
return !math.IsNaN(value) && !math.IsInf(value, 0)
}
// normalizeLines replaces CRLF with LF, removes spaces and tabs at the end of
// every line, and removes trailing empty lines.
func normalizeLines(text []byte) []byte {