aboutsummaryrefslogtreecommitdiff
path: root/test/bun.js/buffer.test.js
blob: 6e9a3c6b43ece74c6837d9eb90a67c6ff782fdd2 (plain) (blame)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// import { describe, it, expect, beforeEach, afterEach } from "bun:test";
// import { gc } from "./gc";

// beforeEach(() => gc());
// afterEach(() => gc());

// it("buffer", () => {
//   var buf = new Buffer(20);
//   gc();
//   // if this fails or infinitely loops, it means there is a memory issue with the JSC::Structure object
//   expect(Object.keys(buf).length > 0).toBe(true);
//   gc();
//   expect(buf.write("hello world ")).toBe(12);
//   expect(buf.write("hello world ", "utf8")).toBe(12);

//   gc();
//   expect(buf.toString("utf8", 0, "hello world ".length)).toBe("hello world ");
//   gc();
//   expect(buf.toString("base64url", 0, "hello world ".length)).toBe(
//     btoa("hello world ")
//   );
//   gc();
//   expect(buf instanceof Uint8Array).toBe(true);
//   gc();
//   expect(buf instanceof Buffer).toBe(true);
//   gc();
//   expect(buf.slice() instanceof Uint8Array).toBe(true);
//   gc();
//   expect(buf.slice(0, 1) instanceof Buffer).toBe(true);
//   gc();
//   expect(buf.slice(0, 1) instanceof Uint8Array).toBe(true);
//   gc();
//   expect(buf.slice(0, 1) instanceof Buffer).toBe(true);
//   gc();
// });

// it("Buffer", () => {
//   var inputs = [
//     "hello world",
//     "hello world".repeat(100),
//     `πŸ˜‹ Get Emoji β€” All Emojis to βœ‚οΈ Copy and πŸ“‹ Paste πŸ‘Œ`,
//   ];
//   var good = inputs.map((a) => new TextEncoder().encode(a));
//   for (let i = 0; i < inputs.length; i++) {
//     var input = inputs[i];
//     expect(new Buffer(input).toString("utf8")).toBe(inputs[i]);
//     gc();
//     expect(Array.from(new Buffer(input)).join(",")).toBe(good[i].join(","));
//     gc();
//     expect(Buffer.byteLength(input)).toBe(good[i].length);
//     gc();
//     expect(Buffer.from(input).byteLength).toBe(Buffer.byteLength(input));
//   }
// });

// it("Buffer.byteLength", () => {
//   expect(Buffer.byteLength("πŸ˜€πŸ˜ƒπŸ˜„πŸ˜πŸ˜†πŸ˜…πŸ˜‚πŸ€£β˜ΊοΈπŸ˜ŠπŸ˜ŠπŸ˜‡")).toBe(
//     new TextEncoder().encode("πŸ˜€πŸ˜ƒπŸ˜„πŸ˜πŸ˜†πŸ˜…πŸ˜‚πŸ€£β˜ΊοΈπŸ˜ŠπŸ˜ŠπŸ˜‡").byteLength
//   );
// });

// it("Buffer.isBuffer", () => {
//   expect(Buffer.isBuffer(new Buffer(1))).toBe(true);
//   gc();
//   expect(Buffer.isBuffer(new Buffer(0))).toBe(true);
//   gc();
//   expect(Buffer.isBuffer(new Uint8Array(0))).toBe(false);
//   gc();
//   expect(Buffer.isBuffer(new Uint8Array(1))).toBe(false);
//   gc();
//   var a = new Uint8Array(1);
//   gc();
//   expect(Buffer.isBuffer(a)).toBe(false);
//   gc();
//   Buffer.toBuffer(a);
//   gc();
//   expect(Buffer.isBuffer(a)).toBe(true);
//   gc();
// });

