|
| 1 | +function bufferFrom<T>(values: valueof<T>[]): T { |
| 2 | + let buffer = instantiate<T>(values.length); |
| 3 | + // @ts-ignore |
| 4 | + for (let i = 0; i < values.length; i++) buffer[i] = values[i]; |
| 5 | + return buffer; |
| 6 | +} |
| 7 | + |
1 | 8 | /**
|
2 | 9 | * This is the buffer test suite. For each prototype function, put a single test
|
3 | 10 | * function call here.
|
@@ -42,4 +49,47 @@ describe("buffer", () => {
|
42 | 49 | // TODO: expectFn(() => { Buffer.allocUnsafe(-1); }).toThrow();
|
43 | 50 | // TODO: expectFn(() => { Buffer.allocUnsafe(BLOCK_MAXSIZE + 1); }).toThrow();
|
44 | 51 | });
|
| 52 | + |
| 53 | + |
| 54 | + /** |
| 55 | + * This specification is a tradeoff, because Buffer.from() takes _many_ parameters. |
| 56 | + * Instead, the only common parameter is the first one, which results in Buffer.from |
| 57 | + * acting in a very naive fashion. Perhaps an optional encoding parameter might be |
| 58 | + * possible for strings, at least. However, this makes things more complicated. |
| 59 | + * There are no good solutions. Only tradeoffs. Function overloading is the only |
| 60 | + * way to fix this problem. |
| 61 | + */ |
| 62 | + test("#from", () => { |
| 63 | + // TODO: Switch to expect<Buffer>() when 2.2.1 releases |
| 64 | + |
| 65 | + // Buffer.from uses the array buffer reference |
| 66 | + let buff = new ArrayBuffer(100); |
| 67 | + for (let i = 0; i < 100; i++) store<u8>(changetype<usize>(buff), u8(i)); |
| 68 | + let abBuffer = Buffer.from<ArrayBuffer>(buff); |
| 69 | + expect<ArrayBuffer>(abBuffer.buffer).toStrictEqual(buff); |
| 70 | + expect<ArrayBuffer>(abBuffer.buffer).toBe(buff); |
| 71 | + |
| 72 | + // strings are utf8 encoded by default |
| 73 | + let strBuffer = Buffer.from<string>("Hello world!"); |
| 74 | + let strBufferExpected = bufferFrom<Buffer>([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21]); |
| 75 | + expect<ArrayBuffer>(strBuffer.buffer).toStrictEqual(strBufferExpected.buffer); |
| 76 | + |
| 77 | + // buffer returns a new reference view to the same ArrayBuffer |
| 78 | + let buff2 = Buffer.from<Buffer>(abBuffer); |
| 79 | + expect<Buffer>(buff2).not.toBe(abBuffer); |
| 80 | + expect<ArrayBuffer>(buff2.buffer).toBe(abBuffer.buffer); |
| 81 | + expect<usize>(buff2.dataStart).toBe(abBuffer.dataStart); |
| 82 | + expect<u32>(buff2.dataLength).toBe(abBuffer.dataLength); |
| 83 | + |
| 84 | + // else if it extends ArrayBufferView simply converts all the values |
| 85 | + let floats = bufferFrom<Float32Array>([1.1, 2.2, 3.3]); |
| 86 | + let floatBuff = Buffer.from<Float32Array>(floats); |
| 87 | + let floatBuffExpected = bufferFrom<Buffer>([1, 2, 3]); |
| 88 | + expect<ArrayBuffer>(floatBuff.buffer).toStrictEqual(floatBuffExpected.buffer); |
| 89 | + |
| 90 | + let strArrayExpected = bufferFrom<Buffer>([1, 2, 3, 4, 5, 6, 7, 0, 0, 0]); |
| 91 | + let stringValues: string[] = ["1.1", "2.2", "3.3", "4.4", "5.5", "6.6", "7.7", "Infinity", "NaN", "-Infinity"]; |
| 92 | + let strArrayActual = Buffer.from<Array<String>>(stringValues); |
| 93 | + expect<ArrayBuffer>(strArrayActual.buffer).toStrictEqual(strArrayExpected.buffer); |
| 94 | + }); |
45 | 95 | });
|
0 commit comments