diff --git a/lib/write-type.js b/lib/write-type.js index 94a10ab..ac221ea 100644 --- a/lib/write-type.js +++ b/lib/write-type.js @@ -52,29 +52,28 @@ function getWriteType(options) { } function number(encoder, value) { - var ivalue = value | 0; var type; - if (value !== ivalue) { + if (Math.floor(value) != value) { // float 64 -- 0xcb type = 0xcb; - token[type](encoder, value); - return; - } else if (-0x20 <= ivalue && ivalue <= 0x7F) { + } else if (-0x20 <= value && value <= 0x7F) { // positive fixint -- 0x00 - 0x7f // negative fixint -- 0xe0 - 0xff - type = ivalue & 0xFF; - } else if (0 <= ivalue) { + type = value & 0xFF; + } else if (0 <= value) { // uint 8 -- 0xcc // uint 16 -- 0xcd // uint 32 -- 0xce - type = (ivalue <= 0xFF) ? 0xcc : (ivalue <= 0xFFFF) ? 0xcd : 0xce; + // uint 64 -- 0xcf + type = (value <= 0xFF) ? 0xcc : (value <= 0xFFFF) ? 0xcd : (value <= 0xFFFFFFFF) ? 0xce : 0xcf; } else { // int 8 -- 0xd0 // int 16 -- 0xd1 // int 32 -- 0xd2 - type = (-0x80 <= ivalue) ? 0xd0 : (-0x8000 <= ivalue) ? 0xd1 : 0xd2; + // int 64 -- 0xd3 + type = (-0x80 <= value) ? 0xd0 : (-0x8000 <= value) ? 0xd1 : (-0x80000000 <= value) ? 0xd2 : 0xd3; } - token[type](encoder, ivalue); + token[type](encoder, value); } // uint 64 -- 0xcf diff --git a/test/10.encode.js b/test/10.encode.js index cc1bfbb..75373b4 100755 --- a/test/10.encode.js +++ b/test/10.encode.js @@ -127,7 +127,10 @@ function run_tests(codecopt) { it("cc-cf: uint 8/16/32/64", function() { assert.deepEqual(toArray(msgpack.encode(0xFF, options)), [0xcc, 0xFF]); assert.deepEqual(toArray(msgpack.encode(0xFFFF, options)), [0xcd, 0xFF, 0xFF]); - assert.deepEqual(toArray(msgpack.encode(0x7FFFFFFF, options)), [0xce, 0x7F, 0xFF, 0xFF, 0xFF]); + assert.deepEqual(toArray(msgpack.encode(0xFFFFFFFF, options)), [0xce, 0xFF, 0xFF, 0xFF, 0xFF]); + assert.deepEqual(toArray(msgpack.encode(0x8000000000000000, options)), [0xcf, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); + assert.notDeepEqual(toArray(msgpack.encode(0x8000000000000001, options)), [0xcf, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]); + assert.notDeepEqual(toArray(msgpack.encode(0xFFFFFFFFFFFFFFFF, options)), [0xcf, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]); }); // int 8 -- 0xd0 @@ -138,6 +141,7 @@ function run_tests(codecopt) { assert.deepEqual(toArray(msgpack.encode(-0x80, options)), [0xd0, 0x80]); assert.deepEqual(toArray(msgpack.encode(-0x8000, options)), [0xd1, 0x80, 0x00]); assert.deepEqual(toArray(msgpack.encode(-0x80000000, options)), [0xd2, 0x80, 0x00, 0x00, 0x00]); + assert.notDeepEqual(toArray(msgpack.encode(-0x80000001, options)), [0xd3, 0x80, 0x00, 0x00, 0x01]); }); // str 8 -- 0xd9