diff --git a/index.js b/index.js index 4983233..8aec365 100644 --- a/index.js +++ b/index.js @@ -983,7 +983,10 @@ LongPrototype.multiply = function multiply(multiplier) { if (multiplier.isZero()) return this.unsigned ? UZERO : ZERO; if (this.eq(MIN_VALUE)) return multiplier.isOdd() ? MIN_VALUE : ZERO; - if (multiplier.eq(MIN_VALUE)) return this.isOdd() ? MIN_VALUE : ZERO; + if (multiplier.eq(MIN_VALUE)) { + if (this.isOdd()) return this.unsigned ? MIN_VALUE.toUnsigned() : MIN_VALUE; + return this.unsigned ? UZERO : ZERO; + } if (this.isNegative()) { if (multiplier.isNegative()) return this.neg().mul(multiplier.neg()); diff --git a/tests/index.js b/tests/index.js index 3eab85e..e20f976 100644 --- a/tests/index.js +++ b/tests/index.js @@ -1,4 +1,6 @@ import assert from "assert"; +import fs from "fs"; +import vm from "vm"; import Long from "../index.js"; var tests = [ @@ -198,6 +200,28 @@ var tests = [ assert.strictEqual(x.unsigned, true); }, + function testUnsignedMinValueMultiplication() { + var context = { module: { exports: {} }, WebAssembly: undefined }; + context.exports = context.module.exports; + vm.runInNewContext( + fs.readFileSync(new URL("../umd/index.js", import.meta.url), "utf8"), + context, + ); + var implementations = [Long, context.module.exports]; + var expected = ["0", "9223372036854775808", "0", "9223372036854775808"]; + for (var i = 0; i < implementations.length; i++) { + var LongImpl = implementations[i]; + for (var n = 0; n < expected.length; n++) { + var product = LongImpl.fromInt(n, true).mul(LongImpl.MIN_VALUE); + assert.strictEqual(product.unsigned, true); + assert.strictEqual(product.toString(), expected[n]); + } + var signedProduct = LongImpl.ONE.mul(LongImpl.MIN_VALUE); + assert.strictEqual(signedProduct.unsigned, false); + assert.strictEqual(signedProduct.toString(), "-9223372036854775808"); + } + }, + function testRotateLeft() { var longVal = Long.fromBits(0x01234567, 0x89abcdef); var longValL = Long.fromBits(0x12345678, 0x9abcdef0);