Skip to content

Commit

Permalink
test: check repeat-loop ranges (tact-lang#766)
Browse files Browse the repository at this point in the history
  • Loading branch information
novusnota authored Sep 5, 2024
1 parent 1e71838 commit 926edb4
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/test/exit-codes/contracts/repeat-range.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
contract RepeatRange {
/// To handle deployment
receive() {}

/// from -2^{256} to 0, including both ends
get fun testIgnoredRange(): Bool {
let counter = 0;

repeat ((-pow(2, 255)) * 2) { counter += 1 }
repeat (-1) { counter += 1 }
repeat (0) { counter += 1 }

return counter == 0;
}

/// from 2^{31} to +∞ — repeat range is too big
get fun testInvalidRange(): Bool {
try {
let counter = 0;
repeat (pow(2, 31)) { counter += 1 }

return false;
} catch (exitCode) {
return exitCode == 5;
}
}

/// from 1 to 2^{31} - 1, including both ends
get fun testMinEffectiveRange(): Bool {
let counter = 0;
repeat (1) { counter += 1 }

return counter == 1;
}

/// from 1 to 2^{31} - 1, including both ends
receive("testMaxEffectiveRange") {
let counter = pow(2, 31) - 1;

// This will cause exit code -14 in Compute phase (as expected)
repeat (pow(2, 31) - 1) { counter += 1 }
}
}
57 changes: 57 additions & 0 deletions src/test/exit-codes/repeat-range.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { toNano } from "@ton/core";
import { Blockchain, SandboxContract, TreasuryContract } from "@ton/sandbox";
import { RepeatRange } from "./contracts/output/repeat-range_RepeatRange";
import "@ton/test-utils";

describe("repeat range", () => {
let blockchain: Blockchain;
let treasure: SandboxContract<TreasuryContract>;
let contract: SandboxContract<RepeatRange>;

beforeEach(async () => {
blockchain = await Blockchain.create();
blockchain.verbosity.print = false;
treasure = await blockchain.treasury("treasure", {
resetBalanceIfZero: true,
});

contract = blockchain.openContract(await RepeatRange.fromInit());

const deployResult = await contract.send(
treasure.getSender(),
{ value: toNano("10") },
null,
);

expect(deployResult.transactions).toHaveTransaction({
from: treasure.address,
to: contract.address,
success: true,
deploy: true,
});
});

it("should test repeat range boundaries", async () => {
// ignored range
expect(await contract.getTestIgnoredRange()).toEqual(true);

// invalid range
expect(await contract.getTestInvalidRange()).toEqual(true);

// min effective range
expect(await contract.getTestMinEffectiveRange()).toEqual(true);

// max effective range
const sendResult = await contract.send(
treasure.getSender(),
{ value: toNano("10") },
"testMaxEffectiveRange",
);
expect(sendResult.transactions).toHaveTransaction({
from: treasure.address,
to: contract.address,
success: false,
exitCode: -14,
});
});
});
5 changes: 5 additions & 0 deletions tact.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,11 @@
"name": "init-return",
"path": "./src/test/e2e-emulated/contracts/init-return.tact",
"output": "./src/test/e2e-emulated/contracts/output"
},
{
"name": "repeat-range",
"path": "./src/test/exit-codes/contracts/repeat-range.tact",
"output": "./src/test/exit-codes/contracts/output"
}
]
}

0 comments on commit 926edb4

Please sign in to comment.