Skip to content

Commit

Permalink
Add some more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
quantum5 committed Dec 5, 2018
1 parent 110efa4 commit 0988848
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/generator.js
Expand Up @@ -23,10 +23,15 @@ function pickWords (list, number) {
return getWords(list, array)
}

function pickChar (options) {
function getChar (choices, index) {
return choices[index % choices.length]
}

function pickChar (choices) {
const array = new Uint32Array(1)
window.crypto.getRandomValues(array)
return options[array[0] % options.length]
const index = array[0]
return getChar(choices, index)
}

function generate (options) {
Expand Down Expand Up @@ -61,5 +66,5 @@ function computeBits (options) {
}

module.exports = {
getWordList, getWords, capitalize, generate, lengthBits, computeBits
getWordList, getWords, capitalize, generate, getChar, lengthBits, computeBits
}
59 changes: 59 additions & 0 deletions tests/generator.js
@@ -0,0 +1,59 @@
const { getWordList, getWords, getChar, lengthBits } = require('../src/generator')
const words = require('../src/words')
const assert = require('assert').strict
const lists = ['small', 'medium', 'large']

describe('getWordList', () => {
lists.forEach(name => {
it(name, () => {
assert.equal(getWordList(name), words[name])
})
})
})

describe('getWords', () => {
lists.forEach(name => {
it(`sanity: ${name}`, () => {
assert.deepEqual(
getWords(words[name], [0, 256, 1337, 2018]),
[words[name][0], words[name][256], words[name][1337], words[name][2018]]
)
})
})

lists.forEach(name => {
it(`all offsets: ${name}`, () => {
const len = words[name].length
;[...Array(65536 / len)].forEach((_, i) => {
assert.deepEqual(
getWords(words[name], [i * len, i * len + 256, i * len + 1337, i * len + 2018]),
[words[name][0], words[name][256], words[name][1337], words[name][2018]]
)
})
})
})
})

describe('getChar', () => {
it('simple', () => {
const choices = ['0', '1', '2']
assert.equal(getChar(choices, 0), '0')
assert.equal(getChar(choices, 1), '1')
assert.equal(getChar(choices, 2), '2')
assert.equal(getChar(choices, 3), '0')
assert.equal(getChar(choices, 4), '1')
assert.equal(getChar(choices, 5), '2')
})
})

describe('lengthBits', () => {
it('simple', () => {
assert.equal(lengthBits(Array(1)), 0)
assert.equal(lengthBits(Array(2)), 1)
assert.equal(lengthBits(Array(4)), 2)
assert.equal(lengthBits(Array(1024)), 10)
assert.equal(lengthBits(Array(2048)), 11)
assert.equal(lengthBits(Array(4096)), 12)
assert.equal(lengthBits(Array(8192)), 13)
})
})

0 comments on commit 0988848

Please sign in to comment.