This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Generator<T> = { | |
generate(mrng: Random): T; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Random { | |
constructor(rng) { | |
this.rng = rng; | |
} | |
next(min, max) { | |
const g = prand.uniformIntDistribution(min, max, this.rng); | |
this.rng = g[1]; | |
return g[0]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// const miniFc = {} | |
miniFc.integer = (min, max) => { | |
return { | |
generate(mrng) { | |
return mrng.next(min, max); | |
} | |
}; | |
} | |
// It can be used as follow: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare function map<T, U>(g: Generator<T>, mapper: (v: T) => U): Generator<U>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function map(g, mapper) { | |
return { | |
generate(mrng) { | |
const value = g.generate(mrng); | |
return mapper(value); | |
} | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
miniFc.boolean = () => map( | |
miniFc.integer(0, 1), | |
Boolean | |
) | |
miniFc.character = () => map( | |
miniFc.integer(0, 25), | |
n => String.fromCharCode(97 + n) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
miniFc.tuple = (...itemGenerators) => { | |
return { | |
generate(mrng) { | |
return itemGenerators.map(g => g.generate(mrng)); | |
} | |
}; | |
} | |
// It can be used as follow: | |
// > miniFc.tuple(miniFc.integer(0, 50), miniFc.boolean()).generate(mrng) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
miniFc.string = () => map( | |
miniFc.array(miniFc.character()), | |
characters => characters.join('') | |
) | |
miniFc.dictionary = (valueGenerator) => map( | |
miniFc.array( | |
miniFc.tuple( | |
miniFc.string(), | |
valueGenerator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Property<T> = { | |
generate(mrng: Random): T; | |
run(valueUnderTest: T): boolean; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
miniFc.property = (generator, predicate) => { | |
return { | |
generate(mrng) { | |
return generator.generate(mrng); | |
}, | |
run(valueUnderTest) { | |
return predicate(valueUnderTest); | |
} | |
} | |
} |