1
|
|
|
|
2
|
|
|
var assert = require('assert'); |
3
|
|
|
|
4
|
|
|
describe('extras', function() { |
5
|
|
|
|
6
|
|
|
let byteData = require('../index.js'); |
7
|
|
|
|
8
|
|
|
// packNibbles |
9
|
|
|
it('should pack two nibbles as one byte', function() { |
10
|
|
|
packed = byteData.packNibbles([1,4]) |
11
|
|
|
assert.deepEqual(packed, [20]); |
12
|
|
|
}); |
13
|
|
|
it('should pack two nibbles as one byte', function() { |
14
|
|
|
packed = byteData.packNibbles([1,15]) |
15
|
|
|
assert.deepEqual(packed, [31]); |
16
|
|
|
}); |
17
|
|
|
it('should pack two nibbles as one byte', function() { |
18
|
|
|
packed = byteData.packNibbles([15,15]) |
19
|
|
|
assert.deepEqual(packed, [255]); |
20
|
|
|
}); |
21
|
|
|
it('should pack a stream of nibbles into a stream of bytes', function() { |
22
|
|
|
packed = byteData.packNibbles([15,15,1,4,1,15]) |
23
|
|
|
assert.deepEqual(packed, [255,20,31]); |
24
|
|
|
}); |
25
|
|
|
it('packed should be half the size of the original', function() { |
26
|
|
|
let original = [15,15,1,4,1,15]; |
27
|
|
|
packed = byteData.packNibbles(original) |
28
|
|
|
assert.deepEqual(packed.length, original.length / 2); |
29
|
|
|
}); |
30
|
|
|
it('should pad when packing a uneven number of nibbles', function() { |
31
|
|
|
let original = [15,15,1,4,1,15,1]; |
32
|
|
|
packed = byteData.packNibbles(original) |
33
|
|
|
assert.deepEqual(packed, [255,20,31,16]); // 1 => 0001 => 00010000 = 16 |
34
|
|
|
}); |
35
|
|
|
|
36
|
|
|
// unpackNibbles |
37
|
|
|
it('should unpack one nibbles as two byte', function() { |
38
|
|
|
unpacked = byteData.unpackNibbles([20]) |
39
|
|
|
assert.deepEqual(unpacked, [1,4]); |
40
|
|
|
}); |
41
|
|
|
it('should unpack one nibbles as two byte', function() { |
42
|
|
|
unpacked = byteData.unpackNibbles([31]) |
43
|
|
|
assert.deepEqual(unpacked, [1,15]); |
44
|
|
|
}); |
45
|
|
|
it('should unpack one nibbles as two byte', function() { |
46
|
|
|
unpacked = byteData.unpackNibbles([255]) |
47
|
|
|
assert.deepEqual(unpacked, [15,15]); |
48
|
|
|
}); |
49
|
|
|
it('should pack a stream of bytes into a stream of nibbles', function() { |
50
|
|
|
unpacked = byteData.unpackNibbles([255,20,31]) |
51
|
|
|
assert.deepEqual(unpacked, [15,15,1,4,1,15]); |
52
|
|
|
}); |
53
|
|
|
it('should read a padding zero when unpacking a uneven number of nibbles', function() { |
54
|
|
|
unpacked = byteData.unpackNibbles([255,20,31,16]) |
55
|
|
|
assert.deepEqual(unpacked, [15,15,1,4,1,15,1,0]); |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
// packCrumbs |
59
|
|
|
it('should pack 4 crumbs as 1 byte', function() { |
60
|
|
|
packed = byteData.packCrumbs([1,2,0,3]) |
61
|
|
|
assert.deepEqual(packed, [99]); |
62
|
|
|
}); |
63
|
|
|
it('should pack 4 crumbs as 1 byte', function() { |
64
|
|
|
packed = byteData.packCrumbs([1,2,3,0]) |
65
|
|
|
assert.deepEqual(packed, [108]); |
66
|
|
|
}); |
67
|
|
|
it('should pack 4 crumbs as 1 byte (0s)', function() { |
68
|
|
|
packed = byteData.packCrumbs([0,0,0,0]) |
69
|
|
|
assert.deepEqual(packed, [0]); |
70
|
|
|
}); |
71
|
|
|
it('should pack 4 crumbs as 1 byte (3s)', function() { |
72
|
|
|
packed = byteData.packCrumbs([3,3,3,3]) |
73
|
|
|
assert.deepEqual(packed, [255]); |
74
|
|
|
}); |
75
|
|
|
it('should pad when packing a uneven number of crumbs 3', function() { |
76
|
|
|
let original = [1,1,1]; |
77
|
|
|
packed = byteData.packCrumbs(original) |
78
|
|
|
assert.deepEqual(packed, [84]); |
79
|
|
|
}); |
80
|
|
|
it('should pad when packing a uneven number of crumbs 2', function() { |
81
|
|
|
let original = [1,1]; |
82
|
|
|
packed = byteData.packCrumbs(original) |
83
|
|
|
assert.deepEqual(packed, [80]); |
84
|
|
|
}); |
85
|
|
|
it('should pad when packing a uneven number of crumbs', function() { |
86
|
|
|
let original = [1]; |
87
|
|
|
packed = byteData.packCrumbs(original) |
88
|
|
|
assert.deepEqual(packed, [64]); |
89
|
|
|
}); |
90
|
|
|
it('should pad when packing a uneven number of crumbs', function() { |
91
|
|
|
let original = [0,0,0,0,1]; |
92
|
|
|
packed = byteData.packCrumbs(original) |
93
|
|
|
assert.deepEqual(packed, [0,64]); |
94
|
|
|
}); |
95
|
|
|
it('should pad when packing a uneven number of crumbs', function() { |
96
|
|
|
let original = [0,0,0,1,1]; |
97
|
|
|
packed = byteData.packCrumbs(original) |
98
|
|
|
assert.deepEqual(packed, [1,64]); |
99
|
|
|
}); |
100
|
|
|
it('should pad when packing a uneven number of crumbs', function() { |
101
|
|
|
let original = [1,1,1,0]; |
102
|
|
|
packed = byteData.packCrumbs(original) |
103
|
|
|
assert.deepEqual(packed, [84]); |
104
|
|
|
}); |
105
|
|
|
it('should pad when packing a uneven number of crumbs', function() { |
106
|
|
|
let original = [1,1,0,0]; |
107
|
|
|
packed = byteData.packCrumbs(original) |
108
|
|
|
assert.deepEqual(packed, [80]); |
109
|
|
|
}); |
110
|
|
|
it('should pad when packing a uneven number of crumbs', function() { |
111
|
|
|
let original = [1,0,0,0]; |
112
|
|
|
packed = byteData.packCrumbs(original) |
113
|
|
|
assert.deepEqual(packed, [64]); |
114
|
|
|
}); |
115
|
|
|
|
116
|
|
|
// unpackCrumbs |
117
|
|
|
it('should unpack one byte as 4 crumbs (3,3,3,3)', function() { |
118
|
|
|
unpacked = byteData.unpackCrumbs([255]) |
119
|
|
|
assert.deepEqual(unpacked, [3,3,3,3]); |
120
|
|
|
}); |
121
|
|
|
it('should unpack one byte as 4 crumbs (1,1,0,0)', function() { |
122
|
|
|
unpacked = byteData.unpackCrumbs([80]) |
123
|
|
|
assert.deepEqual(unpacked, [1,1,0,0]); |
124
|
|
|
}); |
125
|
|
|
it('should unpack one byte as 4 crumbs (1,2,0,3)', function() { |
126
|
|
|
unpacked = byteData.unpackCrumbs([99]) |
127
|
|
|
assert.deepEqual(unpacked, [1,2,0,3]); |
128
|
|
|
}); |
129
|
|
|
it('should unpack 4 crumbs as 1 byte (1,2,3,0)', function() { |
130
|
|
|
unpacked = byteData.unpackCrumbs([108]) |
131
|
|
|
assert.deepEqual(unpacked, [1,2,3,0]); |
132
|
|
|
}); |
133
|
|
|
it('should pack a stream of bytes into a stream of crumbs', function() { |
134
|
|
|
unpacked = byteData.unpackCrumbs([255,108,80]) |
135
|
|
|
assert.deepEqual(unpacked, [3,3,3,3,1,2,3,0,1,1,0,0]); |
136
|
|
|
}); |
137
|
|
|
|
138
|
|
|
// packBooleans |
139
|
|
|
it('should pack 8 booleans as 1 byte', function() { |
140
|
|
|
packed = byteData.packBooleans([0,0,0,0,0,0,0,0]) |
141
|
|
|
assert.deepEqual(packed, [0]); |
142
|
|
|
}); |
143
|
|
|
it('should pack 8 booleans as 1 byte', function() { |
144
|
|
|
packed = byteData.packBooleans([1,1,1,1,1,1,1,1]) |
145
|
|
|
assert.deepEqual(packed, [255]); |
146
|
|
|
}); |
147
|
|
|
it('should pack 8 booleans as 1 byte (0s)', function() { |
148
|
|
|
packed = byteData.packBooleans([0,1,0,0,1,1,0,1]) |
149
|
|
|
assert.deepEqual(packed, [77]); |
150
|
|
|
}); |
151
|
|
|
it('should pack 8 booleans as 1 byte (0s)', function() { |
152
|
|
|
packed = byteData.packBooleans([0,1,0,0,1,1,0,0]) |
153
|
|
|
assert.deepEqual(packed, [76]); |
154
|
|
|
}); |
155
|
|
|
it('should pad when packing less than 8 booleans', function() { |
156
|
|
|
packed = byteData.packBooleans([0,1,0,0,1,1,0]) |
157
|
|
|
assert.deepEqual(packed, [76]); |
158
|
|
|
}); |
159
|
|
|
it('should pad when packing less than 8 booleans', function() { |
160
|
|
|
packed = byteData.packBooleans([0,0,0,0,0,0,0,0,0,1,0,0,1,1,0]) |
161
|
|
|
assert.deepEqual(packed, [0,76]); |
162
|
|
|
}); |
163
|
|
|
|
164
|
|
|
// unpack booleans |
165
|
|
|
it('should pack 8 booleans as 1 byte', function() { |
166
|
|
|
unpacked = byteData.unpackBooleans([0]) |
167
|
|
|
assert.deepEqual(unpacked, [0,0,0,0,0,0,0,0]); |
168
|
|
|
}); |
169
|
|
|
it('should pack 8 booleans as 1 byte', function() { |
170
|
|
|
unpacked = byteData.unpackBooleans([255]) |
171
|
|
|
assert.deepEqual(unpacked, [1,1,1,1,1,1,1,1]); |
172
|
|
|
}); |
173
|
|
|
it('should pack 8 booleans as 1 byte (0s)', function() { |
174
|
|
|
unpacked = byteData.unpackBooleans([77]) |
175
|
|
|
assert.deepEqual(unpacked, [0,1,0,0,1,1,0,1]); |
176
|
|
|
}); |
177
|
|
|
it('should pack 8 booleans as 1 byte (0s)', function() { |
178
|
|
|
unpacked = byteData.unpackBooleans([76]) |
179
|
|
|
assert.deepEqual(unpacked, [0,1,0,0,1,1,0,0]); |
180
|
|
|
}); |
181
|
|
|
it('should pad when packing less than 8 booleans', function() { |
182
|
|
|
unpacked = byteData.unpackBooleans([76]) |
183
|
|
|
assert.deepEqual(unpacked, [0,1,0,0,1,1,0,0]); |
184
|
|
|
}); |
185
|
|
|
it('should pad when packing less than 8 booleans', function() { |
186
|
|
|
unpacked = byteData.unpackBooleans([0,76]) |
187
|
|
|
assert.deepEqual(unpacked, [0,0,0,0,0,0,0,0,0,1,0,0,1,1,0,0]); |
188
|
|
|
}); |
189
|
|
|
}); |
190
|
|
|
|