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