mirror of
https://github.com/ton-blockchain/ton
synced 2025-02-12 11:12:16 +00:00
Tests for func with scripts
This commit is contained in:
parent
6a72aba9af
commit
5c2ad4a6c7
25 changed files with 1011 additions and 0 deletions
91
crypto/func/auto-tests/run_tests.py
Normal file
91
crypto/func/auto-tests/run_tests.py
Normal file
|
@ -0,0 +1,91 @@
|
|||
import os
|
||||
import os.path
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
def getenv(name, default=None):
|
||||
if name in os.environ:
|
||||
return os.environ[name]
|
||||
if default is None:
|
||||
print("Environment variable", name, "is not set", file=sys.stderr)
|
||||
exit(1)
|
||||
return default
|
||||
|
||||
FUNC_EXECUTABLE = getenv("FUNC_EXECUTABLE", "func")
|
||||
FIFT_EXECUTABLE = getenv("FIFT_EXECUTABLE", "fift")
|
||||
#FUNC_STDLIB = getenv("FUNC_STDLIB")
|
||||
FIFT_LIBS = getenv("FIFT_LIBS")
|
||||
TMP_DIR = tempfile.mkdtemp()
|
||||
COMPILED_FIF = os.path.join(TMP_DIR, "compiled.fif")
|
||||
RUNNER_FIF = os.path.join(TMP_DIR, "runner.fif")
|
||||
|
||||
if len(sys.argv) != 2:
|
||||
print("Usage : run_tests.py tests_dir", file=sys.stderr)
|
||||
exit(1)
|
||||
TESTS_DIR = sys.argv[1]
|
||||
|
||||
class ExecutionError(Exception):
|
||||
pass
|
||||
|
||||
def compile_func(f):
|
||||
res = subprocess.run([FUNC_EXECUTABLE, "-o", COMPILED_FIF, "-SPA", f], capture_output=True, timeout=10)
|
||||
if res.returncode != 0:
|
||||
raise ExecutionError(str(res.stderr, "utf-8"))
|
||||
|
||||
def run_runner():
|
||||
res = subprocess.run([FIFT_EXECUTABLE, "-I", FIFT_LIBS, RUNNER_FIF], capture_output=True, timeout=10)
|
||||
if res.returncode != 0:
|
||||
raise ExecutionError(str(res.stderr, "utf-8"))
|
||||
s = str(res.stdout, "utf-8")
|
||||
s = [x.strip() for x in s.split("\n")]
|
||||
return [x for x in s if x != ""]
|
||||
|
||||
tests = [s for s in os.listdir(TESTS_DIR) if s.endswith(".fc")]
|
||||
tests.sort()
|
||||
print("Found", len(tests), "tests", file=sys.stderr)
|
||||
for ti, tf in enumerate(tests):
|
||||
print("Running test %d/%d: %s" % (ti + 1, len(tests), tf), file=sys.stderr)
|
||||
tf = os.path.join(TESTS_DIR, tf)
|
||||
try:
|
||||
compile_func(tf)
|
||||
except ExecutionError as e:
|
||||
print(file=sys.stderr)
|
||||
print("Compilation error", file=sys.stderr)
|
||||
print(e, file=sys.stderr)
|
||||
exit(2)
|
||||
with open(tf, "r") as fd:
|
||||
lines = fd.readlines()
|
||||
cases = []
|
||||
for s in lines:
|
||||
s = [x.strip() for x in s.split("|")]
|
||||
if len(s) == 4 and s[0].strip() == "TESTCASE":
|
||||
cases.append(s[1:])
|
||||
if len(cases) == 0:
|
||||
print(file=sys.stderr)
|
||||
print("Error: no test cases", file=sys.stderr)
|
||||
exit(2)
|
||||
|
||||
with open(RUNNER_FIF, "w") as f:
|
||||
print("\"%s\" include <s constant code" % COMPILED_FIF, file=f)
|
||||
for function, test_in, _ in cases:
|
||||
print(test_in, function, "code 1 runvmx abort\"exitcode is not 0\" .s cr { drop } depth 1- times", file=f)
|
||||
try:
|
||||
func_out = run_runner()
|
||||
if len(func_out) != len(cases):
|
||||
raise ExecutionError("Unexpected number of lines")
|
||||
for i in range(len(func_out)):
|
||||
if func_out[i] != cases[i][2]:
|
||||
raise ExecutionError("Error on case %d: expected '%s', found '%s'" % (i + 1, cases[i][2], func_out[i]))
|
||||
except ExecutionError as e:
|
||||
print(file=sys.stderr)
|
||||
print("Error:", file=sys.stderr)
|
||||
print(e, file=sys.stderr)
|
||||
print(file=sys.stderr)
|
||||
print("Compiled:", file=sys.stderr)
|
||||
with open(COMPILED_FIF, "r") as f:
|
||||
print(f.read(), file=sys.stderr)
|
||||
exit(2)
|
||||
print(" OK, %d cases" % len(cases), file=sys.stderr)
|
||||
|
||||
print("Done", file=sys.stderr)
|
239
crypto/func/auto-tests/stress_tester.py
Normal file
239
crypto/func/auto-tests/stress_tester.py
Normal file
|
@ -0,0 +1,239 @@
|
|||
import os
|
||||
import os.path
|
||||
import random
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
def getenv(name, default=None):
|
||||
if name in os.environ:
|
||||
return os.environ[name]
|
||||
if default is not None:
|
||||
return default
|
||||
print("Environemnt variable", name, "is not set", file=sys.stderr)
|
||||
exit(1)
|
||||
|
||||
VAR_CNT = 5
|
||||
TMP_DIR = tempfile.mkdtemp()
|
||||
FUNC_EXECUTABLE = getenv("FUNC_EXECUTABLE", "func")
|
||||
FIFT_EXECUTABLE = getenv("FIFT_EXECUTABLE", "fift")
|
||||
FIFT_LIBS = getenv("FIFT_LIBS")
|
||||
MAGIC = 123456789
|
||||
|
||||
var_idx = 0
|
||||
def gen_var_name():
|
||||
global var_idx
|
||||
var_idx += 1
|
||||
return "i%d" % var_idx
|
||||
|
||||
class State:
|
||||
def __init__(self, x):
|
||||
self.x = x
|
||||
self.vs = [0] * VAR_CNT
|
||||
|
||||
class Code:
|
||||
pass
|
||||
|
||||
class CodeEmpty(Code):
|
||||
def execute(self, state):
|
||||
return None
|
||||
|
||||
def write(self, f, indent=0):
|
||||
pass
|
||||
|
||||
class CodeReturn(Code):
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
def execute(self, state):
|
||||
return [self.value] + state.vs
|
||||
|
||||
def write(self, f, indent=0):
|
||||
print(" " * indent + "return (%d, %s);" % (self.value, ", ".join("v%d" % i for i in range(VAR_CNT))), file=f)
|
||||
|
||||
class CodeAdd(Code):
|
||||
def __init__(self, i, value):
|
||||
self.i = i
|
||||
self.value = value
|
||||
|
||||
def execute(self, state):
|
||||
state.vs[self.i] += self.value
|
||||
return None
|
||||
|
||||
def write(self, f, indent=0):
|
||||
print(" " * indent + "v%d += %d;" % (self.i, self.value), file=f)
|
||||
|
||||
class CodeBlock(Code):
|
||||
def __init__(self, code):
|
||||
self.code = code
|
||||
|
||||
def execute(self, state):
|
||||
for c in self.code:
|
||||
res = c.execute(state)
|
||||
if res is not None:
|
||||
return res
|
||||
return None
|
||||
|
||||
def write(self, f, indent=0):
|
||||
for c in self.code:
|
||||
c.write(f, indent)
|
||||
|
||||
class CodeIfRange(Code):
|
||||
def __init__(self, l, r, c1, c2):
|
||||
self.l = l
|
||||
self.r = r
|
||||
self.c1 = c1
|
||||
self.c2 = c2
|
||||
|
||||
def execute(self, state):
|
||||
if self.l <= state.x < self.r:
|
||||
return self.c1.execute(state)
|
||||
else:
|
||||
return self.c2.execute(state)
|
||||
|
||||
def write(self, f, indent=0):
|
||||
print(" " * indent + "if (in(x, %d, %d)) {" % (self.l, self.r), file=f)
|
||||
self.c1.write(f, indent + 1)
|
||||
if isinstance(self.c2, CodeEmpty):
|
||||
print(" " * indent + "}", file=f)
|
||||
else:
|
||||
print(" " * indent + "} else {", file=f)
|
||||
self.c2.write(f, indent + 1)
|
||||
print(" " * indent + "}", file=f)
|
||||
|
||||
class CodeRepeat(Code):
|
||||
def __init__(self, n, c, loop_type):
|
||||
if loop_type == 2:
|
||||
n = max(n, 1)
|
||||
self.n = n
|
||||
self.c = c
|
||||
self.loop_type = loop_type
|
||||
|
||||
def execute(self, state):
|
||||
for _ in range(self.n):
|
||||
res = self.c.execute(state)
|
||||
if res is not None:
|
||||
return res
|
||||
return None
|
||||
|
||||
def write(self, f, indent=0):
|
||||
if self.loop_type == 0:
|
||||
print(" " * indent + "repeat (%d) {" % self.n, file=f)
|
||||
self.c.write(f, indent + 1)
|
||||
print(" " * indent + "}", file=f)
|
||||
elif self.loop_type == 1:
|
||||
var = gen_var_name()
|
||||
print(" " * indent + "int %s = 0;" % var, file=f)
|
||||
print(" " * indent + "while (%s < %d) {" % (var, self.n), file=f)
|
||||
self.c.write(f, indent + 1)
|
||||
print(" " * (indent + 1) + "%s += 1;" % var, file=f)
|
||||
print(" " * indent + "}", file=f)
|
||||
else:
|
||||
var = gen_var_name()
|
||||
print(" " * indent + "int %s = 0;" % var, file=f)
|
||||
print(" " * indent + "do {", file=f)
|
||||
self.c.write(f, indent + 1)
|
||||
print(" " * (indent + 1) + "%s += 1;" % var, file=f)
|
||||
print(" " * indent + "} until (%s >= %d);" % (var, self.n), file=f)
|
||||
|
||||
def write_function(f, name, body, inline=False, inline_ref=False, method_id=None):
|
||||
print("_ %s(int x)" % name, file=f, end="")
|
||||
if inline:
|
||||
print(" inline", file=f, end="")
|
||||
if inline_ref:
|
||||
print(" inline_ref", file=f, end="")
|
||||
if method_id is not None:
|
||||
print(" method_id(%d)" % method_id, file=f, end="")
|
||||
print(" {", file=f)
|
||||
for i in range(VAR_CNT):
|
||||
print(" int v%d = 0;" % i, file=f)
|
||||
body.write(f, 1);
|
||||
print("}", file=f)
|
||||
|
||||
def gen_code(xl, xr, with_return, loop_depth=0):
|
||||
code = []
|
||||
for _ in range(random.randint(0, 2)):
|
||||
if random.randint(0, 3) == 0 and loop_depth < 3:
|
||||
c = gen_code(xl, xr, False, loop_depth + 1)
|
||||
code.append(CodeRepeat(random.randint(0, 3), c, random.randint(0, 2)))
|
||||
elif xr - xl > 1:
|
||||
xmid = random.randrange(xl + 1, xr)
|
||||
ret = random.choice((0, 0, 0, 0, 0, 1, 2))
|
||||
c1 = gen_code(xl, xmid, ret == 1, loop_depth)
|
||||
if random.randrange(5) == 0:
|
||||
c2 = CodeEmpty()
|
||||
else:
|
||||
c2 = gen_code(xmid, xr, ret == 2, loop_depth)
|
||||
code.append(CodeIfRange(xl, xmid, c1, c2))
|
||||
if with_return:
|
||||
if xr - xl == 1:
|
||||
code.append(CodeReturn(random.randrange(10**9)))
|
||||
else:
|
||||
xmid = random.randrange(xl + 1, xr)
|
||||
c1 = gen_code(xl, xmid, True, loop_depth)
|
||||
c2 = gen_code(xmid, xr, True, loop_depth)
|
||||
code.append(CodeIfRange(xl, xmid, c1, c2))
|
||||
for _ in range(random.randint(0, 3)):
|
||||
pos = random.randint(0, len(code))
|
||||
code.insert(pos, CodeAdd(random.randrange(VAR_CNT), random.randint(0, 10**6)))
|
||||
if len(code) == 0:
|
||||
return CodeEmpty()
|
||||
return CodeBlock(code)
|
||||
|
||||
class ExecutionError(Exception):
|
||||
pass
|
||||
|
||||
def compile_func(fc, fif):
|
||||
res = subprocess.run([FUNC_EXECUTABLE, "-o", fif, "-SPA", fc], capture_output=True)
|
||||
if res.returncode != 0:
|
||||
raise ExecutionError(str(res.stderr, "utf-8"))
|
||||
|
||||
def runvm(compiled_fif, xl, xr):
|
||||
runner = os.path.join(TMP_DIR, "runner.fif")
|
||||
with open(runner, "w") as f:
|
||||
print("\"%s\" include <s constant code" % compiled_fif, file=f)
|
||||
for x in range(xl, xr):
|
||||
print("%d 0 code 1 runvmx abort\"exitcode is not 0\" .s cr { drop } depth 1- times" % x, file=f)
|
||||
res = subprocess.run([FIFT_EXECUTABLE, "-I", FIFT_LIBS, runner], capture_output=True)
|
||||
if res.returncode != 0:
|
||||
raise ExecutionError(str(res.stderr, "utf-8"))
|
||||
output = []
|
||||
for s in str(res.stdout, "utf-8").split("\n"):
|
||||
if s.strip() != "":
|
||||
output.append(list(map(int, s.split())))
|
||||
return output
|
||||
|
||||
cnt_ok = 0
|
||||
cnt_fail = 0
|
||||
for test_id in range(0, 1000000):
|
||||
random.seed(test_id)
|
||||
inline = random.randint(0, 2)
|
||||
xr = random.randint(1, 15)
|
||||
var_idx = 0
|
||||
code = gen_code(0, xr, True)
|
||||
fc = os.path.join(TMP_DIR, "code.fc")
|
||||
fif = os.path.join(TMP_DIR, "compiled.fif")
|
||||
with open(fc, "w") as f:
|
||||
print("int in(int x, int l, int r) impure { return (l <= x) & (x < r); }", file=f)
|
||||
write_function(f, "foo", code, inline=(inline == 1), inline_ref=(inline == 2))
|
||||
print("_ main(int x) {", file=f)
|
||||
print(" (int ret, %s) = foo(x);" % ", ".join("int v%d" % i for i in range(VAR_CNT)), file=f)
|
||||
print(" return (ret, %s, %d);" % (", ".join("v%d" % i for i in range(VAR_CNT)), MAGIC), file=f)
|
||||
print("}", file=f)
|
||||
compile_func(fc, fif)
|
||||
ok = True
|
||||
try:
|
||||
output = runvm(fif, 0, xr)
|
||||
for x in range(xr):
|
||||
my_out = code.execute(State(x)) + [MAGIC]
|
||||
fc_out = output[x]
|
||||
if my_out != fc_out:
|
||||
ok = False
|
||||
break
|
||||
except ExecutionError:
|
||||
ok = False
|
||||
if ok:
|
||||
cnt_ok += 1
|
||||
else:
|
||||
cnt_fail += 1
|
||||
print("Test %-6d %-6s ok:%-6d fail:%-6d" % (test_id, "OK" if ok else "FAIL", cnt_ok, cnt_fail), file=sys.stderr)
|
17
crypto/func/auto-tests/tests/a10.fc
Normal file
17
crypto/func/auto-tests/tests/a10.fc
Normal file
|
@ -0,0 +1,17 @@
|
|||
_ main(int a, int x) {
|
||||
int y = 0;
|
||||
int z = 0;
|
||||
while ((y = x * x) > a) {
|
||||
x -= 1;
|
||||
z = 1;
|
||||
}
|
||||
return (y, z);
|
||||
}
|
||||
|
||||
{-
|
||||
method_id | in | out
|
||||
TESTCASE | 0 | 101 15 | 100 1
|
||||
TESTCASE | 0 | 101 14 | 100 1
|
||||
TESTCASE | 0 | 101 10 | 100 0
|
||||
TESTCASE | 0 | 100 10 | 100 0
|
||||
-}
|
89
crypto/func/auto-tests/tests/a6.fc
Normal file
89
crypto/func/auto-tests/tests/a6.fc
Normal file
|
@ -0,0 +1,89 @@
|
|||
(int, int) f(int a, int b, int c, int d, int e, int f) {
|
||||
;; solve a 2x2 linear equation
|
||||
int D = a * d - b * c;
|
||||
int Dx = e * d - b * f;
|
||||
int Dy = a * f - e * c;
|
||||
return (Dx / D, Dy / D);
|
||||
}
|
||||
|
||||
int calc_phi() {
|
||||
var n = 1;
|
||||
repeat (70) { n *= 10; }
|
||||
var p = var q = 1;
|
||||
do {
|
||||
(p, q) = (q, p + q);
|
||||
} until (q > n);
|
||||
return muldivr(p, n, q);
|
||||
}
|
||||
|
||||
int calc_sqrt2() {
|
||||
var n = 1;
|
||||
repeat (70) { n *= 10; }
|
||||
var p = var q = 1;
|
||||
do {
|
||||
var t = p + q;
|
||||
(p, q) = (q, t + q);
|
||||
} until (q > n);
|
||||
return muldivr(p, n, q);
|
||||
}
|
||||
|
||||
var calc_root(m) {
|
||||
int base = 1;
|
||||
repeat(70) { base *= 10; }
|
||||
var (a, b, c) = (1, 0, - m);
|
||||
var (p1, q1, p2, q2) = (1, 0, 0, 1);
|
||||
do {
|
||||
int k = -1;
|
||||
var (a1, b1, c1) = (0, 0, 0);
|
||||
do {
|
||||
k += 1;
|
||||
(a1, b1, c1) = (a, b, c);
|
||||
c += b;
|
||||
c += b += a;
|
||||
} until (c > 0);
|
||||
(a, b, c) = (- c1, - b1, - a1);
|
||||
(p1, q1) = (k * p1 + q1, p1);
|
||||
(p2, q2) = (k * p2 + q2, p2);
|
||||
} until (p1 > base);
|
||||
return (p1, q1, p2, q2);
|
||||
}
|
||||
|
||||
{-
|
||||
operator _/%_ infix 20;
|
||||
|
||||
(int, int) ((int x) /% (int y)) {
|
||||
return (x / y, x % y);
|
||||
}
|
||||
|
||||
(int, int) _/%_ (int x, int y) {
|
||||
return (x / y, x % y);
|
||||
}
|
||||
-}
|
||||
|
||||
int ataninv(int base, int q) { ;; computes base*atan(1/q)
|
||||
base ~/= q;
|
||||
q *= - q;
|
||||
int sum = 0;
|
||||
int n = 1;
|
||||
do {
|
||||
sum += base ~/ n;
|
||||
base ~/= q;
|
||||
n += 2;
|
||||
} until base == 0;
|
||||
return sum;
|
||||
}
|
||||
|
||||
int calc_pi() {
|
||||
int base = 64;
|
||||
repeat (70) { base *= 10; }
|
||||
return (ataninv(base << 2, 5) - ataninv(base, 239)) ~>> 4;
|
||||
}
|
||||
|
||||
int main() {
|
||||
return calc_pi();
|
||||
}
|
||||
|
||||
{-
|
||||
method_id | in | out
|
||||
TESTCASE | 0 | | 31415926535897932384626433832795028841971693993751058209749445923078164
|
||||
-}
|
16
crypto/func/auto-tests/tests/a6_1.fc
Normal file
16
crypto/func/auto-tests/tests/a6_1.fc
Normal file
|
@ -0,0 +1,16 @@
|
|||
(int, int) main(int a, int b, int c, int d, int e, int f) {
|
||||
int D = a * d - b * c;
|
||||
int Dx = e * d - b * f;
|
||||
int Dy = a * f - e * c;
|
||||
return (Dx / D, Dy / D);
|
||||
}
|
||||
|
||||
{-
|
||||
method_id | in | out
|
||||
TESTCASE | 0 | 1 1 1 -1 10 6 | 8 2
|
||||
TESTCASE | 0 | 817 -31 624 -241 132272 272276 | 132 -788
|
||||
TESTCASE | 0 | -886 562 498 -212 -36452 -68958 | -505 -861
|
||||
TESTCASE | 0 | 448 -433 -444 792 150012 -356232 | -218 -572
|
||||
TESTCASE | 0 | -40 -821 433 -734 -721629 -741724 | -206 889
|
||||
TESTCASE | 0 | -261 -98 -494 868 -166153 733738 | 263 995
|
||||
-}
|
24
crypto/func/auto-tests/tests/a6_5.fc
Normal file
24
crypto/func/auto-tests/tests/a6_5.fc
Normal file
|
@ -0,0 +1,24 @@
|
|||
var twice(f, x) {
|
||||
return f (f x);
|
||||
}
|
||||
|
||||
_ sqr(x) {
|
||||
return x * x;
|
||||
}
|
||||
|
||||
var main(x) {
|
||||
var f = sqr;
|
||||
return twice(f, x) * f(x);
|
||||
}
|
||||
|
||||
var pow6(x) method_id(4) {
|
||||
return twice(sqr, x) * sqr(x);
|
||||
}
|
||||
|
||||
{-
|
||||
method_id | in | out
|
||||
TESTCASE | 0 | 3 | 729
|
||||
TESTCASE | 0 | 10 | 1000000
|
||||
TESTCASE | 4 | 3 | 729
|
||||
TESTCASE | 4 | 10 | 1000000
|
||||
-}
|
24
crypto/func/auto-tests/tests/a7.fc
Normal file
24
crypto/func/auto-tests/tests/a7.fc
Normal file
|
@ -0,0 +1,24 @@
|
|||
() main() { }
|
||||
int steps(int x) method_id(1) {
|
||||
var n = 0;
|
||||
while (x > 1) {
|
||||
n += 1;
|
||||
if (x & 1) {
|
||||
x = 3 * x + 1;
|
||||
} else {
|
||||
x >>= 1;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
{-
|
||||
method_id | in | out
|
||||
TESTCASE | 1 | 1 | 0
|
||||
TESTCASE | 1 | 2 | 1
|
||||
TESTCASE | 1 | 5 | 5
|
||||
TESTCASE | 1 | 19 | 20
|
||||
TESTCASE | 1 | 27 | 111
|
||||
TESTCASE | 1 | 100 | 25
|
||||
-}
|
||||
|
17
crypto/func/auto-tests/tests/c2.fc
Normal file
17
crypto/func/auto-tests/tests/c2.fc
Normal file
|
@ -0,0 +1,17 @@
|
|||
global ((int, int) -> int) op;
|
||||
|
||||
int check_assoc(int a, int b, int c) {
|
||||
return op(op(a, b), c) == op(a, op(b, c));
|
||||
}
|
||||
|
||||
int main(int x, int y, int z) {
|
||||
op = _+_;
|
||||
return check_assoc(x, y, z);
|
||||
}
|
||||
|
||||
{-
|
||||
method_id | in | out
|
||||
TESTCASE | 0 | 2 3 9 | -1
|
||||
TESTCASE | 0 | 11 22 44 | -1
|
||||
TESTCASE | 0 | -1 -10 -20 | -1
|
||||
-}
|
14
crypto/func/auto-tests/tests/c2_1.fc
Normal file
14
crypto/func/auto-tests/tests/c2_1.fc
Normal file
|
@ -0,0 +1,14 @@
|
|||
_ check_assoc(op, a, b, c) {
|
||||
return op(op(a, b), c) == op(a, op(b, c));
|
||||
}
|
||||
|
||||
int main(int x, int y, int z) {
|
||||
return check_assoc(_+_, x, y, z);
|
||||
}
|
||||
|
||||
{-
|
||||
method_id | in | out
|
||||
TESTCASE | 0 | 2 3 9 | -1
|
||||
TESTCASE | 0 | 11 22 44 | -1
|
||||
TESTCASE | 0 | -1 -10 -20 | -1
|
||||
-}
|
60
crypto/func/auto-tests/tests/co1.fc
Normal file
60
crypto/func/auto-tests/tests/co1.fc
Normal file
|
@ -0,0 +1,60 @@
|
|||
const int1 = 1, int2 = 2;
|
||||
|
||||
const int int101 = 101;
|
||||
const int int111 = 111;
|
||||
|
||||
const int1r = int1;
|
||||
|
||||
const str1 = "const1", str2 = "aabbcc"s;
|
||||
|
||||
const slice str2r = str2;
|
||||
|
||||
const str1int = 0x636f6e737431;
|
||||
const str2int = 0xAABBCC;
|
||||
|
||||
const int nibbles = 4;
|
||||
|
||||
int iget1() { return int1; }
|
||||
int iget2() { return int2; }
|
||||
int iget3() { return int1 + int2; }
|
||||
|
||||
int iget1r() { return int1r; }
|
||||
|
||||
slice sget1() { return str1; }
|
||||
slice sget2() { return str2; }
|
||||
slice sget2r() { return str2r; }
|
||||
|
||||
const int int240 = ((int1 + int2) * 10) << 3;
|
||||
|
||||
int iget240() { return int240; }
|
||||
|
||||
builder newc() asm "NEWC";
|
||||
slice endcs(builder b) asm "ENDC" "CTOS";
|
||||
int sdeq (slice s1, slice s2) asm "SDEQ";
|
||||
builder stslicer(builder b, slice s) asm "STSLICER";
|
||||
|
||||
_ main() {
|
||||
int i1 = iget1();
|
||||
int i2 = iget2();
|
||||
int i3 = iget3();
|
||||
|
||||
throw_unless(int101, i1 == 1);
|
||||
throw_unless(102, i2 == 2);
|
||||
throw_unless(103, i3 == 3);
|
||||
|
||||
slice s1 = sget1();
|
||||
slice s2 = sget2();
|
||||
slice s3 = newc().stslicer(str1).stslicer(str2r).endcs();
|
||||
|
||||
throw_unless(int111, sdeq(s1, newc().store_uint(str1int, 12 * nibbles).endcs()));
|
||||
throw_unless(112, sdeq(s2, newc().store_uint(str2int, 6 * nibbles).endcs()));
|
||||
throw_unless(113, sdeq(s3, newc().store_uint(0x636f6e737431AABBCC, 18 * nibbles).endcs()));
|
||||
|
||||
int i4 = iget240();
|
||||
throw_unless(104, i4 == 240);
|
||||
return 0;
|
||||
}
|
||||
|
||||
{-
|
||||
TESTCASE | 0 | | 0
|
||||
-}
|
19
crypto/func/auto-tests/tests/code_after_ifelse.fc
Normal file
19
crypto/func/auto-tests/tests/code_after_ifelse.fc
Normal file
|
@ -0,0 +1,19 @@
|
|||
int foo(int x) inline method_id(1) {
|
||||
if (x == 1) {
|
||||
return 111;
|
||||
} else {
|
||||
x *= 2;
|
||||
}
|
||||
return x + 1;
|
||||
}
|
||||
(int, int) main(int x) {
|
||||
return (foo(x), 222);
|
||||
}
|
||||
|
||||
{-
|
||||
method_id | in | out
|
||||
TESTCASE | 1 | 1 | 111
|
||||
TESTCASE | 1 | 3 | 7
|
||||
TESTCASE | 0 | 1 | 111 222
|
||||
TESTCASE | 0 | 3 | 7 222
|
||||
-}
|
61
crypto/func/auto-tests/tests/inline_big.fc
Normal file
61
crypto/func/auto-tests/tests/inline_big.fc
Normal file
|
@ -0,0 +1,61 @@
|
|||
int foo(int x) inline {
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
x = x * 10 + 1;
|
||||
return x;
|
||||
}
|
||||
|
||||
(int) main(int x) {
|
||||
return foo(x) * 10 + 5;
|
||||
}
|
||||
{-
|
||||
method_id | in | out
|
||||
TESTCASE | 0 | 9 | 9111111111111111111111111111111111111111111111111115
|
||||
-}
|
26
crypto/func/auto-tests/tests/inline_if.fc
Normal file
26
crypto/func/auto-tests/tests/inline_if.fc
Normal file
|
@ -0,0 +1,26 @@
|
|||
int foo1(int x) {
|
||||
if (x == 1) {
|
||||
return 1;
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
int foo2(int x) inline {
|
||||
if (x == 1) {
|
||||
return 11;
|
||||
}
|
||||
return 22;
|
||||
}
|
||||
int foo3(int x) inline_ref {
|
||||
if (x == 1) {
|
||||
return 111;
|
||||
}
|
||||
return 222;
|
||||
}
|
||||
(int, int, int) main(int x) {
|
||||
return (foo1(x) + 1, foo2(x) + 1, foo3(x) + 1);
|
||||
}
|
||||
{-
|
||||
method_id | in | out
|
||||
TESTCASE | 0 | 1 | 2 12 112
|
||||
TESTCASE | 0 | 2 | 3 23 223
|
||||
-}
|
43
crypto/func/auto-tests/tests/inline_loops.fc
Normal file
43
crypto/func/auto-tests/tests/inline_loops.fc
Normal file
|
@ -0,0 +1,43 @@
|
|||
global int g;
|
||||
|
||||
_ foo_repeat() impure inline {
|
||||
g = 1;
|
||||
repeat(5) {
|
||||
g *= 2;
|
||||
}
|
||||
}
|
||||
|
||||
int foo_until() impure inline {
|
||||
g = 1;
|
||||
int i = 0;
|
||||
do {
|
||||
g *= 2;
|
||||
i += 1;
|
||||
} until (i >= 8);
|
||||
return i;
|
||||
}
|
||||
|
||||
int foo_while() impure inline {
|
||||
g = 1;
|
||||
int i = 0;
|
||||
while (i < 10) {
|
||||
g *= 2;
|
||||
i += 1;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
_ main() {
|
||||
foo_repeat();
|
||||
int x = g;
|
||||
foo_until();
|
||||
int y = g;
|
||||
foo_while();
|
||||
int z = g;
|
||||
return (x, y, z);
|
||||
}
|
||||
|
||||
{-
|
||||
method_id | in | out
|
||||
TESTCASE | 0 | | 32 256 1024
|
||||
-}
|
12
crypto/func/auto-tests/tests/method_id.fc
Normal file
12
crypto/func/auto-tests/tests/method_id.fc
Normal file
|
@ -0,0 +1,12 @@
|
|||
int foo1() method_id(1) { return 111; }
|
||||
int foo2() method_id(3) { return 222; }
|
||||
int foo3() method_id(10) { return 333; }
|
||||
int main() { return 999; }
|
||||
|
||||
{-
|
||||
method_id | in | out
|
||||
TESTCASE | 1 | | 111
|
||||
TESTCASE | 3 | | 222
|
||||
TESTCASE | 10 | | 333
|
||||
TESTCASE | 0 | | 999
|
||||
-}
|
54
crypto/func/auto-tests/tests/s1.fc
Normal file
54
crypto/func/auto-tests/tests/s1.fc
Normal file
|
@ -0,0 +1,54 @@
|
|||
slice ascii_slice() method_id {
|
||||
return "string";
|
||||
}
|
||||
|
||||
slice raw_slice() method_id {
|
||||
return "abcdef"s;
|
||||
}
|
||||
|
||||
slice addr_slice() method_id {
|
||||
return "Ef8zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzM0vF"a;
|
||||
}
|
||||
|
||||
int string_hex() method_id {
|
||||
return "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345"u;
|
||||
}
|
||||
|
||||
int string_minihash() method_id {
|
||||
return "transfer(slice, int)"h;
|
||||
}
|
||||
|
||||
int string_maxihash() method_id {
|
||||
return "transfer(slice, int)"H;
|
||||
}
|
||||
|
||||
int string_crc32() method_id {
|
||||
return "transfer(slice, int)"c;
|
||||
}
|
||||
|
||||
builder newc() asm "NEWC";
|
||||
slice endcs(builder b) asm "ENDC" "CTOS";
|
||||
int sdeq (slice s1, slice s2) asm "SDEQ";
|
||||
|
||||
_ main() {
|
||||
slice s_ascii = ascii_slice();
|
||||
slice s_raw = raw_slice();
|
||||
slice s_addr = addr_slice();
|
||||
int i_hex = string_hex();
|
||||
int i_mini = string_minihash();
|
||||
int i_maxi = string_maxihash();
|
||||
int i_crc = string_crc32();
|
||||
throw_unless(101, sdeq(s_ascii, newc().store_uint(0x737472696E67, 12 * 4).endcs()));
|
||||
throw_unless(102, sdeq(s_raw, newc().store_uint(0xABCDEF, 6 * 4).endcs()));
|
||||
throw_unless(103, sdeq(s_addr, newc().store_uint(4, 3).store_int(-1, 8)
|
||||
.store_uint(0x3333333333333333333333333333333333333333333333333333333333333333, 256).endcs()));
|
||||
throw_unless(104, i_hex == 0x4142434445464748494A4B4C4D4E4F505152535455565758595A303132333435);
|
||||
throw_unless(105, i_mini == 0x7a62e8a8);
|
||||
throw_unless(106, i_maxi == 0x7a62e8a8ebac41bd6de16c65e7be363bc2d2cbc6a0873778dead4795c13db979);
|
||||
throw_unless(107, i_crc == 2235694568);
|
||||
return 0;
|
||||
}
|
||||
|
||||
{-
|
||||
TESTCASE | 0 | | 0
|
||||
-}
|
17
crypto/func/auto-tests/tests/unbalanced_ret.fc
Normal file
17
crypto/func/auto-tests/tests/unbalanced_ret.fc
Normal file
|
@ -0,0 +1,17 @@
|
|||
(int, int) main(int x) {
|
||||
int y = 5;
|
||||
if (x < 0) {
|
||||
x *= 2;
|
||||
y += 1;
|
||||
if (x == -10) {
|
||||
return (111, 0);
|
||||
}
|
||||
}
|
||||
return (x + 1, y);
|
||||
}
|
||||
{-
|
||||
method_id | in | out
|
||||
TESTCASE | 0 | 10 | 11 5
|
||||
TESTCASE | 0 | -5 | 111 0
|
||||
TESTCASE | 0 | -4 | -7 6
|
||||
-}
|
18
crypto/func/auto-tests/tests/unbalanced_ret_inline.fc
Normal file
18
crypto/func/auto-tests/tests/unbalanced_ret_inline.fc
Normal file
|
@ -0,0 +1,18 @@
|
|||
int foo(int x) inline {
|
||||
if (x < 0) {
|
||||
x *= 2;
|
||||
if (x == -10) {
|
||||
return 111;
|
||||
}
|
||||
}
|
||||
return x + 1;
|
||||
}
|
||||
int main(int x) {
|
||||
return foo(x) * 10;
|
||||
}
|
||||
{-
|
||||
method_id | in | out
|
||||
TESTCASE | 0 | 10 | 110
|
||||
TESTCASE | 0 | -5 | 1110
|
||||
TESTCASE | 0 | -4 | -70
|
||||
-}
|
48
crypto/func/auto-tests/tests/unbalanced_ret_loops.fc
Normal file
48
crypto/func/auto-tests/tests/unbalanced_ret_loops.fc
Normal file
|
@ -0,0 +1,48 @@
|
|||
_ main() { }
|
||||
|
||||
int foo_repeat(int x) method_id(1) {
|
||||
repeat(10) {
|
||||
x += 10;
|
||||
if (x >= 100) {
|
||||
return x;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int foo_while(int x) method_id(2) {
|
||||
int i = 0;
|
||||
while (i < 10) {
|
||||
x += 10;
|
||||
if (x >= 100) {
|
||||
return x;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int foo_until(int x) method_id(3) {
|
||||
int i = 0;
|
||||
do {
|
||||
x += 10;
|
||||
if (x >= 100) {
|
||||
return x;
|
||||
}
|
||||
i += 1;
|
||||
} until (i >= 10);
|
||||
return -1;
|
||||
}
|
||||
|
||||
{-
|
||||
method_id | in | out
|
||||
TESTCASE | 1 | 40 | 100
|
||||
TESTCASE | 1 | 33 | 103
|
||||
TESTCASE | 1 | -5 | -1
|
||||
TESTCASE | 2 | 40 | 100
|
||||
TESTCASE | 2 | 33 | 103
|
||||
TESTCASE | 2 | -5 | -1
|
||||
TESTCASE | 3 | 40 | 100
|
||||
TESTCASE | 3 | 33 | 103
|
||||
TESTCASE | 3 | -5 | -1
|
||||
-}
|
35
crypto/func/auto-tests/tests/unbalanced_ret_nested.fc
Normal file
35
crypto/func/auto-tests/tests/unbalanced_ret_nested.fc
Normal file
|
@ -0,0 +1,35 @@
|
|||
int foo(int y) {
|
||||
if (y < 0) {
|
||||
y *= 2;
|
||||
if (y == -10) {
|
||||
return 111;
|
||||
}
|
||||
}
|
||||
return y + 1;
|
||||
}
|
||||
(int, int) bar(int x, int y) {
|
||||
if (x < 0) {
|
||||
y = foo(y);
|
||||
x *= 2;
|
||||
if (x == -10) {
|
||||
return (111, y);
|
||||
}
|
||||
}
|
||||
return (x + 1, y);
|
||||
}
|
||||
(int, int) main(int x, int y) {
|
||||
(x, y) = bar(x, y);
|
||||
return (x, y * 10);
|
||||
}
|
||||
{-
|
||||
method_id | in | out
|
||||
TESTCASE | 0 | 3 3 | 4 30
|
||||
TESTCASE | 0 | 3 -5 | 4 -50
|
||||
TESTCASE | 0 | 3 -4 | 4 -40
|
||||
TESTCASE | 0 | -5 3 | 111 40
|
||||
TESTCASE | 0 | -5 -5 | 111 1110
|
||||
TESTCASE | 0 | -5 -4 | 111 -70
|
||||
TESTCASE | 0 | -4 3 | -7 40
|
||||
TESTCASE | 0 | -4 -5 | -7 1110
|
||||
TESTCASE | 0 | -4 -4 | -7 -70
|
||||
-}
|
14
crypto/func/auto-tests/tests/w1.fc
Normal file
14
crypto/func/auto-tests/tests/w1.fc
Normal file
|
@ -0,0 +1,14 @@
|
|||
(int, int) main(int id) {
|
||||
if (id > 0) {
|
||||
if (id > 10) {
|
||||
return (2 * id, 3 * id);
|
||||
}
|
||||
}
|
||||
return (5, 6);
|
||||
}
|
||||
{-
|
||||
method_id | in | out
|
||||
TESTCASE | 0 | 0 | 5 6
|
||||
TESTCASE | 0 | 4 | 5 6
|
||||
TESTCASE | 0 | 11 | 22 33
|
||||
-}
|
18
crypto/func/auto-tests/tests/w2.fc
Normal file
18
crypto/func/auto-tests/tests/w2.fc
Normal file
|
@ -0,0 +1,18 @@
|
|||
_ f(cs) {
|
||||
return (cs~load_uint(8), cs~load_uint(8), cs~load_uint(8), cs~load_uint(8),
|
||||
cs~load_uint(8), cs~load_uint(8), cs~load_uint(8), cs~load_uint(8),
|
||||
cs~load_uint(8), cs~load_uint(8), cs~load_uint(8), cs~load_uint(8),
|
||||
cs~load_uint(8), cs~load_uint(8), cs~load_uint(8), cs~load_uint(8),
|
||||
cs~load_uint(8), cs~load_uint(8), cs~load_uint(8), cs~load_uint(8));
|
||||
}
|
||||
|
||||
_ main(cs) {
|
||||
var (x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10,
|
||||
x11, x12, x13, x14, x15, x16, x17, x18, x19) = f(cs);
|
||||
return x0 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9
|
||||
+ x10 + x11 + x12 + x13 + x14 + x15 + x16 + x17 + x18 + x19;
|
||||
}
|
||||
{-
|
||||
method_id | in | out
|
||||
TESTCASE | 0 | x{000102030405060708090a0b0c0d0e0f10111213} | 190
|
||||
-}
|
17
crypto/func/auto-tests/tests/w6.fc
Normal file
17
crypto/func/auto-tests/tests/w6.fc
Normal file
|
@ -0,0 +1,17 @@
|
|||
int main(int x) {
|
||||
int i = 0;
|
||||
;; int f = false;
|
||||
do {
|
||||
i = i + 1;
|
||||
if (i > 5) {
|
||||
return 1;
|
||||
}
|
||||
int f = (i * i == 64);
|
||||
} until (f);
|
||||
return -1;
|
||||
}
|
||||
|
||||
{-
|
||||
method_id | in | out
|
||||
TESTCASE | 0 | 0 | 1
|
||||
-}
|
24
crypto/func/auto-tests/tests/w7.fc
Normal file
24
crypto/func/auto-tests/tests/w7.fc
Normal file
|
@ -0,0 +1,24 @@
|
|||
int test(int y) method_id(1) {
|
||||
int x = 1;
|
||||
if (y > 0) {
|
||||
return 1;
|
||||
}
|
||||
return x > 0;
|
||||
}
|
||||
|
||||
int f(int y) method_id(2) {
|
||||
if (y > 0) {
|
||||
return 1;
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
|
||||
_ main() { }
|
||||
|
||||
{-
|
||||
method_id | in | out
|
||||
TESTCASE | 1 | 10 | 1
|
||||
TESTCASE | 1 | -5 | -1
|
||||
TESTCASE | 2 | 10 | 1
|
||||
TESTCASE | 2 | -5 | 2
|
||||
-}
|
14
crypto/func/auto-tests/tests/w9.fc
Normal file
14
crypto/func/auto-tests/tests/w9.fc
Normal file
|
@ -0,0 +1,14 @@
|
|||
_ main(s) {
|
||||
var (z, t) = (17, s);
|
||||
while (z > 0) {
|
||||
t = s;
|
||||
z -= 1;
|
||||
}
|
||||
return ~ t;
|
||||
}
|
||||
|
||||
{-
|
||||
method_id | in | out
|
||||
TESTCASE | 0 | 1 | -2
|
||||
TESTCASE | 0 | 5 | -6
|
||||
-}
|
Loading…
Reference in a new issue