Number Generators
These generators create numbers
Most number generators have a large variant which will generate larger numbers.
qc.ureal and qc.ureal.large
Generates a random positive number.
qc.real and qc.real.large
Generates a random number.
qc.uint and qc.uint.large
Generates a random positive integer.
qc.int and qc.int.large
Generates a random integer.
qc.int.between(min, max)
Generates a random integer falling between min and max.
qc.natural and qc.natural.large
Generates a random natural number (i.e. postive integer greater than zero).
qc.random
Generates a random number between 0 and 1.
qc.range([gen])
Generates an array of two numbers where the second is guaranteed to be greater than the first. Be default the numbers are supplied by qc.real, but you can customize the generator.
So for example qc.range(qc.natural) will generate a range of natural numbers.
qc.range.inclusive([gen])
Generates an array of two numbers where the second is guaranteed to be greater or equal than the first. Be default the numbers are supplied by qc.real, but you can customize the generator.
So for example qc.range.inclusive(qc.natural) will generate a range of natural numbers.
expect(function(arg) {
return arg[0] < arg[1];
}).forAll(qc.range());
expect(function(arg) {
return arg[0] <= arg[1];
}).forAll(qc.range.inclusive(qc.real));
expect(function(arg) {
return (0 <= arg[0] && arg[0] < arg[1]);
}).forAll(qc.range(qc.ureal));
expect(function(arg) {
return (0 < arg[0] && arg[0] < arg[1]);
}).forAll(qc.range(qc.natural));expect(([min, max]) -> min < max).forAll(qc.range())
expect(([min, max]) -> min <= max).forAll(qc.range.inclusive(qc.real))
expect(([min, max]) -> 0 <= min < max).forAll(qc.range(qc.ureal))
expect(([min, max]) -> 0 < min < max).forAll(qc.range(qc.natural))expect(([min, max]) => min < max).forAll(qc.range());
expect(([min, max]) => min <= max).forAll(qc.range.inclusive(qc.real));
expect(([min, max]) => 0 <= min && min < max).forAll(qc.range(qc.ureal));
expect(([min, max]) => 0 < min && min < max).forAll(qc.range(qc.natural));qc.dice(diceDSL)
This generator is meant for generating integers, but particularly if the tests should use integers from a particular distribution.
It uses a D&D inspired language for generating distributions, which is quite intuitive, as it models dice. So d6 means roll a six sided die, 2d4 means roll two four sided dies and add up their results.
Here is an overview of the language:
Token | Explanation | Example |
|---|---|---|
xdy | Roll |
|
| Flip |
|
a ? b : c | If | Useful especially with coin flips: |
+, -, * | Standard arithmetical operators |
|
(, ) | Brackets can be used to group operators. Bracketing operands to |
|
qc.dice('d3') === function() {
return Math.ceil(qc.random() * 3);
};
qc.dice('d2 + d4 + 3') === function() {
return Math.ceil(qc.random() * 2) + Math.ceil(qc.random() * 4) + 3;
};
qc.dice('2d6') === function() {
return Math.ceil(qc.random() * 6) + Math.ceil(qc.random() * 6);
};qc.dice('d3') == -> Math.ceil(qc.random() * 3)
qc.dice('d2 + d4 + 3') == ->
Math.ceil(qc.random() * 2) + Math.ceil(qc.random() * 4) + 3
qc.dice('2d6') == ->
Math.ceil(qc.random() * 6) + Math.ceil(qc.random() * 6)qc.dice('d3') === ((size) => Math.ceil(qc.random() * 3))
qc.dice('d2 + d4 + 3') === ((size) => Math.ceil(qc.random() * 2) + Math.ceil(qc.random() * 4) + 3)
qc.dice('2d6') === ((size) => Math.ceil(qc.random() * 6) + Math.ceil(qc.random() * 6))Updated 9 months ago
