-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
78 lines (62 loc) · 2.12 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
const tape = require('tape')
const fixtures = require('./fixtures')
const bint = require('./')
tape('toString methods', t => {
for (const f of fixtures) {
const arr = new Uint8Array(f.test)
t.same(bint.toString(arr, 'hex'), f.hex)
t.same(bint.toString(arr, 'base64'), f.base64)
t.same(bint.toString(arr, 'utf-8'), f.utf8)
}
t.end()
})
tape('fromString methods', t => {
for (const f of fixtures) {
const arr = new Uint8Array(f.test)
const from = new Uint8Array(f.utf8from)
t.same(bint.fromString(f.hex, 'hex'), arr)
t.same(bint.fromString(f.base64, 'base64'), arr)
t.same(bint.fromString(f.base64, 'utf-8'), from)
}
t.throws(() => bint.fromString('ABCOPQRSTUVWX%$rstuvwxyz0123456789+/', 'base64'))
t.throws(() => bint.fromString('ABCOPQRSTUVWX=rstuvwxyz0123456789+/=', 'base64'))
t.throws(() => bint.fromString('ABCOPQRSTUVWXrstuvwxyz0123456789+/=', 'base64'))
t.throws(() => bint.fromString('0xdeadbeef', 'hex'))
t.end()
})
tape('compare', t => {
t.equal(bint.compare(Buffer.alloc(2), Buffer.alloc(3)), -1)
t.equal(bint.compare(Buffer.alloc(2), Buffer.alloc(2, 1)), -1)
t.equal(bint.compare(Buffer.alloc(2, 1), Buffer.alloc(2)), 1)
t.equal(bint.compare(Buffer.alloc(2), Buffer.alloc(2)), 0)
t.equal(bint.compare(Buffer.alloc(4096, 0x7f), Buffer.alloc(4096, 0x7f)), 0)
t.equal(bint.compare(Buffer.alloc(4096, 0x7f), Buffer.alloc(4096, 0x80)), -1)
t.end()
})
tape('equals', t => {
const bufs = fixtures.map(f => new Uint8Array(f.test))
for (let i = 0; i < bufs.length; i++) {
for (let j = 0; j < bufs.length; j++) {
if (bint.equals(bufs[i], bufs[j]) !== (i === j)) {
t.fail(`fixture[${i}] !== fixture[${j}]`)
}
}
}
t.end()
})
tape('concat', t => {
for (let i = 0; i < fixtures.length; i++) {
const toConcat = fixtures.slice(0, i + 1).map(t => new Uint8Array(t.test))
const sum = bint.concat(toConcat)
const check = new Uint8Array(fixtures[i].sum)
t.same(sum, check)
}
t.end()
})
tape('allocUnsafe', t => {
for (let i = 1; i < 2 ** 30; i *= 2) {
const u = bint.allocUnsafe(i)
t.equal(u.byteLength, i)
}
t.end()
})