summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/serial/resources/common.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
commit43a97878ce14b72f0981164f87f2e35e14151312 (patch)
tree620249daf56c0258faa40cbdcf9cfba06de2a846 /testing/web-platform/tests/serial/resources/common.js
parentInitial commit. (diff)
downloadfirefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz
firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/serial/resources/common.js')
-rw-r--r--testing/web-platform/tests/serial/resources/common.js33
1 files changed, 33 insertions, 0 deletions
diff --git a/testing/web-platform/tests/serial/resources/common.js b/testing/web-platform/tests/serial/resources/common.js
new file mode 100644
index 0000000000..5177f83a86
--- /dev/null
+++ b/testing/web-platform/tests/serial/resources/common.js
@@ -0,0 +1,33 @@
+// Compare two Uint8Arrays.
+function compareArrays(actual, expected) {
+ assert_true(actual instanceof Uint8Array, 'actual is Uint8Array');
+ assert_true(expected instanceof Uint8Array, 'expected is Uint8Array');
+ assert_equals(actual.byteLength, expected.byteLength, 'lengths equal');
+ for (let i = 0; i < expected.byteLength; ++i)
+ assert_equals(actual[i], expected[i], `Mismatch at position ${i}.`);
+}
+
+// Reads from |reader| until at least |targetLength| is read or the stream is
+// closed. The data is returned as a combined Uint8Array.
+async function readWithLength(reader, targetLength) {
+ const chunks = [];
+ let actualLength = 0;
+
+ while (true) {
+ let {value, done} = await reader.read();
+ chunks.push(value);
+ actualLength += value.byteLength;
+
+ if (actualLength >= targetLength || done) {
+ // It would be better to allocate |buffer| up front with the number of
+ // of bytes expected but this is the best that can be done without a
+ // BYOB reader to control the amount of data read.
+ const buffer = new Uint8Array(actualLength);
+ chunks.reduce((offset, chunk) => {
+ buffer.set(chunk, offset);
+ return offset + chunk.byteLength;
+ }, 0);
+ return buffer;
+ }
+ }
+}