-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.js
More file actions
75 lines (66 loc) · 2.7 KB
/
Copy pathtest.js
File metadata and controls
75 lines (66 loc) · 2.7 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
/*
* Copyright (c) 2018-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const assert = require('assert');
const Z85 = require('..');
describe('Z85', () => {
it('encodes/decodes', done => {
const bytes = [0xC, 0x0, 0xF, 0xF, 0xE, 0xE];
const buffer = new ArrayBuffer(bytes.length);
const view = new DataView(buffer);
for (let i = 0; i < bytes.length; ++i) {
view.setUint8(i, bytes[i]);
}
const encoded1 = Z85.encode(bytes);
const encoded2 = Z85.encode(buffer);
const encoded3 = Z85.encode(view);
assert.strictEqual(encoded1, encoded2);
assert.strictEqual(encoded2, encoded3);
const decoded1 = Z85.decode(encoded1);
const decoded2 = Z85.decode(encoded2);
const decoded3 = Z85.decode(encoded3);
assert.strictEqual(decoded1 instanceof ArrayBuffer, true);
assert.strictEqual(decoded2 instanceof ArrayBuffer, true);
assert.strictEqual(decoded3 instanceof ArrayBuffer, true);
assert.strictEqual(decoded1.byteLength, bytes.length);
assert.strictEqual(decoded2.byteLength, bytes.length);
assert.strictEqual(decoded3.byteLength, bytes.length);
const result1 = new DataView(decoded1);
const result2 = new DataView(decoded2);
const result3 = new DataView(decoded3);
for (let i = 0; i < bytes.length; ++i) {
assert.strictEqual(bytes[i], result1.getUint8(i));
assert.strictEqual(bytes[i], result2.getUint8(i));
assert.strictEqual(bytes[i], result3.getUint8(i));
}
done();
});
[
{ name: 'invalid ASCII', encoded: 'a_b', expected: "Character '_' in position 2 is not valid Z85" },
{ name: 'invalid Uniucode', encoded: '12Ø', expected: "Character 'Ø' in position 3 is not valid Z85" },
].forEach(test => {
it('properly chokes on ' + test.name, done => {
let err = null;
try {
Z85.decode(test.encoded);
} catch (e) {
err = e;
}
assert.strictEqual(err.message, test.expected);
done();
});
});
});