Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 71c0a35

Browse files
committed
Test bytesNeeded
1 parent 1a5945c commit 71c0a35

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

test/charSet.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import test from 'ava'
22

3-
import CharSet from '../lib/charSet';
3+
import CharSet, {charSet64, charSet32, charSet16, charSet8, charSet4, charSet2} from '../lib/charSet'
44

55
test.beforeEach('Create CharSets', t => {
66
t.context.charSet64 = new CharSet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_')
@@ -167,3 +167,23 @@ test('Custom chars: 2', t => {
167167
t.regex(error.message, /.*count/)
168168
})
169169

170+
test('Bytes needed', t => {
171+
let BITS_PER_BYTE = 8
172+
let doTest = (charSet, bits) => {
173+
let bytesNeeded = charSet.bytesNeeded(bits)
174+
let atLeast = Math.ceil(bits/BITS_PER_BYTE)
175+
t.true(atLeast <= bytesNeeded, 'CharSet: ' + charSet.chars() + ', Bits ' + bits)
176+
let atMost = atLeast + 1
177+
t.true(bytesNeeded <= atMost, 'CharSet: ' + charSet.chars() + ', Bits ' + bits)
178+
}
179+
180+
let charSets = [charSet64, charSet32, charSet16, charSet8, charSet4, charSet2]
181+
charSets.forEach( (charSet) => {
182+
for (let bits = 0; bits <= 10; bits++) {
183+
doTest(charSet, bits)
184+
}
185+
for (let bits = 12; bits <= 132; bits += 5) {
186+
doTest(charSet, bits)
187+
}
188+
})
189+
})

test/random.js

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -103,29 +103,6 @@ test('Session ID', t => {
103103
t.is(random.sessionID(charSet2).length, 128)
104104
})
105105

106-
test('Bytes needed', t => {
107-
let random = new Random()
108-
let charSets = [charSet64, charSet32, charSet16, charSet8, charSet4, charSet2]
109-
110-
let doTest = (charSet, bits) => {
111-
let bytesNeeded = charSet.bytesNeeded(bits)
112-
let atLeast = Math.ceil(bits/8)
113-
t.true(atLeast <= bytesNeeded, 'CharSet: ' + charSet.chars() + ', Bits ' + bits)
114-
let atMost = atLeast + 1
115-
t.true(bytesNeeded <= atMost, 'CharSet: ' + charSet.chars() + ', Bits ' + bits)
116-
}
117-
118-
charSets.forEach( (charSet) => {
119-
random.use(charSet)
120-
for (let bits = 0; bits < 10; bits++) {
121-
doTest(charSet, bits)
122-
}
123-
for (let bits = 12; bits < 133; bits += 5) {
124-
doTest(charSet, bits)
125-
}
126-
})
127-
})
128-
129106
test('Invalid bytes', t => {
130107
let random
131108
let regex = /Insufficient/

0 commit comments

Comments
 (0)