Skip to content

Commit

Permalink
test: arrangements of Structs in asm functions
Browse files Browse the repository at this point in the history
  • Loading branch information
novusnota committed Dec 24, 2024
1 parent 70f5a70 commit 7677881
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
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;
}

0 comments on commit 7677881

Please sign in to comment.