UNPKG

@syntest/analysis-javascript

Version:

SynTest CFG JavaScript is a library for generating control flow graphs for the JavaScript language

154 lines 5.09 kB
"use strict"; /* * Copyright 2020-2023 Delft University of Technology and SynTest contributors * * This file is part of SynTest Framework - SynTest JavaScript. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ConstantPool = void 0; const prng_1 = require("@syntest/prng"); class ConstantPool { constructor() { this._numericPool = new Map(); this.addNumeric(Math.PI); this.addNumeric(Math.E); this._integerPool = new Map(); this.addInteger(-1); this.addInteger(0); this.addInteger(+1); this._bigIntPool = new Map(); this._stringPool = new Map(); this.addString(""); } addNumeric(value) { if (this._numericPool.has(value)) { this._numericPool.set(value, this._numericPool.get(value) + 1); } else { this._numericPool.set(value, 1); } this._numericCount++; } addInteger(value) { if (this._integerPool.has(value)) { this._integerPool.set(value, this._integerPool.get(value) + 1); } else { this._integerPool.set(value, 1); } this._integerCount++; this.addNumeric(value); } addBigInt(value) { if (this._bigIntPool.has(value)) { this._bigIntPool.set(value, this._bigIntPool.get(value) + 1); } else { this._bigIntPool.set(value, 1); } this._bigIntCount++; } addString(value) { if (this._stringPool.has(value)) { this._stringPool.set(value, this._stringPool.get(value) + 1); } else { this._stringPool.set(value, 1); } this._stringCount++; } getRandomNumeric(frequencyBased = false) { if (this._numericPool.size === 0) { return undefined; } if (frequencyBased) { let index = prng_1.prng.nextDouble() * this._numericCount; for (const [value, frequency] of this._numericPool.entries()) { if (index >= frequency) { return value; } else { index -= frequency; } } return prng_1.prng.pickOne([...this._numericPool.keys()]); } else { return prng_1.prng.pickOne([...this._numericPool.keys()]); } } getRandomInteger(frequencyBased = false) { if (this._integerPool.size === 0) { return undefined; } if (frequencyBased) { let index = prng_1.prng.nextDouble() * this._integerCount; for (const [value, frequency] of this._integerPool.entries()) { if (index >= frequency) { return value; } else { index -= frequency; } } return prng_1.prng.pickOne([...this._integerPool.keys()]); } else { return prng_1.prng.pickOne([...this._integerPool.keys()]); } } getRandomBigInt(frequencyBased = false) { if (this._bigIntPool.size === 0) { return undefined; } if (frequencyBased) { let index = prng_1.prng.nextDouble() * this._bigIntCount; for (const [value, frequency] of this._bigIntPool.entries()) { if (index >= frequency) { return value; } else { index -= frequency; } } return prng_1.prng.pickOne([...this._bigIntPool.keys()]); } else { return prng_1.prng.pickOne([...this._bigIntPool.keys()]); } } getRandomString(frequencyBased = false) { if (this._stringPool.size === 0) { return undefined; } if (frequencyBased) { let index = prng_1.prng.nextDouble() * this._stringCount; for (const [value, frequency] of this._stringPool.entries()) { if (index >= frequency) { return value; } else { index -= frequency; } } return prng_1.prng.pickOne([...this._stringPool.keys()]); } else { return prng_1.prng.pickOne([...this._stringPool.keys()]); } } } exports.ConstantPool = ConstantPool; //# sourceMappingURL=ConstantPool.js.map