-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.test.ts
55 lines (50 loc) · 1.92 KB
/
util.test.ts
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
55
import {
assertEquals,
assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
import {
bigIntAbs,
countTrailingZeros,
extractExp,
getDecimals,
} from "./util.ts";
Deno.test("getDecimals() with string input", () => {
assertEquals(getDecimals("0.52135"), 5);
assertEquals(getDecimals("0.521135"), 6);
assertEquals(getDecimals("9846515.521135"), 6);
assertEquals(getDecimals("9846515.52113500"), 8);
assertEquals(getDecimals("9846515.0"), 1);
assertEquals(getDecimals("9846515"), 0);
assertEquals(getDecimals("22e6"), 0);
assertThrows(() => getDecimals("woiejoif23423"), Error, "InvalidNumber");
assertThrows(() => getDecimals("2.5.626"), Error, "InvalidNumber");
assertThrows(() => getDecimals("25e6e9"), Error, "InvalidNumber");
});
Deno.test("getDecimals() with number input", () => {
assertEquals(getDecimals(0.52135), 5);
assertEquals(getDecimals(0.521135), 6);
assertEquals(getDecimals(9846515.521135), 6);
assertEquals(getDecimals(9846515.52113500), 6);
assertEquals(getDecimals(9846515.521135006), 9);
assertEquals(getDecimals(9846515), 0);
});
Deno.test("extractExp()", () => {
assertEquals(extractExp("23"), ["23", 0]);
assertEquals(extractExp("23.5"), ["23.5", 0]);
assertEquals(extractExp("23e5"), ["23", 5]);
assertEquals(extractExp("23.6e5"), ["23.6", 5]);
assertThrows(() => extractExp("23.6e2e5"), Error, "InvalidNumber");
});
Deno.test("countTrailingZeros()", () => {
assertEquals(countTrailingZeros(0n, 100), 0);
assertEquals(countTrailingZeros(5213522323n, 100), 0);
assertEquals(countTrailingZeros(52113500000n, 100), 5);
assertEquals(countTrailingZeros(-9846515000000000000000n, 100), 15);
assertEquals(countTrailingZeros(-9846515000000000000000n, 10), 10);
});
Deno.test("bigIntAbs()", () => {
assertEquals(bigIntAbs(0n), 0n);
assertEquals(bigIntAbs(-0n), 0n);
assertEquals(bigIntAbs(698465465n), 698465465n);
assertEquals(bigIntAbs(-98498465n), 98498465n);
});