Skip to content

Commit

Permalink
feat: cover new functions with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusarich committed Nov 28, 2024
1 parent b7da3b7 commit 8a5fed9
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 3 deletions.
28 changes: 28 additions & 0 deletions src/test/e2e-emulated/contracts/math.tact
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,32 @@ contract MathTester with Deployable {
a >>= b;
return a;
}

get fun sign(x: Int): Int {
return sign(x);
}

get fun divc(x: Int, y: Int): Int {
return divc(x, y);
}

get fun muldivc(x: Int, y: Int, z: Int): Int {
return muldivc(x, y, z);
}

get fun mulShiftRight(x: Int, y: Int, z: Int): Int {
return mulShiftRight(x, y, z);
}

get fun mulShiftRightRound(x: Int, y: Int, z: Int): Int {
return mulShiftRightRound(x, y, z);
}

get fun mulShiftRightCeil(x: Int, y: Int, z: Int): Int {
return mulShiftRightCeil(x, y, z);
}

get fun sqrt(x: Int): Int {
return sqrt(x);
}
}
68 changes: 67 additions & 1 deletion src/test/e2e-emulated/contracts/stdlib.tact
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,70 @@ contract StdlibTest {
get fun parseVarAddress(slice: Slice): VarAddress {
return parseVarAddress(slice);
}
}

get fun builderDepth(bl: Builder): Int {
return bl.depth();
}

get fun skipLastBits(sc: Slice, n: Int): Slice {
return sc.skipLastBits(n);
}

get fun firstBits(sc: Slice, n: Int): Slice {
return sc.firstBits(n);
}

get fun lastBits(sc: Slice, n: Int): Slice {
return sc.lastBits(n);
}

get fun sliceDepth(sc: Slice): Int {
return sc.depth();
}

get fun addressNone(): Address? {
return addressNone();
}

get fun computeDataSizeCell(c: Cell, maxCells: Int): DataSize {
return c.computeDataSize(maxCells);
}

get fun computeDataSizeSlice(sc: Slice, maxCells: Int): DataSize {
return sc.computeDataSize(maxCells);
}

get fun cellDepth(c: Cell): Int {
return c.depth();
}

get fun curLt(): Int {
return curLt();
}

get fun blockLt(): Int {
return blockLt();
}

get fun setGasLimit(gl: Int): Int {
setGasLimit(gl);
let x = 0;
repeat (100) {
x += 1;
}
return gasConsumed();
}

get fun getSeed(): Int {
return getSeed();
}

get fun setSeed(seed: Int): Int {
setSeed(seed);
return getSeed();
}

