var B = require('../').Buffer
var test = require('tape')
-var data = [
- '',
- ''
-]
+test('base64: ignore whitespace', function (t) {
+ var text = '\n YW9ldQ== '
+ var buf = new B(text, 'base64')
+ t.equal(buf.toString(), 'aoeu')
+ t.end()
+})
+
+test('base64: strings without padding', function (t) {
+ t.equal((new B('YW9ldQ', 'base64').toString()), 'aoeu')
+ t.end()
+})
-test('base64 strings with newlines / invalid charaters', function (t) {
- // newline in utf8 -- should not be an issue
+test('base64: newline in utf8 -- should not be an issue', function (t) {
t.equal(
new B('LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK', 'base64').toString('utf8'),
'---\ntitle: Three dashes marks the spot\ntags:\n'
)
+ t.end()
+})
- // newline in base64 -- should get stripped
+test('base64: newline in base64 -- should get stripped', function (t) {
t.equal(
new B('LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK\nICAtIHlhbWwKICAtIGZyb250LW1hdHRlcgogIC0gZGFzaGVzCmV4cGFuZWQt', 'base64').toString('utf8'),
'---\ntitle: Three dashes marks the spot\ntags:\n - yaml\n - front-matter\n - dashes\nexpaned-'
)
+ t.end()
+})
- // tab characters in base64 - should get stripped
+test('base64: tab characters in base64 - should get stripped', function (t) {
t.equal(
new B('LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK\t\t\t\tICAtIHlhbWwKICAtIGZyb250LW1hdHRlcgogIC0gZGFzaGVzCmV4cGFuZWQt', 'base64').toString('utf8'),
'---\ntitle: Three dashes marks the spot\ntags:\n - yaml\n - front-matter\n - dashes\nexpaned-'
)
t.end()
})
+
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'
- )
+test('indexes from a string', function(t) {
+ var buf = new B('abc')
+ t.equal(buf[0], 97)
+ t.equal(buf[1], 98)
+ t.equal(buf[2], 99)
t.end()
})
-test('new buffer from array w/ negatives', function (t) {
- t.equal(
- new B([-1, -2, -3]).toString('hex'),
- 'fffefd'
- )
+test('indexes from an array', function(t) {
+ var buf = new B([ 97, 98, 99 ])
+ t.equal(buf[0], 97)
+ t.equal(buf[1], 98)
+ t.equal(buf[2], 99)
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 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('buffer toArrayBuffer()', function (t) {
- var data = [1, 2, 3, 4, 5, 6, 7, 8]
- if (typeof Uint8Array !== 'undefined') {
- var result = new B(data).toArrayBuffer()
- var expected = new Uint8Array(data).buffer
- for (var i = 0; i < expected.byteLength; i++) {
- t.equal(result[i], expected[i])
- }
- } else {
- t.pass('No toArrayBuffer() method provided in old browsers')
- }
- t.end()
-})
-
-test('buffer toJSON()', function (t) {
- var data = [1, 2, 3, 4]
- t.deepEqual(
- new B(data).toJSON(),
- { type: 'Buffer', data: [1,2,3,4] }
- )
- 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()
-})
-
-test('buffer copy example', function (t) {
- var buf1 = new B(26)
- var buf2 = new B(26)
-
- for (var i = 0 ; i < 26 ; i++) {
- buf1[i] = i + 97; // 97 is ASCII a
- buf2[i] = 33; // ASCII !
- }
-
- buf1.copy(buf2, 8, 16, 20)
+test('setting index value should modify buffer contents', function(t) {
+ var buf = new B([ 97, 98, 99 ])
+ t.equal(buf[2], 99)
+ t.equal(buf.toString(), 'abc')
- t.equal(
- buf2.toString('ascii', 0, 25),
- '!!!!!!!!qrst!!!!!!!!!!!!!'
- )
+ buf[2] += 10
+ t.equal(buf[2], 109)
+ t.equal(buf.toString(), 'abm')
t.end()
})
var B = require('../').Buffer
var test = require('tape')
-test('compare', function (t) {
+test('buffer.compare', function (t) {
var b = new B(1).fill('a')
var c = new B(1).fill('c')
var d = new B(2).fill('aa')
t.end()
})
-test('compare argument validation', function (t) {
- var b = new B(1).fill('a')
- var c = new B(1).fill('c')
- var d = new B(2).fill('aa')
-
+test('buffer.compare argument validation', function (t) {
t.throws(function () {
var b = new B(1)
B.compare(b, 'abc')
t.end()
})
-test('equals', function (t) {
+test('buffer.equals', function (t) {
var b = new B(5).fill('abcdf')
var c = new B(5).fill('abcdf')
var d = new B(5).fill('abcde')
t.end()
})
-test('equals argument validation', function (t) {
- var b = new B(5).fill('abcdf')
- var c = new B(5).fill('abcdf')
- var d = new B(5).fill('abcde')
- var e = new B(6).fill('abcdef')
-
+test('buffer.equals argument validation', function (t) {
t.throws(function () {
var b = new B(1)
b.equals('abc')
--- /dev/null
+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 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()
+})
+
+++ /dev/null
-var B = require('../').Buffer
-var test = require('tape')
-
-test('indexes from a string', function(t) {
- var buf = new B('abc')
- t.equal(buf[0], 97)
- t.equal(buf[1], 98)
- t.equal(buf[2], 99)
- t.end()
-})
-
-test('indexes from an array', function(t) {
- var buf = new B([ 97, 98, 99 ])
- t.equal(buf[0], 97)
- t.equal(buf[1], 98)
- t.equal(buf[2], 99)
- t.end()
-})
-
-test('set then modify indexes from an array', function(t) {
- var buf = new B([ 97, 98, 99 ])
- t.equal(buf[2], 99)
- t.equal(buf.toString(), 'abc')
-
- buf[2] += 10
- t.equal(buf[2], 109)
- t.equal(buf.toString(), 'abm')
- t.end()
-})
+++ /dev/null
-var B = require('../').Buffer
-var test = require('tape')
-
-test('Buffer.isBuffer', function (t) {
- t.equal(B.isBuffer(new B('hey', 'utf8')), true)
- t.equal(B.isBuffer(new B([1, 2, 3], 'utf8')), true)
- t.equal(B.isBuffer('hey'), false)
- t.end()
-})
+++ /dev/null
-var B = require('../').Buffer
-var test = require('tape')
-
-test('Buffer.isEncoding', function (t) {
- t.equal(B.isEncoding('HEX'), true)
- t.equal(B.isEncoding('hex'), true)
- t.equal(B.isEncoding('bad'), false)
- t.end()
-})
var B = require('../').Buffer
var test = require('tape')
-test('utf8 buffer to base64', function (t) {
- t.equal(
- new B('Ձאab', 'utf8').toString('base64'),
- '1YHXkGFi'
- )
- t.end()
-})
-
-test('utf8 buffer to hex', function (t) {
- t.equal(
- new B('Ձאab', 'utf8').toString('hex'),
- 'd581d7906162'
- )
- t.end()
-})
-
-test('utf8 to utf8', function (t) {
- t.equal(
- new B('öäüõÖÄÜÕ', 'utf8').toString('utf8'),
- 'öäüõÖÄÜÕ'
+test('buffer.toJSON', function (t) {
+ var data = [1, 2, 3, 4]
+ t.deepEqual(
+ new B(data).toJSON(),
+ { type: 'Buffer', data: [1,2,3,4] }
)
t.end()
})
-test('utf16le to utf16', function (t) {
- t.equal(
- new B(new B('abcd', 'utf8').toString('utf16le'), 'utf16le').toString('utf8'),
- 'abcd'
- )
- t.end()
-})
-
-test('utf16le to hex', function (t) {
- t.equal(
- new B('abcd', 'utf16le').toString('hex'),
- '6100620063006400'
- )
- t.end()
-})
-
-test('ascii buffer to base64', function (t) {
- t.equal(
- new B('123456!@#$%^', 'ascii').toString('base64'),
- 'MTIzNDU2IUAjJCVe'
- )
- t.end()
-})
-
-test('ascii buffer to hex', function (t) {
- t.equal(
- new B('123456!@#$%^', 'ascii').toString('hex'),
- '31323334353621402324255e'
- )
- t.end()
-})
-
-test('base64 buffer to utf8', function (t) {
- t.equal(
- new B('1YHXkGFi', 'base64').toString('utf8'),
- 'Ձאab'
- )
- t.end()
-})
+test('buffer.copy', function (t) {
+ // copied from nodejs.org example
+ var buf1 = new B(26)
+ var buf2 = new B(26)
-test('hex buffer to utf8', function (t) {
- t.equal(
- new B('d581d7906162', 'hex').toString('utf8'),
- 'Ձאab'
- )
- t.end()
-})
-
-test('base64 buffer to ascii', function (t) {
- t.equal(
- new B('MTIzNDU2IUAjJCVe', 'base64').toString('ascii'),
- '123456!@#$%^'
- )
- t.end()
-})
-
-test('hex buffer to ascii', function (t) {
- t.equal(
- new B('31323334353621402324255e', 'hex').toString('ascii'),
- '123456!@#$%^'
- )
- t.end()
-})
-
-test('base64 buffer to binary', function (t) {
- t.equal(
- new B('MTIzNDU2IUAjJCVe', 'base64').toString('binary'),
- '123456!@#$%^'
- )
- t.end()
-})
+ for (var i = 0 ; i < 26 ; i++) {
+ buf1[i] = i + 97; // 97 is ASCII a
+ buf2[i] = 33; // ASCII !
+ }
-test('hex buffer to binary', function (t) {
- t.equal(
- new B('31323334353621402324255e', 'hex').toString('binary'),
- '123456!@#$%^'
- )
- t.end()
-})
+ buf1.copy(buf2, 8, 16, 20)
-test('utf8 to binary', function (t) {
t.equal(
- new B('öäüõÖÄÜÕ', 'utf8').toString('binary'),
- 'öäüõÃ\96Ã\84Ã\9cÃ\95'
+ buf2.toString('ascii', 0, 25),
+ '!!!!!!!!qrst!!!!!!!!!!!!!'
)
t.end()
})
t.end()
})
-test('base64 ignore whitespace', function (t) {
- var text = '\n YW9ldQ== '
- var buf = new B(text, 'base64')
- t.equal(buf.toString(), 'aoeu')
- t.end()
-})
-
test('buffer.slice sets indexes', function (t) {
t.equal((new B('hallo')).slice(0, 5).toString(), 'hallo')
t.end()
t.equal((new B('hallo')).slice(10, 2).toString(), '')
t.end()
})
-
-test('base64 strings without padding', function (t) {
- t.equal((new B('YW9ldQ', 'base64').toString()), 'aoeu')
- t.end()
-})
--- /dev/null
+var B = require('../').Buffer
+var test = require('tape')
+
+test('Buffer.isEncoding', function (t) {
+ t.equal(B.isEncoding('HEX'), true)
+ t.equal(B.isEncoding('hex'), true)
+ t.equal(B.isEncoding('bad'), false)
+ t.end()
+})
+
+test('Buffer.isBuffer', function (t) {
+ t.equal(B.isBuffer(new B('hey', 'utf8')), true)
+ t.equal(B.isBuffer(new B([1, 2, 3], 'utf8')), true)
+ t.equal(B.isBuffer('hey'), false)
+ t.end()
+})
+
+test('Buffer.toArrayBuffer', function (t) {
+ var data = [1, 2, 3, 4, 5, 6, 7, 8]
+ if (typeof Uint8Array !== 'undefined') {
+ var result = new B(data).toArrayBuffer()
+ var expected = new Uint8Array(data).buffer
+ for (var i = 0; i < expected.byteLength; i++) {
+ t.equal(result[i], expected[i])
+ }
+ } else {
+ t.pass('No toArrayBuffer() method provided in old browsers')
+ }
+ t.end()
+})
--- /dev/null
+var B = require('../').Buffer
+var test = require('tape')
+
+test('utf8 buffer to base64', function (t) {
+ t.equal(
+ new B('Ձאab', 'utf8').toString('base64'),
+ '1YHXkGFi'
+ )
+ t.end()
+})
+
+test('utf8 buffer to hex', function (t) {
+ t.equal(
+ new B('Ձאab', 'utf8').toString('hex'),
+ 'd581d7906162'
+ )
+ t.end()
+})
+
+test('utf8 to utf8', function (t) {
+ t.equal(
+ new B('öäüõÖÄÜÕ', 'utf8').toString('utf8'),
+ 'öäüõÖÄÜÕ'
+ )
+ t.end()
+})
+
+test('utf16le to utf16', function (t) {
+ t.equal(
+ new B(new B('abcd', 'utf8').toString('utf16le'), 'utf16le').toString('utf8'),
+ 'abcd'
+ )
+ t.end()
+})
+
+test('utf16le to hex', function (t) {
+ t.equal(
+ new B('abcd', 'utf16le').toString('hex'),
+ '6100620063006400'
+ )
+ t.end()
+})
+
+test('ascii buffer to base64', function (t) {
+ t.equal(
+ new B('123456!@#$%^', 'ascii').toString('base64'),
+ 'MTIzNDU2IUAjJCVe'
+ )
+ t.end()
+})
+
+test('ascii buffer to hex', function (t) {
+ t.equal(
+ new B('123456!@#$%^', 'ascii').toString('hex'),
+ '31323334353621402324255e'
+ )
+ t.end()
+})
+
+test('base64 buffer to utf8', function (t) {
+ t.equal(
+ new B('1YHXkGFi', 'base64').toString('utf8'),
+ 'Ձאab'
+ )
+ t.end()
+})
+
+test('hex buffer to utf8', function (t) {
+ t.equal(
+ new B('d581d7906162', 'hex').toString('utf8'),
+ 'Ձאab'
+ )
+ t.end()
+})
+
+test('base64 buffer to ascii', function (t) {
+ t.equal(
+ new B('MTIzNDU2IUAjJCVe', 'base64').toString('ascii'),
+ '123456!@#$%^'
+ )
+ t.end()
+})
+
+test('hex buffer to ascii', function (t) {
+ t.equal(
+ new B('31323334353621402324255e', 'hex').toString('ascii'),
+ '123456!@#$%^'
+ )
+ t.end()
+})
+
+test('base64 buffer to binary', function (t) {
+ t.equal(
+ new B('MTIzNDU2IUAjJCVe', 'base64').toString('binary'),
+ '123456!@#$%^'
+ )
+ t.end()
+})
+
+test('hex buffer to binary', function (t) {
+ t.equal(
+ new B('31323334353621402324255e', 'hex').toString('binary'),
+ '123456!@#$%^'
+ )
+ t.end()
+})
+
+test('utf8 to binary', function (t) {
+ t.equal(
+ new B('öäüõÖÄÜÕ', 'utf8').toString('binary'),
+ 'öäüõÃ\96Ã\84Ã\9cÃ\95'
+ )
+ t.end()
+})