11import Random from '../lib/random'
2- import CharSet from '../lib/charSet'
2+ import CharSet , { charSet64 , charSet32 , charSet16 , charSet8 , charSet4 , charSet2 } from '../lib/charSet'
33
44import test from 'ava'
55
6- test . beforeEach ( 'Create CharSets' , t => {
7- t . context . charSet64 = new CharSet ( 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_' )
8- t . context . charSet32 = new CharSet ( '2346789bdfghjmnpqrtBDFGHJLMNPQRT' )
9- t . context . charSet16 = new CharSet ( '0123456789abcdef' )
10- t . context . charSet8 = new CharSet ( '01234567' )
11- t . context . charSet4 = new CharSet ( 'ATCG' )
12- t . context . charSet2 = new CharSet ( '01' )
13- } )
14-
15-
166test ( 'Char Set Base 64 Strings' , t => {
17- let random = new Random ( t . context . charSet64 )
7+ let random = new Random ( charSet64 )
188 t . is ( random . stringWithBytes ( 6 , Buffer . from ( [ 0xdd ] ) ) , '3' )
199 t . is ( random . stringWithBytes ( 12 , Buffer . from ( [ 0x78 , 0xfc ] ) ) , 'eP' )
2010 t . is ( random . stringWithBytes ( 18 , Buffer . from ( [ 0xc5 , 0x6f , 0x21 ] ) ) , 'xW8' )
@@ -30,7 +20,7 @@ test('Char Set Base 64 Strings', t => {
3020} )
3121
3222test ( 'Char Set Base 32 Strings' , t => {
33- let random = new Random ( t . context . charSet32 )
23+ let random = new Random ( charSet32 )
3424 t . is ( random . stringWithBytes ( 5 , Buffer . from ( [ 0xdd ] ) ) , 'N' )
3525 t . is ( random . stringWithBytes ( 10 , Buffer . from ( [ 0x78 , 0xfc ] ) ) , 'p6' )
3626 t . is ( random . stringWithBytes ( 15 , Buffer . from ( [ 0x78 , 0xfc ] ) ) , 'p6R' )
@@ -45,7 +35,7 @@ test('Char Set Base 32 Strings', t => {
4535} )
4636
4737test ( 'Char Set Base 16 Strings' , t => {
48- let random = new Random ( t . context . charSet16 )
38+ let random = new Random ( charSet16 )
4939 t . is ( random . stringWithBytes ( 4 , Buffer . from ( [ 0x9d ] ) ) , '9' )
5040 t . is ( random . stringWithBytes ( 8 , Buffer . from ( [ 0xae ] ) ) , 'ae' )
5141 t . is ( random . stringWithBytes ( 12 , Buffer . from ( [ 0x01 , 0xf2 ] ) ) , '01f' )
@@ -54,7 +44,7 @@ test('Char Set Base 16 Strings', t => {
5444} )
5545
5646test ( 'Char Set Base 8 Strings' , t => {
57- let random = new Random ( t . context . charSet8 )
47+ let random = new Random ( charSet8 )
5848 t . is ( random . stringWithBytes ( 3 , Buffer . from ( [ 0x5a ] ) ) , '2' )
5949 t . is ( random . stringWithBytes ( 6 , Buffer . from ( [ 0x5a ] ) ) , '26' )
6050 t . is ( random . stringWithBytes ( 9 , Buffer . from ( [ 0x21 , 0xa4 ] ) ) , '103' )
@@ -68,7 +58,7 @@ test('Char Set Base 8 Strings', t => {
6858} )
6959
7060test ( 'Char Set Base 4 Strings' , t => {
71- let random = new Random ( t . context . charSet4 )
61+ let random = new Random ( charSet4 )
7262 t . is ( random . stringWithBytes ( 2 , Buffer . from ( [ 0x5a ] ) ) , 'T' )
7363 t . is ( random . stringWithBytes ( 4 , Buffer . from ( [ 0x5a ] ) ) , 'TT' )
7464 t . is ( random . stringWithBytes ( 6 , Buffer . from ( [ 0x93 ] ) ) , 'CTA' )
@@ -80,7 +70,7 @@ test('Char Set Base 4 Strings', t => {
8070} )
8171
8272test ( 'Char Set Base 2 Strings' , t => {
83- let random = new Random ( t . context . charSet2 )
73+ let random = new Random ( charSet2 )
8474 t . is ( random . stringWithBytes ( 1 , Buffer . from ( [ 0x27 ] ) ) , '0' )
8575 t . is ( random . stringWithBytes ( 2 , Buffer . from ( [ 0x27 ] ) ) , '00' )
8676 t . is ( random . stringWithBytes ( 3 , Buffer . from ( [ 0x27 ] ) ) , '001' )
@@ -95,25 +85,25 @@ test('Char Set Base 2 Strings', t => {
9585
9686test ( 'Char Set Strings' , t => {
9787 let random = new Random ( )
98- t . is ( random . stringWithBytes ( 30 , [ 0xa5 , 0x62 , 0x20 , 0x87 ] , t . context . charSet64 ) , 'pWIgh' )
99- t . is ( random . stringWithBytes ( 25 , [ 0xa5 , 0x62 , 0x20 , 0x87 ] , t . context . charSet32 ) , 'DFr43' )
100- t . is ( random . stringWithBytes ( 16 , [ 0xc7 , 0xc9 ] , t . context . charSet16 ) , 'c7c9' )
101- t . is ( random . stringWithBytes ( 24 , [ 0xfd , 0x93 , 0xd1 ] , t . context . charSet8 ) , '77311721' )
102- t . is ( random . stringWithBytes ( 12 , [ 0x20 , 0xf1 ] , t . context . charSet4 ) , 'ACAAGG' )
103- t . is ( random . stringWithBytes ( 6 , [ 0x27 ] , t . context . charSet2 ) , '001001' )
88+ t . is ( random . stringWithBytes ( 30 , [ 0xa5 , 0x62 , 0x20 , 0x87 ] , charSet64 ) , 'pWIgh' )
89+ t . is ( random . stringWithBytes ( 25 , [ 0xa5 , 0x62 , 0x20 , 0x87 ] , charSet32 ) , 'DFr43' )
90+ t . is ( random . stringWithBytes ( 16 , [ 0xc7 , 0xc9 ] , charSet16 ) , 'c7c9' )
91+ t . is ( random . stringWithBytes ( 24 , [ 0xfd , 0x93 , 0xd1 ] , charSet8 ) , '77311721' )
92+ t . is ( random . stringWithBytes ( 12 , [ 0x20 , 0xf1 ] , charSet4 ) , 'ACAAGG' )
93+ t . is ( random . stringWithBytes ( 6 , [ 0x27 ] , charSet2 ) , '001001' )
10494} )
10595
10696test ( 'Invalid bytes' , t => {
10797 let random
10898 let regex = / I n s u f f i c i e n t /
10999
110- random = new Random ( t . context . charSet64 )
100+ random = new Random ( charSet64 )
111101 t . regex ( invalidBytes ( random , 7 , [ 1 ] ) , regex )
112102 t . regex ( invalidBytes ( random , 13 , [ 1 , 2 ] ) , regex )
113103 t . regex ( invalidBytes ( random , 25 , [ 1 , 2 , 3 ] ) , regex )
114104 t . regex ( invalidBytes ( random , 31 , [ 1 , 2 , 3 , 4 ] ) , regex )
115105
116- random = new Random ( t . context . charSet32 )
106+ random = new Random ( charSet32 )
117107 t . regex ( invalidBytes ( random , 6 , [ 1 ] ) , regex )
118108 t . regex ( invalidBytes ( random , 16 , [ 1 , 2 ] ) , regex )
119109 t . regex ( invalidBytes ( random , 21 , [ 1 , 2 , 3 ] ) , regex )
@@ -122,21 +112,21 @@ test('Invalid bytes', t => {
122112 t . regex ( invalidBytes ( random , 46 , [ 1 , 2 , 3 , 4 , 5 , 6 ] ) , regex )
123113 t . regex ( invalidBytes ( random , 32 , [ 250 , 200 , 150 , 100 ] ) , regex )
124114
125- random = new Random ( t . context . charSet16 )
115+ random = new Random ( charSet16 )
126116 t . regex ( invalidBytes ( random , 9 , [ 1 ] ) , regex )
127117 t . regex ( invalidBytes ( random , 17 , [ 1 , 2 ] ) , regex )
128118
129- random = new Random ( t . context . charSet8 )
119+ random = new Random ( charSet8 )
130120 t . regex ( invalidBytes ( random , 7 , [ 1 ] ) , regex )
131121 t . regex ( invalidBytes ( random , 16 , [ 1 , 2 ] ) , regex )
132122 t . regex ( invalidBytes ( random , 25 , [ 1 , 2 , 3 ] ) , regex )
133123 t . regex ( invalidBytes ( random , 31 , [ 1 , 2 , 3 , 4 ] ) , regex )
134124
135- random = new Random ( t . context . charSet4 )
125+ random = new Random ( charSet4 )
136126 t . regex ( invalidBytes ( random , 9 , [ 1 ] ) , regex )
137127 t . regex ( invalidBytes ( random , 17 , [ 1 , 2 ] ) , regex )
138128
139- random = new Random ( t . context . charSet2 )
129+ random = new Random ( charSet2 )
140130 t . regex ( invalidBytes ( random , 9 , [ 1 ] ) , regex )
141131 t . regex ( invalidBytes ( random , 17 , [ 1 , 2 ] ) , regex )
142132} )
0 commit comments