Skip to content

Commit

Permalink
feat(tests): Add clarity-parser-string-to-cv.prop.test.ts property-ba…
Browse files Browse the repository at this point in the history
…sed tests

- Implemented property-based testing for string, uint, and tuple conversions using fast-check.
- Defined custom arbitraries for generating 128-bit unsigned (uint128) and signed (int128) integers.
- Enhanced test robustness and coverage by automatically testing a wide range of input values.
- Included fast-check's shrinking feature to identify minimal failure cases, aiding in effective debugging.
  • Loading branch information
moodmosaic committed Dec 9, 2023
1 parent c85906d commit 3e9b3af
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
37 changes: 37 additions & 0 deletions contrib/core-contract-tests/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions contrib/core-contract-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@hirosystems/clarinet-sdk": "^1.1.0",
"@stacks/transactions": "^6.9.0",
"chokidar-cli": "^3.0.0",
"fast-check": "^3.13.1",
"path": "^0.12.7",
"typescript": "^5.2.2",
"vite": "^4.4.9",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { describe, expect, it } from "vitest";
import { stringToCV } from "./utils/string-to-cv";
import { ClarityType, intCV, stringUtf8CV, uintCV } from "@stacks/transactions";
import fc from "fast-check";

const uint128 = fc
.tuple(fc.bigUintN(64), fc.bigUintN(64))
.map(([hi, lo]) => (hi << BigInt(64)) | lo);

const int128 = fc
.tuple(fc.bigIntN(64), fc.bigIntN(64))
.map(([hi, lo]) => (hi << BigInt(64)) | lo);

describe("verify string to cv conversion", () => {
it("should convert string to cv", () => {
fc.assert(fc.property(fc.string(), (someStr) => {
const result = stringToCV(someStr, { "string-utf8": { length: 100 } });
expect(result).toEqual({
type: "string",
value: stringUtf8CV(someStr),
});
}));
});

it("should convert uint to cv", () => {
fc.assert(fc.property(uint128, (someUInt) => {
const result = stringToCV(`u${someUInt}`, "uint128");
expect(result).toEqual({
type: "uint",
value: uintCV(someUInt),
});
}));
});

it("should convert tuple to cv", () => {
fc.assert(fc.property(fc.record({
key: fc.stringMatching(/^[a-zA-Z][a-zA-Z0-9]{0,9}$/),
val: int128 }), (r) => {
const result = stringToCV(`{${r.key}: ${r.val}}`, {
tuple: [{ name: r.key, type: "int128" }],
});
expect(result).toEqual({
type: "tuple",
value: { data: { [r.key]: intCV(r.val) }, type: ClarityType.Tuple },
});
}));
});
});

describe("custom arbitraries for 128-bit unsigned/signed integers", () => {
it("generates 128-bit unsigned integers in range [0, 2^128 - 1]", () => {
fc.assert(fc.property(uint128, (actual) =>
actual >= BigInt(0) &&
actual <= BigInt("340282366920938463463374607431768211455")
));
});

it("generates 128-bit signed integers in range [-2^127, 2^127 - 1]", () => {
fc.assert(fc.property(int128, (actual) =>
actual >= BigInt("-170141183460469231731687303715884105728") &&
actual <= BigInt( "170141183460469231731687303715884105727")
));
});
});

0 comments on commit 3e9b3af

Please sign in to comment.