-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
138 lines (130 loc) · 2.58 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const test = require("ava");
const binarize = require("./binarize");
const unbinarize = require("./unbinarize");
test("binarize binary string", (t) => {
const input = [0, 1, 0, 1];
const { data, nbits } = binarize({ data: input });
t.is(data, "0101");
t.is(nbits, 1);
});
test("binarize 2-bit numbers string", (t) => {
const input = [0, 1, 2, 3];
const { data, nbits } = binarize({ data: input });
t.is(data, "00011011");
t.is(nbits, 2);
});
test("binarize 8-bit array", (t) => {
const input = [
36,
40,
161,
66,
231,
138,
9,
30,
60,
56,
177,
132,
5,
142,
26,
52,
72,
225,
228,
70,
17,
22,
40,
88,
178,
68,
197,
138,
18,
88,
120,
226,
6,
135,
146,
30,
];
const { data, nbits } = binarize({ data: input });
t.is(nbits, 8);
t.is(new Set(data).size, 2);
});
test("binarize Uint8Array", (t) => {
const input = Uint8Array.from([
36,
40,
161,
66,
231,
138,
9,
30,
60,
56,
177,
132,
5,
142,
26,
52,
72,
225,
228,
70,
17,
22,
40,
88,
178,
68,
197,
138,
18,
88,
120,
226,
6,
135,
146,
30,
]);
const { data, nbits } = binarize({ data: input, debug: false });
t.is(nbits, 8);
t.is(new Set(data).size, 2);
});
test("force binarize 2-bit numbers string to 8-bits", (t) => {
const input = [0, 1, 2, 3];
const { data, nbits } = binarize({ data: input, nbits: 8, sep: " " });
t.is(data, "00000000 00000001 00000010 00000011");
t.is(nbits, 8);
});
test("unbinarize 1-bit string", (t) => {
const data = "0101";
const nbits = 1;
const arr = unbinarize({ data, nbits });
t.deepEqual(arr, [0, 1, 0, 1]);
});
test("unbinarize 2-bit string", (t) => {
const data = "00011011";
const nbits = 2;
const arr = unbinarize({ data, nbits });
t.deepEqual(arr, [0, 1, 2, 3]);
});
test("throw error when try to unbinarize with nbits 0", (t) => {
let error;
try {
const data = "00011011";
const nbits = 0;
unbinarize({ data, nbits });
} catch (e) {
error = e;
}
t.is(error.message, "[fast-bin] nbits cannot be zero");
});