Files
judge/dsl/memory_test.go
2026-04-10 18:45:40 +03:00

96 lines
2.0 KiB
Go

package dsl
import "testing"
func TestParseSizeLiteral(t *testing.T) {
cases := []struct {
in string
want int64
}{
{"256", 256},
{"256B", 256},
{"1K", 1024},
{"2KB", 2 * 1024},
{"4KiB", 4 * 1024},
{"256M", 256 * 1024 * 1024},
{"256MB", 256 * 1024 * 1024},
{"512MiB", 512 * 1024 * 1024},
{"1G", 1024 * 1024 * 1024},
{"2GB", 2 * 1024 * 1024 * 1024},
{"3GiB", 3 * 1024 * 1024 * 1024},
}
for _, c := range cases {
got, err := parseSizeLiteral(c.in, 0, 0)
if err != nil {
t.Errorf("parseSizeLiteral(%q) error: %v", c.in, err)
continue
}
if got != c.want {
t.Errorf("parseSizeLiteral(%q) = %d, want %d", c.in, got, c.want)
}
}
}
func TestParseSizeLiteralInvalid(t *testing.T) {
bad := []string{"abc", "100TB", "10XB", ""}
for _, s := range bad {
if _, err := parseSizeLiteral(s, 0, 0); err == nil {
t.Errorf("parseSizeLiteral(%q) expected error", s)
}
}
}
func TestParseMemoryLimit(t *testing.T) {
src := `
build "go build -o solution ."
timeout 10s
memory_limit = 256MB
group("g1") {
weight = 0.5
memory_limit = 128MiB
test("t1") {
stdout = "ok\n"
}
test("t2") {
memory_limit = 64M
stdout = "ok\n"
}
}
group("g2") {
weight = 0.5
test("inherits") {
stdout = "ok\n"
}
}
`
f, _, err := Parse(src)
if err != nil {
t.Fatalf("parse error: %v", err)
}
if f.MemoryLimit != 256*1024*1024 {
t.Errorf("file memory: got %d", f.MemoryLimit)
}
g1 := f.Groups[0]
if g1.MemoryLimit != 128*1024*1024 {
t.Errorf("g1 memory: got %d", g1.MemoryLimit)
}
if g1.Tests[0].MemoryLimit != 128*1024*1024 {
t.Errorf("g1.t1 memory (inherited from group): got %d", g1.Tests[0].MemoryLimit)
}
if g1.Tests[1].MemoryLimit != 64*1024*1024 {
t.Errorf("g1.t2 memory (override): got %d", g1.Tests[1].MemoryLimit)
}
g2 := f.Groups[1]
if g2.MemoryLimit != 256*1024*1024 {
t.Errorf("g2 memory (inherited from file): got %d", g2.MemoryLimit)
}
if g2.Tests[0].MemoryLimit != 256*1024*1024 {
t.Errorf("g2.inherits memory: got %d", g2.Tests[0].MemoryLimit)
}
}