Skip to content

Commit

Permalink
use deep equal for param order checks
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Nov 7, 2023
1 parent 912f404 commit 64fd977
Show file tree
Hide file tree
Showing 24 changed files with 147 additions and 188 deletions.
8 changes: 8 additions & 0 deletions property_tests/arbitraries/candid/constructed/vec_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ function generateValueLiteral<T extends CandidType>(

const valueLiteral = `[${valueLiterals}]`;

if (candidType === 'int64') {
return `BigInt64Array.from(${valueLiteral})`;
}

if (candidType === 'int32') {
return `Int32Array.from(${valueLiteral})`;
}
Expand All @@ -108,6 +112,10 @@ function generateValueLiteral<T extends CandidType>(
return `Int8Array.from(${valueLiteral})`;
}

if (candidType === 'nat64') {
return `BigUint64Array.from(${valueLiteral})`;
}

if (candidType === 'nat32') {
return `Uint32Array.from(${valueLiteral})`;
}
Expand Down
18 changes: 18 additions & 0 deletions property_tests/are_params_correctly_ordered.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { CandidMeta } from './arbitraries/candid/candid_arb';
import { CandidType } from './arbitraries/candid/candid_type_arb';

export function areParamsCorrectlyOrdered<T extends CandidType>(
paramNames: string[],
params: CandidMeta<T>[]
) {
return paramNames
.map((paramName, index) => {
const areEqual = `deepEqual(
${paramName},
${params[index].src.valueLiteral}
)`;

return `if (!${areEqual}) throw new Error('${paramName} is incorrectly ordered')`;
})
.join('\n');
}
17 changes: 5 additions & 12 deletions property_tests/tests/blob/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createUniquePrimitiveArb } from '../../../arbitraries/unique_primitive_
import { getActor, runPropTests } from '../../../../property_tests';
import { CandidMeta } from '../../../arbitraries/candid/candid_arb';
import { Test } from '../../../../test';
import { areParamsCorrectlyOrdered } from '../../../are_params_correctly_ordered';

const BlobTestArb = fc
.tuple(
Expand Down Expand Up @@ -50,18 +51,10 @@ function generateBody(
})
.join('\n');

// TODO this ordering check is not perfect
// TODO but turning the vec into a string seems a bit difficult...we need to figure out how to check perfecly for the values that we want
// TODO maybe a global variable that we can write into and call would work
const paramsCorrectlyOrdered = paramNames
.map((paramName, index) => {
const paramIsCorrectLength = `${paramName}.length === ${paramBlobs[index].value.length}`;

const throwError = `throw new Error('${paramName} is incorrectly ordered')`;

return `if (!(${paramIsCorrectLength})) ${throwError};`;
})
.join('\n');
const paramsCorrectlyOrdered = areParamsCorrectlyOrdered(
paramNames,
paramBlobs
);

