From 3049c6038a546f0f43ae0b078775790e744e4ab7 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 28 May 2014 21:25:23 +1000 Subject: [PATCH] add failing test for mixed signed input --- index.js | 8 ++++++-- test/basic.js | 8 ++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 92769c9..a4ba764 100644 --- a/index.js +++ b/index.js @@ -93,8 +93,12 @@ function Buffer (subject, encoding, noZero) { for (i = 0; i < length; i++) { if (Buffer.isBuffer(subject)) buf[i] = subject.readUInt8(i) - else - buf.writeInt8(subject[i], i) + else { + if (subject[i] < 0) + buf[i] = subject[i] + 256 + else + buf[i] = subject[i] + } } } else if (type === 'string') { buf.write(subject, 0, encoding) diff --git a/test/basic.js b/test/basic.js index 53994fd..a369c62 100644 --- a/test/basic.js +++ b/test/basic.js @@ -17,6 +17,14 @@ test('new buffer from array w/ negatives', function (t) { 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(), -- 2.34.1