diff options
author | Thiago Padilha <thiago@padilha.cc> | 2021-05-08 18:31:40 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-08 23:31:40 +0200 |
commit | 18a684ab1c20914e13c27bc10e20bda6396ea38d (patch) | |
tree | 42389bca4d7ec23ac3e310bf4cb99478815b4a77 /cli/tests | |
parent | a051a7f7bc8dab2a4360c146d08b549cbcf17b8d (diff) |
fix: TextEncoder#encodeInto spec compliance + perf gains (#10129)
Diffstat (limited to 'cli/tests')
-rw-r--r-- | cli/tests/text_encoder_into_perf.js | 34 | ||||
-rw-r--r-- | cli/tests/unit/text_encoding_test.ts | 56 |
2 files changed, 90 insertions, 0 deletions
diff --git a/cli/tests/text_encoder_into_perf.js b/cli/tests/text_encoder_into_perf.js new file mode 100644 index 000000000..8d60e9f00 --- /dev/null +++ b/cli/tests/text_encoder_into_perf.js @@ -0,0 +1,34 @@ +const mixed = "@ฤเน๐"; + +function generateRandom(bytes) { + let result = ""; + let i = 0; + while (i < bytes) { + const toAdd = Math.floor(Math.random() * Math.min(4, bytes - i)); + switch (toAdd) { + case 0: + result += mixed[0]; + i++; + break; + case 1: + result += mixed[1]; + i++; + break; + case 2: + result += mixed[2]; + i++; + break; + case 3: + result += mixed[3]; + result += mixed[4]; + i += 2; + break; + } + } + return result; +} + +const randomData = generateRandom(1024); +const encoder = new TextEncoder(); +const targetBuffer = new Uint8Array(randomData.length * 4); +for (let i = 0; i < 10_000; i++) encoder.encodeInto(randomData, targetBuffer); diff --git a/cli/tests/unit/text_encoding_test.ts b/cli/tests/unit/text_encoding_test.ts index 7a15c9376..42c221cb2 100644 --- a/cli/tests/unit/text_encoding_test.ts +++ b/cli/tests/unit/text_encoding_test.ts @@ -157,6 +157,62 @@ unitTest(function textEncodeInto3(): void { ]); }); +unitTest(function loneSurrogateEncodeInto(): void { + const fixture = "lone๐\ud888surrogate"; + const encoder = new TextEncoder(); + const bytes = new Uint8Array(20); + const result = encoder.encodeInto(fixture, bytes); + assertEquals(result.read, 16); + assertEquals(result.written, 20); + // deno-fmt-ignore + assertEquals(Array.from(bytes), [ + 0x6c, 0x6f, 0x6e, 0x65, + 0xf0, 0x9d, 0x84, 0x9e, + 0xef, 0xbf, 0xbd, 0x73, + 0x75, 0x72, 0x72, 0x6f, + 0x67, 0x61, 0x74, 0x65 + ]); +}); + +unitTest(function loneSurrogateEncodeInto2(): void { + const fixture = "\ud800"; + const encoder = new TextEncoder(); + const bytes = new Uint8Array(3); + const result = encoder.encodeInto(fixture, bytes); + assertEquals(result.read, 1); + assertEquals(result.written, 3); + // deno-fmt-ignore + assertEquals(Array.from(bytes), [ + 0xef, 0xbf, 0xbd + ]); +}); + +unitTest(function loneSurrogateEncodeInto3(): void { + const fixture = "\udc00"; + const encoder = new TextEncoder(); + const bytes = new Uint8Array(3); + const result = encoder.encodeInto(fixture, bytes); + assertEquals(result.read, 1); + assertEquals(result.written, 3); + // deno-fmt-ignore + assertEquals(Array.from(bytes), [ + 0xef, 0xbf, 0xbd + ]); +}); + +unitTest(function swappedSurrogatePairEncodeInto4(): void { + const fixture = "\udc00\ud800"; + const encoder = new TextEncoder(); + const bytes = new Uint8Array(8); + const result = encoder.encodeInto(fixture, bytes); + assertEquals(result.read, 2); + assertEquals(result.written, 6); + // deno-fmt-ignore + assertEquals(Array.from(bytes), [ + 0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd, 0x00, 0x00 + ]); +}); + unitTest(function textDecoderSharedUint8Array(): void { const ab = new SharedArrayBuffer(6); const dataView = new DataView(ab); |