const returnStatement = `Uint8Array.from([${[...returnBlob.value]} ${
returnBlob.value.length > 0 ? ',' : ''
Expand Down
11 changes: 5 additions & 6 deletions property_tests/tests/bool/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createUniquePrimitiveArb } from '../../../arbitraries/unique_primitive_
import { getActor, runPropTests } from '../../../../property_tests';
import { CandidMeta } from '../../../arbitraries/candid/candid_arb';
import { Test } from '../../../../test';
import { areParamsCorrectlyOrdered } from '../../../are_params_correctly_ordered';

const BoolTestArb = fc
.tuple(
Expand Down Expand Up @@ -55,12 +56,10 @@ function generateBody(
})
.join('\n');

const paramLiterals = paramBools.map((bool) => bool.src.valueLiteral);
const paramsCorrectlyOrdered = paramNames
.map((paramName, index) => {
return `if (${paramName} !== ${paramLiterals[index]}) throw new Error('${paramName} is incorrectly ordered')`;
})
.join('\n');
const paramsCorrectlyOrdered = areParamsCorrectlyOrdered(
paramNames,
paramBools
);

const returnStatement = paramNames.reduce((acc, paramName) => {
return `${acc} && ${paramName}`;
Expand Down
11 changes: 4 additions & 7 deletions property_tests/tests/float32/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createUniquePrimitiveArb } from '../../../arbitraries/unique_primitive_
import { getActor, runPropTests } from '../../../../property_tests';
import { CandidMeta } from '../../../arbitraries/candid/candid_arb';
import { Test } from '../../../../test';
import { areParamsCorrectlyOrdered } from '../../../are_params_correctly_ordered';

const Float32TestArb = fc
.tuple(
Expand Down Expand Up @@ -61,14 +62,10 @@ function generateBody(
})
.join('\n');

const paramLiterals = paramFloat32s.map(
(float32s) => float32s.src.valueLiteral
const paramsCorrectlyOrdered = areParamsCorrectlyOrdered(
paramNames,
paramFloat32s
);
const paramsCorrectlyOrdered = paramNames
.map((paramName, index) => {
return `if (!deepEqual(${paramName}, ${paramLiterals[index]})) throw new Error('${paramName} is incorrectly ordered')`;
})
.join('\n');

const returnStatement =
paramFloat32s.length === 0
Expand Down
11 changes: 4 additions & 7 deletions property_tests/tests/float64/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createUniquePrimitiveArb } from '../../../arbitraries/unique_primitive_
import { getActor, runPropTests } from '../../../../property_tests';
import { CandidMeta } from '../../../arbitraries/candid/candid_arb';
import { Test } from '../../../../test';
import { areParamsCorrectlyOrdered } from '../../../are_params_correctly_ordered';

const Float64TestArb = fc
.tuple(
Expand Down Expand Up @@ -61,14 +62,10 @@ function generateBody(
})
.join('\n');

const paramLiterals = paramFloat64s.map(
(float64) => float64.src.valueLiteral
const paramsCorrectlyOrdered = areParamsCorrectlyOrdered(
paramNames,
paramFloat64s
);
const paramsCorrectlyOrdered = paramNames
.map((paramName, index) => {
return `if (!deepEqual(${paramName}, ${paramLiterals[index]})) throw new Error('${paramName} is incorrectly ordered')`;
})
.join('\n');

const sum = paramNames.reduce((acc, paramName) => {
return `${acc} + ${paramName}`;
Expand Down
25 changes: 11 additions & 14 deletions property_tests/tests/func/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { FuncArb, Func } from '../../../arbitraries/candid/reference/func_arb';
import { TestSample } from '../../../arbitraries/test_sample_arb';
import { UniqueIdentifierArb } from '../../../arbitraries/unique_identifier_arb';
import { Test } from '../../../../test';
import { areParamsCorrectlyOrdered } from '../../../are_params_correctly_ordered';

const FuncTestArb = fc
.tuple(
Expand Down Expand Up @@ -44,7 +45,7 @@ const FuncTestArb = fc

const returnCandidType = returnFunc.src.candidType;

const body = generateBody(paramFuncs, returnFunc);
const body = generateBody(paramNames, paramFuncs, returnFunc);

const test = generateTest(functionName, paramFuncs, returnFunc);

Expand All @@ -63,12 +64,13 @@ const FuncTestArb = fc
runPropTests(FuncTestArb);

function generateBody(
funcs: CandidMeta<Func>[],
paramNames: string[],
paramFuncs: CandidMeta<Func>[],
returnFunc: CandidMeta<Func>
): string {
const paramsAreFuncs = funcs
const paramsAreFuncs = paramFuncs
.map((_, index) => {
const paramName = `param${index}`;
const paramName = paramNames[index];

const paramIsArray = `Array.isArray(${paramName})`;
const paramHas2Fields = `${paramName}.length === 2`;
Expand All @@ -88,18 +90,13 @@ function generateBody(
})
.join('\n');

const paramsCorrectlyOrdered = funcs
.map((func, paramIndex): string => {
const paramName = `param${paramIndex}`;

const throwError = `throw new Error('${paramName} is incorrectly ordered')`;

return `if (!deepEqual(${paramName}, ${func.src.valueLiteral})) ${throwError}`;
})
.join('\n');
const paramsCorrectlyOrdered = areParamsCorrectlyOrdered(
paramNames,
paramFuncs
);

const returnStatement =
funcs.length === 0 ? returnFunc.src.valueLiteral : `param0`;
paramFuncs.length === 0 ? returnFunc.src.valueLiteral : `param0`;

return `
${paramsAreFuncs}
Expand Down
11 changes: 5 additions & 6 deletions property_tests/tests/int/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createUniquePrimitiveArb } from '../../../arbitraries/unique_primitive_
import { getActor, runPropTests } from '../../../../property_tests';
import { CandidMeta } from '../../../arbitraries/candid/candid_arb';
import { Test } from '../../../../test';
import { areParamsCorrectlyOrdered } from '../../../are_params_correctly_ordered';

