Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
24 changes: 24 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import assert from "assert";
import fs from "fs";
import vm from "vm";
import Long from "../index.js";

var tests = [
Expand Down Expand Up @@ -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);
Expand Down