From 76778817f2378538f15f1d6fa60332ed31435f88 Mon Sep 17 00:00:00 2001 From: Novus Nota <68142933+novusnota@users.noreply.github.com> Date: Mon, 23 Dec 2024 20:26:14 +0100 Subject: [PATCH] test: arrangements of Structs in `asm` functions --- src/test/e2e-emulated/asm-functions.spec.ts | 37 +++++++++++++- .../e2e-emulated/contracts/asm-functions.tact | 51 +++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/src/test/e2e-emulated/asm-functions.spec.ts b/src/test/e2e-emulated/asm-functions.spec.ts index c67084b51..81c63f613 100644 --- a/src/test/e2e-emulated/asm-functions.spec.ts +++ b/src/test/e2e-emulated/asm-functions.spec.ts @@ -1,4 +1,4 @@ -import { toNano } from "@ton/core"; +import { beginCell, toNano } from "@ton/core"; import { Blockchain, SandboxContract, TreasuryContract } from "@ton/sandbox"; import { AsmFunctionsTester as TestContract } from "./contracts/output/asm-functions_AsmFunctionsTester"; import "@ton/test-utils"; @@ -32,8 +32,43 @@ describe("asm functions", () => { it("should implement asm functions correctly", async () => { expect(await contract.getTestAsmStoreDict()).toEqual(true); expect(await contract.getTestAsmLoadCoins()).toEqual(true); + expect(await contract.getTestAsmLoadCoinsMut()).toEqual(true); + expect( + await contract.getTestAsmLoadCoinsMutRuntime( + beginCell().storeCoins(42n).endCell(), + ), + ).toEqual(42n); expect(await contract.getTestAsmLoadInt()).toEqual(true); expect(await contract.getTestAsmDebugStr()).toEqual(true); expect(await contract.getTestAsmCreateUseWord()).toEqual(true); + + // Struct arrangements + expect(await contract.getTestAsmSecondToLast()).toEqual(true); + expect( + await contract.getTestAsmSecondToLastRuntime( + { $$type: "Two", a: 1n, b: 2n }, + { $$type: "Two", a: 3n, b: 4n }, + ), + ).toEqual(3n); + expect(await contract.getTestAsmFirst()).toEqual(true); + expect( + await contract.getTestAsmFirstRuntime( + { + $$type: "TwoInTwo", + a: { $$type: "Two", a: 1n, b: 2n }, + b: { $$type: "Two", a: 3n, b: 4n }, + }, + { + $$type: "TwoInTwo", + a: { $$type: "Two", a: 5n, b: 6n }, + b: { $$type: "Two", a: 7n, b: 8n }, + }, + { + $$type: "TwoInTwo", + a: { $$type: "Two", a: 9n, b: 10n }, + b: { $$type: "Two", a: 11n, b: 12n }, + }, + ), + ).toEqual(1n); }); }); diff --git a/src/test/e2e-emulated/contracts/asm-functions.tact b/src/test/e2e-emulated/contracts/asm-functions.tact index 698c1ae2f..9360e7e32 100644 --- a/src/test/e2e-emulated/contracts/asm-functions.tact +++ b/src/test/e2e-emulated/contracts/asm-functions.tact @@ -18,6 +18,19 @@ contract AsmFunctionsTester { return s.asmLoadCoins().val == 42; } + get fun testAsmLoadCoinsMut(): Bool { + let s = beginCell().storeCoins(42).asSlice(); + return s.asmLoadCoinsMut() == 42 && s.empty(); + } + + // asmLoadCoinsMut(), but with data supplied at runtime + get fun testAsmLoadCoinsMutRuntime(c: Cell): Int { + let s = c.asSlice(); + let res = s.asmLoadCoinsMut(); + s.endParse(); // like .empty(), but throws on failure + return res; + } + get fun testAsmLoadInt(): Bool { let s = beginCell().storeInt(42, 7).asSlice(); return s.asmLoadInt(7).val == 42; @@ -31,6 +44,28 @@ contract AsmFunctionsTester { get fun testAsmCreateUseWord(): Bool { return asmCreateUseWord(6) == 7; } + + get fun testAsmSecondToLast(): Bool { + return asmSecondToLast(Two{ a: 1, b: 2 }, Two{ a: 3, b: 4 }) == 3; + } + + // asmSecondToLast(), but with data supplied at runtime + get fun testAsmSecondToLastRuntime(s1: Two, s2: Two): Int { + return asmSecondToLast(s1, s2); + } + + get fun testAsmFirst(): Bool { + return asmFirst( + TwoInTwo{ a: Two{ a: 1, b: 2}, b: Two{ a: 3, b: 4 } }, + TwoInTwo{ a: Two{ a: 5, b: 6}, b: Two{ a: 7, b: 8 } }, + TwoInTwo{ a: Two{ a: 9, b: 10}, b: Two{ a: 11, b: 12 } }, + ) == 1; + } + + // asmFirst(), but with data supplied at runtime + get fun testAsmFirstRuntime(s1: TwoInTwo, s2: TwoInTwo, s3: TwoInTwo): Int { + return asmFirst(s1, s2, s3); + } } // Functions to test @@ -41,8 +76,14 @@ asm extends fun asmLoadMapIntInt(self: Slice): MapIntIntSlice { LDDICT } asm extends fun asmLoadCoins(self: Slice): IntSlice { LDVARUINT16 } +asm(-> 1 0) extends mutates fun asmLoadCoinsMut(self: Slice): Int { LDVARUINT16 } + asm(self len -> 1 0) extends fun asmLoadInt(self: Slice, len: Int): SliceInt { LDIX } +asm(b a) fun asmSecondToLast(a: Two, b: Two): Int { DROP DROP DROP } + +asm(a c b) fun asmFirst(a: TwoInTwo, b: TwoInTwo, c: TwoInTwo): Int { DROP2 DROP2 DROP2 DROP2 DROP2 DROP } + asm fun asmDebugStr() { "Works!" DEBUGSTR } asm fun asmCreateUseWord(x: Int): Int { @@ -67,3 +108,13 @@ struct SliceInt { rem: Slice; val: Int; } + +struct Two { + a: Int; + b: Int; +} + +struct TwoInTwo { + a: Two; + b: Two; +}