const IntTestArb = fc
.tuple(
Expand Down Expand Up @@ -57,12 +58,10 @@ function generateBody(
return `${acc} + ${paramName}`;
}, returnInt.src.valueLiteral);

const paramLiterals = paramInts.map((sample) => sample.src.valueLiteral);
const paramsCorrectlyOrdered = paramNames
.map((paramName, index) => {
return `if (${paramName} !== ${paramLiterals[index]}) throw new Error('${paramName} is incorrectly ordered')`;
})
.join('\n');
const paramsCorrectlyOrdered = areParamsCorrectlyOrdered(
paramNames,
paramInts
);

return `
${paramsAreBigInts}
Expand Down
11 changes: 4 additions & 7 deletions property_tests/tests/int16/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createUniquePrimitiveArb } from '../../../arbitraries/unique_primitive_
import { getActor, runPropTests } from '../../../../property_tests';
import { CandidMeta } from '../../../arbitraries/candid/candid_arb';
import { Test } from '../../../../test';
import { areParamsCorrectlyOrdered } from '../../../are_params_correctly_ordered';

const Int16TestArb = fc
.tuple(
Expand Down Expand Up @@ -57,14 +58,10 @@ function generateBody(
})
.join('\n');

const paramValueLiterals = paramInt16s.map(
(sample) => sample.src.valueLiteral
const paramsCorrectlyOrdered = areParamsCorrectlyOrdered(
paramNames,
paramInt16s
);
const paramsCorrectlyOrdered = paramNames
.map((paramName, index) => {
return `if (${paramName} !== ${paramValueLiterals[index]}) throw new Error('${paramName} is incorrectly ordered')`;
})
.join('\n');

const sum = paramNames.reduce((acc, paramName) => {
return `${acc} + ${paramName}`;
Expand Down
11 changes: 5 additions & 6 deletions property_tests/tests/int32/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createUniquePrimitiveArb } from '../../../arbitraries/unique_primitive_
import { getActor, runPropTests } from '../../../../property_tests';
import { CandidMeta } from '../../../arbitraries/candid/candid_arb';
import { Test } from '../../../../test';
import { areParamsCorrectlyOrdered } from '../../../are_params_correctly_ordered';

const Int32TestArb = fc
.tuple(
Expand Down Expand Up @@ -63,12 +64,10 @@ function generateBody(
const count = paramInt32s.length + 1;
const average = `Math.floor((${sum}) / ${count})`;

const paramLiterals = paramInt32s.map((sample) => sample.src.valueLiteral);
const paramsCorrectlyOrdered = paramNames
.map((paramName, index) => {
return `if (${paramName} !== ${paramLiterals[index]}) throw new Error('${paramName} is incorrectly ordered')`;
})
.join('\n');
const paramsCorrectlyOrdered = areParamsCorrectlyOrdered(
paramNames,
paramInt32s
);

return `
${paramsAreNumbers}
Expand Down
12 changes: 5 additions & 7 deletions property_tests/tests/int64/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createUniquePrimitiveArb } from '../../../arbitraries/unique_primitive_
import { getActor, runPropTests } from '../../../../property_tests';
import { CandidMeta } from '../../../arbitraries/candid/candid_arb';
import { Test } from '../../../../test';
import { areParamsCorrectlyOrdered } from '../../../are_params_correctly_ordered';

