Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: arrangements of Structs in asm functions #1240

Merged
merged 2 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/test/e2e-emulated/asm-functions.spec.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);
});
});
51 changes: 51 additions & 0 deletions src/test/e2e-emulated/contracts/asm-functions.tact
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -67,3 +108,13 @@ struct SliceInt {
rem: Slice;
val: Int;
}

struct Two {
a: Int;
b: Int;
}

struct TwoInTwo {
a: Two;
b: Two;
}
Loading