deno.land / std@0.224.0 / assert / assert_almost_equals_test.ts

assert_almost_equals_test.ts
View Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.import { assertAlmostEquals, AssertionError, assertThrows } from "./mod.ts";
Deno.test("assertAlmostEquals() matches values within default precision range", () => { assertAlmostEquals(-0, +0); assertAlmostEquals(Math.PI, Math.PI); assertAlmostEquals(0.1 + 0.2, 0.3); assertAlmostEquals(NaN, NaN); assertAlmostEquals(Number.NaN, Number.NaN);});
Deno.test("assertAlmostEquals() throws values outside default precision range", () => { assertThrows(() => assertAlmostEquals(1, 2)); assertThrows(() => assertAlmostEquals(1, 1.1));});
Deno.test("assertAlmostEquals() matches values within higher precision range", () => { assertAlmostEquals(0.1 + 0.2, 0.3, 1e-16);});
Deno.test("assertAlmostEquals() throws values outside higher precision range", () => { 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()}"`, );});
Deno.test("assertAlmostEquals() matches infinity with inifinity", () => { assertAlmostEquals(Infinity, Infinity);});
Deno.test("assertAlmostEquals() throws when special numbers do not match", () => { 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"', );});
std

Version Info

Tagged at
6 months ago