Repository navigation
Expand file tree
/
Copy pathvararray.js
More file actions
110 lines (92 loc) · 2.88 KB
/
Copy pathvararray.js
File metadata and controls
110 lines (92 loc) · 2.88 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
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
'use strict'
var test = require('tape').test
var varstruct = require('../')
var vararray = varstruct.VarArray(varstruct.UInt32BE, varstruct.Buffer(42))
test('asserts on codec creation', function (t) {
t.test('lengthType is invalid codec', function (t) {
t.throws(function () {
varstruct.VarArray(null)
}, /^TypeError: lengthType is invalid codec$/)
t.end()
})
t.test('itemType is invalid codec', function (t) {
t.throws(function () {
varstruct.VarArray(varstruct.UInt32BE)
}, /^TypeError: itemType is invalid codec$/)
t.end()
})
t.end()
})
test('encode', function (t) {
t.test('value must be an Array instance', function (t) {
t.throws(function () {
vararray.encode(null)
}, /^TypeError: value must be an Array instance$/)
t.end()
})
var expectedError = new RangeError('index out of range')
if (process.version.split('.').slice(0, 2).join('') === 'v010') {
expectedError = new RangeError('Trying to write outside buffer length')
}
t.test('index out of range #1', function (t) {
t.throws(function () {
vararray.encode(new Array(42), Buffer.allocUnsafe(3))
}, expectedError)
t.end()
})
t.test('index out of range #2', function (t) {
t.throws(function () {
vararray.encode(new Array(42), undefined, 1765)
}, expectedError)
t.end()
})
t.test('write buffers', function (t) {
var buffers = new Array(42)
for (var i = 0; i < buffers.length; ++i) buffers[i] = Buffer.allocUnsafe(42)
var buf = vararray.encode(buffers)
t.same(vararray.encode.bytes, 1768)
t.same(buf.slice(0, 4).toString('hex'), '0000002a')
t.same(buf.slice(4).toString('hex'), Buffer.concat(buffers).toString('hex'))
t.end()
})
t.end()
})
test('decode', function (t) {
var buf = Buffer.allocUnsafe(1768)
buf.writeUInt32BE(42, 0)
t.test('not enough data for decode #1', function (t) {
t.throws(function () {
vararray.decode(buf.slice(1))
}, /^RangeError: not enough data for decode$/)
t.end()
})
t.test('not enough data for decode #2', function (t) {
t.throws(function () {
vararray.decode(buf, 1)
}, /^RangeError: not enough data for decode$/)
t.end()
})
t.test('read buffers', function (t) {
var buffers = vararray.decode(buf)
t.plan(43)
t.same(vararray.decode.bytes, 1768)
for (var i = 0, offset = 4; i < buffers.length; ++i, offset += 42) {
t.same(buffers[i].toString('hex'), buf.slice(offset, offset + 42).toString('hex'))
}
t.end()
})
t.end()
})
test('encodingLength', function (t) {
t.test('value must be an Array instance', function (t) {
t.throws(function () {
vararray.encodingLength(null)
}, /^TypeError: value must be an Array instance$/)
t.end()
})
t.test('length for 42 items', function (t) {
t.same(vararray.encodingLength(new Array(42)), 1768) // 4 + 42 * 42
t.end()
})
t.end()
})