Skip to content

Commit 73d29c7

Browse files
authored
tests: short-circuit of ternary operator _ ? _ : _ (#1726)
1 parent 27187f3 commit 73d29c7

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

src/test/e2e-emulated/contracts/semantics.tact

+94
Original file line numberDiff line numberDiff line change
@@ -1912,6 +1912,100 @@ contract SemanticsTester {
19121912
return result;
19131913
}
19141914

1915+
/// Tests _ ? _ : _ short-circuiting with mutating functions
1916+
get fun ternaryMutateShortCircuit(): Bool {
1917+
1918+
let s = 1;
1919+
1920+
// It will evaluate the full expression to false and increment s to 2. The false branch will not increment s again.
1921+
let m1 = true ? s.increment() > 2 : s.increment() > 2;
1922+
1923+
let result = s == 2;
1924+
1925+
// It will evaluate the full expression to true and increment s to 3. The true branch will not increment s again.
1926+
let m2 = false ? s.increment() > 2 : s.increment() > 2;
1927+
1928+
result &&= s == 3;
1929+
1930+
// Condition evaluates to false, so that s should not be incremented. m3 is true.
1931+
let m3 = s > 3 ? s.increment() > 2 : s > 2;
1932+
1933+
result &&= s == 3;
1934+
1935+
// Condition evaluates to true, so that s should not be incremented. m4 is true.
1936+
let m4 = s >= 3 ? s > 2 : s.increment() > 2;
1937+
1938+
result &&= s == 3;
1939+
1940+
// Condition will increment s to 4 and evaluate to true. False branch does not increment s again. m5 is true.
1941+
let m5 = s.increment() > 3 ? s == 4 : s.increment() > 3;
1942+
1943+
result &&= s == 4;
1944+
1945+
// Condition will increment s to 5 and evaluate to false. True branch does not increment s again. m6 is false.
1946+
let m6 = s.increment() > 5 ? s.increment() > 3 : s > 5 ;
1947+
1948+
result &&= s == 5;
1949+
1950+
// Condition will increment s to 6 and evaluate to true. True branch increments s again. m7 is true.
1951+
let m7 = s.increment() > 5 ? s.increment() == 7 : s == 6;
1952+
1953+
result &&= s == 7;
1954+
1955+
// Condition will increment s to 8 and evaluate to false. False branch increments s again. m8 is true.
1956+
let m8 = s.increment() > 8 ? s == 8 : s.increment() == 9;
1957+
1958+
result &&= s == 9;
1959+
1960+
result &&= !m1 && m2 && m3 && m4 && m5 && !m6 && m7 && m8;
1961+
1962+
return result;
1963+
}
1964+
1965+
/// Tests _ ? _ : _ short-circuiting with infinite loops
1966+
get fun ternaryInfiniteLoopShortCircuit(): Bool {
1967+
1968+
let s = 1;
1969+
1970+
// It will evaluate the full expression to false. The false branch will not execute the loop.
1971+
let l1 = true ? s > 1 : infiniteLoop();
1972+
1973+
// It will evaluate the full expression to false. The true branch will not execute the loop.
1974+
let l2 = false ? infiniteLoop() : s > 1;
1975+
1976+
// It will evaluate the full expression to false. The false branch will not execute the loop.
1977+
let l3 = s >= 1 ? s > 1 : infiniteLoop();
1978+
1979+
// It will evaluate the full expression to false. The true branch will not execute the loop.
1980+
let l4 = s > 1 ? infiniteLoop() : s > 1;
1981+
1982+
let result = !l1 && !l2 && !l3 && !l4;
1983+
1984+
return result;
1985+
}
1986+
1987+
/// Tests _ ? _ : _ short-circuiting with exception throwing
1988+
get fun ternaryExceptionShortCircuit(): Bool {
1989+
1990+
let s = 1;
1991+
1992+
// It will evaluate the full expression to false. The false branch will not throw an exception.
1993+
let t1 = true ? s > 1 : throwException(s-1);
1994+
1995+
// It will evaluate the full expression to false. The true branch will not throw an exception.
1996+
let t2 = false ? throwException(s-1) : s > 1;
1997+
1998+
// It will evaluate the full expression to false. The false branch will not throw an exception.
1999+
let t3 = s >= 1 ? s > 1 : throwException(s-1);
2000+
2001+
// It will evaluate the full expression to false. The true branch will not throw an exception.
2002+
let t4 = s > 1 ? throwException(s-1) : s > 1;
2003+
2004+
let result = !t1 && !t2 && !t3 && !t4;
2005+
2006+
return result;
2007+
}
2008+
19152009

19162010
/*************** Addresses ********************/
19172011

src/test/e2e-emulated/semantics.spec.ts

+5
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ describe("semantics", () => {
7979
expect(await contract.getOrMutateShortCircuit()).toEqual(true);
8080
expect(await contract.getOrInfiniteLoopShortCircuit()).toEqual(true);
8181
expect(await contract.getOrExceptionShortCircuit()).toEqual(true);
82+
expect(await contract.getTernaryMutateShortCircuit()).toEqual(true);
83+
expect(await contract.getTernaryInfiniteLoopShortCircuit()).toEqual(
84+
true,
85+
);
86+
expect(await contract.getTernaryExceptionShortCircuit()).toEqual(true);
8287

8388
// Contracts
8489

0 commit comments

Comments
 (0)