Skip to content

Commit

Permalink
Improve enc/dec test suite, most notably test on sio2jail (it was fixed)
Browse files Browse the repository at this point in the history
  • Loading branch information
top-sekret committed Dec 18, 2023
1 parent 6656d71 commit f839277
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 8 deletions.
50 changes: 50 additions & 0 deletions sio/executors/test/sources/rlecrashdec.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <cassert>
#include <cstddef>
#include <cstdio>
#include <cstdlib>

void enkoder() {
int last = '\n';
int ch;
std::size_t count = 0;

while ((ch = std::getchar()) != '\n') {
if (ch != last) {
if (last != '\n') {
std::putchar(last);
std::printf("%zu;", count);
}

count = 0;
}

++count;
last = ch;
}

if (last != '\n') {
std::putchar(last);
std::printf("%zu;", count);
}

std::putchar('\n');
}

void dekoder() { std::abort(); }

// rlelib.cpp
//
#include <cassert>
#include <cstdio>

extern void dekoder();
extern void enkoder();

int main() {
int ch = std::getchar();

assert(ch == 'D' || ch == 'E');
assert(std::getchar() == '\n');

(ch == 'D' ? dekoder : enkoder)();
}
35 changes: 35 additions & 0 deletions sio/executors/test/sources/rlecrashenc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <cassert>
#include <cstddef>
#include <cstdio>
#include <cstdlib>

void enkoder() { std::abort(); }

void dekoder() {
int ch;

while ((ch = std::getchar()) != '\n') {
std::size_t count;
assert(std::scanf("%zu;", &count) == 1);
while (count--) std::putchar(ch);
}

std::putchar('\n');
}

// rlelib.cpp
//
#include <cassert>
#include <cstdio>

extern void dekoder();
extern void enkoder();

int main() {
int ch = std::getchar();

assert(ch == 'D' || ch == 'E');
assert(std::getchar() == '\n');

(ch == 'D' ? dekoder : enkoder)();
}
56 changes: 56 additions & 0 deletions sio/executors/test/sources/rlememdec.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <cassert>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <vector>

void enkoder() {
int last = '\n';
int ch;
std::size_t count = 0;

while ((ch = std::getchar()) != '\n') {
if (ch != last) {
if (last != '\n') {
std::putchar(last);
std::printf("%zu;", count);
}

count = 0;
}

++count;
last = ch;
}

if (last != '\n') {
std::putchar(last);
std::printf("%zu;", count);
}

std::putchar('\n');
}

void dekoder() {
std::vector<void *> ps;
while (true) {
ps.push_back(std::malloc(1024 * 1024));
}
}

// rlelib.cpp
//
#include <cassert>
#include <cstdio>

extern void dekoder();
extern void enkoder();

int main() {
int ch = std::getchar();

assert(ch == 'D' || ch == 'E');
assert(std::getchar() == '\n');

(ch == 'D' ? dekoder : enkoder)();
}
44 changes: 36 additions & 8 deletions sio/executors/test/test_encdec.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
from sio.assertion_utils import ok_, eq_
from sio.compilers.job import run as run_compiler
import sio.executors.unsafe_exec
import sio.executors.sio2jail_exec
from sio.testing_utils import str_to_bool
from sio.workers import ft
from sio.workers.util import TemporaryCwd, tempcwd



EXECUTORS = {
'sio2jail': sio.executors.sio2jail_exec,
'unsafe': sio.executors.unsafe_exec
}
# Stolen from a sample problem package I used to test the encdec
EXECUTORS = {'unsafe': sio.executors.unsafe_exec}
RLE_TESTS = ['0a', '1']
SOURCES = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'sources')

Expand Down Expand Up @@ -41,11 +45,13 @@ def common_preparations():

def compile_file(file):
with TemporaryCwd('compile_code'):
run_compiler({
renv = run_compiler({
'source_file': '/%s.cpp' % file,
'compiler': 'system-cpp',
'out_file': '/%s.e' % file,
})
eq_(renv['result_code'], 'OK')
return renv


def in_(a, b, msg=None):
Expand All @@ -54,11 +60,12 @@ def in_(a, b, msg=None):
raise AssertionError(msg or "%r not in %r" % (a, b))


def make_run_env(file, test, new_settings=None):
def make_run_env(compile_renv, file, test, new_settings=None):
result = {
'chn_file': '/rlechn.e',
'chk_file': '/rlechk.e',
'exe_file': '/%s.e' % file,
'exec_info': compile_renv['exec_info'],
'hint_file': '/rle%s.hint' % test,
'in_file': '/rle%s.in' % test,
'encoder_memory_limit': '65536',
Expand All @@ -84,11 +91,12 @@ def print_env(env):


def run_all_configurations(file, func, new_settings=None):
compile_renv = compile_file(file)
for execname, executor in EXECUTORS.items():
for t in RLE_TESTS:
with TemporaryCwd('run_%s_%s' % (execname, t)):
print('Running test %s under executor %s' % (t, execname))
renv = executor.encdec_run(make_run_env(file, t, new_settings(execname, t) if new_settings else None))
renv = executor.encdec_run(make_run_env(compile_renv, file, t, new_settings(execname, t) if new_settings else None))
print_env(renv)
func(execname, t, renv)

Expand All @@ -104,7 +112,6 @@ def upload_files():

def test_encdec_run():
common_preparations()
compile_file('rle')
def check(execname, t, renv):
not_in_('failed_step', renv)
eq_(renv['checker_result_percentage'], 100.)
Expand All @@ -113,7 +120,6 @@ def check(execname, t, renv):

def test_encdec_encoder_timeout():
common_preparations()
compile_file('rleloopenc')
def check(execname, t, renv):
eq_(renv['failed_step'], 'encoder')
eq_(renv['encoder_result_code'], 'TLE')
Expand All @@ -122,17 +128,39 @@ def check(execname, t, renv):

def test_encdec_encoder_outofmem():
common_preparations()
compile_file('rlememenc')
def check(execname, t, renv):
eq_(renv['failed_step'], 'encoder')
in_(renv['encoder_result_code'], ('MLE', 'RE'))
run_all_configurations('rlememenc', check)


def test_encdec_encoder_crash():
common_preparations()
def check(execname, t, renv):
eq_(renv['failed_step'], 'encoder')
in_(renv['encoder_result_code'], ('RE'))
run_all_configurations('rlecrashenc', check)


def test_encdec_decoder_timeout():
common_preparations()
compile_file('rleloopdec')
def check(execname, t, renv):
eq_(renv['failed_step'], 'decoder')
eq_(renv['decoder_result_code'], 'TLE')
run_all_configurations('rleloopdec', check)


def test_encdec_decoder_outofmem():
common_preparations()
def check(execname, t, renv):
eq_(renv['failed_step'], 'decoder')
in_(renv['decoder_result_code'], ('MLE', 'RE'))
run_all_configurations('rlememdec', check)


def test_encdec_decoder_crash():
common_preparations()
def check(execname, t, renv):
eq_(renv['failed_step'], 'decoder')
in_(renv['decoder_result_code'], ('RE'))
run_all_configurations('rlecrashdec', check)

0 comments on commit f839277

Please sign in to comment.