@@ -93,6 +93,39 @@ test('Char Set Strings', t => {
93
93
t . is ( random . stringWithBytes ( 6 , [ 0x27 ] , charSet2 ) , '001001' )
94
94
} )
95
95
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
+
96
129
test ( 'Invalid bytes' , t => {
97
130
let random
98
131
let regex = / I n s u f f i c i e n t /
0 commit comments