From 7e26cd06cb9ae0d4f7551e67f87e7fda4ec678dc Mon Sep 17 00:00:00 2001 From: Dimitri Stallenberg Date: Mon, 7 Aug 2023 23:34:52 +0200 Subject: [PATCH] fix: rename some options --- .../sampling/JavaScriptRandomSampler.ts | 172 +++++++++--------- .../sampling/JavaScriptTestCaseSampler.ts | 71 +++++--- .../testcase/sampling/generators/Generator.ts | 18 +- .../action/ConstructorCallGenerator.ts | 13 +- .../statements/primitive/StringStatement.ts | 7 +- tools/javascript/lib/JavaScriptLauncher.ts | 9 +- tools/javascript/lib/commands/test.ts | 50 +++-- .../plugins/sampler/RandomSamplerPlugin.ts | 14 +- 8 files changed, 215 insertions(+), 139 deletions(-) diff --git a/libraries/search-javascript/lib/testcase/sampling/JavaScriptRandomSampler.ts b/libraries/search-javascript/lib/testcase/sampling/JavaScriptRandomSampler.ts index 0544d7a5c..116470e67 100644 --- a/libraries/search-javascript/lib/testcase/sampling/JavaScriptRandomSampler.ts +++ b/libraries/search-javascript/lib/testcase/sampling/JavaScriptRandomSampler.ts @@ -61,6 +61,10 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler { constantPoolManager: ConstantPoolManager, constantPoolEnabled: boolean, constantPoolProbability: number, + typePoolEnabled: boolean, + typePoolProbability: number, + statementPoolEnabled: boolean, + statementPoolProbability: number, typeInferenceMode: string, randomTypeProbability: number, incorporateExecutionInformation: boolean, @@ -69,15 +73,17 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler { stringMaxLength: number, resampleGeneProbability: number, deltaMutationProbability: number, - exploreIllegalValues: boolean, - reuseStatementProbability: number, - useMockedObjectProbability: number + exploreIllegalValues: boolean ) { super( subject, constantPoolManager, constantPoolEnabled, constantPoolProbability, + typePoolEnabled, + typePoolProbability, + statementPoolEnabled, + statementPoolProbability, typeInferenceMode, randomTypeProbability, incorporateExecutionInformation, @@ -86,9 +92,7 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler { stringMaxLength, resampleGeneProbability, deltaMutationProbability, - exploreIllegalValues, - reuseStatementProbability, - useMockedObjectProbability + exploreIllegalValues ); } @@ -473,10 +477,16 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler { } // take from pool - const statementFromPool = this.statementPool.getRandomStatement(chosenType); - - if (statementFromPool && prng.nextBoolean(this.reuseStatementProbability)) { - return statementFromPool; + if (this.statementPoolEnabled) { + const statementFromPool = + this.statementPool.getRandomStatement(chosenType); + + if ( + statementFromPool && + prng.nextBoolean(this.statementPoolProbability) + ) { + return statementFromPool; + } } switch (chosenType) { @@ -514,88 +524,88 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler { .getTypeModel() .getObjectDescription(typeId); - const typeFromTypePool = this.rootContext - .getTypePool() - // .getRandomMatchingType(typeObject) - // TODO this prevents the sampling of function return types - // maybe we want this actually? - .getRandomMatchingType( - typeObject, - (type_) => type_.kind !== DiscoveredObjectKind.FUNCTION - ); + if (this.typePoolEnabled) { + // TODO maybe we should sample from the typepool for the other stuff as well (move this to sample arg for example) + const typeFromTypePool = this.rootContext + .getTypePool() + // .getRandomMatchingType(typeObject) + // TODO this prevents the sampling of function return types + // maybe we want this actually? + .getRandomMatchingType( + typeObject, + (type_) => type_.kind !== DiscoveredObjectKind.FUNCTION + ); - if ( - typeFromTypePool && - prng.nextBoolean(1 - this.useMockedObjectProbability) - ) { - // always prefer type from type pool - switch (typeFromTypePool.kind) { - case DiscoveredObjectKind.CLASS: { - // find constructor of class - const targets = this.rootContext.getSubTargets( - typeFromTypePool.id.split(":")[0] - ); - const constructor_ = ( - targets.find( - (target) => - target.type === TargetType.METHOD && - (target).methodType === "constructor" && - (target).classId === typeFromTypePool.id - ) - ); + if (typeFromTypePool && prng.nextBoolean(this.typePoolProbability)) { + // always prefer type from type pool + switch (typeFromTypePool.kind) { + case DiscoveredObjectKind.CLASS: { + // find constructor of class + const targets = this.rootContext.getSubTargets( + typeFromTypePool.id.split(":")[0] + ); + const constructor_ = ( + targets.find( + (target) => + target.type === TargetType.METHOD && + (target).methodType === "constructor" && + (target).classId === typeFromTypePool.id + ) + ); + + if (constructor_) { + return this.constructorCallGenerator.generate( + depth, + id, // variable id + constructor_.typeId, // constructor call id + typeFromTypePool.id, // class export id + name, + this.statementPool + ); + } - if (constructor_) { return this.constructorCallGenerator.generate( depth, id, // variable id - constructor_.typeId, // constructor call id + typeFromTypePool.id, // constructor call id typeFromTypePool.id, // class export id name, this.statementPool ); } - - return this.constructorCallGenerator.generate( - depth, - id, // variable id - typeFromTypePool.id, // constructor call id - typeFromTypePool.id, // class export id - name, - this.statementPool - ); - } - case DiscoveredObjectKind.FUNCTION: { - return this.functionCallGenerator.generate( - depth, - id, - typeFromTypePool.id, - typeFromTypePool.id, - name, - this.statementPool - ); - } - case DiscoveredObjectKind.INTERFACE: { - // TODO - return this.constructorCallGenerator.generate( - depth, - id, - typeFromTypePool.id, - typeFromTypePool.id, - name, - this.statementPool - ); - } - case DiscoveredObjectKind.OBJECT: { - return this.constantObjectGenerator.generate( - depth, - id, - typeFromTypePool.id, - typeFromTypePool.id, - name, - this.statementPool - ); + case DiscoveredObjectKind.FUNCTION: { + return this.functionCallGenerator.generate( + depth, + id, + typeFromTypePool.id, + typeFromTypePool.id, + name, + this.statementPool + ); + } + case DiscoveredObjectKind.INTERFACE: { + // TODO + return this.constructorCallGenerator.generate( + depth, + id, + typeFromTypePool.id, + typeFromTypePool.id, + name, + this.statementPool + ); + } + case DiscoveredObjectKind.OBJECT: { + return this.constantObjectGenerator.generate( + depth, + id, + typeFromTypePool.id, + typeFromTypePool.id, + name, + this.statementPool + ); + } + // No default } - // No default } } diff --git a/libraries/search-javascript/lib/testcase/sampling/JavaScriptTestCaseSampler.ts b/libraries/search-javascript/lib/testcase/sampling/JavaScriptTestCaseSampler.ts index d7ec5560e..b2e05c725 100644 --- a/libraries/search-javascript/lib/testcase/sampling/JavaScriptTestCaseSampler.ts +++ b/libraries/search-javascript/lib/testcase/sampling/JavaScriptTestCaseSampler.ts @@ -60,6 +60,13 @@ export abstract class JavaScriptTestCaseSampler extends EncodingSampler { protected _sampler: JavaScriptTestCaseSampler; protected _rootContext: RootContext; - protected _reuseStatementProbability: number; + + protected _statementPoolEnabled: boolean; + protected _statementPoolProbability: number; constructor( sampler: JavaScriptTestCaseSampler, rootContext: RootContext, - reuseStatementProbability: number + statementPoolEnabled: boolean, + statementPoolProbability: number ) { this._sampler = sampler; this._rootContext = rootContext; - this._reuseStatementProbability = reuseStatementProbability; + this._statementPoolEnabled = statementPoolEnabled; + this._statementPoolProbability = statementPoolProbability; } abstract generate( @@ -52,7 +56,11 @@ export abstract class Generator { return this._rootContext; } - get reuseStatementProbability() { - return this._reuseStatementProbability; + get statementPoolEnabled() { + return this._statementPoolEnabled; + } + + get statementPoolProbability() { + return this._statementPoolProbability; } } diff --git a/libraries/search-javascript/lib/testcase/sampling/generators/action/ConstructorCallGenerator.ts b/libraries/search-javascript/lib/testcase/sampling/generators/action/ConstructorCallGenerator.ts index 17c367b93..7844770cd 100644 --- a/libraries/search-javascript/lib/testcase/sampling/generators/action/ConstructorCallGenerator.ts +++ b/libraries/search-javascript/lib/testcase/sampling/generators/action/ConstructorCallGenerator.ts @@ -50,11 +50,16 @@ export class ConstructorCallGenerator extends CallGenerator { // ); // } - const statementFromPool = - statementPool.getRandomStatement(exportIdentifier); + if (this.statementPoolEnabled) { + const statementFromPool = + statementPool.getRandomStatement(exportIdentifier); - if (statementFromPool && prng.nextBoolean(this.reuseStatementProbability)) { - return statementFromPool; + if ( + statementFromPool && + prng.nextBoolean(this.statementPoolProbability) + ) { + return statementFromPool; + } } const type_ = this.rootContext diff --git a/libraries/search-javascript/lib/testcase/statements/primitive/StringStatement.ts b/libraries/search-javascript/lib/testcase/statements/primitive/StringStatement.ts index 123819bcb..ee4bce0a5 100644 --- a/libraries/search-javascript/lib/testcase/statements/primitive/StringStatement.ts +++ b/libraries/search-javascript/lib/testcase/statements/primitive/StringStatement.ts @@ -204,9 +204,10 @@ export class StringStatement extends PrimitiveStatement { override decode(): Decoding[] { let value = this.value; - value = value.replace(/\n/g, "\\n"); - value = value.replace(/\r/g, "\\r"); - value = value.replace(/\t/g, "\\t"); + value = value.replaceAll(/\\/g, "\\\\"); + value = value.replaceAll(/\n/g, "\\n"); + value = value.replaceAll(/\r/g, "\\r"); + value = value.replaceAll(/\t/g, "\\t"); value = value.replace(/"/g, '\\"'); return [ { diff --git a/tools/javascript/lib/JavaScriptLauncher.ts b/tools/javascript/lib/JavaScriptLauncher.ts index a204adb89..ec97538ad 100644 --- a/tools/javascript/lib/JavaScriptLauncher.ts +++ b/tools/javascript/lib/JavaScriptLauncher.ts @@ -675,6 +675,11 @@ export class JavaScriptLauncher extends Launcher { constantPoolManager, (this.arguments_).constantPool, (this.arguments_).constantPoolProbability, + (this.arguments_).typePool, + (this.arguments_).typePoolProbability, + (this.arguments_).statementPool, + (this.arguments_).statementPoolProbability, + (this.arguments_).typeInferenceMode, (this.arguments_).randomTypeProbability, (this.arguments_).incorporateExecutionInformation, @@ -683,9 +688,7 @@ export class JavaScriptLauncher extends Launcher { this.arguments_.stringMaxLength, this.arguments_.resampleGeneProbability, this.arguments_.deltaMutationProbability, - this.arguments_.exploreIllegalValues, - (this.arguments_).reuseStatementProbability, - (this.arguments_).useMockedObjectProbability + this.arguments_.exploreIllegalValues ); sampler.rootContext = rootContext; diff --git a/tools/javascript/lib/commands/test.ts b/tools/javascript/lib/commands/test.ts index 704e6efdb..0af5e1b07 100644 --- a/tools/javascript/lib/commands/test.ts +++ b/tools/javascript/lib/commands/test.ts @@ -64,40 +64,58 @@ export function getTestCommand( type: "number", }); - options.set("reuse-statement-probability", { + options.set("constant-pool", { alias: [], - default: 0.9, + default: true, + description: "Enable constant pool.", + group: samplingGroup, + hidden: false, + type: "boolean", + }); + + options.set("constant-pool-probability", { + alias: [], + default: 0.5, description: - "The probability we reuse a statement instead of generating a new one.", + "Probability to sample from the constant pool instead creating random values", group: samplingGroup, hidden: false, type: "number", }); - options.set("use-mocked-object-probability", { + options.set("type-pool", { alias: [], - default: 0.1, + default: true, + description: "Enable type pool.", + group: samplingGroup, + hidden: false, + type: "boolean", + }); + + options.set("type-pool-probability", { + alias: [], + default: 0.9, description: - "The probability we use a mocked object instead of generating an actual instance.", + "Probability to sample from the type pool instead creating random values", group: samplingGroup, hidden: false, type: "number", }); - options.set("constant-pool", { + options.set("statement-pool", { alias: [], - default: false, - description: "Enable constant pool.", + default: true, + description: "Enable statement pool.", group: samplingGroup, hidden: false, type: "boolean", }); - options.set("constant-pool-probability", { + options.set("statement-pool-probability", { alias: [], - default: 0.5, + default: 0.9, description: - "Probability to sample from the constant pool instead creating random values", + "Probability to sample from the statement pool instead creating new values", group: samplingGroup, hidden: false, type: "number", @@ -105,7 +123,7 @@ export function getTestCommand( options.set("execution-timeout", { alias: [], - default: 5000, + default: 2000, description: "The timeout for one execution of one test (must be larger than the test-timeout).", group: executorGroup, @@ -145,10 +163,12 @@ export type TestCommandOptions = { incorporateExecutionInformation: boolean; typeInferenceMode: string; randomTypeProbability: number; - reuseStatementProbability: number; - useMockedObjectProbability: number; constantPool: boolean; constantPoolProbability: number; + typePool: boolean; + typePoolProbability: number; + statementPool: boolean; + statementPoolProbability: number; executionTimeout: number; testTimeout: number; }; diff --git a/tools/javascript/lib/plugins/sampler/RandomSamplerPlugin.ts b/tools/javascript/lib/plugins/sampler/RandomSamplerPlugin.ts index b6622820c..3ae0bd3f3 100644 --- a/tools/javascript/lib/plugins/sampler/RandomSamplerPlugin.ts +++ b/tools/javascript/lib/plugins/sampler/RandomSamplerPlugin.ts @@ -39,9 +39,13 @@ export class RandomSamplerPlugin extends SamplerPlugin { ): EncodingSampler { return new JavaScriptRandomSampler( options.subject as unknown as JavaScriptSubject, - undefined, - undefined, - undefined, + undefined, // TODO incorrect constant pool should be part of sampler options + ((this.args)).constantPool, + ((this.args)).constantPoolProbability, + ((this.args)).typePool, + ((this.args)).typePoolProbability, + ((this.args)).statementPool, + ((this.args)).statementPoolProbability, ((this.args)).typeInferenceMode, ((this.args)).randomTypeProbability, (( @@ -52,9 +56,7 @@ export class RandomSamplerPlugin extends SamplerPlugin { ((this.args)).stringMaxLength, ((this.args)).resampleGeneProbability, ((this.args)).deltaMutationProbability, - ((this.args)).exploreIllegalValues, - ((this.args)).reuseStatementProbability, - ((this.args)).useMockedObjectProbability + ((this.args)).exploreIllegalValues ); }