Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix binding of +Infinity, -Infinity, -0, and extremely large number values #91

Merged
merged 4 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class DB {
value = value ? 1 : 0;
// fall through
case "number":
if (Math.floor(value) === value) {
if (Number.isSafeInteger(value) && !Object.is(value, -0)) {
jeremyBanks marked this conversation as resolved.
Show resolved Hide resolved
status = this._wasm.bind_int(stmt, i + 1, value);
} else {
status = this._wasm.bind_double(stmt, i + 1, value);
Expand Down
88 changes: 88 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,3 +786,91 @@ Deno.test("outputToObjectArrayEmpty", function () {
"Result is not an array or does not have the correct length",
);
});

Deno.test("veryLargeAndVerySmall", function () {
const db = new DB();

// return
jeremyBanks marked this conversation as resolved.
Show resolved Hide resolved

const [[selectedPositiveInfinity]] = db.query("SELECT +8e8888");
assertEquals(
selectedPositiveInfinity,
+Infinity,
"Failed to return correct value for +Infinity.",
);

const [[selectedNegativeInfinity]] = db.query("SELECT -8e8888");
assertEquals(
selectedNegativeInfinity,
-Infinity,
"Failed to return correct value for -Infinity.",
);

const [[selectedPositiveZero]] = db.query("SELECT +1/8e8888");
assert(
Object.is(selectedPositiveZero, +0),
"Failed to return correct value for +0.",
);

const [[selectedNegativeZero]] = db.query("SELECT -1/8e8888");
assert(
Object.is(selectedNegativeZero, -0),
"Failed to return correct value for -0.",
);

const [[selectedPositiveMassive]] = db.query("SELECT +20e20");
assertEquals(
selectedPositiveMassive,
+20e20,
"Failed to return correct value for +20e20.",
);

const [[selectedNegativeMassive]] = db.query("SELECT -20e20");
assertEquals(
selectedNegativeMassive,
-20e20,
"Failed to return correct value for -20e20.",
);

// bind
jeremyBanks marked this conversation as resolved.
Show resolved Hide resolved

const [[boundPositiveInfinity]] = db.query("SELECT ?", [+Infinity]);
jeremyBanks marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(
boundPositiveInfinity,
Infinity,
"Failed to bind correct value for +Infinity.",
);

const [[boundNegativeInfinity]] = db.query("SELECT ?", [-Infinity]);
assertEquals(
boundNegativeInfinity,
-Infinity,
"Failed to bind correct value for -Infinity.",
);

const [[boundPositiveZero]] = db.query("SELECT ?", [+0]);
jeremyBanks marked this conversation as resolved.
Show resolved Hide resolved
assert(
Object.is(boundPositiveZero, 0),
"Failed to bind correct value for +0.",
);

const [[boundNegativeZero]] = db.query("SELECT ?", [-0]);
assert(
Object.is(boundNegativeZero, -0),
"Failed to bind correct value for -0.",
);

const [[boundPositiveMassive]] = db.query("SELECT ?", [+20e20]);
jeremyBanks marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(
boundPositiveMassive,
+20e20,
"Failed to bind correct value for +20e20.",
);

const [[boundNegativeMassive]] = db.query("SELECT ?", [-20e20]);
assertEquals(
boundNegativeMassive,
-20e20,
"Failed to bind correct value for -20e20.",
);
});