Skip to content

Commit

Permalink
test: add cases covering recursive functions (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorpy authored May 27, 2024
1 parent 9915bd1 commit 5a79bd8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Tests for recursive functions: PR [#359](https://github.com/tact-lang/tact/pull/359)

### Changed

- Refactor AST types to simplify access to third-party tools: PR [#325](https://github.com/tact-lang/tact/pull/325)
Expand Down
31 changes: 31 additions & 0 deletions src/test/feature-recursion.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { toNano } from "@ton/core";
import { ContractSystem } from "@tact-lang/emulator";
import { __DANGER_resetNodeId } from "../grammar/ast";
import { RecursionTester } from "./features/output/recursion_RecursionTester";

describe("feature-recursion", () => {
beforeEach(() => {
__DANGER_resetNodeId();
});
it("should perform recursive operations correctly", async () => {
const system = await ContractSystem.create();
const treasure = system.treasure("treasure");
const contract = system.open(await RecursionTester.fromInit());
await contract.send(
treasure,
{ value: toNano("10") },
{ $$type: "Deploy", queryId: 0n },
);
await system.run();

expect(await contract.getFib(0n)).toBe(0n);
expect(await contract.getFib(1n)).toBe(1n);
expect(await contract.getFib(2n)).toBe(1n);
expect(await contract.getFib(3n)).toBe(2n);

expect(await contract.getFact(0n)).toBe(1n);
expect(await contract.getFact(1n)).toBe(1n);
expect(await contract.getFact(2n)).toBe(2n);
expect(await contract.getFact(3n)).toBe(6n);
});
});
18 changes: 18 additions & 0 deletions src/test/features/recursion.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import "@stdlib/deploy";

fun factorial(n: Int): Int {
if (n == 0) { return 1; }
return n * factorial(n - 1);
}

contract RecursionTester with Deployable {
get fun fib(n: Int): Int {
if (n <= 0) { return 0; }
if (n == 1) { return 1; }
return self.fib(n - 1) + self.fib(n - 2);
}

get fun fact(n: Int): Int {
return factorial(n);
}
}
8 changes: 8 additions & 0 deletions tact.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@
"debug": true
}
},
{
"name": "recursion",
"path": "./src/test/features/recursion.tact",
"output": "./src/test/features/output",
"options": {
"debug": true
}
},
{
"name": "benchmark_functions",
"path": "./src/benchmarks/contracts/functions.tact",
Expand Down

0 comments on commit 5a79bd8

Please sign in to comment.