2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-07-13 07:04:30 +00:00
|
|
|
import { assertAlmostEquals, AssertionError, assertThrows } from "./mod.ts";
|
|
|
|
|
2023-12-08 16:04:29 +00:00
|
|
|
Deno.test("assertAlmostEquals() matches values within default precision range", () => {
|
2023-07-13 07:04:30 +00:00
|
|
|
assertAlmostEquals(-0, +0);
|
|
|
|
assertAlmostEquals(Math.PI, Math.PI);
|
|
|
|
assertAlmostEquals(0.1 + 0.2, 0.3);
|
|
|
|
assertAlmostEquals(NaN, NaN);
|
|
|
|
assertAlmostEquals(Number.NaN, Number.NaN);
|
2024-06-03 06:55:39 +00:00
|
|
|
assertAlmostEquals(9e20, 9.0000000001e20);
|
2024-06-10 11:09:27 +00:00
|
|
|
assertAlmostEquals(-9e20, -9.0000000001e20);
|
2024-06-03 06:55:39 +00:00
|
|
|
assertAlmostEquals(1.000000001e-8, 1.000000002e-8);
|
2024-06-10 11:09:27 +00:00
|
|
|
assertAlmostEquals(-1.000000001e-8, -1.000000002e-8);
|
2023-12-08 16:04:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("assertAlmostEquals() throws values outside default precision range", () => {
|
2023-07-13 07:04:30 +00:00
|
|
|
assertThrows(() => assertAlmostEquals(1, 2));
|
|
|
|
assertThrows(() => assertAlmostEquals(1, 1.1));
|
2024-06-03 06:55:39 +00:00
|
|
|
assertThrows(() => assertAlmostEquals(9e20, 9.01e20));
|
|
|
|
assertThrows(() => assertAlmostEquals(5e-7, 6e-7)); // approx 20% different value
|
|
|
|
assertThrows(() => assertAlmostEquals(1e-8, 1e-9)); // different order of magnitude
|
2023-12-08 16:04:29 +00:00
|
|
|
});
|
2023-07-13 07:04:30 +00:00
|
|
|
|
2023-12-08 16:04:29 +00:00
|
|
|
Deno.test("assertAlmostEquals() matches values within higher precision range", () => {
|
2023-07-13 07:04:30 +00:00
|
|
|
assertAlmostEquals(0.1 + 0.2, 0.3, 1e-16);
|
2023-12-08 16:04:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("assertAlmostEquals() throws values outside higher precision range", () => {
|
2023-07-13 07:04:30 +00:00
|
|
|
assertThrows(
|
|
|
|
() => assertAlmostEquals(0.1 + 0.2, 0.3, 1e-17),
|
|
|
|
AssertionError,
|
|
|
|
`Expected actual: "${
|
|
|
|
(
|
|
|
|
0.1 + 0.2
|
|
|
|
).toExponential()
|
|
|
|
}" to be close to "${(0.3).toExponential()}"`,
|
|
|
|
);
|
2023-12-08 16:04:29 +00:00
|
|
|
});
|
2023-07-13 07:04:30 +00:00
|
|
|
|
2023-12-08 16:04:29 +00:00
|
|
|
Deno.test("assertAlmostEquals() matches infinity with inifinity", () => {
|
2023-07-13 07:04:30 +00:00
|
|
|
assertAlmostEquals(Infinity, Infinity);
|
2023-12-08 16:04:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("assertAlmostEquals() throws when special numbers do not match", () => {
|
2023-07-13 07:04:30 +00:00
|
|
|
assertThrows(
|
|
|
|
() => assertAlmostEquals(0, Infinity),
|
|
|
|
AssertionError,
|
|
|
|
'Expected actual: "0" to be close to "Infinity"',
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => assertAlmostEquals(-Infinity, +Infinity),
|
|
|
|
AssertionError,
|
|
|
|
'Expected actual: "-Infinity" to be close to "Infinity"',
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => assertAlmostEquals(Infinity, NaN),
|
|
|
|
AssertionError,
|
|
|
|
'Expected actual: "Infinity" to be close to "NaN"',
|
|
|
|
);
|
|
|
|
});
|
2024-05-07 00:08:16 +00:00
|
|
|
|
|
|
|
Deno.test("assertAlmostEquals() throws with custom message", () => {
|
|
|
|
assertThrows(
|
|
|
|
() => assertAlmostEquals(-Infinity, +Infinity, 1e-17, "CUSTOM MESSAGE"),
|
|
|
|
AssertionError,
|
|
|
|
`Expected actual: "-Infinity" to be close to "Infinity": delta "Infinity" is greater than "1e-17": CUSTOM MESSAGE`,
|
|
|
|
);
|
|
|
|
});
|