mirror of
https://github.com/dmuth/diceware.git
synced 2024-12-01 00:10:19 -05:00
111 lines
5 KiB
JavaScript
111 lines
5 KiB
JavaScript
|
|
/**
|
|
* Our unit tests.
|
|
* Documentation on assert() can be found at https://api.qunitjs.com/category/assert/
|
|
*/
|
|
|
|
QUnit.module( "Diceware" );
|
|
|
|
QUnit.test("Test random numbers", function(assert) {
|
|
|
|
assert.ok(isNaN(Diceware.getRandomValue(-1)), "Negative numbers should be NaN");
|
|
assert.ok(isNaN(Diceware.getRandomValue(0)), "0 should be NaN");
|
|
assert.equal(Diceware.getRandomValue(1), 0, "1 should be NaN");
|
|
assert.ok(Diceware.getRandomValue(2) < 2, "getRandomValue(2) < 2");
|
|
|
|
});
|
|
|
|
|
|
QUnit.test("Base 6 conversion", function(assert) {
|
|
|
|
assert.deepEqual(Diceware.getBase6(0, 1), [0]);
|
|
assert.deepEqual(Diceware.getBase6(1, 1), [1]);
|
|
assert.deepEqual(Diceware.getBase6(5, 1), [5]);
|
|
assert.deepEqual(Diceware.getBase6(6, 2), [1, 0]);
|
|
assert.deepEqual(Diceware.getBase6(12, 2), [2, 0]);
|
|
assert.deepEqual(Diceware.getBase6(35, 2), [5, 5]);
|
|
assert.deepEqual(Diceware.getBase6(36, 3), [1, 0, 0]);
|
|
assert.deepEqual(Diceware.getBase6(180, 3), [5, 0, 0]);
|
|
assert.deepEqual(Diceware.getBase6(215, 3), [5, 5, 5]);
|
|
assert.deepEqual(Diceware.getBase6(216, 4), [1, 0, 0, 0]);
|
|
assert.deepEqual(Diceware.getBase6(1080, 4), [5, 0, 0, 0]);
|
|
assert.deepEqual(Diceware.getBase6(1295, 4), [5, 5, 5, 5]);
|
|
assert.deepEqual(Diceware.getBase6(7775, 5), [5, 5, 5, 5, 5]);
|
|
assert.deepEqual(Diceware.getBase6(46655, 6), [5, 5, 5, 5, 5, 5]);
|
|
assert.deepEqual(Diceware.getBase6(279935, 7), [5, 5, 5, 5, 5, 5, 5]);
|
|
assert.deepEqual(Diceware.getBase6(1679615, 8), [5, 5, 5, 5, 5, 5, 5, 5]);
|
|
|
|
assert.throws(function() {Diceware.getBase6(6, 1); }, /too large/, "Value too large");
|
|
assert.throws(function() {Diceware.getBase6(36, 2); }, /too large/, "Value too large");
|
|
assert.throws(function() {Diceware.getBase6(216, 3); }, /too large/, "Value too large");
|
|
assert.throws(function() {Diceware.getBase6(-1, 1); }, /negative/, "Negative value");
|
|
|
|
});
|
|
|
|
|
|
QUnit.test("Convert Base 6 to Dice Roll", function(assert) {
|
|
|
|
assert.deepEqual(Diceware.convertBase6ToDice([0], 1), [1]);
|
|
assert.deepEqual(Diceware.convertBase6ToDice([5], 1), [6]);
|
|
assert.deepEqual(Diceware.convertBase6ToDice([1, 0], 2), [2, 1]);
|
|
assert.deepEqual(Diceware.convertBase6ToDice([2, 0], 2), [3, 1]);
|
|
assert.deepEqual(Diceware.convertBase6ToDice([5, 5], 2), [6, 6]);
|
|
assert.deepEqual(Diceware.convertBase6ToDice([1, 0, 0], 3), [2, 1, 1]);
|
|
assert.deepEqual(Diceware.convertBase6ToDice([5, 0, 0], 3), [6, 1, 1]);
|
|
assert.deepEqual(Diceware.convertBase6ToDice([5, 5, 5], 3), [6, 6, 6]);
|
|
assert.deepEqual(Diceware.convertBase6ToDice([1, 0, 0, 0], 4), [2, 1, 1, 1]);
|
|
assert.deepEqual(Diceware.convertBase6ToDice([5, 0, 0, 0], 4), [6, 1, 1, 1]);
|
|
assert.deepEqual(Diceware.convertBase6ToDice([5, 5, 5, 5], 4), [6, 6, 6, 6]);
|
|
assert.deepEqual(Diceware.convertBase6ToDice([5, 5, 5, 5, 5], 5), [6, 6, 6, 6, 6]);
|
|
assert.deepEqual(Diceware.convertBase6ToDice([5, 5, 5, 5, 5, 5], 6), [6, 6, 6, 6, 6, 6]);
|
|
assert.deepEqual(Diceware.convertBase6ToDice([5, 5, 5, 5, 5, 5, 5], 7), [6, 6, 6, 6, 6, 6, 6]);
|
|
assert.deepEqual(Diceware.convertBase6ToDice([5, 5, 5, 5, 5, 5, 5, 5], 8), [6, 6, 6, 6, 6, 6, 6, 6]);
|
|
|
|
assert.throws(function() {Diceware.convertBase6ToDice([-1], 1); }, /negative/, "Negative value");
|
|
assert.throws(function() {Diceware.convertBase6ToDice([0, -1], 2); }, /negative/, "Negative value");
|
|
assert.throws(function() {Diceware.convertBase6ToDice([-1, 0], 2); }, /negative/, "Negative value");
|
|
assert.throws(function() {Diceware.convertBase6ToDice([6], 1); }, /too large/, "too large");
|
|
assert.throws(function() {Diceware.convertBase6ToDice([6, 0], 2); }, /too large/, "too large");
|
|
assert.throws(function() {Diceware.convertBase6ToDice([0, 6], 2); }, /too large/, "too large");
|
|
|
|
assert.throws(function() {Diceware.convertBase6ToDice([0], 2); }, /mismatch/i, "Mismatch");
|
|
assert.throws(function() {Diceware.convertBase6ToDice([0, 0], 1); }, /mismatch/i, "Mismatch");
|
|
|
|
});
|
|
|
|
|
|
QUnit.test("Roll some dice!", function(assert) {
|
|
|
|
//
|
|
// Test out our helper function first
|
|
//
|
|
assert.equal(Diceware.getNumValuesFromNumDice(1), 6);
|
|
assert.equal(Diceware.getNumValuesFromNumDice(2), 36);
|
|
assert.equal(Diceware.getNumValuesFromNumDice(3), 216);
|
|
assert.equal(Diceware.getNumValuesFromNumDice(4), 1296);
|
|
assert.equal(Diceware.getNumValuesFromNumDice(5), 7776);
|
|
assert.equal(Diceware.getNumValuesFromNumDice(6), 46656);
|
|
assert.equal(Diceware.getNumValuesFromNumDice(7), 279936);
|
|
assert.equal(Diceware.getNumValuesFromNumDice(8), 1679616);
|
|
|
|
assert.throws(function() {Diceware.getNumValuesFromNumDice(0); }, /zero/, "Zero");
|
|
assert.throws(function() {Diceware.getNumValuesFromNumDice(-1); }, /negative/, "Negative value");
|
|
|
|
assert.equal(Diceware.rollDice(1).roll.length, 1);
|
|
assert.equal(Diceware.rollDice(3).roll.length, 3);
|
|
assert.equal(Diceware.rollDice(8).roll.length, 8);
|
|
|
|
//
|
|
// These may fail infrequently if the random number is zero.
|
|
//
|
|
assert.ok(parseInt(Diceware.rollDice(3).value));
|
|
assert.ok(parseInt(Diceware.rollDice(8).value));
|
|
|
|
assert.throws(function() {Diceware.rollDice(0); }, /zero/, "Zero");
|
|
assert.throws(function() {Diceware.rollDice(-1); }, /negative/, "Negative value");
|
|
|
|
|
|
});
|
|
|
|
|
|
|