// it("Buffer.toBuffer throws", () => {
//   const checks = [
//     [],
//     {},
//     "foo",
//     new Uint16Array(),
//     new DataView(new Uint8Array(14).buffer),
//   ];
//   for (let i = 0; i < checks.length; i++) {
//     try {
//       Buffer.toBuffer(checks[i]);
//       expect(false).toBe(true);
//     } catch (exception) {
//       expect(exception.message).toBe("Expected Uint8Array");
//     }
//   }
//   expect(true).toBe(true);
// });

// it("Buffer.toBuffer works", () => {
//   var array = new Uint8Array(20);
//   expect(array instanceof Buffer).toBe(false);
//   var buf = Buffer.toBuffer(array);
//   expect(array instanceof Buffer).toBe(true);
//   // if this fails or infinitely loops, it means there is a memory issue with the JSC::Structure object
//   expect(Object.keys(buf).length > 0).toBe(true);

//   expect(buf.write("hello world ")).toBe(12);
//   gc();
//   expect(buf.toString("utf8", 0, "hello world ".length)).toBe("hello world ");
//   gc();
//   expect(buf.toString("base64url", 0, "hello world ".length)).toBe(
//     btoa("hello world ")
//   );
//   gc();

//   expect(buf instanceof Uint8Array).toBe(true);
//   expect(buf instanceof Buffer).toBe(true);
//   expect(buf.slice() instanceof Uint8Array).toBe(true);
//   expect(buf.slice(0, 1) instanceof Buffer).toBe(true);
//   expect(buf.slice(0, 1) instanceof Uint8Array).toBe(true);
//   expect(buf.slice(0, 1) instanceof Buffer).toBe(true);
//   expect(new Buffer(buf) instanceof Buffer).toBe(true);
//   expect(new Buffer(buf.buffer) instanceof Buffer).toBe(true);
// });

// it("writeInt", () => {
//   var buf = new Buffer(1024);
//   var data = new DataView(buf.buffer);
//   buf.writeInt32BE(100);
//   expect(data.getInt32(0, false)).toBe(100);
//   buf.writeInt32BE(100);
//   expect(data.getInt32(0, false)).toBe(100);
//   var childBuf = buf.subarray(0, 4);
//   expect(data.getInt32(0, false)).toBe(100);
//   expect(childBuf.readInt32BE(0, false)).toBe(100);
// });

// it("Buffer.from", () => {
//   expect(Buffer.from("hello world").toString("utf8")).toBe("hello world");
//   expect(Buffer.from("hello world", "ascii").toString("utf8")).toBe(
//     "hello world"
//   );
//   expect(Buffer.from("hello world", "latin1").toString("utf8")).toBe(
//     "hello world"
//   );
//   gc();
//   expect(Buffer.from([254]).join(",")).toBe("254");
//   expect(Buffer.from(123).join(",")).toBe(Uint8Array.from(123).join(","));
//   expect(Buffer.from({ length: 124 }).join(",")).toBe(
//     Uint8Array.from({ length: 124 }).join(",")
//   );

//   expect(Buffer.from(new ArrayBuffer(1024), 0, 512).join(",")).toBe(
//     new Uint8Array(512).join(",")
//   );

//   expect(Buffer.from(new Buffer(new ArrayBuffer(1024), 0, 512)).join(",")).toBe(
//     new Uint8Array(512).join(",")
//   );
//   gc();
// });

// it("Buffer.equals", () => {
//   var a = new Uint8Array(10);
//   a[2] = 1;
//   var b = new Uint8Array(10);
//   b[2] = 1;
//   Buffer.toBuffer(a);
//   Buffer.toBuffer(b);
//   expect(a.equals(b)).toBe(true);
//   b[2] = 0;
//   expect(a.equals(b)).toBe(false);
// });

// it("Buffer.compare", () => {
//   var a = new Uint8Array(10);
//   a[2] = 1;
//   var b = new Uint8Array(10);
//   b[2] = 1;
//   Buffer.toBuffer(a);
//   Buffer.toBuffer(b);
//   expect(a.compare(b)).toBe(0);
//   b[2] = 0;
//   expect(a.compare(b)).toBe(1);
//   expect(b.compare(a)).toBe(-1);
// });

