Generating data that depends on other data? #5214
Unanswered
bertrand-caron
asked this question in
Q&A
Replies: 1 comment 4 replies
-
Actually your code is chaining arrays not corresponding to the variable import fc from "fast-check"
const arrayArbitrary = fc.array(fc.nat({ max: 100 }), { minLength: 2 })
it("Can generate indices within the array bounds", () => {
fc.assert(
fc.property(
arrayArbitrary.chain((array) => fc.record({
array: fc.constant(array), // edited after comment
i1: fc.integer({ min: 0, max: array.length - 1 }),
i2: fc.integer({ min: 0, max: array.length - 1 }),
})),
({array, i1, i2}) => {
expect(i1).toBeLessThan(array.length)
expect(i2).toBeLessThan(array.length)
},
),
)
}) |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Let's say I am trying to test a method that works on arrays. To do so, I want to generate random arrays, and then generate random valid indices within that array. I have tried the following, which produces indices outside the bounds of the array.
Am I missing something?
I could easily filter the values with
fc.pre(...)
but that seems like a hack.Beta Was this translation helpful? Give feedback.
All reactions