11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst path = require('path');
51cb0ef41Sopenharmony_ciconst fs = require('fs').promises;
61cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_citmpdir.refresh();
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst expected = 'ümlaut. Лорем 運務ホソモ指及 आपको करने विकास 紙読決多密所 أضف';
111cb0ef41Sopenharmony_ciconst exptectedBuff = Buffer.from(expected);
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cilet cnt = 0;
141cb0ef41Sopenharmony_cifunction getFileName() {
151cb0ef41Sopenharmony_ci  return path.join(tmpdir.path, `readv_promises_${++cnt}.txt`);
161cb0ef41Sopenharmony_ci}
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst allocateEmptyBuffers = (combinedLength) => {
191cb0ef41Sopenharmony_ci  const bufferArr = [];
201cb0ef41Sopenharmony_ci  // Allocate two buffers, each half the size of exptectedBuff
211cb0ef41Sopenharmony_ci  bufferArr[0] = Buffer.alloc(Math.floor(combinedLength / 2));
221cb0ef41Sopenharmony_ci  bufferArr[1] = Buffer.alloc(combinedLength - bufferArr[0].length);
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  return bufferArr;
251cb0ef41Sopenharmony_ci};
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci(async () => {
281cb0ef41Sopenharmony_ci  {
291cb0ef41Sopenharmony_ci    const filename = getFileName();
301cb0ef41Sopenharmony_ci    await fs.writeFile(filename, exptectedBuff);
311cb0ef41Sopenharmony_ci    const handle = await fs.open(filename, 'r');
321cb0ef41Sopenharmony_ci    const bufferArr = allocateEmptyBuffers(exptectedBuff.length);
331cb0ef41Sopenharmony_ci    const expectedLength = exptectedBuff.length;
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci    let { bytesRead, buffers } = await handle.readv([Buffer.from('')],
361cb0ef41Sopenharmony_ci                                                    null);
371cb0ef41Sopenharmony_ci    assert.strictEqual(bytesRead, 0);
381cb0ef41Sopenharmony_ci    assert.deepStrictEqual(buffers, [Buffer.from('')]);
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci    ({ bytesRead, buffers } = await handle.readv(bufferArr, null));
411cb0ef41Sopenharmony_ci    assert.strictEqual(bytesRead, expectedLength);
421cb0ef41Sopenharmony_ci    assert.deepStrictEqual(buffers, bufferArr);
431cb0ef41Sopenharmony_ci    assert(Buffer.concat(bufferArr).equals(await fs.readFile(filename)));
441cb0ef41Sopenharmony_ci    handle.close();
451cb0ef41Sopenharmony_ci  }
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  {
481cb0ef41Sopenharmony_ci    const filename = getFileName();
491cb0ef41Sopenharmony_ci    await fs.writeFile(filename, exptectedBuff);
501cb0ef41Sopenharmony_ci    const handle = await fs.open(filename, 'r');
511cb0ef41Sopenharmony_ci    const bufferArr = allocateEmptyBuffers(exptectedBuff.length);
521cb0ef41Sopenharmony_ci    const expectedLength = exptectedBuff.length;
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci    let { bytesRead, buffers } = await handle.readv([Buffer.from('')]);
551cb0ef41Sopenharmony_ci    assert.strictEqual(bytesRead, 0);
561cb0ef41Sopenharmony_ci    assert.deepStrictEqual(buffers, [Buffer.from('')]);
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci    ({ bytesRead, buffers } = await handle.readv(bufferArr));
591cb0ef41Sopenharmony_ci    assert.strictEqual(bytesRead, expectedLength);
601cb0ef41Sopenharmony_ci    assert.deepStrictEqual(buffers, bufferArr);
611cb0ef41Sopenharmony_ci    assert(Buffer.concat(bufferArr).equals(await fs.readFile(filename)));
621cb0ef41Sopenharmony_ci    handle.close();
631cb0ef41Sopenharmony_ci  }
641cb0ef41Sopenharmony_ci})().then(common.mustCall());
65