-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinutil.test.js
More file actions
58 lines (57 loc) · 1.96 KB
/
binutil.test.js
File metadata and controls
58 lines (57 loc) · 1.96 KB
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
56
57
58
import * as t from "https://deno.land/std/testing/asserts.ts";
import { bin2short, short2bin, i2bin, bin2i, bincat, eqbin, setbin, subbin, findbin } from "./binutil.js";
Deno.test("short bigendian", () => {
const b = new Uint8Array(10);
short2bin(b, 2, 1024);
t.assertEquals(bin2short(b, 2), 1024);
t.assertEquals(bin2short(b, 0), 0);
t.assertEquals(bin2short(b, 1), 1024 >> 8);
t.assertEquals(bin2short(b, 3), 1024 & 0xff);
});
Deno.test("short littleendian", () => {
const b = new Uint8Array(10);
short2bin(b, 2, 1024, true);
t.assertEquals(bin2short(b, 2, true), 1024);
t.assertEquals(bin2short(b, 0, true), 0);
t.assertEquals(bin2short(b, 1, true), 1024 & 0xff);
t.assertEquals(bin2short(b, 3, true), 1024 >> 8);
});
Deno.test("int", () => {
const b = new Uint8Array(10);
i2bin(b, 2, 123456);
t.assertEquals(bin2i(b, 2), 123456);
i2bin(b, 6, 123456, true);
t.assertEquals(bin2i(b, 6, true), 123456);
t.assert(b[2] != b[6]);
t.assert(b[2 + 3] == b[6]);
});
Deno.test("bincat", () => {
const a = new Uint8Array([1, 2, 3]);
const b = new Uint8Array([4, 5]);
const ab = bincat(a, b);
t.assertEquals(ab, new Uint8Array([1, 2, 3, 4, 5]));
});
Deno.test("eqbin", () => {
const a = new Uint8Array([1, 2, 3]);
const b = new Uint8Array([4, 5]);
t.assert(!eqbin(a, b));
t.assert(eqbin(a, a));
t.assert(eqbin(b, b));
t.assert(!eqbin(a, null));
});
Deno.test("setbin", () => {
const a = new Uint8Array([1, 2, 3]);
const b = new Uint8Array([4, 5]);
t.assertEquals(setbin(a, 1, b), new Uint8Array([1, 4, 5]));
});
Deno.test("subbin", () => {
const a = new Uint8Array([1, 2, 3, 4]);
t.assertEquals(subbin(a, 1), new Uint8Array([2, 3, 4]));
t.assertEquals(subbin(a, 1, 2), new Uint8Array([2, 3]));
t.assertEquals(subbin(a, 2, 2), new Uint8Array([3, 4]));
});
Deno.test("findbin", () => {
const a = new Uint8Array([1, 2, 3, 4]);
t.assertEquals(findbin(a, new Uint8Array([2, 3])), 1);
t.assertEquals(findbin(a, new Uint8Array([4, 5])), -1);
});