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 x y -sided dice and add up their results. | 4d6 - roll 4 six-sided dice, will yield an int between 4 and 24. |
Fx | Flip x coins. If they all turn heads, count 1, otherwise 0. | F2 will be 1 with probability of 1/4. |
a ? b : c | If a is greater than zero, evaluate to b , otherwise to c . | Useful especially with coin flips: F5 ? 3d3 : 1 |
+, -, * | Standard arithmetical operators | 3d3 + 4d2 |
(, ) | Brackets can be used to group operators. Bracketing operands to d and F operators isn't supported yet. | F5 ? (3d3 + 2) : 1 |
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 less than a minute ago