1
1
import Random from '../lib/random'
2
- import CharSet from '../lib/charSet'
2
+ import CharSet , { charSet64 , charSet32 , charSet16 , charSet8 , charSet4 , charSet2 } from '../lib/charSet'
3
3
4
4
import test from 'ava'
5
5
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
-
16
6
test ( 'Char Set Base 64 Strings' , t => {
17
- let random = new Random ( t . context . charSet64 )
7
+ let random = new Random ( charSet64 )
18
8
t . is ( random . stringWithBytes ( 6 , Buffer . from ( [ 0xdd ] ) ) , '3' )
19
9
t . is ( random . stringWithBytes ( 12 , Buffer . from ( [ 0x78 , 0xfc ] ) ) , 'eP' )
20
10
t . is ( random . stringWithBytes ( 18 , Buffer . from ( [ 0xc5 , 0x6f , 0x21 ] ) ) , 'xW8' )
@@ -30,7 +20,7 @@ test('Char Set Base 64 Strings', t => {
30
20
} )
31
21
32
22
test ( 'Char Set Base 32 Strings' , t => {
33
- let random = new Random ( t . context . charSet32 )
23
+ let random = new Random ( charSet32 )
34
24
t . is ( random . stringWithBytes ( 5 , Buffer . from ( [ 0xdd ] ) ) , 'N' )
35
25
t . is ( random . stringWithBytes ( 10 , Buffer . from ( [ 0x78 , 0xfc ] ) ) , 'p6' )
36
26
t . is ( random . stringWithBytes ( 15 , Buffer . from ( [ 0x78 , 0xfc ] ) ) , 'p6R' )
@@ -45,7 +35,7 @@ test('Char Set Base 32 Strings', t => {
45
35
} )
46
36
47
37
test ( 'Char Set Base 16 Strings' , t => {
48
- let random = new Random ( t . context . charSet16 )
38
+ let random = new Random ( charSet16 )
49
39
t . is ( random . stringWithBytes ( 4 , Buffer . from ( [ 0x9d ] ) ) , '9' )
50
40
t . is ( random . stringWithBytes ( 8 , Buffer . from ( [ 0xae ] ) ) , 'ae' )
51
41
t . is ( random . stringWithBytes ( 12 , Buffer . from ( [ 0x01 , 0xf2 ] ) ) , '01f' )
@@ -54,7 +44,7 @@ test('Char Set Base 16 Strings', t => {
54
44
} )
55
45
56
46
test ( 'Char Set Base 8 Strings' , t => {
57
- let random = new Random ( t . context . charSet8 )
47
+ let random = new Random ( charSet8 )
58
48
t . is ( random . stringWithBytes ( 3 , Buffer . from ( [ 0x5a ] ) ) , '2' )
59
49
t . is ( random . stringWithBytes ( 6 , Buffer . from ( [ 0x5a ] ) ) , '26' )
60
50
t . is ( random . stringWithBytes ( 9 , Buffer . from ( [ 0x21 , 0xa4 ] ) ) , '103' )
@@ -68,7 +58,7 @@ test('Char Set Base 8 Strings', t => {
68
58
} )
69
59
70
60
test ( 'Char Set Base 4 Strings' , t => {
71
- let random = new Random ( t . context . charSet4 )
61
+ let random = new Random ( charSet4 )
72
62
t . is ( random . stringWithBytes ( 2 , Buffer . from ( [ 0x5a ] ) ) , 'T' )
73
63
t . is ( random . stringWithBytes ( 4 , Buffer . from ( [ 0x5a ] ) ) , 'TT' )
74
64
t . is ( random . stringWithBytes ( 6 , Buffer . from ( [ 0x93 ] ) ) , 'CTA' )
@@ -80,7 +70,7 @@ test('Char Set Base 4 Strings', t => {
80
70
} )
81
71
82
72
test ( 'Char Set Base 2 Strings' , t => {
83
- let random = new Random ( t . context . charSet2 )
73
+ let random = new Random ( charSet2 )
84
74
t . is ( random . stringWithBytes ( 1 , Buffer . from ( [ 0x27 ] ) ) , '0' )
85
75
t . is ( random . stringWithBytes ( 2 , Buffer . from ( [ 0x27 ] ) ) , '00' )
86
76
t . is ( random . stringWithBytes ( 3 , Buffer . from ( [ 0x27 ] ) ) , '001' )
@@ -95,25 +85,25 @@ test('Char Set Base 2 Strings', t => {
95
85
96
86
test ( 'Char Set Strings' , t => {
97
87
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' )
104
94
} )
105
95
106
96
test ( 'Invalid bytes' , t => {
107
97
let random
108
98
let regex = / I n s u f f i c i e n t /
109
99
110
- random = new Random ( t . context . charSet64 )
100
+ random = new Random ( charSet64 )
111
101
t . regex ( invalidBytes ( random , 7 , [ 1 ] ) , regex )
112
102
t . regex ( invalidBytes ( random , 13 , [ 1 , 2 ] ) , regex )
113
103
t . regex ( invalidBytes ( random , 25 , [ 1 , 2 , 3 ] ) , regex )
114
104
t . regex ( invalidBytes ( random , 31 , [ 1 , 2 , 3 , 4 ] ) , regex )
115
105
116
- random = new Random ( t . context . charSet32 )
106
+ random = new Random ( charSet32 )
117
107
t . regex ( invalidBytes ( random , 6 , [ 1 ] ) , regex )
118
108
t . regex ( invalidBytes ( random , 16 , [ 1 , 2 ] ) , regex )
119
109
t . regex ( invalidBytes ( random , 21 , [ 1 , 2 , 3 ] ) , regex )
@@ -122,21 +112,21 @@ test('Invalid bytes', t => {
122
112
t . regex ( invalidBytes ( random , 46 , [ 1 , 2 , 3 , 4 , 5 , 6 ] ) , regex )
123
113
t . regex ( invalidBytes ( random , 32 , [ 250 , 200 , 150 , 100 ] ) , regex )
124
114
125
- random = new Random ( t . context . charSet16 )
115
+ random = new Random ( charSet16 )
126
116
t . regex ( invalidBytes ( random , 9 , [ 1 ] ) , regex )
127
117
t . regex ( invalidBytes ( random , 17 , [ 1 , 2 ] ) , regex )
128
118
129
- random = new Random ( t . context . charSet8 )
119
+ random = new Random ( charSet8 )
130
120
t . regex ( invalidBytes ( random , 7 , [ 1 ] ) , regex )
131
121
t . regex ( invalidBytes ( random , 16 , [ 1 , 2 ] ) , regex )
132
122
t . regex ( invalidBytes ( random , 25 , [ 1 , 2 , 3 ] ) , regex )
133
123
t . regex ( invalidBytes ( random , 31 , [ 1 , 2 , 3 , 4 ] ) , regex )
134
124
135
- random = new Random ( t . context . charSet4 )
125
+ random = new Random ( charSet4 )
136
126
t . regex ( invalidBytes ( random , 9 , [ 1 ] ) , regex )
137
127
t . regex ( invalidBytes ( random , 17 , [ 1 , 2 ] ) , regex )
138
128
139
- random = new Random ( t . context . charSet2 )
129
+ random = new Random ( charSet2 )
140
130
t . regex ( invalidBytes ( random , 9 , [ 1 ] ) , regex )
141
131
t . regex ( invalidBytes ( random , 17 , [ 1 , 2 ] ) , regex )
142
132
} )
0 commit comments