-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
121 lines (107 loc) · 3.48 KB
/
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
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
const assert = require('assert')
const { encode, decode } = require('./base-256')
const testDecoder = () => {
const inputOutputPairs = [["", 0],
["\x82\x32\x80", 0, 2],
["\x80\x00", 0],
["\x80\x00\x00", 0],
["\xbf", (1 << 6) - 1],
["\xbf\xff", (1 << 14) - 1],
["\xbb\xbf\xff\xff", (1 << 22) - 1, 1],
["\xff", -1],
["\xff\xff", -1],
["\xff\xff\xff", -1],
["\xc0", -1 * (1 << 6)],
["\xc0\x00", -1 * (1 << 14)],
["\xc0\x00\x00", -1 * (1 << 22)],
["\x80\x7f\xff\xff\xff", 2147483647],
["\x11\x11\x11\x11\x11\x11\x80\x00\x00\x00\x00\x7f\xff\xff\xff", 2147483647, 6],
["\xff\x80\x00\x00\x00", -2147483648],
["\xff\xff\xff\xff\x80\x00\x00\x00", -2147483648],
["\x80\x00\x00\x1f\xff\xff\xff\xff\xfe\xff", Number.MAX_SAFE_INTEGER - Math.pow(2, 8)],
["\xff\xff\xff\xe0\x00\x00\x00\x00\x01\x01", -1 * (Number.MAX_SAFE_INTEGER - Math.pow(2, 8))]]
inputOutputPairs.forEach(pair => {
const buf = new Buffer(pair[0], 'binary')
const offset = pair[2]
const decodedBigNum = decode(buf, offset)
assert.equal(decodedBigNum, pair[1])
})
}
const testEncoder = () => {
const inputOutputPairs = [[-1, "\xff"],
[-1, "\xff\xff"],
[-1, "\xff\xff\xff"],
[(1 << 0), "0"],
[(1 << 8) - 1, "\x80\xff"],
[(1 << 8), "0\x00"],
[(1 << 16) - 1, "\x80\xff\xff"],
[(1 << 16), "00\x00"],
[-1 * (1 << 0), "\xff"],
[-1*(1<<0) - 1, "0"],
[-1 * (1 << 8), "\xff\x00"],
[-1*(1<<8) - 1, "0\x00"],
[-1 * (1 << 16), "\xff\x00\x00"],
[-1*(1<<16) - 1, "00\x00"],
[2147483647, "\x80\x7f\xff\xff\xff"],
[2147483647, "\x80\x00\x00\x00\x00\x7f\xff\xff\xff"],
[-2147483648, "\xff\x80\x00\x00\x00"],
[-2147483648, "\xff\xff\xff\xff\x80\x00\x00\x00"],
[Number.MAX_SAFE_INTEGER - Math.pow(2, 8), "\x80\x00\x00\x1f\xff\xff\xff\xff\xfe\xff"],
[-1 * (Number.MAX_SAFE_INTEGER - Math.pow(2, 8)), "\xff\xff\xff\xe0\x00\x00\x00\x00\x01\x01"]]
inputOutputPairs.forEach(pair => {
const tempbuf = new Buffer(pair[1], 'binary')
const buf = new Buffer(tempbuf.length)
tempbuf.copy(buf)
encode(buf, pair[0])
assert.ok(areBuffersEqual(buf, tempbuf))
})
}
const testErrors = () => {
testDecoderErrors()
testEncoderErrors()
}
const testDecoderErrors = () => {
[
"\x80\x7f\xff\xff\xff\xff\xff\xff\xff",
"\x80\x80\x00\x00\x00\x00\x00\x00\x00",
"\xff\x80\x00\x00\x00\x00\x00\x00\x00",
"\xff\x7f\xff\xff\xff\xff\xff\xff\xff",
"\xf5\xec\xd1\xc7\x7e\x5f\x26\x48\x81\x9f\x8f\x9b"
].forEach(b => {
const buf = new Buffer(b, 'binary')
assert.throws(
() => { decode(buf) },
/^TypeError: Result out of range.$/
)
})
assert.throws(
() => { decode({}) },
/^TypeError: decode only accepts buffer.$/
)
}
const testEncoderErrors = () => {
const buf = new Buffer("\x80\x00\x00\x00\x00\x7f\xff\xff\xff", 'binary')
assert.throws(
() => { encode(buf, Number.MAX_SAFE_INTEGER) },
/^TypeError: number should be between range -9007199254740991 to 9007199254740991.$/
)
assert.throws(
() => { encode({}, 2147483648) },
/^TypeError: encode expects first parameter to be buffer.$/
)
assert.throws(
() => { encode(buf, {}) },
/^TypeError: encode expects second parameter to be integer.$/
)
}
const areBuffersEqual = (buf, buf2) => {
if (buf.length !== buf2.length) return false
var isBuffer = true
for (var i = 0; i < buf.length; i++) {
if (buf[i] !== buf2[i]) isBuffer = false
}
return isBuffer
}
testDecoder()
testEncoder()
testErrors()