Skip to content

Commit fc65c3a

Browse files
committed
Add tests
Session ID and bytes needed
1 parent dddc8b8 commit fc65c3a

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

test/random.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,39 @@ test('Char Set Strings', t => {
9393
t.is(random.stringWithBytes( 6, [0x27], charSet2), '001001')
9494
})
9595

96+
test('Session ID', t => {
97+
let random = new Random()
98+
t.is(random.sessionID(charSet64).length, 22)
99+
t.is(random.sessionID().length, 26)
100+
t.is(random.sessionID(charSet16).length, 32)
101+
t.is(random.sessionID(charSet8).length, 43)
102+
t.is(random.sessionID(charSet4).length, 64)
103+
t.is(random.sessionID(charSet2).length, 128)
104+
})
105+
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+
96129
test('Invalid bytes', t => {
97130
let random
98131
let regex = /Insufficient/

0 commit comments

Comments
 (0)