Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80529 views
1
var B = require('../').Buffer
2
var test = require('tape')
3
if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false
4
5
test('new buffer from array', function (t) {
6
t.equal(
7
new B([1, 2, 3]).toString(),
8
'\u0001\u0002\u0003'
9
)
10
t.end()
11
})
12
13
test('new buffer from array w/ negatives', function (t) {
14
t.equal(
15
new B([-1, -2, -3]).toString('hex'),
16
'fffefd'
17
)
18
t.end()
19
})
20
21
test('new buffer from array with mixed signed input', function (t) {
22
t.equal(
23
new B([-255, 255, -128, 128, 512, -512, 511, -511]).toString('hex'),
24
'01ff80800000ff01'
25
)
26
t.end()
27
})
28
29
test('new buffer from string', function (t) {
30
t.equal(
31
new B('hey', 'utf8').toString(),
32
'hey'
33
)
34
t.end()
35
})
36
37
test('new buffer from buffer', function (t) {
38
var b1 = new B('asdf')
39
var b2 = new B(b1)
40
t.equal(b1.toString('hex'), b2.toString('hex'))
41
t.end()
42
})
43
44
test('new buffer from Uint8Array', function (t) {
45
if (typeof Uint8Array !== 'undefined') {
46
var b1 = new Uint8Array([0, 1, 2, 3])
47
var b2 = new B(b1)
48
t.equal(b1.length, b2.length)
49
t.equal(b1[0], 0)
50
t.equal(b1[1], 1)
51
t.equal(b1[2], 2)
52
t.equal(b1[3], 3)
53
t.equal(b1[4], undefined)
54
}
55
t.end()
56
})
57
58
test('new buffer from Uint16Array', function (t) {
59
if (typeof Uint16Array !== 'undefined') {
60
var b1 = new Uint16Array([0, 1, 2, 3])
61
var b2 = new B(b1)
62
t.equal(b1.length, b2.length)
63
t.equal(b1[0], 0)
64
t.equal(b1[1], 1)
65
t.equal(b1[2], 2)
66
t.equal(b1[3], 3)
67
t.equal(b1[4], undefined)
68
}
69
t.end()
70
})
71
72
test('new buffer from Uint32Array', function (t) {
73
if (typeof Uint32Array !== 'undefined') {
74
var b1 = new Uint32Array([0, 1, 2, 3])
75
var b2 = new B(b1)
76
t.equal(b1.length, b2.length)
77
t.equal(b1[0], 0)
78
t.equal(b1[1], 1)
79
t.equal(b1[2], 2)
80
t.equal(b1[3], 3)
81
t.equal(b1[4], undefined)
82
}
83
t.end()
84
})
85
86
test('new buffer from Int16Array', function (t) {
87
if (typeof Int16Array !== 'undefined') {
88
var b1 = new Int16Array([0, 1, 2, 3])
89
var b2 = new B(b1)
90
t.equal(b1.length, b2.length)
91
t.equal(b1[0], 0)
92
t.equal(b1[1], 1)
93
t.equal(b1[2], 2)
94
t.equal(b1[3], 3)
95
t.equal(b1[4], undefined)
96
}
97
t.end()
98
})
99
100
test('new buffer from Int32Array', function (t) {
101
if (typeof Int32Array !== 'undefined') {
102
var b1 = new Int32Array([0, 1, 2, 3])
103
var b2 = new B(b1)
104
t.equal(b1.length, b2.length)
105
t.equal(b1[0], 0)
106
t.equal(b1[1], 1)
107
t.equal(b1[2], 2)
108
t.equal(b1[3], 3)
109
t.equal(b1[4], undefined)
110
}
111
t.end()
112
})
113
114
test('new buffer from Float32Array', function (t) {
115
if (typeof Float32Array !== 'undefined') {
116
var b1 = new Float32Array([0, 1, 2, 3])
117
var b2 = new B(b1)
118
t.equal(b1.length, b2.length)
119
t.equal(b1[0], 0)
120
t.equal(b1[1], 1)
121
t.equal(b1[2], 2)
122
t.equal(b1[3], 3)
123
t.equal(b1[4], undefined)
124
}
125
t.end()
126
})
127
128
test('new buffer from Float64Array', function (t) {
129
if (typeof Float64Array !== 'undefined') {
130
var b1 = new Float64Array([0, 1, 2, 3])
131
var b2 = new B(b1)
132
t.equal(b1.length, b2.length)
133
t.equal(b1[0], 0)
134
t.equal(b1[1], 1)
135
t.equal(b1[2], 2)
136
t.equal(b1[3], 3)
137
t.equal(b1[4], undefined)
138
}
139
t.end()
140
})
141
142
test('new buffer from buffer.toJSON() output', function (t) {
143
if (typeof JSON === 'undefined') {
144
// ie6, ie7 lack support
145
t.end()
146
return
147
}
148
var buf = new B('test')
149
var json = JSON.stringify(buf)
150
var obj = JSON.parse(json)
151
var copy = new B(obj)
152
t.ok(buf.equals(copy))
153
t.end()
154
})
155
156