diff --git a/CHANGELOG.md b/CHANGELOG.md index 295339380..8181e86d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/test/feature-recursion.spec.ts b/src/test/feature-recursion.spec.ts new file mode 100644 index 000000000..e2e8864ef --- /dev/null +++ b/src/test/feature-recursion.spec.ts @@ -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); + }); +}); diff --git a/src/test/features/recursion.tact b/src/test/features/recursion.tact new file mode 100644 index 000000000..63dbe0ab4 --- /dev/null +++ b/src/test/features/recursion.tact @@ -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); + } +} \ No newline at end of file diff --git a/tact.config.json b/tact.config.json index d5b2e7b55..3eac0aec2 100644 --- a/tact.config.json +++ b/tact.config.json @@ -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",