const Int64TestArb = fc
.tuple(
Expand Down Expand Up @@ -63,13 +64,10 @@ function generateBody(
const count = paramInt64s.length + 1;
const average = `(${sum}) / ${count}n`;

const paramLiterals = paramInt64s.map((sample) => sample.src.valueLiteral);

const paramsCorrectlyOrdered = paramNames
.map((paramName, index) => {
return `if (${paramName} !== ${paramLiterals[index]}) throw new Error('${paramName} is incorrectly ordered')`;
})
.join('\n');
const paramsCorrectlyOrdered = areParamsCorrectlyOrdered(
paramNames,
paramInt64s
);

return `
${paramsAreBigInts}
Expand Down
12 changes: 5 additions & 7 deletions property_tests/tests/int8/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createUniquePrimitiveArb } from '../../../arbitraries/unique_primitive_
import { getActor, runPropTests } from '../../../../property_tests';
import { CandidMeta } from '../../../arbitraries/candid/candid_arb';
import { Test } from '../../../../test';
import { areParamsCorrectlyOrdered } from '../../../are_params_correctly_ordered';

const Int8TestArb = fc
.tuple(
Expand Down Expand Up @@ -59,13 +60,10 @@ function generateBody(
const count = paramInt8s.length + 1;
const average = `Math.floor((${sum}) / ${count})`;

const paramLiterals = paramInt8s.map((sample) => sample.src.valueLiteral);

const paramsCorrectlyOrdered = paramNames
.map((paramName, index) => {
return `if (${paramName} !== ${paramLiterals[index]}) throw new Error('${paramName} is incorrectly ordered')`;
})
.join('\n');
const paramsCorrectlyOrdered = areParamsCorrectlyOrdered(
paramNames,
paramInt8s
);

return `
${paramsAreNumbers}
Expand Down
11 changes: 5 additions & 6 deletions property_tests/tests/nat/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createUniquePrimitiveArb } from '../../../arbitraries/unique_primitive_
import { getActor, runPropTests } from '../../../../property_tests';
import { CandidMeta } from '../../../arbitraries/candid/candid_arb';
import { Test } from '../../../../test';
import { areParamsCorrectlyOrdered } from '../../../are_params_correctly_ordered';

const NatTestArb = fc
.tuple(
Expand Down Expand Up @@ -57,13 +58,11 @@ function generateBody(
return `${acc} + ${paramName}`;
}, returnNat.src.valueLiteral);

const paramLiterals = paramNats.map((sample) => sample.src.valueLiteral);
const paramsCorrectlyOrdered = areParamsCorrectlyOrdered(
paramNames,
paramNats
);

const paramsCorrectlyOrdered = paramNames
.map((paramName, index) => {
return `if (${paramName} !== ${paramLiterals[index]}) throw new Error('${paramName} is incorrectly ordered')`;
})
.join('\n');
return `
${paramsCorrectlyOrdered}
Expand Down
11 changes: 5 additions & 6 deletions property_tests/tests/nat16/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createUniquePrimitiveArb } from '../../../arbitraries/unique_primitive_
import { getActor, runPropTests } from '../../../../property_tests';
import { CandidMeta } from '../../../arbitraries/candid/candid_arb';
import { Test } from '../../../../test';
import { areParamsCorrectlyOrdered } from '../../../are_params_correctly_ordered';

const Nat16TestArb = fc
.tuple(
Expand Down Expand Up @@ -63,12 +64,10 @@ function generateBody(
const count = paramNat16s.length + 1;
const average = `Math.floor((${sum}) / ${count})`;

const paramLiterals = paramNat16s.map((sample) => sample.src.valueLiteral);
const paramsCorrectlyOrdered = paramNames
.map((paramName, index) => {
return `if (${paramName} !== ${paramLiterals[index]}) throw new Error('${paramName} is incorrectly ordered')`;
})
.join('\n');
const paramsCorrectlyOrdered = areParamsCorrectlyOrdered(
paramNames,
paramNat16s
);

return `
${paramsAreNumbers}
Expand Down
Loading

0 comments on commit 64fd977

Please sign in to comment.