Skip to content

Commit

Permalink
feat: testing for repeat ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
novusnota committed Aug 28, 2024
1 parent 12def54 commit 7f347ad
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/test/e2e-emulated/contracts/repeat-range.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
contract RepeatRange {
/// Effective range counter
effCounter: Int = 0;

/// To handle deployment
receive() {}

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

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

return counter; // 0, if things go right
}

/// from 2^{31} to +∞ — repeat range is too big
get fun testInvalidRange(): Int {
let caseCounter = 0;

try {
repeat (pow(2, 31)) { caseCounter -= 1 }
} catch (_) { caseCounter += 1 }

return caseCounter; // 1, if things go right
}

/// from 1 to 2^{31} - 1, including both ends
receive("effectiveRange") {
self.effCounter = 0;

repeat (1) { self.effCounter += 1}
repeat (pow(2, 31) - 1) { self.effCounter += 1}
}

get fun effectiveRangeCounter(): Int {
return self.effCounter; // 2^{31}, if ran after receiving "effectiveRange" msg
}
}
67 changes: 67 additions & 0 deletions src/test/e2e-emulated/repeat-range.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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 () => {
const LARGE_SUM = 10_000_000_000n;

blockchain = await Blockchain.create();
treasure = await blockchain.treasury("treasure", {
balance: LARGE_SUM,
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,
});

const topUpResult = await treasure.send({
to: contract.address,
value: LARGE_SUM,
});

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

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

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

// effective range
const sendResult = await contract.send(
treasure.getSender(),
{ value: toNano("10") },
"effectiveRange",
);
expect(sendResult.transactions).toHaveTransaction({
from: treasure.address,
to: contract.address,
success: true,
});
expect(await contract.getEffectiveRangeCounter()).toEqual(2n ** 31n);
});
});
5 changes: 5 additions & 0 deletions tact.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@
"name": "traits",
"path": "./src/test/e2e-emulated/contracts/traits.tact",
"output": "./src/test/e2e-emulated/contracts/output"
},
{
"name": "repeat-range",
"path": "./src/test/e2e-emulated/contracts/repeat-range.tact",
"output": "./src/test/e2e-emulated/contracts/output"
}
]
}

0 comments on commit 7f347ad

Please sign in to comment.