// it("Buffer.copy", () => {
//   var array1 = new Uint8Array(128);
//   array1.fill(100);
//   Buffer.toBuffer(array1);
//   var array2 = new Uint8Array(128);
//   array2.fill(200);
//   Buffer.toBuffer(array2);
//   var array3 = new Uint8Array(128);
//   Buffer.toBuffer(array3);
//   gc();
//   expect(array1.copy(array2)).toBe(128);
//   expect(array1.join("")).toBe(array2.join(""));
// });

// it("Buffer.concat", () => {
//   var array1 = new Uint8Array(128);
//   array1.fill(100);
//   var array2 = new Uint8Array(128);
//   array2.fill(200);
//   var array3 = new Uint8Array(128);
//   array3.fill(300);
//   gc();
//   expect(Buffer.concat([array1, array2, array3]).join("")).toBe(
//     array1.join("") + array2.join("") + array3.join("")
//   );
//   expect(Buffer.concat([array1, array2, array3], 222).length).toBe(222);
//   expect(
//     Buffer.concat([array1, array2, array3], 222).subarray(0, 128).join("")
//   ).toBe("100".repeat(128));
//   expect(
//     Buffer.concat([array1, array2, array3], 222).subarray(129, 222).join("")
//   ).toBe("200".repeat(222 - 129));
// });

// it("read", () => {
//   var buf = new Buffer(1024);
//   var data = new DataView(buf.buffer);
//   function reset() {
//     new Uint8Array(buf.buffer).fill(0);
//   }
//   data.setBigInt64(0, BigInt(1000), false);
//   expect(buf.readBigInt64BE(0)).toBe(BigInt(1000));
//   reset();

//   data.setBigInt64(0, BigInt(1000), false);
//   expect(buf.readBigInt64LE(0)).toBe(BigInt(1000));
//   reset();

//   data.setBigUint64(0, BigInt(1000), false);
//   expect(buf.readBigUInt64BE(0)).toBe(BigInt(1000));
//   reset();

//   data.setBigUint64(0, BigInt(1000), false);
//   expect(buf.readBigUInt64LE(0)).toBe(BigInt(1000));
//   reset();

//   data.setFloat64(0, 1000, false);
//   expect(buf.readDoubleBE(0)).toBe(1000);
//   reset();

//   data.setFloat64(0, 1000, true);
//   expect(buf.readDoubleLE(0)).toBe(1000);
//   reset();

//   data.setFloat32(0, 1000, false);
//   expect(buf.readFloatBE(0)).toBe(1000);
//   reset();

//   data.setFloat32(0, 1000, true);
//   expect(buf.readFloatLE(0)).toBe(1000);
//   reset();

//   data.setInt16(0, 1000, false);
//   expect(buf.readInt16BE(0)).toBe(1000);
//   reset();

//   data.setInt16(0, 1000, true);
//   expect(buf.readInt16LE(0)).toBe(1000);
//   reset();

//   data.setInt32(0, 1000, false);
//   expect(buf.readInt32BE(0)).toBe(1000);
//   reset();

//   data.setInt32(0, 1000, true);
//   expect(buf.readInt32LE(0)).toBe(1000);
//   reset();

//   data.setInt8(0, 100, false);
//   expect(buf.readInt8(0)).toBe(100);
//   reset();

//   data.setUint16(0, 1000, false);
//   expect(buf.readUInt16BE(0)).toBe(1000);
//   reset();

//   data.setUint16(0, 1000, true);
//   expect(buf.readUInt16LE(0)).toBe(1000);
//   reset();

//   data.setUint32(0, 1000, false);
//   expect(buf.readUInt32BE(0)).toBe(1000);
//   reset();

//   data.setUint32(0, 1000, true);
//   expect(buf.readUInt32LE(0)).toBe(1000);
//   reset();

//   data.setUint8(0, 255, false);
//   expect(buf.readUInt8(0)).toBe(255);
//   reset();

//   data.setUint8(0, 255, false);
//   expect(buf.readUInt8(0)).toBe(255);
//   reset();
// });