Skip to content

Commit

Permalink
pr fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Jan 16, 2024
1 parent ced0f24 commit b978c97
Show file tree
Hide file tree
Showing 25 changed files with 100 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ export function complexCandidDefinitionMemo(
): CandidDefinitionMemo {
const weights = constraints.weights ?? {};
const newConstraints: DefinitionConstraints = {
n: constraints.n,
depthLevel: constraints.depthLevel,
weights:
constraints.recursive_weights ?? false
constraints.recursiveWeights ?? false
? constraints.weights ?? {}
: {},
recursive_weights: constraints.recursive_weights
recursiveWeights: constraints.recursiveWeights
};
return fc.memo((n) => {
return fc.memo((depthLevel) => {
return fc.oneof(
{
arbitrary: BlobDefinitionArb(),
weight: weights.blob ?? 1
},
{
arbitrary: FuncDefinitionArb(
candidDefinitionMemo([], newConstraints)(n)
candidDefinitionMemo([], newConstraints)(depthLevel)
),
weight: weights.func ?? 1
},
Expand All @@ -63,13 +63,13 @@ export function complexCandidDefinitionMemo(
},
{
arbitrary: RecordDefinitionArb(
candidDefinitionMemo([], newConstraints)(n)
candidDefinitionMemo([], newConstraints)(depthLevel)
),
weight: weights.record ?? 1
},
{
arbitrary: TupleDefinitionArb(
candidDefinitionMemo([], newConstraints)(n)
candidDefinitionMemo([], newConstraints)(depthLevel)
),
weight: weights.tuple ?? 1
},
Expand All @@ -91,7 +91,7 @@ export function complexCandidDefinitionMemo(
},
{
arbitrary: ServiceDefinitionArb(
candidDefinitionMemo([], newConstraints)(n)
candidDefinitionMemo([], newConstraints)(depthLevel)
),
weight: weights.service ?? 0
// TODO Service is disabled until it is more refined. Maybe the
Expand Down
14 changes: 8 additions & 6 deletions property_tests/arbitraries/candid/candid_definition_arb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import {
REC_ARB_COUNT,
recursiveCandidDefinitionMemo
} from './recursive_candid_definition_memo';
import { DEFAULT_DEF_MAX_DEPTH } from '../../config';
import { DEFAULT_DEFINITION_MAX_DEPTH } from '../../config';

export function candidDefinitionArb(
maxDepth: number = DEFAULT_DEF_MAX_DEPTH,
maxDepth: number = DEFAULT_DEFINITION_MAX_DEPTH,
parents: RecursiveCandidName[] = [],
constraints: DefinitionConstraints = {}
): CandidDefinitionArb {
Expand All @@ -31,8 +31,8 @@ export function candidDefinitionMemo(
parents: RecursiveCandidName[],
constraints: DefinitionConstraints = {}
): CandidDefinitionMemo {
return fc.memo((n) => {
if (n <= 1) {
return fc.memo((depthLevel) => {
if (depthLevel <= 1) {
return primitiveCandidDefinitionArb();
}
return fc.oneof(
Expand All @@ -44,11 +44,13 @@ export function candidDefinitionMemo(
arbitrary: complexCandidDefinitionMemo(
parents,
constraints
)(n - 1),
)(depthLevel - 1),
weight: COMPLEX_ARB_COUNT
},
{
arbitrary: recursiveCandidDefinitionMemo(parents)(n - 1),
arbitrary: recursiveCandidDefinitionMemo(parents)(
depthLevel - 1
),
weight: REC_ARB_COUNT
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export const REC_ARB_COUNT = 0;
export function recursiveCandidDefinitionMemo(
parents: RecursiveCandidName[]
): CandidDefinitionMemo {
return fc.memo((n) =>
RecursiveDefinitionArb(complexCandidDefinitionMemo, parents, { n })
return fc.memo((depthLevel) =>
RecursiveDefinitionArb(complexCandidDefinitionMemo, parents, {
depthLevel: depthLevel
})
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function primitiveCandidDefinitionArb(
{ arbitrary: Nat16DefinitionArb(), weight: constraints.nat16 ?? 1 },
{ arbitrary: Nat32DefinitionArb(), weight: constraints.nat32 ?? 1 },
{ arbitrary: Nat64DefinitionArb(), weight: constraints.nat64 ?? 1 },
{ arbitrary: NullDefinitionArb(), weight: constraints.null ?? 0 }, // Null must be excluded until https://github.com/demergent-labs/azle/issues/1453 gets resolved
{ arbitrary: NullDefinitionArb(), weight: constraints.null ?? 0 }, // TODO Null must be excluded until https://github.com/demergent-labs/azle/issues/1453 gets resolved
{ arbitrary: TextDefinitionArb(), weight: constraints.text ?? 1 },
{
arbitrary: PrincipalDefinitionArb(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { ServiceMethodDefinition } from '../reference/service_arb/service_method
import { PrimitiveDefinitionWeights } from './simple_candid_definition_arb';
import { ComplexDefinitionWeights } from './complex_candid_definition_memo';

export type CandidDefinitionMemo = (n: number) => CandidDefinitionArb;
export type CandidDefinitionMemo = (depthLevel: number) => CandidDefinitionArb;
export type RecursiveCandidDefinitionMemo = (
parents: RecursiveCandidName[],
constraints?: DefinitionConstraints
) => CandidDefinitionMemo;

export type DefinitionConstraints = Partial<{
n: number;
recursive_weights: boolean;
depthLevel: number;
recursiveWeights: boolean;
weights: CandidDefinitionWeights;
}>;

Expand Down
27 changes: 18 additions & 9 deletions property_tests/arbitraries/candid/candid_values_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
} from './candid_definition_arb/types';
import { BlobValuesArb } from './constructed/blob_arb/values_arb';
import { CorrespondingJSType } from './corresponding_js_type';
import { RecursivePlaceHolderValuesArb } from './recursive/values_arb';
import { RecursiveNameValuesArb } from './recursive/values_arb';

export type CandidValues<T extends CorrespondingJSType, E = T> = {
agentArgumentValue: T;
Expand All @@ -46,26 +46,35 @@ export type CandidValues<T extends CorrespondingJSType, E = T> = {

export function CandidValueArb(
candidTypeMeta: CandidDefinition,
n: number
depthLevel: number
): fc.Arbitrary<CandidValues<CorrespondingJSType>> {
const candidType = candidTypeMeta.candidMeta.candidType;
if (candidType === 'blob') {
return BlobValuesArb();
}
if (candidType === 'Opt') {
return OptValuesArb(candidTypeMeta as OptCandidDefinition, n);
return OptValuesArb(candidTypeMeta as OptCandidDefinition, depthLevel);
}
if (candidType === 'Record') {
return RecordValuesArb(candidTypeMeta as RecordCandidDefinition, n);
return RecordValuesArb(
candidTypeMeta as RecordCandidDefinition,
depthLevel
);
}
if (candidType === 'Tuple') {
return TupleValuesArb(candidTypeMeta as TupleCandidDefinition, n);
return TupleValuesArb(
candidTypeMeta as TupleCandidDefinition,
depthLevel
);
}
if (candidType === 'Variant') {
return VariantValuesArb(candidTypeMeta as VariantCandidDefinition, n);
return VariantValuesArb(
candidTypeMeta as VariantCandidDefinition,
depthLevel
);
}
if (candidType === 'Vec') {
return VecValuesArb(candidTypeMeta as VecCandidDefinition, n);
return VecValuesArb(candidTypeMeta as VecCandidDefinition, depthLevel);
}
if (candidType === 'bool') {
return BoolValueArb();
Expand Down Expand Up @@ -125,9 +134,9 @@ export function CandidValueArb(
return ServiceValueArb(candidTypeMeta as ServiceCandidDefinition);
}
if (candidType === 'Recursive') {
return RecursivePlaceHolderValuesArb(
return RecursiveNameValuesArb(
candidTypeMeta as RecursiveCandidName | RecursiveCandidDefinition,
n
depthLevel
);
}
throw new Error('Unreachable');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,16 @@ function possiblyRecursiveArb(
parents: RecursiveCandidName[],
constraints: DefinitionConstraints
): fc.Arbitrary<CandidDefinition> {
const n = constraints.n ?? 0;
const depthLevel = constraints.depthLevel ?? 0;
return fc.nat(Math.max(parents.length - 1, 0)).chain((randomIndex) => {
if (parents.length === 0 || n < 1) {
// If there are no recursive parents or this is the first variant field just do a regular arb field
return candidArb(parents)(n);
if (parents.length === 0 || depthLevel < 1) {
// If there are no recursive parents or we have reached a depth
// level of 0 just do a regular arb inner type
return candidArb(parents)(depthLevel);
}
return fc.oneof(
{ arbitrary: fc.constant(parents[randomIndex]), weight: 1 },
{ arbitrary: candidArb(parents)(n), weight: 1 }
{ arbitrary: candidArb(parents)(depthLevel), weight: 1 }
);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { OptValuesArb } from './values_arb';
import { CandidValueAndMetaArbGenerator } from '../../candid_value_and_meta_arb_generator';
import { candidDefinitionMemo } from '../../candid_definition_arb';
import { CorrespondingJSType } from '../../corresponding_js_type';
import { DEFAULT_DEF_MAX_DEPTH } from '../../../config';
import { DEFAULT_DEFINITION_MAX_DEPTH } from '../../../config';

export type Opt = [CorrespondingJSType] | never[];

export function OptArb(): fc.Arbitrary<CandidValueAndMeta<Opt>> {
return CandidValueAndMetaArbGenerator(
OptDefinitionArb(candidDefinitionMemo, [], {
n: DEFAULT_DEF_MAX_DEPTH
depthLevel: DEFAULT_DEFINITION_MAX_DEPTH
}),
OptValuesArb
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ type SomeOrNone = 'Some' | 'None';

export function OptValuesArb(
optDefinition: OptCandidDefinition,
n: number
depthLevel: number
): fc.Arbitrary<CandidValues<Opt>> {
if (n < 1) {
if (depthLevel < 1) {
return fc.constant(generateNoneValue());
}
const innerValue = fc.tuple(
fc.constantFrom('Some', 'None') as fc.Arbitrary<SomeOrNone>,
CandidValueArb(optDefinition.innerType, n - 1)
CandidValueArb(optDefinition.innerType, depthLevel - 1)
);

return innerValue.map(([someOrNone, innerType]) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ type ArbField = [string, fc.Arbitrary<CandidValues<CorrespondingJSType>>];

export function RecordValuesArb(
recordDefinition: RecordCandidDefinition,
n: number
depthLevel: number
): fc.Arbitrary<CandidValues<Record>> {
const fieldValues = recordDefinition.innerTypes.map(([name, innerType]) => {
const result: ArbField = [name, CandidValueArb(innerType, n - 1)];
const result: ArbField = [
name,
CandidValueArb(innerType, depthLevel - 1)
];
return result;
});
const arbitraryFieldValues = fieldValues.map(([key, arbValue]) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { CandidValues, CandidValueArb } from '../../candid_values_arb';

export function TupleValuesArb(
tupleDefinition: TupleCandidDefinition,
n: number
depthLevel: number
): fc.Arbitrary<CandidValues<Tuple, ReturnTuple>> {
const fieldValues = tupleDefinition.innerTypes.map((innerType) => {
const result: fc.Arbitrary<CandidValues<CorrespondingJSType>> =
CandidValueArb(innerType, n - 1);
CandidValueArb(innerType, depthLevel - 1);
return result;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,21 @@ function VariantFieldsArb(
// will not allow a recursive option to be picked for that field. A more
// complicated approach would involve the guaranteed base case being in any one
// of the fields, instead of always the first one.
// TODO rename and move to the bottom.
function possiblyRecursiveArb(
candidArb: RecursiveCandidDefinitionMemo,
index: number,
parents: RecursiveCandidName[],
constraints: DefinitionConstraints
): fc.Arbitrary<CandidDefinition> {
const n = constraints.n ?? 0;
const depthLevel = constraints.depthLevel ?? 0;
return fc.nat(Math.max(parents.length - 1, 0)).chain((randomIndex) => {
if (parents.length === 0 || index < 1) {
// If there are no recursive parents or this is the first variant field just do a regular arb field
return candidArb(parents)(n);
return candidArb(parents)(depthLevel);
}
return fc.oneof(
{ arbitrary: fc.constant(parents[randomIndex]), weight: 1 },
{ arbitrary: candidArb(parents)(n), weight: 1 }
{ arbitrary: candidArb(parents)(depthLevel), weight: 1 }
);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CandidValueAndMeta } from '../../candid_value_and_meta_arb';
import { VariantDefinitionArb } from './definition_arbs';
import { VariantValuesArb } from './values_arb';
import { CandidValueAndMetaArbGenerator } from '../../candid_value_and_meta_arb_generator';
import { DEFAULT_DEF_MAX_DEPTH } from '../../../config';
import { DEFAULT_DEFINITION_MAX_DEPTH } from '../../../config';

export type Variant = {
[x: string]: CorrespondingJSType;
Expand All @@ -15,7 +15,7 @@ export type Variant = {
export function VariantArb(): fc.Arbitrary<CandidValueAndMeta<Variant>> {
return CandidValueAndMetaArbGenerator(
VariantDefinitionArb(candidDefinitionMemo, [], {
n: DEFAULT_DEF_MAX_DEPTH - 1
depthLevel: DEFAULT_DEFINITION_MAX_DEPTH - 1
}),
VariantValuesArb
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ type Field = [string, CandidValues<CorrespondingJSType>];

export function VariantValuesArb(
variantDefinition: VariantCandidDefinition,
n: number
depthLevel: number
): fc.Arbitrary<CandidValues<Variant>> {
return fc
.nat(n < 1 ? 0 : variantDefinition.innerTypes.length - 1)
.nat(depthLevel < 1 ? 0 : variantDefinition.innerTypes.length - 1)
.chain((randomIndex) => {
if (variantDefinition.innerTypes.length === 0) {
return fc.constant({
Expand All @@ -23,7 +23,7 @@ export function VariantValuesArb(

const [name, innerType] = variantDefinition.innerTypes[randomIndex];

const fieldValues = CandidValueArb(innerType, n - 1).map(
const fieldValues = CandidValueArb(innerType, depthLevel - 1).map(
(values): Field => {
return [name, values];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ function possiblyRecursiveArb(
parents: RecursiveCandidName[],
constraints: DefinitionConstraints
): fc.Arbitrary<CandidDefinition> {
const n = constraints.n ?? 0;
const depthLevel = constraints.depthLevel ?? 0;
return fc.nat(Math.max(parents.length - 1, 0)).chain((randomIndex) => {
if (parents.length === 0) {
// If there are no recursive parents or this is the first variant field just do a regular arb field
return candidArb(parents)(n);
return candidArb(parents)(depthLevel);
}
return fc.oneof(
{ arbitrary: fc.constant(parents[randomIndex]), weight: 1 },
{ arbitrary: candidArb(parents)(n), weight: 1 }
{ arbitrary: candidArb(parents)(depthLevel), weight: 1 }
);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CandidValueAndMeta } from '../../candid_value_and_meta_arb';
import { VecDefinitionArb } from './definition_arb';
import { VecValuesArb } from './values_arb';
import { CandidValueAndMetaArbGenerator } from '../../candid_value_and_meta_arb_generator';
import { DEFAULT_DEF_MAX_DEPTH } from '../../../config';
import { DEFAULT_DEFINITION_MAX_DEPTH } from '../../../config';

export type Vec =
| CorrespondingJSType[]
Expand All @@ -22,7 +22,7 @@ export type Vec =
export function VecArb(): fc.Arbitrary<CandidValueAndMeta<Vec>> {
return CandidValueAndMetaArbGenerator(
VecDefinitionArb(candidDefinitionMemo, [], {
n: DEFAULT_DEF_MAX_DEPTH
depthLevel: DEFAULT_DEFINITION_MAX_DEPTH
}),
VecValuesArb
);
Expand Down
Loading

0 comments on commit b978c97

Please sign in to comment.