mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
[Tolk] Compiler built-in __expect_type()
for testing purposes
Currently, tolk-tester can test various "output" of the compiler: pass input and check output, validate fif codegen, etc. But it can not test compiler internals and AST representation. I've added an ability to have special functions to check/expose internal compiler state. The first (and the only now) is: > __expect_type(some_expr, "<type>"); Such a call has special treatment in a compilation process. Compilation fails if this expression doesn't have requested type. It's intended to be used in tests only. Not present in stdlib.
This commit is contained in:
parent
c720204199
commit
989629a832
9 changed files with 147 additions and 7 deletions
|
@ -1,6 +1,9 @@
|
|||
fun f(a: int, b: int, c: int, d: int, e: int, f: int): (int, int) {
|
||||
// solve a 2x2 linear equation
|
||||
var D: int = a*d - b*c;;;; var Dx: int = e*d-b*f ;;;; var Dy: int = a * f - e * c;
|
||||
__expect_type(D, "int");
|
||||
__expect_type(D*D, "int");
|
||||
__expect_type(calc_phi, "() -> int");
|
||||
return (Dx/D,Dy/D);
|
||||
};;;;
|
||||
|
||||
|
|
|
@ -41,10 +41,12 @@ fun manyEq<T1, T2, T3>(a: T1, b: T2, c: T3): [T1, T2, T3] {
|
|||
|
||||
@method_id(104)
|
||||
fun test104(f: int) {
|
||||
return (
|
||||
var result = (
|
||||
manyEq(1 ? 1 : 1, f ? 0 : null, !f ? getTwo() as int : null),
|
||||
manyEq((f ? null as int : eq2(2), beginCell().storeBool(true).endCell().beginParse().loadBool()), 0, eq4(f))
|
||||
);
|
||||
__expect_type(result, "([int, int, int], [(int, bool), int, int])");
|
||||
return result;
|
||||
}
|
||||
|
||||
fun calcSum<X>(x: X, y: X) { return x + y; }
|
||||
|
@ -68,6 +70,7 @@ fun abstractTransform<X, Y, R>(xToY: (X) -> Y, yToR: (((Y))) -> R, initialX: X):
|
|||
@method_id(106)
|
||||
fun test106() {
|
||||
var c = beginCell().storeInt(106, 32).endCell();
|
||||
__expect_type(calcYPlus1<int>, "(int) -> int");
|
||||
return [
|
||||
abstractTransform(cellToSlice, calcLoad32, c),
|
||||
abstractTransform(calcYPlus1<int>, calcYPlus1<int>, 0),
|
||||
|
|
94
tolk-tester/tests/inference-tests.tolk
Normal file
94
tolk-tester/tests/inference-tests.tolk
Normal file
|
@ -0,0 +1,94 @@
|
|||
// the goal of this file is not only to @testcase results —
|
||||
// but to check that this file compiles
|
||||
|
||||
fun eq<X>(value: X): X { return value; }
|
||||
|
||||
fun test1(x: int, y: int) {
|
||||
__expect_type(0, "int");
|
||||
__expect_type("0"c, "int");
|
||||
__expect_type(x, "int");
|
||||
__expect_type(x + y, "int");
|
||||
__expect_type(x * y, "int");
|
||||
__expect_type(x & y, "int");
|
||||
__expect_type(x << y, "int");
|
||||
__expect_type((((x))), "int");
|
||||
__expect_type(x = x, "int");
|
||||
__expect_type(x += x, "int");
|
||||
__expect_type(x &= x, "int");
|
||||
__expect_type(random() ? x : y, "int");
|
||||
__expect_type(eq(x), "int");
|
||||
__expect_type(eq<int>(x), "int");
|
||||
__expect_type(eq<int>(null), "int");
|
||||
__expect_type(x as int, "int");
|
||||
__expect_type(+x, "int");
|
||||
__expect_type(~x, "int");
|
||||
{
|
||||
var x: slice = beginCell().endCell().beginParse();
|
||||
__expect_type(x, "slice");
|
||||
__expect_type(beginCell(), "builder");
|
||||
__expect_type(beginCell().endCell(), "cell");
|
||||
}
|
||||
}
|
||||
|
||||
fun test2(x: int, y: bool) {
|
||||
__expect_type(!x, "bool");
|
||||
__expect_type(x != x, "bool");
|
||||
__expect_type(x <= x, "bool");
|
||||
__expect_type(x <=> x, "bool");
|
||||
__expect_type(x <=> x, "bool");
|
||||
__expect_type(!random(), "bool");
|
||||
__expect_type(!!(x != null), "bool");
|
||||
__expect_type(x ? x != null : null == x, "bool");
|
||||
__expect_type(y & true, "bool");
|
||||
__expect_type(y ^= false, "bool");
|
||||
__expect_type(x && y, "bool");
|
||||
__expect_type(true && false && true, "bool");
|
||||
__expect_type(x || x, "bool");
|
||||
__expect_type(x || !x || (true & false), "bool");
|
||||
}
|
||||
|
||||
fun test3() {
|
||||
__expect_type(true as int, "int");
|
||||
__expect_type(!random() as int, "int");
|
||||
}
|
||||
|
||||
fun test4(x: int) {
|
||||
__expect_type((), "()");
|
||||
__expect_type((x, x), "(int, int)");
|
||||
__expect_type((x, (x, x), x), "(int, (int, int), int)");
|
||||
}
|
||||
|
||||
fun test5(x: int) {
|
||||
__expect_type([], "[]");
|
||||
__expect_type([x], "[int]");
|
||||
__expect_type([x, x >= 1], "[int, bool]");
|
||||
__expect_type([x, x >= 1, null as slice], "[int, bool, slice]");
|
||||
__expect_type((x, [x], [[x], x]), "(int, [int], [[int], int])");
|
||||
__expect_type(getMyOriginalBalanceWithExtraCurrencies(), "[int, cell]");
|
||||
}
|
||||
|
||||
fun test6() {
|
||||
var t = createEmptyTuple();
|
||||
__expect_type(t, "tuple");
|
||||
t.tuplePush(1);
|
||||
__expect_type(t, "tuple");
|
||||
}
|
||||
|
||||
fun test7() {
|
||||
__expect_type(test3(), "void");
|
||||
__expect_type(test3, "() -> void");
|
||||
var cb = test1;
|
||||
__expect_type(cb, "(int, int) -> void");
|
||||
var t = createEmptyTuple();
|
||||
__expect_type(beginCell().endCell, "(builder) -> cell");
|
||||
// __expect_type(eq<(int, slice)>, "(int, slice) -> (int, slice)");
|
||||
}
|
||||
|
||||
|
||||
fun main() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@testcase | 0 | | 0
|
||||
*/
|
Loading…
Add table
Add a link
Reference in a new issue