test.js
1.29 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
var Buffer = require('buffer').Buffer
var test = require('tape')
var toArrayBuffer = require('.')
function elementsEqual (ab, buffer) {
var view = new Uint8Array(ab)
for (var i = 0; i < view.length; i++) {
if (view[i] !== buffer[i]) {
return false
}
}
return true
}
test('Basic behavior', function (t) {
var buf = new Buffer(10)
for (var i = 0; i < 10; i++) {
buf[i] = i
}
var ab = toArrayBuffer(buf)
t.equals(ab.byteLength, 10, 'correct length')
t.ok(elementsEqual(ab, buf), 'elements equal')
t.end()
})
test('Behavior when input is a subarray 1', function (t) {
var origBuf = new Buffer(10)
for (var i = 0; i < 10; i++) {
origBuf[i] = i
}
var buf = origBuf.slice(1)
var ab = toArrayBuffer(buf)
t.equals(ab.byteLength, 9, 'correct length')
t.ok(elementsEqual(ab, buf), 'elements equal')
t.notOk(ab === buf.buffer, 'the underlying ArrayBuffer is not returned when incorrect')
t.end()
})
test('Behavior when input is a subarray 2', function (t) {
var origBuf = new Buffer(10)
for (var i = 0; i < 10; i++) {
origBuf[i] = i
}
var buf = origBuf.slice(0, 9)
var ab = toArrayBuffer(buf)
t.equals(ab.byteLength, 9, 'correct length')
t.ok(elementsEqual(ab, buf), 'elements equal')
t.notOk(ab === buf.buffer, 'the underlying ArrayBuffer is not returned when incorrect')
t.end()
})