Skip to content

Commit

Permalink
feat: allow removal of arguments (#211)
Browse files Browse the repository at this point in the history
Next to adding anonymous arguments (even though the function description specified less), it is now possible for arguments to removed (even though the function description specified more).

This is usefull for example when dealing with arguments with default values.
  • Loading branch information
dstallenberg authored Dec 11, 2023
1 parent 052d8a6 commit 3f32471
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -904,8 +904,9 @@ export class InferenceTypeModelFactory extends TypeModelFactory {
originalInvolved: string[]
) {
const [objectId, propertyId] = involved;
const [, originalProperty] = originalInvolved;
const [originalObject, originalProperty] = originalInvolved;

const objectElement = elementMap.get(originalObject);
const propertyElement = elementMap.get(originalProperty);

if (propertyElement === undefined) {
Expand All @@ -921,6 +922,15 @@ export class InferenceTypeModelFactory extends TypeModelFactory {
// add array type to object
this._typeModel.addElementType(objectId, relation.id);

if (
objectElement &&
objectElement.type === ElementType.Identifier &&
objectElement.name === "arguments"
) {
// e.g. arguments[0]
// TODO get function parent and add the argument (impossible right now)
}

break;
}
case ElementType.StringLiteral: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler {
stringAlphabet: string,
stringMaxLength: number,
deltaMutationProbability: number,
exploreIllegalValues: boolean
exploreIllegalValues: boolean,
addRemoveArgumentProbability: number,
addArgumentProbability: number,
removeArgumentProbability: number
) {
super(
subject,
Expand All @@ -94,7 +97,10 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler {
stringAlphabet,
stringMaxLength,
deltaMutationProbability,
exploreIllegalValues
exploreIllegalValues,
addRemoveArgumentProbability,
addArgumentProbability,
removeArgumentProbability
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export abstract class JavaScriptTestCaseSampler extends EncodingSampler<JavaScri

private _exploreIllegalValues: boolean;

protected _addRemoveArgumentProbability: number;
protected _addArgumentProbability: number;
protected _removeArgumentProbability: number;

private _statementPool: StatementPool | null;

private _functionCallGenerator: FunctionCallGenerator;
Expand Down Expand Up @@ -106,7 +110,10 @@ export abstract class JavaScriptTestCaseSampler extends EncodingSampler<JavaScri
stringAlphabet: string,
stringMaxLength: number,
deltaMutationProbability: number,
exploreIllegalValues: boolean
exploreIllegalValues: boolean,
addRemoveArgumentProbability: number,
addArgumentProbability: number,
removeArgumentProbability: number
) {
super(subject);
this._constantPoolManager = constantPoolManager;
Expand All @@ -127,6 +134,10 @@ export abstract class JavaScriptTestCaseSampler extends EncodingSampler<JavaScri
this._stringMaxLength = stringMaxLength;
this._deltaMutationProbability = deltaMutationProbability;
this._exploreIllegalValues = exploreIllegalValues;

this._addRemoveArgumentProbability = addRemoveArgumentProbability;
this._addArgumentProbability = addArgumentProbability;
this._removeArgumentProbability = removeArgumentProbability;
}

get rootContext() {
Expand All @@ -140,43 +151,64 @@ export abstract class JavaScriptTestCaseSampler extends EncodingSampler<JavaScri
this,
rootContext,
this._statementPoolEnabled,
this._statementPoolProbability
this._statementPoolProbability,
this._addRemoveArgumentProbability,
this._addArgumentProbability,
this._removeArgumentProbability
);
this._constructorCallGenerator = new ConstructorCallGenerator(
this,
rootContext,
this._statementPoolEnabled,
this._statementPoolProbability
this._statementPoolProbability,
this._addRemoveArgumentProbability,
this._addArgumentProbability,
this._removeArgumentProbability
);
this._methodCallGenerator = new MethodCallGenerator(
this,
rootContext,
this._statementPoolEnabled,
this._statementPoolProbability
this._statementPoolProbability,
this._addRemoveArgumentProbability,
this._addArgumentProbability,
this._removeArgumentProbability
);
this._getterGenerator = new GetterGenerator(
this,
rootContext,
this._statementPoolEnabled,
this._statementPoolProbability
this._statementPoolProbability,
this._addRemoveArgumentProbability,
this._addArgumentProbability,
this._removeArgumentProbability
);
this._setterGenerator = new SetterGenerator(
this,
rootContext,
this._statementPoolEnabled,
this._statementPoolProbability
this._statementPoolProbability,
this._addRemoveArgumentProbability,
this._addArgumentProbability,
this._removeArgumentProbability
);
this._constantObjectGenerator = new ConstantObjectGenerator(
this,
rootContext,
this._statementPoolEnabled,
this._statementPoolProbability
this._statementPoolProbability,
this._addRemoveArgumentProbability,
this._addArgumentProbability,
this._removeArgumentProbability
);
this._objectFunctionCallGenerator = new ObjectFunctionCallGenerator(
this,
rootContext,
this._statementPoolEnabled,
this._statementPoolProbability
this._statementPoolProbability,
this._addRemoveArgumentProbability,
this._addArgumentProbability,
this._removeArgumentProbability
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,26 @@ export abstract class Generator<S extends Statement> {
protected _statementPoolEnabled: boolean;
protected _statementPoolProbability: number;

protected _addRemoveArgumentProbability: number;
protected _addArgumentProbability: number;
protected _removeArgumentProbability: number;

constructor(
sampler: JavaScriptTestCaseSampler,
rootContext: RootContext,
statementPoolEnabled: boolean,
statementPoolProbability: number
statementPoolProbability: number,
addRemoveArgumentProbability: number,
addArgumentProbability: number,
removeArgumentProbability: number
) {
this._sampler = sampler;
this._rootContext = rootContext;
this._statementPoolEnabled = statementPoolEnabled;
this._statementPoolProbability = statementPoolProbability;
this._addRemoveArgumentProbability = addRemoveArgumentProbability;
this._addArgumentProbability = addArgumentProbability;
this._removeArgumentProbability = removeArgumentProbability;
}

abstract generate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,21 @@ export abstract class CallGenerator<S extends Statement> extends Generator<S> {
}
}

for (let index = 0; index < 10; index++) {
if (prng.nextBoolean(0.05)) {
// TODO make this a config parameter
// 50/50 to either maybe remove or add an argument
if (prng.nextBoolean(this._addRemoveArgumentProbability)) {
// remove args
for (let index = 0; index < arguments_.length; index++) {
if (!prng.nextBoolean(this._removeArgumentProbability)) {
break;
}
arguments_.pop();
}
} else {
// add args
for (let index = 0; index < 10; index++) {
if (!prng.nextBoolean(this._addArgumentProbability)) {
break;
}
arguments_.push(this.sampler.sampleArgument(depth + 1, "anon", "anon"));
}
}
Expand Down
5 changes: 4 additions & 1 deletion tools/javascript/lib/JavaScriptLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,10 @@ export class JavaScriptLauncher extends Launcher<JavaScriptArguments> {
this.arguments_.stringAlphabet,
this.arguments_.stringMaxLength,
this.arguments_.deltaMutationProbability,
this.arguments_.exploreIllegalValues
this.arguments_.exploreIllegalValues,
this.arguments_.addRemoveArgumentProbability,
this.arguments_.addArgumentProbability,
this.arguments_.removeArgumentProbability
);
sampler.rootContext = rootContext;

Expand Down
35 changes: 35 additions & 0 deletions tools/javascript/lib/commands/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,36 @@ export function getTestCommand(
type: "number",
});

options.set("add-remove-argument-probability", {
alias: [],
default: 0.5,
description:
"Probability to maybe add an argument as oposed to maybe remove one.",
group: samplingGroup,
hidden: false,
type: "number",
});

options.set("add-argument-probability", {
alias: [],
default: 0.1,
description:
"Probability to add one extra anonymous argument (probability to add two args is equal to probablity squared, etc.) (maximum of 10).",
group: samplingGroup,
hidden: false,
type: "number",
});

options.set("remove-argument-probability", {
alias: [],
default: 0.1,
description:
"Probability to remove an argument (probability to remove two args is equal to probablity squared, etc.).",
group: samplingGroup,
hidden: false,
type: "number",
});

options.set("execution-timeout", {
alias: [],
default: 2000,
Expand Down Expand Up @@ -187,6 +217,11 @@ export type TestCommandOptions = {
typePoolProbability: number;
statementPool: boolean;
statementPoolProbability: number;

addRemoveArgumentProbability: number;
addArgumentProbability: number;
removeArgumentProbability: number;

executionTimeout: number;
testTimeout: number;

Expand Down
5 changes: 4 additions & 1 deletion tools/javascript/lib/plugins/sampler/RandomSamplerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ export class RandomSamplerPlugin extends SamplerPlugin<JavaScriptTestCase> {
(<JavaScriptArguments>(<unknown>this.args)).stringAlphabet,
(<JavaScriptArguments>(<unknown>this.args)).stringMaxLength,
(<JavaScriptArguments>(<unknown>this.args)).deltaMutationProbability,
(<JavaScriptArguments>(<unknown>this.args)).exploreIllegalValues
(<JavaScriptArguments>(<unknown>this.args)).exploreIllegalValues,
(<JavaScriptArguments>(<unknown>this.args)).addRemoveArgumentProbability,
(<JavaScriptArguments>(<unknown>this.args)).addArgumentProbability,
(<JavaScriptArguments>(<unknown>this.args)).removeArgumentProbability
);
}

Expand Down

0 comments on commit 3f32471

Please sign in to comment.