1[ 2 { 3 "input": "Hi", 4 "read": 0, 5 "destinationLength": 0, 6 "written": [] 7 }, 8 { 9 "input": "A", 10 "read": 1, 11 "destinationLength": 10, 12 "written": [0x41] 13 }, 14 { 15 "input": "\u{1D306}", // "\uD834\uDF06" 16 "read": 2, 17 "destinationLength": 4, 18 "written": [0xF0, 0x9D, 0x8C, 0x86] 19 }, 20 { 21 "input": "\u{1D306}A", 22 "read": 0, 23 "destinationLength": 3, 24 "written": [] 25 }, 26 { 27 "input": "\uD834A\uDF06A¥Hi", 28 "read": 5, 29 "destinationLength": 10, 30 "written": [0xEF, 0xBF, 0xBD, 0x41, 0xEF, 0xBF, 0xBD, 0x41, 0xC2, 0xA5] 31 }, 32 { 33 "input": "A\uDF06", 34 "read": 2, 35 "destinationLength": 4, 36 "written": [0x41, 0xEF, 0xBF, 0xBD] 37 }, 38 { 39 "input": "¥¥", 40 "read": 2, 41 "destinationLength": 4, 42 "written": [0xC2, 0xA5, 0xC2, 0xA5] 43 } 44].forEach(testData => { 45 [ 46 { 47 "bufferIncrease": 0, 48 "destinationOffset": 0, 49 "filler": 0 50 }, 51 { 52 "bufferIncrease": 10, 53 "destinationOffset": 4, 54 "filler": 0 55 }, 56 { 57 "bufferIncrease": 0, 58 "destinationOffset": 0, 59 "filler": 0x80 60 }, 61 { 62 "bufferIncrease": 10, 63 "destinationOffset": 4, 64 "filler": 0x80 65 }, 66 { 67 "bufferIncrease": 0, 68 "destinationOffset": 0, 69 "filler": "random" 70 }, 71 { 72 "bufferIncrease": 10, 73 "destinationOffset": 4, 74 "filler": "random" 75 } 76 ].forEach(destinationData => { 77 test(() => { 78 // Setup 79 const bufferLength = testData.destinationLength + destinationData.bufferIncrease, 80 destinationOffset = destinationData.destinationOffset, 81 destinationLength = testData.destinationLength, 82 destinationFiller = destinationData.filler, 83 encoder = new TextEncoder(), 84 buffer = new ArrayBuffer(bufferLength), 85 view = new Uint8Array(buffer, destinationOffset, destinationLength), 86 fullView = new Uint8Array(buffer), 87 control = new Array(bufferLength); 88 let byte = destinationFiller; 89 for (let i = 0; i < bufferLength; i++) { 90 if (destinationFiller === "random") { 91 byte = Math.floor(Math.random() * 256); 92 } 93 control[i] = byte; 94 fullView[i] = byte; 95 } 96 97 // It's happening 98 const result = encoder.encodeInto(testData.input, view); 99 100 // Basics 101 assert_equals(view.byteLength, destinationLength); 102 assert_equals(view.length, destinationLength); 103 104 // Remainder 105 assert_equals(result.read, testData.read); 106 assert_equals(result.written, testData.written.length); 107 for (let i = 0; i < bufferLength; i++) { 108 if (i < destinationOffset || i >= (destinationOffset + testData.written.length)) { 109 assert_equals(fullView[i], control[i]); 110 } else { 111 assert_equals(fullView[i], testData.written[i - destinationOffset]); 112 } 113 } 114 }, "encodeInto() with " + testData.input + " and destination length " + testData.destinationLength + ", offset " + destinationData.destinationOffset + ", filler " + destinationData.filler); 115 }); 116}); 117 118[DataView, 119 Int8Array, 120 Int16Array, 121 Int32Array, 122 Uint16Array, 123 Uint32Array, 124 Uint8ClampedArray, 125 Float32Array, 126 Float64Array].forEach(view => { 127 test(() => { 128 assert_throws(new TypeError(), () => new TextEncoder().encodeInto("", new view(new ArrayBuffer(0)))); 129 }, "Invalid encodeInto() destination: " + view.name); 130 }); 131 132test(() => { 133 assert_throws(new TypeError(), () => new TextEncoder().encodeInto("", new ArrayBuffer(10))); 134}, "Invalid encodeInto() destination: ArrayBuffer"); 135 136test(() => { 137 const buffer = new ArrayBuffer(10), 138 view = new Uint8Array(buffer); 139 let { read, written } = new TextEncoder().encodeInto("", view); 140 assert_equals(read, 0); 141 assert_equals(written, 0); 142 new MessageChannel().port1.postMessage(buffer, [buffer]); 143 ({ read, written } = new TextEncoder().encodeInto("", view)); 144 assert_equals(read, 0); 145 assert_equals(written, 0); 146 ({ read, written } = new TextEncoder().encodeInto("test", view)); 147 assert_equals(read, 0); 148 assert_equals(written, 0); 149}, "encodeInto() and a detached output buffer"); 150