contract

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package contract reads the contract and the shared cases of a problem node, and decides whether an output is right (spec 09.1 to 09.3).

Index

Constants

This section is empty.

Variables

View Source
var CaseFiles = []string{"basic", "edge", "regress"}

CaseFiles are the case files of a problem, in the order that the runner uses (spec 09.2).

View Source
var Checkers = []string{"exact", "unordered", "multiset", "float", "any_of", "custom"}

Checkers are the checker kinds of spec 09.3.

Functions

func Exists

func Exists(dir string) bool

Exists tells if dir has contract.yaml.

func Load

func Load(dir string) (*Contract, []Case, error)

Load reads contract.yaml and the case files of the problem node in dir.

func Short

func Short(s string) string

Short cuts a long JSON text for a report.

Types

type Case

type Case struct {
	Name      string                     `json:"name"`
	Class     []string                   `json:"class,omitempty"`
	T         string                     `json:"T,omitempty"`
	Args      map[string]json.RawMessage `json:"args"`
	Expect    json.RawMessage            `json:"expect,omitempty"`
	ExpectAny []json.RawMessage          `json:"expect_any,omitempty"`

	// File is the case file without .jsonl, such as basic. Line is the line in it.
	File string `json:"-"`
	Line int    `json:"-"`
}

Case is one line of a case file.

func (Case) ID

func (c Case) ID() string

ID is the id of the case in the protocol and in reports: the file and the name, such as basic/duplicates.

func (Case) Type

func (c Case) Type() string

Type is the type of T for the case: int unless the case says otherwise.

type Checker

type Checker struct {
	Kind string  `yaml:"kind" json:"kind"`
	Abs  float64 `yaml:"abs,omitempty" json:"abs,omitempty"`
	Rel  float64 `yaml:"rel,omitempty" json:"rel,omitempty"`
}

Checker is the checker kind of a contract. In YAML it is a word, such as exact, or a mapping with the tolerances of the float kind.

func (*Checker) UnmarshalYAML

func (c *Checker) UnmarshalYAML(n *yaml.Node) error

UnmarshalYAML accepts a word or a mapping.

type Contract

type Contract struct {
	Problem     string            `yaml:"problem" json:"problem"`
	Entry       string            `yaml:"entry" json:"entry"`
	Params      []Param           `yaml:"params" json:"params"`
	Returns     Return            `yaml:"returns" json:"returns"`
	Generic     map[string]string `yaml:"generic,omitempty" json:"generic,omitempty"`
	Bounds      map[string]any    `yaml:"bounds,omitempty" json:"bounds,omitempty"`
	Checker     Checker           `yaml:"checker" json:"checker"`
	Oracle      map[string]any    `yaml:"oracle,omitempty" json:"oracle,omitempty"`
	EdgeClasses []string          `yaml:"edge_classes,omitempty" json:"edge_classes,omitempty"`
	Metamorphic []string          `yaml:"metamorphic,omitempty" json:"metamorphic,omitempty"`
	Reviewed    map[string]string `yaml:"reviewed,omitempty" json:"reviewed,omitempty"`

	// Dir is the directory of the problem node.
	Dir string `yaml:"-" json:"-"`
}

Contract is contract.yaml of a problem node.

func (*Contract) CheckCase

func (k *Contract) CheckCase(c Case) error

CheckCase checks that a case gives each parameter a value of its type.

func (*Contract) Output

func (k *Contract) Output() int

Output is the index of the inout parameter whose value is the output, or -1 when the return value is the output.

func (*Contract) OutputType

func (k *Contract) OutputType(t string) Type

OutputType is the type of the output for a case whose T is t.

func (*Contract) StartCustom

func (k *Contract) StartCustom() (*Custom, error)

StartCustom starts the checker of the contract.

func (*Contract) Types

func (k *Contract) Types() []string

Types are the types that a generic contract is tested with (spec 08.3.4). A contract that is not generic has only int.

func (*Contract) Validate

func (k *Contract) Validate() error

Validate checks the parts of the contract that the runner needs.

func (*Contract) Verdict

func (k *Contract) Verdict(c Case, raw json.RawMessage, custom *Custom) (string, error)

Verdict tells if the output of an adapter is right for a case. It returns an empty reason when the output is right. The checker of the contract decides (spec 09.3). Before it, the output must have the output type, so an int result that is a float fails.

type Custom

type Custom struct {
	// contains filtered or unexported fields
}

Custom runs checker.py of a problem in one Python process (spec 09.3).

func (*Custom) Check

func (c *Custom) Check(cs Case, output, expected any) (string, error)

Check returns the reason of the checker, or an empty reason when the checker accepts the output.

func (*Custom) Close

func (c *Custom) Close()

Close stops the checker.

type Param

type Param struct {
	Name string `yaml:"name" json:"name"`
	Type string `yaml:"type" json:"type"`
	// Mode is "in" or "inout". An inout parameter is changed by the call, and its value after the call is the output when the entry returns unit.
	Mode string `yaml:"mode,omitempty" json:"mode,omitempty"`
	Pre  string `yaml:"pre,omitempty" json:"pre,omitempty"`
}

Param is one parameter of the entry.

type Return

type Return struct {
	Type string `yaml:"type" json:"type"`
	Post string `yaml:"post,omitempty" json:"post,omitempty"`
}

Return is the return value of the entry.

type Type

type Type struct {
	Name string
	Args []Type
}

Type is a type of the neutral type language (spec 08.3.3), such as list<int> or option<index>.

func Parse

func Parse(s string) (Type, error)

Parse reads a type. A single capital letter, such as T, is a type variable of a generic contract.

func (Type) Check

func (t Type) Check(v any) error

Check tells if the decoded JSON value v has the type t. It also checks the kind of each value, so a float where the type is int fails.

func (Type) HasVar

func (t Type) HasVar() bool

HasVar tells if the type has a type variable in it.

func (Type) IsVar

func (t Type) IsVar() bool

IsVar tells if the type is a type variable, such as T.

func (Type) String

func (t Type) String() string

func (Type) Subst

func (t Type) Subst(v, by string) Type

Subst replaces the type variable v with the type named by.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL