forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer-test.ts
More file actions
211 lines (184 loc) · 6.55 KB
/
buffer-test.ts
File metadata and controls
211 lines (184 loc) · 6.55 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* Sanity checks our buffer implementation. The Node tests assume that all
* numerical transformations work, so they do not test these cases.
*/
import assert from '../../harness/wrapped-assert';
export default function() {
var i: number, buff = new Buffer(8);
/**
* Simple get/set tests
*/
function testFunction(readFunc: string, writeFunc: string) {
return function(nums: number | number[]) {
let writeNum: number, expectNum: number;
if (Array.isArray(nums)) {
writeNum = nums[0];
expectNum = nums[1];
} else {
writeNum = expectNum = nums;
}
(<any>buff)[writeFunc](writeNum, 0, true);
if (!isNaN(expectNum)) {
assert.strictEqual(expectNum, (<any>buff)[readFunc](0));
} else {
assert(isNaN((<any>buff)[readFunc](0)));
}
};
}
var oneByteInts = [
0, 0x7F, [0xFF, -1], -1*0x7F, -1
];
oneByteInts.forEach(testFunction('readInt8', 'writeInt8'));
var readMethods = ['readInt16LE', 'readInt16BE'];
var writeMethods = ['writeInt16LE', 'writeInt16BE'];
var twoByteInts = [
0, 0x7FFF, [0xFFFF, -1], -1*0x7FFF, -1
];
for (i = 0; i < 2; i++) {
twoByteInts.forEach(testFunction(readMethods[i], writeMethods[i]));
}
var fourByteInts = [
0, 0x7FFFFFFF, [0xFFFFFFFF, 0xFFFFFFFF|0], -1, -1*0x7FFFFFFF
];
var readMethods4B = ['readInt32LE', 'readInt32BE'];
var writeMethods4B = ['writeInt32LE', 'writeInt32BE'];
for (i = 0; i < 2; i++) {
fourByteInts.forEach(testFunction(readMethods4B[i], writeMethods4B[i]));
}
var floatVals = [
0, -1, 1,
[Math.pow(2, 128), Number.POSITIVE_INFINITY],
[-1 * Math.pow(2, 128), Number.NEGATIVE_INFINITY],
NaN
];
floatVals.forEach(testFunction('readFloatLE', 'writeFloatLE'));
floatVals.forEach(testFunction('readFloatBE', 'writeFloatBE'));
// int -> float
var int2float = [[0x7F800000, Number.POSITIVE_INFINITY],
[-8388608, Number.NEGATIVE_INFINITY],
[0x7fc00000, Number.NaN]];
int2float.forEach(testFunction('readFloatLE', 'writeInt32LE'));
int2float.forEach(testFunction('readFloatBE', 'writeInt32BE'));
var doubleVals = [
0, -1, 1, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY, NaN
];
doubleVals.forEach(testFunction('readDoubleLE', 'writeDoubleLE'));
doubleVals.forEach(testFunction('readDoubleBE', 'writeDoubleBE'));
// long -> double
var long2double = [
[0, 0, 0],
[0x80000000, 0, 0],
[0x7FF00000, 0, Number.POSITIVE_INFINITY],
[0xFFF00000, 0, Number.NEGATIVE_INFINITY],
[0x7FF00001, 0, Number.NaN],
[0xFFF00001, 0, Number.NaN],
// Denormalized
[0, 1, 4.94065645841246544176568792868E-324]
];
long2double.forEach(function(nums) {
// LE
buff.writeUInt32LE(nums[1], 0);
buff.writeUInt32LE(nums[0], 4);
if (isNaN(nums[2])) {
assert(isNaN(buff.readDoubleLE(0)));
} else {
assert.strictEqual(nums[2], buff.readDoubleLE(0));
}
// BE
buff.writeUInt32BE(nums[0], 0);
buff.writeUInt32BE(nums[1], 4);
if (isNaN(nums[2])) {
assert(isNaN(buff.readDoubleBE(0)));
} else {
assert.strictEqual(nums[2], buff.readDoubleBE(0));
}
});
// signed vs unsigned
buff.writeUInt8(0xFF, 0);
assert.strictEqual(-1, buff.readInt8(0));
buff.writeInt8(-1, 0);
assert.strictEqual(0xFF, buff.readUInt8(0));
buff.writeUInt16LE(0xFFFF, 0);
assert.strictEqual(-1, buff.readInt16LE(0));
assert.strictEqual(-1, buff.readInt16BE(0));
buff.writeInt16LE(-1, 0);
assert.strictEqual(0xFFFF, buff.readUInt16LE(0));
assert.strictEqual(0xFFFF, buff.readUInt16BE(0));
buff.writeUInt32LE(0xFFFFFFFF, 0);
assert.strictEqual(-1, buff.readInt32LE(0));
assert.strictEqual(-1, buff.readInt32BE(0));
buff.writeInt32LE(-1, 0);
assert.strictEqual(0xFFFFFFFF, buff.readUInt32LE(0));
assert.strictEqual(0xFFFFFFFF, buff.readUInt32BE(0));
/**
* Endianness Fun Time (tm)
*/
buff.writeUInt8(1 << 7, 0);
buff.writeUInt8(1, 1);
assert.strictEqual(384, buff.readInt16LE(0));
assert.strictEqual(-32767, buff.readInt16BE(0));
buff.writeUInt8(1, 0);
buff.writeUInt8(1 << 1, 1);
buff.writeUInt8(1 << 2, 2);
buff.writeUInt8(1 << 3, 3);
assert.strictEqual(134480385, buff.readInt32LE(0));
assert.strictEqual(16909320, buff.readInt32BE(0));
assert.strictEqual(3.972466068346319e-34, buff.readFloatLE(0));
assert.strictEqual(2.388012128110808e-38, buff.readFloatBE(0));
buff.writeUInt8(1 << 4, 4);
buff.writeUInt8(1 << 5, 5);
buff.writeUInt8(1 << 6, 6);
buff.writeUInt8(1 << 7, 7);
assert.strictEqual(-1.793993013121266e-307, buff.readDoubleLE(0));
assert.strictEqual(8.209688573201296e-304, buff.readDoubleBE(0));
/**
* Slice test! Ensure that sliced buffers share the same backing memory.
*/
var buff1 = new Buffer(4);
var buff2 = buff1.slice(2);
buff1.writeInt16LE(-203, 2);
assert.strictEqual(-203, buff1.readInt16LE(2));
assert.strictEqual(-203, buff2.readInt16LE(0));
/**
* Testing that the 'binary' encoding !== 'ascii' encoding.
*/
// Characters are truncated at 0xFF.
assert.equal(buff.write(String.fromCharCode(0xFFF),0,10,'binary'), 1);
assert.equal(buff.toString('binary', 0, 1), String.fromCharCode(0xFF));
// Characters are truncated at 0x7F.
buff.write(String.fromCharCode(0xFF), 0, 1, 'ascii');
assert.equal(buff.toString('ascii', 0, 1), String.fromCharCode(0x7F));
/**
* Testing extended ASCII support.
* TODO: Test explicitly on ExtendedAscii class, since we no longer
* use our own buffer implementation.
*/
// Write as UTF-8, read as ASCII/Extended ASCII. Boundary condition.
/*buff.write("Hello" + String.fromCharCode(0x7F) + "World");
assert(buff.toString('ascii', 0, 11) === buff.toString('extended_ascii', 0, 11));
buff.write('\u00A6', 0, 1, 'extended_ascii');
assert(buff.toString('ascii', 0, 1) !== buff.toString('extended_ascii', 0, 1));
assert(buff.toString('ascii', 0, 1) !== '\u00A6');
assert(buff.toString('extended_ascii', 0, 1) === '\u00A6');*/
/**
* Array setter accepts signed numbers.
*/
buff[0] = -5;
assert.equal(buff[0], 251);
/**
* Copying to/from buffers that are slices.
*/
var originalBuff1 = new Buffer(10),
originalBuff2 = new Buffer(10),
slice1 = originalBuff1.slice(1),
slice2 = originalBuff2.slice(1);
// Zero first two offsets of destination slice.
originalBuff2.writeUInt8(0, 0);
originalBuff2.writeUInt8(0, 1);
originalBuff1.writeUInt8(1, 0);
originalBuff1.writeUInt8(2, 1);
// slice2[1] should be 2.
slice1.copy(slice2, 0, 0, 1);
assert.equal(slice2.readUInt8(0), 2);
assert.equal(originalBuff2.readUInt8(1), 2);
};