1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-02-15 04:32:21 +00:00
ton/tolk-tester/tests/logical-operators.tolk

138 lines
3 KiB
Text
Raw Normal View History

import "imports/use-dicts.tolk"
fun simpleAllConst() {
return (!0, !!0 & !false, !!!0, !1, !!1, !-1, !!-1, (!5 == 0) == !0, !0 == true);
}
fun compileTimeEval1(x: int) {
// todo now compiler doesn't understand that bool can't be equal to number other than 0/-1
// (but understands that it can't be positive)
// that's why for now, the last condition is evaluated at runtime
return (!x, !x > 10, !x < 10, !!x == 5, !x == -10);
}
@method_id(101)
fun withIfNot(x: int, y: int) {
if (!x) { return 10; }
else if (!y) { return 20; }
return x+y;
}
@method_id(102)
fun withAndOr(x: int, y: int, z: int) {
var return_at_end = -1;
if (!x & !y) {
if (!z & !y) { return 10; }
else if (z | !!y) { return_at_end = 20; }
} else if (!!x & !!y & !z) {
if (!z & (x > 10)) { return_at_end = 30; }
if ((x != 11) & !z) { return 40; }
return_at_end = 50;
} else {
return_at_end = !x ? !y : !z | 1;
}
return return_at_end;
}
@method_id(103)
fun someSum(upto: int) {
var x = 0;
var should_break = false;
while (!x & !should_break) {
if (upto < 10) { x = upto; should_break = true; }
else { upto = upto - 1; }
}
return x;
}
@method_id(104)
fun testDict(last: int) {
// prepare dict: [3 => 30, 4 => 40, 5 => x]
var dict = prepareDict_3_30_4_40_5_x(!last ? 100 : last);
return (lookupIdxByValue(dict, 30), lookupIdxByValue(dict, last), lookupIdxByValue(dict, 100));
}
@method_id(105)
fun testNotNull(x: int) {
return [x == null, null == x, !(x == null), null == null, +(null != null)];
}
fun main() {
}
/**
@testcase | 101 | 0 0 | 10
@testcase | 101 | 5 0 | 20
@testcase | 101 | 5 8 | 13
@testcase | 102 | 0 0 0 | 10
@testcase | 102 | 0 0 5 | 20
@testcase | 102 | 1 2 0 | 40
@testcase | 102 | 11 2 0 | 50
@testcase | 102 | 1 0 0 | -1
@testcase | 102 | 0 1 0 | 0
@testcase | 102 | 1 0 1 | 1
@testcase | 103 | 15 | 9
@testcase | 103 | 6 | 6
@testcase | 103 | -1 | -1
@testcase | 104 | 50 | 3 5 -1
@testcase | 104 | 100 | 3 5 5
@testcase | 104 | 0 | 3 -1 5
@testcase | 105 | 0 | [ 0 0 -1 -1 0 ]
@testcase | 105 | null | [ -1 -1 0 -1 0 ]
@fif_codegen
"""
simpleAllConst PROC:<{
//
-1 PUSHINT
0 PUSHINT
-1 PUSHINT
0 PUSHINT
-1 PUSHINT
0 PUSHINT
-1 PUSHINT
TRUE
TRUE
}>
"""
@fif_codegen
"""
compileTimeEval1 PROC:<{
// x
DUP // x x
0 EQINT // x _1
FALSE // x _1 _4
TRUE // x _1 _4 _7
FALSE // x _1 _4 _7 _11
s0 s4 XCHG // _11 _1 _4 _7 x
0 EQINT // _11 _1 _4 _7 _12
-10 EQINT // _11 _1 _4 _7 _14
s3 s4 XCHG
s1 s3 s0 XCHG3 // _1 _4 _7 _11 _14
}>
"""
@fif_codegen
"""
withIfNot PROC:<{
c2 SAVE
SAMEALTSAVE // x y
OVER // x y x
IFNOTJMP:<{ // x y
2DROP //
10 PUSHINT // _2=10
}> // x y
DUP // x y y
IFNOTJMP:<{ // x y
2DROP //
20 PUSHINT // _3=20
RETALT
}> // x y
ADD // _4
}>
"""
*/