forked from pilcrowonpaper/oslo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbytes.test.ts
34 lines (29 loc) · 959 Bytes
/
bytes.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
import { test, expect } from "vitest";
import {
binaryToInteger,
byteToBinary,
bytesToBinary,
bytesToInteger,
compareBytes
} from "./bytes.js";
test("bitsToInt()", () => {
expect(binaryToInteger("110100101000010101")).toBe(215573);
});
test("byteToBits()", () => {
expect(byteToBinary(101)).toBe("01100101");
});
test("bytesToBits()", () => {
expect(bytesToBinary(new Uint8Array([203, 3, 41, 76]))).toBe("11001011000000110010100101001100");
});
test("bytesToInteger()", () => {
const bytes = Uint8Array.from([54, 204, 4, 128]);
expect(bytesToInteger(bytes)).toBe(new DataView(bytes.buffer).getUint32(0));
});
test("compareBytes()", () => {
const randomBytes = new Uint8Array(32);
crypto.getRandomValues(randomBytes);
expect(compareBytes(randomBytes, randomBytes)).toBe(true);
const anotherRandomBytes = new Uint8Array(32);
crypto.getRandomValues(anotherRandomBytes);
expect(compareBytes(randomBytes, anotherRandomBytes)).toBe(false);
});