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
@@ -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 {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package checker
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -26,6 +27,33 @@ func TestCheckersCompareOutput(t *testing.T) {
|
||||
{"tokens different token", Tokens, "1 2 3", "1 2 4", false},
|
||||
{"tokens are case sensitive", Tokens, "Yes", "yes", false},
|
||||
}
|
||||
numbers := Spec{Kind: Numbers, AbsTolerance: 1e-6}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
expected string
|
||||
actual string
|
||||
accepted bool
|
||||
}{
|
||||
{"numbers within the absolute tolerance", "3.3333333333", "3.3333334", true},
|
||||
{"numbers outside the tolerance", "3.3333333333", "3.33", false},
|
||||
{"numbers in another notation", "0.000001 2", "1e-6 2.0000000", true},
|
||||
{"numbers keep words exact", "YES 1.5", "yes 1.5", false},
|
||||
{"numbers reject NaN", "1", "NaN", false},
|
||||
{"numbers reject infinity", "1e308", "Inf", false},
|
||||
{"numbers count tokens", "1 2", "1 2 3", false},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
check, err := New(numbers)
|
||||
if err != nil {
|
||||
t.Fatalf("New: %v", err)
|
||||
}
|
||||
accepted, err := check.Check(t.Context(), Input{Expected: []byte(tc.expected), Actual: []byte(tc.actual)})
|
||||
if err != nil || accepted != tc.accepted {
|
||||
t.Fatalf("Check(%q, %q) = %v, %v; want %v", tc.expected, tc.actual, accepted, err, tc.accepted)
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
@@ -50,4 +78,13 @@ func TestNewRejectsUnknownKind(t *testing.T) {
|
||||
if _, err := New(Spec{Kind: "float"}); err == nil {
|
||||
t.Fatal("New accepted an unknown checker kind")
|
||||
}
|
||||
if _, err := New(Spec{Kind: Numbers}); err == nil {
|
||||
t.Fatal("New accepted numbers without a tolerance")
|
||||
}
|
||||
if _, err := New(Spec{Kind: Numbers, AbsTolerance: -1}); err == nil {
|
||||
t.Fatal("New accepted a negative tolerance")
|
||||
}
|
||||
if _, err := New(Spec{Kind: Custom}); !errors.Is(err, ErrNeedsSandbox) {
|
||||
t.Fatalf("New(custom) = %v, want ErrNeedsSandbox", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user