Added getRandomValue(), getBase6(), and unit tests for both with Qunit.

This commit is contained in:
Douglas Muth 2017-01-31 22:33:37 -05:00
parent f2682d341f
commit 43b4f4c7b1
3 changed files with 123 additions and 0 deletions

65
main.js
View file

@ -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.
*

16
tests/index.html Normal file
View file

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>QUnit Example</title>
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.1.1.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="https://code.jquery.com/qunit/qunit-2.1.1.js"></script>
<script src="../main.js"></script>
<script src="tests.js"></script>
</body>
</html>

42
tests/tests.js Normal file
View file

@ -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");
});