Skip to content

Commit

Permalink
finish constraint work
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Dec 22, 2023
1 parent 648d81d commit 37706d2
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ export type ComplexDefinitionWeights = Partial<{
service: number;
}>;

export type ComplexDefinitionConstraints = Partial<{
n: number;
recursive_weights: boolean;
weights: ComplexDefinitionWeights;
}>;

// The number of options below (blob, func, opt, etc)
export const COMPLEX_ARB_COUNT = 9; // TODO change this to 8 after we debug the current issue

Expand All @@ -54,7 +48,9 @@ export function complexCandidDefinitionMemo(
weight: weights.blob ?? 1
},
{
arbitrary: FuncDefinitionArb(candidDefinitionMemo([])(n)),
arbitrary: FuncDefinitionArb(
candidDefinitionMemo([], newConstraints)(n)
),
weight: weights.func ?? 1
},
{
Expand All @@ -66,27 +62,37 @@ export function complexCandidDefinitionMemo(
weight: weights.opt ?? 1
},
{
arbitrary: RecordDefinitionArb(candidDefinitionMemo([])(n)),
arbitrary: RecordDefinitionArb(
candidDefinitionMemo([], newConstraints)(n)
),
weight: weights.record ?? 1
},
{
arbitrary: TupleDefinitionArb(candidDefinitionMemo([])(n)),
arbitrary: TupleDefinitionArb(
candidDefinitionMemo([], newConstraints)(n)
),
weight: weights.tuple ?? 1
},
{
arbitrary: VariantDefinitionArb(
candidDefinitionMemo,
parents,
n
newConstraints
),
weight: weights.variant ?? 1
},
{
arbitrary: VecDefinitionArb(candidDefinitionMemo, parents, n),
arbitrary: VecDefinitionArb(
candidDefinitionMemo,
parents,
constraints
),
weight: weights.vec ?? 1
},
{
arbitrary: ServiceDefinitionArb(candidDefinitionMemo([])(n)),
arbitrary: ServiceDefinitionArb(
candidDefinitionMemo([], newConstraints)(n)
),
weight: weights.service ?? 0
// TODO Service is disabled until it is more refined. Maybe the
// only thing missing is deepEquals
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { complexCandidDefinitionMemo } from './complex_candid_definition_memo';
import { RecursiveCandidDefinition, CandidDefinitionMemo } from './types';

// The number of options below (it's just recursive)
export const REC_ARB_COUNT = 1;
export const REC_ARB_COUNT = 0;
// TODO there are a lot of bugs with recursion so we are disabling the whole thing until these issues are resolved
// https://github.com/demergent-labs/azle/issues/1518
// https://github.com/demergent-labs/azle/issues/1513
// https://github.com/demergent-labs/azle/issues/1525

export function recursiveCandidDefinitionMemo(
parents: RecursiveCandidDefinition[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fc from 'fast-check';
import { UniqueIdentifierArb } from '../../../unique_identifier_arb';
import {
CandidDefinition,
DefinitionConstraints,
RecCandidDefMemo,
RecursiveCandidDefinition,
VariantCandidDefinition
Expand All @@ -17,8 +18,9 @@ function possiblyRecursiveArb(
candidArb: RecCandidDefMemo,
index: number,
parents: RecursiveCandidDefinition[],
n: number
constraints: DefinitionConstraints
): fc.Arbitrary<CandidDefinition> {
const n = constraints.n ?? 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
Expand All @@ -34,12 +36,12 @@ function possiblyRecursiveArb(
export function VariantDefinitionArb(
candidTypeArbForFields: RecCandidDefMemo,
parents: RecursiveCandidDefinition[],
n: number
constraints: DefinitionConstraints
): fc.Arbitrary<VariantCandidDefinition> {
return fc
.tuple(
UniqueIdentifierArb('typeDeclaration'),
VariantFieldsArb(candidTypeArbForFields, parents, n),
VariantFieldsArb(candidTypeArbForFields, parents, constraints),
fc.boolean()
)
.map(([name, fields, useTypeDeclaration]): VariantCandidDefinition => {
Expand Down Expand Up @@ -79,7 +81,7 @@ export function VariantDefinitionArb(
function VariantFieldsArb(
candidTypeArb: RecCandidDefMemo,
parents: RecursiveCandidDefinition[],
n: number
constraints: DefinitionConstraints
): fc.Arbitrary<Field[]> {
// Although no minLength is technically required (according to the
// spec), the DFX CLI itself currently errors out trying to pass
Expand All @@ -94,7 +96,12 @@ function VariantFieldsArb(
...fieldsNames.map((name, index) =>
fc.tuple(
fc.constant(name),
possiblyRecursiveArb(candidTypeArb, index, parents, n)
possiblyRecursiveArb(
candidTypeArb,
index,
parents,
constraints
)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export type Variant = {

export function VariantArb(): fc.Arbitrary<CandidValueAndMeta<Variant>> {
return CandidValueAndMetaArbGenerator(
VariantDefinitionArb(candidDefinitionMemo, [], DEFAULT_DEF_MAX_DEPTH),
VariantDefinitionArb(candidDefinitionMemo, [], {
n: DEFAULT_DEF_MAX_DEPTH
}),
VariantValuesArb
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fc from 'fast-check';
import { UniqueIdentifierArb } from '../../../unique_identifier_arb';
import {
CandidDefinition,
DefinitionConstraints,
RecCandidDefMemo,
RecursiveCandidDefinition,
VecCandidDefinition
Expand All @@ -10,8 +11,9 @@ import {
function possiblyRecursiveArb(
candidArb: RecCandidDefMemo,
parents: RecursiveCandidDefinition[],
n: number
constraints: DefinitionConstraints
): fc.Arbitrary<CandidDefinition> {
const n = constraints.n ?? 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
Expand All @@ -27,12 +29,12 @@ function possiblyRecursiveArb(
export function VecDefinitionArb(
candidTypeArb: RecCandidDefMemo,
parents: RecursiveCandidDefinition[],
n: number
constraints: DefinitionConstraints
): fc.Arbitrary<VecCandidDefinition> {
return fc
.tuple(
UniqueIdentifierArb('typeDeclaration'),
possiblyRecursiveArb(candidTypeArb, parents, n),
possiblyRecursiveArb(candidTypeArb, parents, constraints),
fc.boolean()
)
.map(([name, innerType, useTypeDeclaration]): VecCandidDefinition => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export type Vec =

export function VecArb(): fc.Arbitrary<CandidValueAndMeta<Vec>> {
return CandidValueAndMetaArbGenerator(
VecDefinitionArb(candidDefinitionMemo, [], DEFAULT_DEF_MAX_DEPTH),
VecDefinitionArb(candidDefinitionMemo, [], {
n: DEFAULT_DEF_MAX_DEPTH
}),
VecValuesArb
);
}

0 comments on commit 37706d2

Please sign in to comment.