-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from moozzyk/moozzyk/uint8array
Adding support for encoding Uint8Arrays using bin format family
- Loading branch information
Showing
4 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict' | ||
|
||
var Buffer = require('safe-buffer').Buffer | ||
var test = require('tape').test | ||
var msgpack = require('../') | ||
|
||
function build (size) { | ||
var array = [] | ||
var i | ||
|
||
for (i = 0; i < size; i++) { | ||
array.push(42) | ||
} | ||
|
||
return new Uint8Array(array) | ||
} | ||
|
||
test('encode/decode 2^8-1 Uint8Arrays', function (t) { | ||
var encoder = msgpack() | ||
var all = [] | ||
|
||
all.push(build(Math.pow(2, 8) - 1)) | ||
all.push(build(Math.pow(2, 6) + 1)) | ||
all.push(build(1)) | ||
all.push(new Uint8Array(0)) | ||
|
||
all.forEach(function (array) { | ||
t.test('encoding Uint8Array of length ' + array.byteLength + ' bytes', function (t) { | ||
var buf = encoder.encode(array) | ||
t.equal(buf.length, 2 + array.byteLength, 'must have the right length') | ||
t.equal(buf.readUInt8(0), 0xc4, 'must have the proper header') | ||
t.equal(buf.readUInt8(1), array.byteLength, 'must include the buf length') | ||
t.end() | ||
}) | ||
|
||
t.test('mirror test for an Uint8Array of length ' + array.byteLength + ' bytes', function (t) { | ||
t.deepEqual(encoder.decode(encoder.encode(array)), new Buffer(array), 'must stay the same') | ||
t.end() | ||
}) | ||
}) | ||
|
||
t.end() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict' | ||
|
||
var Buffer = require('safe-buffer').Buffer | ||
var test = require('tape').test | ||
var msgpack = require('../') | ||
|
||
function build (size) { | ||
var array = [] | ||
var i | ||
|
||
for (i = 0; i < size; i++) { | ||
array.push(42) | ||
} | ||
|
||
return new Uint8Array(array) | ||
} | ||
|
||
test('encode/decode 2^8-1 Uint8Arrays', function (t) { | ||
var encoder = msgpack() | ||
var all = [] | ||
|
||
all.push(build(Math.pow(2, 8))) | ||
all.push(build(Math.pow(2, 8) + 1)) | ||
all.push(build(Math.pow(2, 12) + 1)) | ||
all.push(build(Math.pow(2, 16) - 1)) | ||
|
||
all.forEach(function (array) { | ||
t.test('encoding Uint8Array of length ' + array.byteLength + ' bytes', function (t) { | ||
var buf = encoder.encode(array) | ||
t.equal(buf.length, 3 + array.byteLength, 'must have the right length') | ||
t.equal(buf.readUInt8(0), 0xc5, 'must have the proper header') | ||
t.equal(buf.readUInt16BE(1), array.byteLength, 'must include the buf length') | ||
t.end() | ||
}) | ||
|
||
t.test('mirror test for an Uint8Array of length ' + array.byteLength + ' bytes', function (t) { | ||
t.deepEqual(encoder.decode(encoder.encode(array)), new Buffer(array), 'must stay the same') | ||
t.end() | ||
}) | ||
}) | ||
|
||
t.end() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict' | ||
|
||
var Buffer = require('safe-buffer').Buffer | ||
var test = require('tape').test | ||
var msgpack = require('../') | ||
|
||
function build (size) { | ||
var array = [] | ||
var i | ||
|
||
for (i = 0; i < size; i++) { | ||
array.push(42) | ||
} | ||
|
||
return new Uint8Array(array) | ||
} | ||
|
||
test('encode/decode 2^8-1 Uint8Arrays', function (t) { | ||
var encoder = msgpack() | ||
var all = [] | ||
|
||
all.push(build(Math.pow(2, 16))) | ||
all.push(build(Math.pow(2, 16) + 1)) | ||
all.push(build(Math.pow(2, 18) + 1)) | ||
|
||
all.forEach(function (array) { | ||
t.test('encoding Uint8Array of length ' + array.byteLength + ' bytes', function (t) { | ||
var buf = encoder.encode(array) | ||
t.equal(buf.length, 5 + array.byteLength, 'must have the right length') | ||
t.equal(buf.readUInt8(0), 0xc6, 'must have the proper header') | ||
t.equal(buf.readUInt32BE(1), array.byteLength, 'must include the buf length') | ||
t.end() | ||
}) | ||
|
||
t.test('mirror test for an Uint8Array of length ' + array.byteLength + ' bytes', function (t) { | ||
t.deepEqual(encoder.decode(encoder.encode(array)), new Buffer(array), 'must stay the same') | ||
t.end() | ||
}) | ||
}) | ||
|
||
t.end() | ||
}) |