get fun myCode(): Cell {
return myCode();
}
}
58 changes: 58 additions & 0 deletions src/test/e2e-emulated/math.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,5 +507,63 @@ describe("math", () => {
expect(await contract.getAugmentedShiftRight(2n, 1n)).toBe(1n);
expect(await contract.getAugmentedShiftRight(4n, 2n)).toBe(1n);
expect(await contract.getAugmentedShiftRight(16n, 3n)).toBe(2n);

// sign
expect(await contract.getSign(123n)).toBe(1n);
expect(await contract.getSign(-123n)).toBe(-1n);
expect(await contract.getSign(0n)).toBe(0n);

// divc: ceil(x/y)
expect(await contract.getDivc(123n, 123n)).toBe(1n);
expect(await contract.getDivc(123n, 124n)).toBe(1n);
expect(await contract.getDivc(0n, 123n)).toBe(0n);
expect(await contract.getDivc(123n, 122n)).toBe(2n);

// muldivc: ceil(x*y/z)
expect(await contract.getMuldivc(123n, 123n, 123n)).toBe(123n);
expect(await contract.getMuldivc(100n, 100n, 10001n)).toBe(1n);
expect(await contract.getMuldivc(100n, 100n, 10000n)).toBe(1n);
expect(await contract.getMuldivc(100n, 100n, 9999n)).toBe(2n);
expect(await contract.getMuldivc(100n, 0n, 1n)).toBe(0n);

// mulShiftRight: floor(x*y/2^z)
expect(await contract.getMulShiftRight(2n, 4n, 3n)).toBe(1n);
expect(await contract.getMulShiftRight(2n, 4n, 2n)).toBe(2n);
expect(await contract.getMulShiftRight(2n, 4n, 1n)).toBe(4n);
expect(await contract.getMulShiftRight(2n, 4n, 0n)).toBe(8n);
expect(await contract.getMulShiftRight(1n, 6n, 0n)).toBe(6n);
expect(await contract.getMulShiftRight(1n, 6n, 3n)).toBe(0n);

// mulShiftRightRound: round(x*y/2^z)
expect(await contract.getMulShiftRightRound(2n, 4n, 3n)).toBe(1n);
expect(await contract.getMulShiftRightRound(2n, 4n, 2n)).toBe(2n);
expect(await contract.getMulShiftRightRound(2n, 4n, 1n)).toBe(4n);
expect(await contract.getMulShiftRightRound(2n, 4n, 0n)).toBe(8n);
expect(await contract.getMulShiftRightRound(1n, 6n, 0n)).toBe(6n);
expect(await contract.getMulShiftRightRound(1n, 4n, 3n)).toBe(1n);
expect(await contract.getMulShiftRightRound(1n, 3n, 3n)).toBe(0n);

// mulShiftRightCeil: ceil(x*y/2^z)
expect(await contract.getMulShiftRightCeil(2n, 4n, 3n)).toBe(1n);
expect(await contract.getMulShiftRightCeil(2n, 4n, 2n)).toBe(2n);
expect(await contract.getMulShiftRightCeil(2n, 4n, 1n)).toBe(4n);
expect(await contract.getMulShiftRightCeil(2n, 4n, 0n)).toBe(8n);
expect(await contract.getMulShiftRightCeil(1n, 6n, 0n)).toBe(6n);
expect(await contract.getMulShiftRightCeil(1n, 7n, 3n)).toBe(1n);
expect(await contract.getMulShiftRightCeil(1n, 4n, 3n)).toBe(1n);
expect(await contract.getMulShiftRightCeil(1n, 3n, 3n)).toBe(1n);

// sqrt: round(sqrt(num))
for (let num = 0n; num <= 100n; num++) {
// console.log(`sqrt(${num}) = ${await contract.getSqrt(num)}`);
expect(await contract.getSqrt(num)).toBe(
BigInt(Math.round(Math.sqrt(Number(num)))),
);
}
for (let num = -3n; num < 0n; num++) {
await expect(contract.getSqrt(num)).rejects.toThrow(
"Unable to execute get method. Got exit_code: 5",
);
}
});
});
62 changes: 60 additions & 2 deletions src/test/e2e-emulated/stdlib.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, beginCell, toNano } from "@ton/core";
import { Address, beginCell, Cell, toNano } from "@ton/core";
import { Blockchain, SandboxContract, TreasuryContract } from "@ton/sandbox";
import { StdlibTest } from "./contracts/output/stdlib_StdlibTest";
import "@ton/test-utils";
Expand Down Expand Up @@ -56,7 +56,7 @@ describe("stdlib", () => {
.toString(),
).toBe(beginCell().storeBit(true).endCell().toString());

expect(await contract.getTvm_2023_07Upgrade()).toEqual(1355n);
expect(await contract.getTvm_2023_07Upgrade()).toEqual(1455n);
expect(await contract.getTvm_2024_04Upgrade()).toEqual(82009144n);

expect(
Expand Down Expand Up @@ -106,5 +106,63 @@ describe("stdlib", () => {
expect(addrVar.address.asCell()).toEqualCell(
beginCell().storeUint(345, 123).endCell(),
);

expect(await contract.getBuilderDepth(beginCell())).toBe(0n);
expect(
await contract.getBuilderDepth(beginCell().storeRef(Cell.EMPTY)),
).toBe(1n);

expect(await contract.getSkipLastBits(slice, 1n)).toEqualSlice(
beginCell()
.storeBit(1)
.storeRef(beginCell().storeBit(1).endCell())
.endCell()
.asSlice(),
);

expect(await contract.getFirstBits(slice, 1n)).toEqualSlice(
beginCell().storeBit(1).endCell().asSlice(),
);

expect(await contract.getLastBits(slice, 1n)).toEqualSlice(
beginCell().storeBit(1).endCell().asSlice(),
);

expect(await contract.getSliceDepth(slice)).toBe(1n);

expect(await contract.getAddressNone()).toEqual(null);

expect(
await contract.getComputeDataSizeCell(slice.asCell(), 1000n),
).toMatchObject({
$$type: "DataSize",
cells: 2n,
bits: 3n,
refs: 1n,
});

expect(
await contract.getComputeDataSizeSlice(slice, 1000n),
).toMatchObject({
$$type: "DataSize",
cells: 1n, // -1 for slice
bits: 3n,
refs: 1n,
});

expect(await contract.getCellDepth(slice.asCell())).toBe(1n);

expect(await contract.getCurLt()).toBe(0n);

expect(await contract.getBlockLt()).toBe(0n);

expect(await contract.getSetGasLimit(5000n)).toBe(3997n); // 5000 just to make sure it's enough, 3997 is how much it actually costs
await expect(contract.getSetGasLimit(3996n)).rejects.toThrow("-14"); // 3996 gas is not enough for sure

expect(await contract.getGetSeed()).toBe(0n);

expect(await contract.getSetSeed(123n)).toBe(123n);

expect(await contract.getMyCode()).toEqualCell(contract.init!.code);
});
});

0 comments on commit 8a5fed9

Please sign in to comment.