@kstasi/jest-tolk
Version:
<p align="center"> <picture> <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/tonkite/tonkite/main/assets/logo-dark.svg"> <img alt="tonkite logo" src="https://raw.githubusercontent.com/tonkite/tonkite/main/a
44 lines (43 loc) • 1.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateIntTree = generateIntTree;
const int_tree_1 = require("./int-tree");
function generateRandom(bits, signed) {
const max = signed
? (BigInt(1) << BigInt(bits - 1)) - BigInt(1)
: (BigInt(1) << BigInt(bits)) - BigInt(1);
const value = BigInt(Math.floor(Math.random() * Number(max)));
return signed && Math.random() < 0.5 ? -value : max;
}
function generateEdge(bits, signed) {
const EDGE_CASES = signed
? [
BigInt(0),
BigInt(1),
BigInt(-1),
(BigInt(1) << BigInt(bits - 1)) - BigInt(1),
-(BigInt(1) << BigInt(bits - 1)),
]
: [BigInt(0), BigInt(1), (BigInt(1) << BigInt(bits)) - BigInt(1)];
return EDGE_CASES[Math.floor(Math.random() * EDGE_CASES.length)];
}
function generateIntTree(bits, signed, fixtures) {
console.log('fixtures:', fixtures);
if (fixtures && fixtures.length > 0) {
fixtures.forEach((item) => {
if (item.type !== 'int') {
throw new Error(`All fixtures must be of type int, but got ${item.type}`);
}
});
return (0, int_tree_1.createIntTree)(
// @ts-ignore
fixtures[Math.floor(Math.random() * fixtures.length)].value, bits, signed, true);
}
const EDGE_WEIGHT = 5;
const RANDOM_WEIGHT = 95;
const totalWeight = EDGE_WEIGHT + RANDOM_WEIGHT;
const bias = Math.floor(Math.random() * totalWeight);
if (bias < EDGE_WEIGHT)
return (0, int_tree_1.createIntTree)(generateEdge(bits, signed), bits, signed);
return (0, int_tree_1.createIntTree)(generateRandom(bits, signed), bits, signed);
}