diff --git a/main.js b/main.js index f42aecc..0812912 100644 --- a/main.js +++ b/main.js @@ -20,6 +20,71 @@ Diceware.i_can_has_good_crypto = function() { } // End of i_can_has_good_crypto() +/** +* Return a random integer between 1 max. +*/ +Diceware.getRandomValue = function(max) { + + if (max <= 0){ + return(NaN); + } + + var a = new Uint32Array(1); + window.crypto.getRandomValues(a); + retval = (a[0] % max); + + return(retval); + +} // End of getRandomValue() + + +/** +* Convert a number from base 10 into base 6. +* +* @param integer roll The random value. +* @param integer num_dice The number of dice we're returning. +* +* @return array An array of the base 6 numbers. +*/ +Diceware.getBase6 = function(roll, num_dice) { + + var retval = []; + + // + // Sanity check + // + var max_dice_roll = Math.pow(6, num_dice) - 1; + if (roll > max_dice_roll) { + throw("Value too large!"); + } + + if (roll < 0) { + throw("Value cannot be negative!"); + } + + // + // Go through each die, starting with the most significant one, and + // get its value. + // + var num_dice_left = num_dice - 1; + var dice_value_left = roll; + + for (i = num_dice_left; i >= 0; i--) { + + var die_value = Math.pow(6, i); + var value = Math.floor( dice_value_left / die_value); + var left = dice_value_left % die_value; + + retval.push(value); + dice_value_left = dice_value_left - (die_value * value); + + } + + return(retval); + +} // End of getBase6() + + /** * Roll a die. * diff --git a/tests/index.html b/tests/index.html new file mode 100644 index 0000000..3d3752e --- /dev/null +++ b/tests/index.html @@ -0,0 +1,16 @@ + + + + + + QUnit Example + + + +
+
+ + + + + diff --git a/tests/tests.js b/tests/tests.js new file mode 100644 index 0000000..63b2e4b --- /dev/null +++ b/tests/tests.js @@ -0,0 +1,42 @@ + +/** +* 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.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"); + +}); + + +