@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
112 lines (111 loc) • 3.47 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IntStrategy = void 0;
class IntValueTree {
lo;
curr;
hi;
fixed;
constructor(start, fixed) {
this.lo = BigInt(0);
this.curr = start;
this.hi = start;
this.fixed = fixed;
}
getCurrent() {
return this.curr;
}
reposition() {
const interval = this.hi - this.lo;
const newMid = this.lo + interval / BigInt(2);
if (newMid === this.curr) {
return false;
}
else {
this.curr = newMid;
return true;
}
}
magnitudeGreater(lhs, rhs) {
if (lhs === BigInt(0))
return false;
return lhs > rhs !== lhs < BigInt(0);
}
simplify() {
if (this.fixed || !this.magnitudeGreater(this.hi, this.lo)) {
return false;
}
this.hi = this.curr;
return this.reposition();
}
complicate() {
if (this.fixed || !this.magnitudeGreater(this.hi, this.lo)) {
return false;
}
this.lo =
this.curr !== BigInt(Number.MIN_SAFE_INTEGER) &&
this.curr !== BigInt(Number.MAX_SAFE_INTEGER)
? this.curr + (this.hi < BigInt(0) ? BigInt(-1) : BigInt(1))
: this.curr;
return this.reposition();
}
}
class IntStrategy {
bits;
fixtures;
edgeWeight;
fixturesWeight;
randomWeight;
constructor(bits, fixtures = []) {
this.bits = bits;
this.fixtures = fixtures;
this.edgeWeight = 10;
this.fixturesWeight = 40;
this.randomWeight = 50;
}
randomBigInt(bits) {
const max = (BigInt(1) << BigInt(bits)) - BigInt(1);
return BigInt(Math.floor(Math.random() * Number(max)));
}
generateEdgeTree() {
const kind = Math.floor(Math.random() * 4);
const offset = BigInt(Math.floor(Math.random() * 4));
switch (kind) {
case 0: // Around MIN
return new IntValueTree(-(BigInt(1) << BigInt(this.bits - 1)) + offset, false);
case 1: // Around -0
return new IntValueTree(-offset - BigInt(1), false);
case 2: // Around +0
return new IntValueTree(offset, false);
case 3: // Around MAX
return new IntValueTree((BigInt(1) << BigInt(this.bits - 1)) - BigInt(1) - offset, false);
default:
throw new Error('Invalid edge case');
}
}
generateFixtureTree() {
if (this.fixtures.length === 0) {
return this.generateRandomTree();
}
const fixture = this.fixtures[Math.floor(Math.random() * this.fixtures.length)];
return new IntValueTree(fixture, false);
}
generateRandomTree() {
const randomValue = this.randomBigInt(this.bits);
return new IntValueTree(randomValue, false);
}
generateTree() {
const totalWeight = this.randomWeight + this.fixturesWeight + this.edgeWeight;
const bias = Math.floor(Math.random() * totalWeight);
if (bias < this.edgeWeight) {
return this.generateEdgeTree();
}
else if (bias < this.edgeWeight + this.fixturesWeight) {
return this.generateFixtureTree();
}
else {
return this.generateRandomTree();
}
}
}
exports.IntStrategy = IntStrategy;