From: Daniel Cousens Date: Wed, 28 May 2014 11:25:23 +0000 (+1000) Subject: add failing test for mixed signed input X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=3049c6038a546f0f43ae0b078775790e744e4ab7;p=buffer.git add failing test for mixed signed input --- 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(),