-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspec.js
155 lines (129 loc) · 4.49 KB
/
spec.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
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
var test = require('tape');
var base32h = require('.');
test('roundtrip encode/decode', function (t) {
t.equal(base32h.decode(base32h.encode(8675309)),
8675309, "Jenny's phone number");
t.end();
});
function testEncode(t, i, o) {
t.equal(base32h.encode(i), o+'', 'encode ' + i + ' -> ' + o);
}
test('Base32H Numeric: Encode', function (t) {
t.test('Digits', function (t) {
for (var i of Array(10).keys()) {
testEncode(t, i, i+'');
}
var abc = "ABCDEFGHJKLMNPQRTVWXYZ";
for (var i of Array(22).keys()) {
testEncode(t, i+10, abc[i]);
}
t.end();
});
t.test('Numbers', function (t) {
testEncode(t, 2**5-1, 'Z');
testEncode(t, 2**10-1, 'ZZ');
testEncode(t, 2**20-1, 'ZZZZ');
testEncode(t, 2**40-1, 'ZZZZZZZZ');
testEncode(t, 2**8-1, '7Z');
testEncode(t, 2**16-1, '1ZZZ');
testEncode(t, 2**32-1, '3ZZZZZZ');
t.end();
});
t.end();
});
function testDecode(t, i, o) {
t.equal(base32h.decode(i), o, 'decode ' + i + ' -> ' + o);
}
test('Base32H Numeric: Decode', function (t) {
t.test('Canonical Digits', function (t) {
for (var i of Array(32).keys()) {
testDecode(t, '0123456789ABCDEFGHJKLMNPQRTVWXYZ'[i], i);
}
t.end();
});
t.test('Alias Digits', function (t) {
testDecode(t, 'o', 0);
testDecode(t, 'O', 0);
testDecode(t, 'i', 1);
testDecode(t, 'I', 1);
testDecode(t, 's', 5);
testDecode(t, 'S', 5);
testDecode(t, 'u', 27);
testDecode(t, 'U', 27);
for (var i of Array(22).keys()) {
testDecode(t, 'abcdefghjklmnpqrtvwxyz'[i], i+10);
}
t.end();
});
t.test('Numbers', function (t) {
testDecode(t, 'Z', 2**5-1);
testDecode(t, 'Zz', 2**10-1);
testDecode(t, 'ZzzZ', 2**20-1);
testDecode(t, 'zZzZZzZz', 2**40-1);
testDecode(t, '7z', 2**8-1);
testDecode(t, 'iZzZ', 2**16-1);
testDecode(t, '3zZzZzZ', 2**32-1);
t.end();
});
t.end();
});
function testBinEncode(t, i, o) {
t.equal(base32h.encodeBin(i), o, 'encode ' + i + ' -> ' + o);
}
test('Base32H Binary: Encode', function (t) {
testBinEncode(t, [255], '0000007Z');
testBinEncode(t, [255,255], '00001ZZZ');
testBinEncode(t, [255,255,255], '000FZZZZ');
testBinEncode(t, [255,255,255,255], '03ZZZZZZ');
testBinEncode(t, [255,255,255,255,255], 'ZZZZZZZZ');
var uint40 = [255,255,255,255,255];
testBinEncode(t, [255, ...uint40], '0000007ZZZZZZZZZ');
testBinEncode(t, [...uint40, ...uint40], 'ZZZZZZZZZZZZZZZZ');
t.end();
});
function arrayEqual(a, b) {
if (a.length !== b.length) return false;
for (var i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false;
}
return true;
}
function testBinDecode(t, i, o) {
t.ok(arrayEqual(base32h.decodeBin(i), o), 'decode ' + i + ' -> ' + o);
}
test('Base32H Binary: Decode', function (t) {
testBinDecode(t, '7z', [0,0,0,0,255]);
testBinDecode(t, '1zZz', [0,0,0,255,255]);
testBinDecode(t, 'fZzZz', [0,0,255,255,255]);
testBinDecode(t, '3zZzZzZ', [0,255,255,255,255]);
testBinDecode(t, 'zZzZzZzZ', [255,255,255,255,255]);
var uint40 = [255,255,255,255,255];
testBinDecode(t, '7ZZZZZZZZZ', [0,0,0,0,255, ...uint40]);
testBinDecode(t, 'zZzZzZzZzZzZzZzZ', [...uint40, ...uint40]);
t.end();
});
test('Base32H Digit: Encode Digit', function (t) {
t.equal(base32h.encodeDigit(0), '0', "Encode 0 to '0'");
t.equal(base32h.encodeDigit(10), 'A', "Encode 10 to 'A'");
t.equal(base32h.encodeDigit(31), 'Z', "Encode 31 to 'Z'");
t.throws(
() => { base32h.encodeDigit(-1) },
/Cannot read property '0' of undefined/,
"Encode -1 should throw"
);
t.throws(
() => { base32h.encodeDigit(32) },
/Cannot read property '0' of undefined/,
"Encode 32 should throw"
);
t.end();
});
test('Base32H Digit: Decode Digit', function (t) {
t.equal(base32h.decodeDigit('0'), 0, "Decode '0' to 0");
t.equal(base32h.decodeDigit('A'), 10, "Decode 'A' to 10");
t.equal(base32h.decodeDigit('Z'), 31, "Decode 'Z' to 31");
t.equal(base32h.decodeDigit('!'), -1, "Decode '!' to -1");
// This isn't a problem, just document here in test.
t.equal(base32h.decodeDigit(''), 0, "Decode '' to 0");
t.end();
});