Files
bun.sh/test/bun.js/concat.test.js
Colin McDonnell f7f1b60444 Add bun-types, add typechecking, add child_process types (#1475)
* Add bun-types to packages

* Improve typing

* Fix types in tests

* Fix dts tests

* Run formatter

* Fix all type errors

* Add strict mode, fix type errors

* Add ffi changes

* Move workflows to root

* Add workflows

* Remove labeler

* Add child_process types

* Fix synthetic defaults issue

* Remove docs

* Move scripts

* Run prettier

* Include examples in typechecking

* captureStackTrace types

* moved captureStackTrace types to globals

* Address reviews

Co-authored-by: Colin McDonnell <colinmcd@alum.mit.edu>
Co-authored-by: Dylan Conway <dylan.conway567@gmail.com>
2022-11-09 15:40:40 -08:00

47 lines
1.3 KiB
JavaScript

import { describe, it, expect } from "bun:test";
import { gcTick } from "./gc";
import { concatArrayBuffers } from "bun";
describe("concat", () => {
function polyfill(chunks) {
var size = 0;
for (const chunk of chunks) {
size += chunk.byteLength;
}
var buffer = new ArrayBuffer(size);
var view = new Uint8Array(buffer);
var offset = 0;
for (const chunk of chunks) {
view.set(chunk, offset);
offset += chunk.byteLength;
}
return buffer;
}
function concatToString(chunks) {
return Array.from(new Uint8Array(concatArrayBuffers(chunks))).join("");
}
function polyfillToString(chunks) {
return Array.from(new Uint8Array(polyfill(chunks))).join("");
}
it("works with one element", () => {
expect(concatToString([new Uint8Array([123])])).toBe(
polyfillToString([new Uint8Array([123])]),
);
});
it("works with two elements", () => {
expect(
concatToString([Uint8Array.from([123]), Uint8Array.from([456])]),
).toBe(polyfillToString([Uint8Array.from([123]), Uint8Array.from([456])]));
});
it("works with mix of ArrayBuffer and TypedArray elements", () => {
expect(
concatToString([Uint8Array.from([123]).buffer, Uint8Array.from([456])]),
).toBe(polyfillToString([Uint8Array.from([123]), Uint8Array.from([456])]));
});
});