forked from EntropyString/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharSet.js
More file actions
189 lines (161 loc) · 4.96 KB
/
charSet.js
File metadata and controls
189 lines (161 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import test from 'ava'
import CharSet, {charSet64, charSet32, charSet16, charSet8, charSet4, charSet2} from '../lib/charSet'
test.beforeEach('Create CharSets', t => {
t.context.charSet64 = new CharSet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_')
t.context.charSet32 = new CharSet('2346789bdfghjmnpqrtBDFGHJLMNPQRT')
t.context.charSet16 = new CharSet('0123456789abcdef')
t.context.charSet8 = new CharSet('01234567')
t.context.charSet4 = new CharSet('ATCG')
t.context.charSet2 = new CharSet('01')
})
test('char set 64', t => {
const charSet = t.context.charSet64
const length = charSet.getChars().length
t.is(length, 64)
const bitsPerChar = Math.log2(length)
t.is(charSet.getBitsPerChar(), bitsPerChar)
t.is(charSet.getCharsPerChunk(), 4)
})
test('char set 32', t => {
const charSet = t.context.charSet32
const length = charSet.getChars().length
t.is(length, 32)
const bitsPerChar = Math.log2(length)
t.is(charSet.getBitsPerChar(), bitsPerChar)
t.is(charSet.getCharsPerChunk(), 8)
})
test('char set 16', t => {
const charSet = t.context.charSet16
const length = charSet.getChars().length
t.is(length, 16)
const bitsPerChar = Math.log2(length)
t.is(charSet.getBitsPerChar(), bitsPerChar)
t.is(charSet.getCharsPerChunk(), 2)
})
test('char set 8', t => {
const charSet = t.context.charSet8
const length = charSet.getChars().length
t.is(length, 8)
const bitsPerChar = Math.log2(length)
t.is(charSet.getBitsPerChar(), bitsPerChar)
t.is(charSet.getCharsPerChunk(), 8)
})
test('char set 4', t => {
const charSet = t.context.charSet4
const length = charSet.getChars().length
t.is(length, 4)
const bitsPerChar = Math.log2(length)
t.is(charSet.getBitsPerChar(), bitsPerChar)
t.is(charSet.getCharsPerChunk(), 4)
})
test('char set 2', t => {
const charSet = t.context.charSet2
const length = charSet.getChars().length
t.is(length, 2)
const bitsPerChar = Math.log2(length)
t.is(charSet.getBitsPerChar(), bitsPerChar)
t.is(charSet.getCharsPerChunk(), 8)
})
test('Custom chars: 64', t => {
let error = t.throws(() => {
new CharSet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ab')
}, Error)
t.regex(error.message, /.*unique/)
error = t.throws(() => {
new CharSet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz)!@#$%^&*(+=0')
}, Error)
t.regex(error.message, /.*count/)
error = t.throws(() => {
new CharSet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz)!@#$%^&*(+')
}, Error)
t.regex(error.message, /.*count/)
})
test('Custom chars: 32', t => {
let error = t.throws(() => {
new CharSet('01234567890123456789012345678901')
}, Error)
t.regex(error.message, /.*unique/)
error = t.throws(() => {
new CharSet('0123456789abcdefghijklmnopqrstu')
}, Error)
t.regex(error.message, /.*count/)
error = t.throws(() => {
new CharSet('0123456789abcdefghijklmnopqrstuvw')
}, Error)
t.regex(error.message, /.*count/)
})
test('Custom chars: 16', t => {
let error = t.throws(() => {
new CharSet('0123456789abcde0')
}, Error)
t.regex(error.message, /.*unique/)
error = t.throws(() => {
new CharSet('0123456789abcde')
}, Error)
t.regex(error.message, /.*count/)
error = t.throws(() => {
new CharSet('0123456789abcdefg')
}, Error)
t.regex(error.message, /.*count/)
})
test('Custom chars: 8', t => {
let error = t.throws(() => {
new CharSet('abcdefga')
}, Error)
t.regex(error.message, /.*unique/)
error = t.throws(() => {
new CharSet('abcdefg')
}, Error)
t.regex(error.message, /.*count/)
error = t.throws(() => {
new CharSet('abcdefghi')
}, Error)
t.regex(error.message, /.*count/)
})
test('Custom chars: 4', t => {
let error = t.throws(() => {
new CharSet('abbc')
}, Error)
t.regex(error.message, /.*unique/)
error = t.throws(() => {
new CharSet('abc')
}, Error)
t.regex(error.message, /.*count/)
error = t.throws(() => {
new CharSet('abcde')
}, Error)
t.regex(error.message, /.*count/)
})
test('Custom chars: 2', t => {
let error = t.throws(() => {
new CharSet('TT')
}, Error)
t.regex(error.message, /.*unique/)
error = t.throws(() => {
new CharSet('T')
}, Error)
t.regex(error.message, /.*count/)
error = t.throws(() => {
new CharSet('H20')
}, Error)
t.regex(error.message, /.*count/)
})
test('Bytes needed', t => {
let BITS_PER_BYTE = 8
let doTest = (charSet, bits) => {
let bytesNeeded = charSet.bytesNeeded(bits)
let atLeast = Math.ceil(bits/BITS_PER_BYTE)
t.true(atLeast <= bytesNeeded, 'CharSet: ' + charSet.chars() + ', Bits ' + bits)
let atMost = atLeast + 1
t.true(bytesNeeded <= atMost, 'CharSet: ' + charSet.chars() + ', Bits ' + bits)
}
let charSets = [charSet64, charSet32, charSet16, charSet8, charSet4, charSet2]
charSets.forEach( (charSet) => {
for (let bits = 0; bits <= 10; bits++) {
doTest(charSet, bits)
}
for (let bits = 12; bits <= 132; bits += 5) {
doTest(charSet, bits)
}
})
})