From 01ff1884a6c9d23352b253dbe6f324f9dddbe889 Mon Sep 17 00:00:00 2001 From: Vitor Py Braga <12871+vitorpy@users.noreply.github.com> Date: Fri, 24 May 2024 21:45:11 +0200 Subject: [PATCH 1/5] feat: add test case covering recursive functions --- .../resolveStatements.spec.ts.snap | 73 +++++++++++++++++++ src/types/stmts/case-22.tact | 7 ++ 2 files changed, 80 insertions(+) create mode 100644 src/types/stmts/case-22.tact diff --git a/src/types/__snapshots__/resolveStatements.spec.ts.snap b/src/types/__snapshots__/resolveStatements.spec.ts.snap index 5c6caf733..706bb967f 100644 --- a/src/types/__snapshots__/resolveStatements.spec.ts.snap +++ b/src/types/__snapshots__/resolveStatements.spec.ts.snap @@ -1407,3 +1407,76 @@ exports[`resolveStatements should resolve statements for case-21 1`] = ` ], ] `; + +exports[`resolveStatements should resolve statements for case-22 1`] = ` +[ + [ + "n", + "Int", + ], + [ + "0", + "Int", + ], + [ + "n <= 0", + "Bool", + ], + [ + "0", + "Int", + ], + [ + "n", + "Int", + ], + [ + "1", + "Int", + ], + [ + "n == 1", + "Bool", + ], + [ + "1", + "Int", + ], + [ + "n", + "Int", + ], + [ + "1", + "Int", + ], + [ + "n - 1", + "Int", + ], + [ + "fib(n - 1)", + "Int", + ], + [ + "n", + "Int", + ], + [ + "2", + "Int", + ], + [ + "n - 2", + "Int", + ], + [ + "fib(n - 2)", + "Int", + ], + [ + "fib(n - 1) + fib(n - 2)", + "Int", + ], +] +`; diff --git a/src/types/stmts/case-22.tact b/src/types/stmts/case-22.tact new file mode 100644 index 000000000..533c56c0d --- /dev/null +++ b/src/types/stmts/case-22.tact @@ -0,0 +1,7 @@ +primitive Int; + +fun fib(n: Int): Int { + if (n <= 0) { return 0; } + if (n == 1) { return 1; } + return fib(n - 1) + fib(n - 2); +} \ No newline at end of file From 621ac008d8e3450ce3d8cd3ad3082c2a60a364d2 Mon Sep 17 00:00:00 2001 From: Vitor Py Braga <12871+vitorpy@users.noreply.github.com> Date: Sun, 26 May 2024 12:06:12 +0200 Subject: [PATCH 2/5] fix: convert into feature test --- src/test/feature-recursion.spec.ts | 26 +++++++ src/test/features/recursion.tact | 9 +++ .../resolveStatements.spec.ts.snap | 73 ------------------- src/types/stmts/case-22.tact | 7 -- tact.config.json | 8 ++ 5 files changed, 43 insertions(+), 80 deletions(-) create mode 100644 src/test/feature-recursion.spec.ts create mode 100644 src/test/features/recursion.tact delete mode 100644 src/types/stmts/case-22.tact diff --git a/src/test/feature-recursion.spec.ts b/src/test/feature-recursion.spec.ts new file mode 100644 index 000000000..81c4e8060 --- /dev/null +++ b/src/test/feature-recursion.spec.ts @@ -0,0 +1,26 @@ +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); + }); +}); diff --git a/src/test/features/recursion.tact b/src/test/features/recursion.tact new file mode 100644 index 000000000..d6699cfd3 --- /dev/null +++ b/src/test/features/recursion.tact @@ -0,0 +1,9 @@ +import "@stdlib/deploy"; + +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); + } +} \ No newline at end of file diff --git a/src/types/__snapshots__/resolveStatements.spec.ts.snap b/src/types/__snapshots__/resolveStatements.spec.ts.snap index 706bb967f..5c6caf733 100644 --- a/src/types/__snapshots__/resolveStatements.spec.ts.snap +++ b/src/types/__snapshots__/resolveStatements.spec.ts.snap @@ -1407,76 +1407,3 @@ exports[`resolveStatements should resolve statements for case-21 1`] = ` ], ] `; - -exports[`resolveStatements should resolve statements for case-22 1`] = ` -[ - [ - "n", - "Int", - ], - [ - "0", - "Int", - ], - [ - "n <= 0", - "Bool", - ], - [ - "0", - "Int", - ], - [ - "n", - "Int", - ], - [ - "1", - "Int", - ], - [ - "n == 1", - "Bool", - ], - [ - "1", - "Int", - ], - [ - "n", - "Int", - ], - [ - "1", - "Int", - ], - [ - "n - 1", - "Int", - ], - [ - "fib(n - 1)", - "Int", - ], - [ - "n", - "Int", - ], - [ - "2", - "Int", - ], - [ - "n - 2", - "Int", - ], - [ - "fib(n - 2)", - "Int", - ], - [ - "fib(n - 1) + fib(n - 2)", - "Int", - ], -] -`; diff --git a/src/types/stmts/case-22.tact b/src/types/stmts/case-22.tact deleted file mode 100644 index 533c56c0d..000000000 --- a/src/types/stmts/case-22.tact +++ /dev/null @@ -1,7 +0,0 @@ -primitive Int; - -fun fib(n: Int): Int { - if (n <= 0) { return 0; } - if (n == 1) { return 1; } - return fib(n - 1) + fib(n - 2); -} \ 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", From 3a3461157ba4727389220130d0d1090ae9b4246b Mon Sep 17 00:00:00 2001 From: Vitor Py Braga <12871+vitorpy@users.noreply.github.com> Date: Sun, 26 May 2024 12:07:54 +0200 Subject: [PATCH 3/5] chore: update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 295339380..cb2efdb54 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 +- Unit 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) From a666a41f140f05d47a69d4014c68dcf1f663e977 Mon Sep 17 00:00:00 2001 From: Vitor Py Braga <12871+vitorpy@users.noreply.github.com> Date: Sun, 26 May 2024 20:02:38 +0200 Subject: [PATCH 4/5] fix: also test static functions --- src/test/feature-recursion.spec.ts | 5 +++++ src/test/features/recursion.tact | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/src/test/feature-recursion.spec.ts b/src/test/feature-recursion.spec.ts index 81c4e8060..e2e8864ef 100644 --- a/src/test/feature-recursion.spec.ts +++ b/src/test/feature-recursion.spec.ts @@ -22,5 +22,10 @@ describe("feature-recursion", () => { 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 index d6699cfd3..63dbe0ab4 100644 --- a/src/test/features/recursion.tact +++ b/src/test/features/recursion.tact @@ -1,9 +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 From f868a41a36f399ee365d329b59232814b4c87324 Mon Sep 17 00:00:00 2001 From: Anton Trunov Date: Mon, 27 May 2024 17:40:23 +0800 Subject: [PATCH 5/5] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb2efdb54..8181e86d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- Unit tests for recursive functions: PR [#359](https://github.com/tact-lang/tact/pull/359) +- Tests for recursive functions: PR [#359](https://github.com/tact-lang/tact/pull/359) ### Changed