-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
buffer.test.js
53 lines (48 loc) · 1.39 KB
/
buffer.test.js
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
import * as t from './testing.js'
import * as buffer from './buffer.js'
import * as prng from './prng.js'
/**
* @param {t.TestCase} tc
* @param {function(Uint8Array):string} encoder
* @param {function(string):Uint8Array} decoder
*/
const testEncodingHelper = (tc, encoder, decoder) => {
const gen = tc.prng
const barr = prng.uint8Array(gen, prng.uint32(gen, 0, 47))
const copied = buffer.copyUint8Array(barr)
const encoded = encoder(barr)
t.assert(encoded.constructor === String)
const decoded = decoder(encoded)
t.assert(decoded.constructor === Uint8Array)
t.assert(decoded.byteLength === barr.byteLength)
for (let i = 0; i < barr.length; i++) {
t.assert(barr[i] === decoded[i])
}
t.compare(copied, decoded)
}
/**
* @param {t.TestCase} tc
*/
export const testRepeatBase64urlEncoding = tc => {
testEncodingHelper(tc, buffer.toBase64UrlEncoded, buffer.fromBase64UrlEncoded)
}
/**
* @param {t.TestCase} tc
*/
export const testRepeatBase64Encoding = tc => {
testEncodingHelper(tc, buffer.toBase64, buffer.fromBase64)
}
/**
* @param {t.TestCase} tc
*/
export const testRepeatHexEncoding = tc => {
testEncodingHelper(tc, buffer.toHexString, buffer.fromHexString)
}
/**
* @param {t.TestCase} _tc
*/
export const testAnyEncoding = _tc => {
const obj = { val: 1, arr: [1, 2], str: '409231dtrnä' }
const res = buffer.decodeAny(buffer.encodeAny(obj))
t.compare(obj, res)
}