blob: 741dc0241e1087eab7d057a9f1c3c08f37860d01 (
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
|
import { test, expect, it, describe } from "bun:test";
import { gc } from "./gc";
it("arrayBufferToString u8", async () => {
var encoder = new TextEncoder();
const bytes = encoder.encode("hello world");
gc(true);
expect(Bun.unsafe.arrayBufferToString(bytes)).toBe("hello world");
gc(true);
await new Promise((resolve) => setTimeout(resolve, 0));
gc(true);
});
it("arrayBufferToString ArrayBuffer", async () => {
var encoder = new TextEncoder();
var bytes = encoder.encode("hello world");
gc(true);
const out = Bun.unsafe.arrayBufferToString(bytes.buffer);
expect(out).toBe("hello world");
gc(true);
await new Promise((resolve) => setTimeout(resolve, 0));
globalThis.bytes = bytes;
gc(true);
expect(out).toBe("hello world");
});
it("arrayBufferToString u16", () => {
var encoder = new TextEncoder();
const bytes = encoder.encode("hello world");
var uint16 = new Uint16Array(bytes.byteLength);
uint16.set(bytes);
const charCodes = Bun.unsafe
.arrayBufferToString(uint16)
.split("")
.map((a) => a.charCodeAt(0));
gc(true);
for (let i = 0; i < charCodes.length; i++) {
expect("hello world"[i]).toBe(String.fromCharCode(charCodes[i]));
}
gc(true);
expect(charCodes.length).toBe("hello world".length);
gc(true);
});
it("Bun.allocUnsafe", () => {
var buffer = Bun.allocUnsafe(1024);
expect(buffer instanceof Uint8Array).toBe(true);
expect(buffer.length).toBe(1024);
buffer[0] = 0;
expect(buffer[0]).toBe(0);
});
|