Skip to content

Commit 2b65e71

Browse files
added unit tests
1 parent 895ac9b commit 2b65e71

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

src/BitUtil.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ export class BitUtil {
299299
* @return true if the number is a positive power of 2, otherwise false.
300300
*/
301301
public static isPowerOfTwo(value: number): boolean {
302-
return value > 0 && (value & -value) === value;
302+
return value > 0 && (value & (~value + 1)) === value;
303303
}
304304

305305
/**
@@ -309,7 +309,7 @@ export class BitUtil {
309309
* @return true if the number is a positive power of 2, otherwise false.
310310
*/
311311
public static isPowerOfTwoBigInt(value: bigint): boolean {
312-
return value > BigInt(0) && (value & -value) === value;
312+
return value > 0n && (value & (~value + 1n)) === value;
313313
}
314314

315315
/**

tests/BitUtil.test.ts

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { BitUtil } from "../src/index";
2+
3+
describe("isEven", () => {
4+
test("check if number is even", () => {
5+
expect(BitUtil.isEven(0)).toBe(true);
6+
expect(BitUtil.isEven(1)).toBe(false);
7+
});
8+
});
9+
10+
describe("previous", () => {
11+
test("should produce current - 1 or max - 1 if 0", () => {
12+
expect(BitUtil.previous(0, 10)).toBe(9);
13+
expect(BitUtil.previous(1, 10)).toBe(0);
14+
expect(BitUtil.previous(2, 10)).toBe(1);
15+
});
16+
});
17+
18+
describe("next", () => {
19+
test("should produce current + 1 or 0 if max - 1", () => {
20+
expect(BitUtil.next(0, 10)).toBe(1);
21+
expect(BitUtil.next(1, 10)).toBe(2);
22+
expect(BitUtil.next(9, 10)).toBe(0);
23+
});
24+
});
25+
26+
describe("isPowerOfTwo", () => {
27+
test("should return true for positive powers of 2", () => {
28+
expect(BitUtil.isPowerOfTwo(1)).toBe(true);
29+
expect(BitUtil.isPowerOfTwo(2)).toBe(true);
30+
expect(BitUtil.isPowerOfTwo(4)).toBe(true);
31+
expect(BitUtil.isPowerOfTwo(8)).toBe(true);
32+
expect(BitUtil.isPowerOfTwo(16)).toBe(true);
33+
expect(BitUtil.isPowerOfTwo(32)).toBe(true);
34+
expect(BitUtil.isPowerOfTwo(64)).toBe(true);
35+
expect(BitUtil.isPowerOfTwo(128)).toBe(true);
36+
expect(BitUtil.isPowerOfTwo(256)).toBe(true);
37+
expect(BitUtil.isPowerOfTwo(512)).toBe(true);
38+
expect(BitUtil.isPowerOfTwo(1024)).toBe(true);
39+
});
40+
41+
test("should return false for non-positive numbers", () => {
42+
expect(BitUtil.isPowerOfTwo(0)).toBe(false);
43+
expect(BitUtil.isPowerOfTwo(-1)).toBe(false);
44+
expect(BitUtil.isPowerOfTwo(-2)).toBe(false);
45+
});
46+
47+
test("should return false for non-power of 2 numbers", () => {
48+
expect(BitUtil.isPowerOfTwo(3)).toBe(false);
49+
expect(BitUtil.isPowerOfTwo(5)).toBe(false);
50+
expect(BitUtil.isPowerOfTwo(6)).toBe(false);
51+
expect(BitUtil.isPowerOfTwo(7)).toBe(false);
52+
expect(BitUtil.isPowerOfTwo(9)).toBe(false);
53+
expect(BitUtil.isPowerOfTwo(10)).toBe(false);
54+
expect(BitUtil.isPowerOfTwo(12)).toBe(false);
55+
expect(BitUtil.isPowerOfTwo(15)).toBe(false);
56+
expect(BitUtil.isPowerOfTwo(18)).toBe(false);
57+
expect(BitUtil.isPowerOfTwo(20)).toBe(false);
58+
});
59+
});
60+
61+
describe("isPowerOfTwoBigInt", () => {
62+
test("should return true for positive powers of 2", () => {
63+
expect(BitUtil.isPowerOfTwoBigInt(1n)).toBe(true);
64+
expect(BitUtil.isPowerOfTwoBigInt(2n)).toBe(true);
65+
expect(BitUtil.isPowerOfTwoBigInt(4n)).toBe(true);
66+
expect(BitUtil.isPowerOfTwoBigInt(8n)).toBe(true);
67+
expect(BitUtil.isPowerOfTwoBigInt(16n)).toBe(true);
68+
expect(BitUtil.isPowerOfTwoBigInt(32n)).toBe(true);
69+
expect(BitUtil.isPowerOfTwoBigInt(64n)).toBe(true);
70+
expect(BitUtil.isPowerOfTwoBigInt(128n)).toBe(true);
71+
expect(BitUtil.isPowerOfTwoBigInt(256n)).toBe(true);
72+
expect(BitUtil.isPowerOfTwoBigInt(512n)).toBe(true);
73+
expect(BitUtil.isPowerOfTwoBigInt(1024n)).toBe(true);
74+
});
75+
76+
test("should return false for non-positive numbers", () => {
77+
expect(BitUtil.isPowerOfTwoBigInt(0n)).toBe(false);
78+
expect(BitUtil.isPowerOfTwoBigInt(-1n)).toBe(false);
79+
expect(BitUtil.isPowerOfTwoBigInt(-2n)).toBe(false);
80+
});
81+
82+
test("should return false for non-power of 2 numbers", () => {
83+
expect(BitUtil.isPowerOfTwoBigInt(3n)).toBe(false);
84+
expect(BitUtil.isPowerOfTwoBigInt(5n)).toBe(false);
85+
expect(BitUtil.isPowerOfTwoBigInt(6n)).toBe(false);
86+
expect(BitUtil.isPowerOfTwoBigInt(7n)).toBe(false);
87+
expect(BitUtil.isPowerOfTwoBigInt(9n)).toBe(false);
88+
expect(BitUtil.isPowerOfTwoBigInt(10n)).toBe(false);
89+
expect(BitUtil.isPowerOfTwoBigInt(12n)).toBe(false);
90+
expect(BitUtil.isPowerOfTwoBigInt(15n)).toBe(false);
91+
expect(BitUtil.isPowerOfTwoBigInt(18n)).toBe(false);
92+
expect(BitUtil.isPowerOfTwoBigInt(20n)).toBe(false);
93+
});
94+
});

0 commit comments

Comments
 (0)