1
|
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false
|
2
|
var B = require('../').Buffer
|
3
|
var test = require('tape')
|
4
|
|
5
|
test('buffer.toJSON', function (t) {
|
6
|
var data = [1, 2, 3, 4]
|
7
|
t.deepEqual(
|
8
|
new B(data).toJSON(),
|
9
|
{ type: 'Buffer', data: [ 1, 2, 3, 4 ] }
|
10
|
)
|
11
|
t.end()
|
12
|
})
|
13
|
|
14
|
test('buffer.copy', function (t) {
|
15
|
// copied from nodejs.org example
|
16
|
var buf1 = new B(26)
|
17
|
var buf2 = new B(26)
|
18
|
|
19
|
for (var i = 0; i < 26; i++) {
|
20
|
buf1[i] = i + 97 // 97 is ASCII a
|
21
|
buf2[i] = 33 // ASCII !
|
22
|
}
|
23
|
|
24
|
buf1.copy(buf2, 8, 16, 20)
|
25
|
|
26
|
t.equal(
|
27
|
buf2.toString('ascii', 0, 25),
|
28
|
'!!!!!!!!qrst!!!!!!!!!!!!!'
|
29
|
)
|
30
|
t.end()
|
31
|
})
|
32
|
|
33
|
test('test offset returns are correct', function (t) {
|
34
|
var b = new B(16)
|
35
|
t.equal(4, b.writeUInt32LE(0, 0))
|
36
|
t.equal(6, b.writeUInt16LE(0, 4))
|
37
|
t.equal(7, b.writeUInt8(0, 6))
|
38
|
t.equal(8, b.writeInt8(0, 7))
|
39
|
t.equal(16, b.writeDoubleLE(0, 8))
|
40
|
t.end()
|
41
|
})
|
42
|
|
43
|
test('concat() a varying number of buffers', function (t) {
|
44
|
var zero = []
|
45
|
var one = [ new B('asdf') ]
|
46
|
var long = []
|
47
|
for (var i = 0; i < 10; i++) {
|
48
|
long.push(new B('asdf'))
|
49
|
}
|
50
|
|
51
|
var flatZero = B.concat(zero)
|
52
|
var flatOne = B.concat(one)
|
53
|
var flatLong = B.concat(long)
|
54
|
var flatLongLen = B.concat(long, 40)
|
55
|
|
56
|
t.equal(flatZero.length, 0)
|
57
|
t.equal(flatOne.toString(), 'asdf')
|
58
|
t.deepEqual(flatOne, one[0])
|
59
|
t.equal(flatLong.toString(), (new Array(10 + 1).join('asdf')))
|
60
|
t.equal(flatLongLen.toString(), (new Array(10 + 1).join('asdf')))
|
61
|
t.end()
|
62
|
})
|
63
|
|
64
|
test('fill', function (t) {
|
65
|
var b = new B(10)
|
66
|
b.fill(2)
|
67
|
t.equal(b.toString('hex'), '02020202020202020202')
|
68
|
t.end()
|
69
|
})
|
70
|
|
71
|
test('fill (string)', function (t) {
|
72
|
var b = new B(10)
|
73
|
b.fill('abc')
|
74
|
t.equal(b.toString(), 'abcabcabca')
|
75
|
b.fill('է')
|
76
|
t.equal(b.toString(), 'էէէէէ')
|
77
|
t.end()
|
78
|
})
|
79
|
|
80
|
test('copy() empty buffer with sourceEnd=0', function (t) {
|
81
|
var source = new B([42])
|
82
|
var destination = new B([43])
|
83
|
source.copy(destination, 0, 0, 0)
|
84
|
t.equal(destination.readUInt8(0), 43)
|
85
|
t.end()
|
86
|
})
|
87
|
|
88
|
test('copy() after slice()', function (t) {
|
89
|
var source = new B(200)
|
90
|
var dest = new B(200)
|
91
|
var expected = new B(200)
|
92
|
for (var i = 0; i < 200; i++) {
|
93
|
source[i] = i
|
94
|
dest[i] = 0
|
95
|
}
|
96
|
|
97
|
source.slice(2).copy(dest)
|
98
|
source.copy(expected, 0, 2)
|
99
|
t.deepEqual(dest, expected)
|
100
|
t.end()
|
101
|
})
|
102
|
|
103
|
test('copy() ascending', function (t) {
|
104
|
var b = new B('abcdefghij')
|
105
|
b.copy(b, 0, 3, 10)
|
106
|
t.equal(b.toString(), 'defghijhij')
|
107
|
t.end()
|
108
|
})
|
109
|
|
110
|
test('copy() descending', function (t) {
|
111
|
var b = new B('abcdefghij')
|
112
|
b.copy(b, 3, 0, 7)
|
113
|
t.equal(b.toString(), 'abcabcdefg')
|
114
|
t.end()
|
115
|
})
|
116
|
|
117
|
test('buffer.slice sets indexes', function (t) {
|
118
|
t.equal((new B('hallo')).slice(0, 5).toString(), 'hallo')
|
119
|
t.end()
|
120
|
})
|
121
|
|
122
|
test('buffer.slice out of range', function (t) {
|
123
|
t.plan(2)
|
124
|
t.equal((new B('hallo')).slice(0, 10).toString(), 'hallo')
|
125
|
t.equal((new B('hallo')).slice(10, 2).toString(), '')
|
126
|
t.end()
|
127
|
})
|