-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary.test.js
50 lines (46 loc) · 1.6 KB
/
binary.test.js
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
const { isBin, toBin } = require(`./binary`);
test(`isBin true`, () => {
expect(isBin(`0b101010`)).toBe(true);
expect(isBin(`0b1_0101_0`)).toBe(true);
expect(isBin(`0b1`)).toBe(true);
expect(isBin(`0B1`)).toBe(true);
expect(isBin(`0b0`)).toBe(true);
expect(isBin(`0B0`)).toBe(true);
expect(isBin(`0B00101010101001010101011001010010010100100001010010001001`)).toBe(true);
});
test(`isBin false`, () => {
expect(isBin(`0b`)).toBe(false);
expect(isBin(`0b1_`)).toBe(false);
expect(isBin(`0b0_`)).toBe(false);
expect(isBin(`0b_0_`)).toBe(false);
expect(isBin(`0b_1`)).toBe(false);
expect(isBin(`0b0_1010__111`)).toBe(false);
expect(isBin(`0b01_101010_`)).toBe(false);
expect(isBin(`0b1013010`)).toBe(false);
expect(isBin(`077`)).toBe(false);
expect(isBin(`000077`)).toBe(false);
expect(isBin(`0o77`)).toBe(false);
expect(isBin(`0000877`)).toBe(false);
expect(isBin(`000o77`)).toBe(false);
expect(isBin(`000oo77`)).toBe(false);
expect(isBin(`053256462753462`)).toBe(false);
expect(isBin(0o77)).toBe(undefined);
expect(isBin(123)).toBe(undefined);
});
test(`toBin return self`, () => {
expect(toBin(`077`)).toBe(`077`);
expect(toBin(`0o17`)).toBe(`0o17`);
expect(toBin(`0532`)).toBe(`0532`);
expect(toBin(`05325646`)).toBe(`05325646`);
expect(toBin(234)).toBe(234);
});
test(`toBin`, () => {
expect(toBin(`0b101010`)).toBe(`42`);
expect(toBin(`0b1`)).toBe(`1`);
expect(toBin(`0B1`)).toBe(`1`);
expect(toBin(`0b0`)).toBe(`0`);
expect(toBin(`0B0`)).toBe(`0`);
expect(
toBin(`0B00101010101001010101011001010010010100100001010010001001`)
).toBe(`12003739188728969`);
});