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