constructor.js
4.31 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false
var B = require('../').Buffer
var test = require('tape')
test('new buffer from array', function (t) {
t.equal(
new B([1, 2, 3]).toString(),
'\u0001\u0002\u0003'
)
t.end()
})
test('new buffer from array w/ negatives', function (t) {
t.equal(
new B([-1, -2, -3]).toString('hex'),
'fffefd'
)
t.end()
})
test('new buffer from array with mixed signed input', function (t) {
t.equal(
new B([-255, 255, -128, 128, 512, -512, 511, -511]).toString('hex'),
'01ff80800000ff01'
)
t.end()
})
test('new buffer from string', function (t) {
t.equal(
new B('hey', 'utf8').toString(),
'hey'
)
t.end()
})
test('new buffer from buffer', function (t) {
var b1 = new B('asdf')
var b2 = new B(b1)
t.equal(b1.toString('hex'), b2.toString('hex'))
t.end()
})
test('new buffer from ArrayBuffer', function (t) {
if (typeof ArrayBuffer !== 'undefined') {
var arraybuffer = new Uint8Array([0, 1, 2, 3]).buffer
var b = new B(arraybuffer)
t.equal(b.length, 4)
t.equal(b[0], 0)
t.equal(b[1], 1)
t.equal(b[2], 2)
t.equal(b[3], 3)
t.equal(b[4], undefined)
}
t.end()
})
test('new buffer from ArrayBuffer, shares memory', function (t) {
if (Buffer.TYPED_ARRAY_SUPPORT) {
var u = new Uint8Array([0, 1, 2, 3])
var arraybuffer = u.buffer
var b = new B(arraybuffer)
t.equal(b.length, 4)
t.equal(b[0], 0)
t.equal(b[1], 1)
t.equal(b[2], 2)
t.equal(b[3], 3)
t.equal(b[4], undefined)
// changing the Uint8Array (and thus the ArrayBuffer), changes the Buffer
u[0] = 10
t.equal(b[0], 10)
u[1] = 11
t.equal(b[1], 11)
u[2] = 12
t.equal(b[2], 12)
u[3] = 13
t.equal(b[3], 13)
}
t.end()
})
test('new buffer from Uint8Array', function (t) {
if (typeof Uint8Array !== 'undefined') {
var b1 = new Uint8Array([0, 1, 2, 3])
var b2 = new B(b1)
t.equal(b1.length, b2.length)
t.equal(b1[0], 0)
t.equal(b1[1], 1)
t.equal(b1[2], 2)
t.equal(b1[3], 3)
t.equal(b1[4], undefined)
}
t.end()
})
test('new buffer from Uint16Array', function (t) {
if (typeof Uint16Array !== 'undefined') {
var b1 = new Uint16Array([0, 1, 2, 3])
var b2 = new B(b1)
t.equal(b1.length, b2.length)
t.equal(b1[0], 0)
t.equal(b1[1], 1)
t.equal(b1[2], 2)
t.equal(b1[3], 3)
t.equal(b1[4], undefined)
}
t.end()
})
test('new buffer from Uint32Array', function (t) {
if (typeof Uint32Array !== 'undefined') {
var b1 = new Uint32Array([0, 1, 2, 3])
var b2 = new B(b1)
t.equal(b1.length, b2.length)
t.equal(b1[0], 0)
t.equal(b1[1], 1)
t.equal(b1[2], 2)
t.equal(b1[3], 3)
t.equal(b1[4], undefined)
}
t.end()
})
test('new buffer from Int16Array', function (t) {
if (typeof Int16Array !== 'undefined') {
var b1 = new Int16Array([0, 1, 2, 3])
var b2 = new B(b1)
t.equal(b1.length, b2.length)
t.equal(b1[0], 0)
t.equal(b1[1], 1)
t.equal(b1[2], 2)
t.equal(b1[3], 3)
t.equal(b1[4], undefined)
}
t.end()
})
test('new buffer from Int32Array', function (t) {
if (typeof Int32Array !== 'undefined') {
var b1 = new Int32Array([0, 1, 2, 3])
var b2 = new B(b1)
t.equal(b1.length, b2.length)
t.equal(b1[0], 0)
t.equal(b1[1], 1)
t.equal(b1[2], 2)
t.equal(b1[3], 3)
t.equal(b1[4], undefined)
}
t.end()
})
test('new buffer from Float32Array', function (t) {
if (typeof Float32Array !== 'undefined') {
var b1 = new Float32Array([0, 1, 2, 3])
var b2 = new B(b1)
t.equal(b1.length, b2.length)
t.equal(b1[0], 0)
t.equal(b1[1], 1)
t.equal(b1[2], 2)
t.equal(b1[3], 3)
t.equal(b1[4], undefined)
}
t.end()
})
test('new buffer from Float64Array', function (t) {
if (typeof Float64Array !== 'undefined') {
var b1 = new Float64Array([0, 1, 2, 3])
var b2 = new B(b1)
t.equal(b1.length, b2.length)
t.equal(b1[0], 0)
t.equal(b1[1], 1)
t.equal(b1[2], 2)
t.equal(b1[3], 3)
t.equal(b1[4], undefined)
}
t.end()
})
test('new buffer from buffer.toJSON() output', function (t) {
if (typeof JSON === 'undefined') {
// ie6, ie7 lack support
t.end()
return
}
var buf = new B('test')
var json = JSON.stringify(buf)
var obj = JSON.parse(json)
var copy = new B(obj)
t.ok(buf.equals(copy))
t.end()
})