diff options
author | 2022-08-10 21:26:20 -0700 | |
---|---|---|
committer | 2022-08-10 21:26:20 -0700 | |
commit | d9ae284463c25f6c1e6056c2ee4d5bf81f1d11cb (patch) | |
tree | e4221d5571233981da5b28f17b9076c5d183c550 /test/bun.js | |
parent | 00d5f6699b39799b383dc305d4120da7d79c7bce (diff) | |
download | bun-d9ae284463c25f6c1e6056c2ee4d5bf81f1d11cb.tar.gz bun-d9ae284463c25f6c1e6056c2ee4d5bf81f1d11cb.tar.zst bun-d9ae284463c25f6c1e6056c2ee4d5bf81f1d11cb.zip |
Fix console.log with typed arrays
Diffstat (limited to 'test/bun.js')
-rw-r--r-- | test/bun.js/ffi.test.js | 32 | ||||
-rw-r--r-- | test/bun.js/inspect.test.js | 80 |
2 files changed, 84 insertions, 28 deletions
diff --git a/test/bun.js/ffi.test.js b/test/bun.js/ffi.test.js index e24aaaf66..873a7f7aa 100644 --- a/test/bun.js/ffi.test.js +++ b/test/bun.js/ffi.test.js @@ -359,9 +359,9 @@ function ffiRunner(types) { }, close, } = dlopen("/tmp/bun-ffi-test.dylib", types); - + Bun.gc(true); expect(returns_true()).toBe(true); - + Bun.gc(true); expect(returns_false()).toBe(false); expect(returns_42_char()).toBe(42); @@ -371,7 +371,7 @@ function ffiRunner(types) { // returns_42_uint64_t().valueOf() === returns_42_uint64_t() // ); // expect(returns_42_uint64_t().valueOf()).toBe(42); - + Bun.gc(true); expect(Math.fround(returns_42_float())).toBe(Math.fround(42.41999804973602)); expect(returns_42_double()).toBe(42.42); expect(returns_42_uint8_t()).toBe(42); @@ -382,6 +382,7 @@ function ffiRunner(types) { expect(returns_neg_42_int16_t()).toBe(-42); expect(returns_neg_42_int32_t()).toBe(-42); expect(identity_int32_t(10)).toBe(10); + Bun.gc(true); // expect(returns_neg_42_int64_t()).toBe(-42); expect(identity_char(10)).toBe(10); @@ -399,8 +400,8 @@ function ffiRunner(types) { expect(identity_uint8_t(10)).toBe(10); expect(identity_uint16_t(10)).toBe(10); expect(identity_uint32_t(10)).toBe(10); - // expect(identity_uint64_t(10)).toBe(10); - + // expect(identity_uint64_t(10)).toBe(10); + Bun.gc(true); var bigArray = new BigUint64Array(8); new Uint8Array(bigArray.buffer).fill(255); var bigIntArray = new BigInt64Array(bigArray.buffer); @@ -420,8 +421,9 @@ function ffiRunner(types) { // expect(identity_int64_t(bigIntArray[0] - BigInt(1))).toBe( // bigIntArray[0] - BigInt(1) // ); + Bun.gc(true); + expect(add_char.native(1, 1)).toBe(2); - expect(add_char(1, 1)).toBe(2); expect(add_float(2.4, 2.8)).toBe(Math.fround(5.2)); expect(add_double(4.2, 0.1)).toBe(4.3); expect(add_int8_t(1, 1)).toBe(2); @@ -431,7 +433,7 @@ function ffiRunner(types) { expect(add_uint8_t(1, 1)).toBe(2); expect(add_uint16_t(1, 1)).toBe(2); expect(add_uint32_t(1, 1)).toBe(2); - + Bun.gc(true); const cptr = ptr_should_point_to_42_as_int32_t(); expect(cptr != 0).toBe(true); expect(typeof cptr === "number").toBe(true); @@ -463,20 +465,18 @@ function ffiRunner(types) { // callback, no userData constructor(bufferPtr, 0, 128, getDeallocatorCallback()); - // callback, userData + // callback, userData; constructor(bufferPtr, 0, 128, bufferPtr, getDeallocatorCallback()); } Bun.gc(true); expect(getDeallocatorCalledCount() >= 190).toBe(true); + Bun.gc(true); } - // close the library - close(); - - /* - --- - This style of callback is not implemented yet + /* + --- + This style of callback is not implemented yet */ // function identityBool() { // return true; @@ -574,9 +574,9 @@ function ffiRunner(types) { } it("run ffi fast", () => { - return ffiRunner(getTypes(true)); + ffiRunner(getTypes(true)); }); it("run ffi", () => { - return ffiRunner(getTypes(false)); + ffiRunner(getTypes(false)); }); diff --git a/test/bun.js/inspect.test.js b/test/bun.js/inspect.test.js index f7b364953..7428933c9 100644 --- a/test/bun.js/inspect.test.js +++ b/test/bun.js/inspect.test.js @@ -57,23 +57,79 @@ Request (0 KB) { ); }); +it("MessageEvent", () => { + expect(Bun.inspect(new MessageEvent("message", { data: 123 }))).toBe( + `MessageEvent { + type: "message", + data: 123 +}` + ); +}); + // https://github.com/oven-sh/bun/issues/561 it("TypedArray prints", () => { - // TODO: add tests for all variants of typed arrays - // even if the code is the same for each implementation, we should test it - const buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + for (let TypedArray of [ + Uint8Array, + Uint16Array, + Uint32Array, + Uint8ClampedArray, + Int8Array, + Int16Array, + Int32Array, + Float32Array, + Float64Array, + ]) { + const buffer = new TypedArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + const input = Bun.inspect(buffer); + + expect(input).toBe( + `${TypedArray.name}(${buffer.length}) [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]` + ); + for (let i = 1; i < buffer.length + 1; i++) { + expect(Bun.inspect(buffer.subarray(i))).toBe( + `${TypedArray.name}(${buffer.length - i}) [ ` + + [...buffer.subarray(i)].join(", ") + + " ]" + ); + } + } +}); - const input = Bun.inspect(buffer); +it("BigIntArray", () => { + for (let TypedArray of [BigInt64Array, BigUint64Array]) { + const buffer = new TypedArray([1n, 2n, 3n, 4n, 5n, 6n, 7n, 8n, 9n, 10n]); + const input = Bun.inspect(buffer); - expect(input).toBe( - `Uint8Array(${buffer.length}) [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]` - ); - for (let i = 1; i < buffer.length + 1; i++) { - expect(Bun.inspect(buffer.subarray(i))).toBe( - `Uint8Array(${buffer.length - i}) [ ` + - [...buffer.subarray(i)].join(", ") + - " ]" + expect(input).toBe( + `${TypedArray.name}(${buffer.length}) [ 1n, 2n, 3n, 4n, 5n, 6n, 7n, 8n, 9n, 10n ]` + ); + for (let i = 1; i < buffer.length + 1; i++) { + expect(Bun.inspect(buffer.subarray(i))).toBe( + `${TypedArray.name}(${buffer.length - i}) [ ` + + [...buffer.subarray(i)].map((a) => a.toString(10) + "n").join(", ") + + " ]" + ); + } + } +}); + +it("FloatArray", () => { + for (let TypedArray of [Float32Array, Float64Array]) { + const buffer = new TypedArray([Math.fround(42.68)]); + const input = Bun.inspect(buffer); + + expect(input).toBe( + `${TypedArray.name}(${buffer.length}) [ ${[Math.fround(42.68)].join( + ", " + )} ]` ); + for (let i = 1; i < buffer.length + 1; i++) { + expect(Bun.inspect(buffer.subarray(i))).toBe( + `${TypedArray.name}(${buffer.length - i}) [ ` + + [...buffer.subarray(i)].join(", ") + + " ]" + ); + } } }); |