feat(runner): add the own code runner contract, client and nsjail service

This commit is contained in:
shira committed 2026-10-11 08:23:42 +03:00
1 parent 5e1db37746
commit cdf6bcbb91
109 files changed
+24084 -28

No files matched your search

+93
View File
@@ -0,0 +1,93 @@
// Package checker compares the output of a sandboxed program with the expected
// output of a test.
//
// A checker is selected by a Spec. Kinds that need parameters, such as a float
// tolerance, add fields to Spec; kinds that need to run code, such as a custom
// checker program, implement Checker with their own sandbox and use the
// context and the error result for cancellation and infrastructure failures.
package checker
import (
"bytes"
"context"
"fmt"
)
// Kind names a comparison rule of the runner contract.
type Kind string
const (
// Exact accepts texts that are equal after line-ending and trailing
// whitespace normalization.
Exact Kind = "exact"
// Tokens accepts texts with the same sequence of whitespace-separated
// tokens.
Tokens Kind = "tokens"
)
// Spec selects and parameterizes a checker.
type Spec struct {
Kind Kind `json:"kind"`
}
// Input is everything a checker may look at for one test.
type Input struct {
TestInput []byte
Expected []byte
Actual []byte
}
// Checker decides whether the actual output of a test is accepted.
type Checker interface {
// Check reports whether the output is accepted. An error means that the
// checker itself failed, not that the output is wrong.
Check(ctx context.Context, input Input) (bool, error)
}
// New returns the checker for spec.
func New(spec Spec) (Checker, error) {
switch spec.Kind {
case Exact:
return exactChecker{}, nil
case Tokens:
return tokensChecker{}, nil
default:
return nil, fmt.Errorf("checker.New - unknown kind %q", spec.Kind)
}
}
type exactChecker struct{}
func (exactChecker) Check(_ context.Context, input Input) (bool, error) {
return bytes.Equal(normalizeLines(input.Expected), normalizeLines(input.Actual)), nil
}
type tokensChecker struct{}
func (tokensChecker) 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 !bytes.Equal(expected[index], actual[index]) {
return false, nil
}
}
return true, nil
}
// 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 {
lines := bytes.Split(bytes.ReplaceAll(text, []byte("\r\n"), []byte("\n")), []byte("\n"))
for index, line := range lines {
lines[index] = bytes.TrimRight(line, " \t")
}
last := len(lines)
for last > 0 && len(lines[last-1]) == 0 {
last--
}
return bytes.Join(lines[:last], []byte("\n"))
}