11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst path = require('path');
61cb0ef41Sopenharmony_ciconst fs = require('fs');
71cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_citmpdir.refresh();
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst expected = 'ümlaut. Лорем 運務ホソモ指及 आपको करने विकास 紙読決多密所 أضف';
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cilet cnt = 0;
141cb0ef41Sopenharmony_ciconst getFileName = () => path.join(tmpdir.path, `readv_${++cnt}.txt`);
151cb0ef41Sopenharmony_ciconst exptectedBuff = Buffer.from(expected);
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst allocateEmptyBuffers = (combinedLength) => {
181cb0ef41Sopenharmony_ci  const bufferArr = [];
191cb0ef41Sopenharmony_ci  // Allocate two buffers, each half the size of exptectedBuff
201cb0ef41Sopenharmony_ci  bufferArr[0] = Buffer.alloc(Math.floor(combinedLength / 2));
211cb0ef41Sopenharmony_ci  bufferArr[1] = Buffer.alloc(combinedLength - bufferArr[0].length);
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  return bufferArr;
241cb0ef41Sopenharmony_ci};
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciconst getCallback = (fd, bufferArr) => {
271cb0ef41Sopenharmony_ci  return common.mustSucceed((bytesRead, buffers) => {
281cb0ef41Sopenharmony_ci    assert.deepStrictEqual(bufferArr, buffers);
291cb0ef41Sopenharmony_ci    const expectedLength = exptectedBuff.length;
301cb0ef41Sopenharmony_ci    assert.deepStrictEqual(bytesRead, expectedLength);
311cb0ef41Sopenharmony_ci    fs.closeSync(fd);
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci    assert(Buffer.concat(bufferArr).equals(exptectedBuff));
341cb0ef41Sopenharmony_ci  });
351cb0ef41Sopenharmony_ci};
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci// fs.readv with array of buffers with all parameters
381cb0ef41Sopenharmony_ci{
391cb0ef41Sopenharmony_ci  const filename = getFileName();
401cb0ef41Sopenharmony_ci  const fd = fs.openSync(filename, 'w+');
411cb0ef41Sopenharmony_ci  fs.writeSync(fd, exptectedBuff);
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  const bufferArr = allocateEmptyBuffers(exptectedBuff.length);
441cb0ef41Sopenharmony_ci  const callback = getCallback(fd, bufferArr);
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  fs.readv(fd, bufferArr, 0, callback);
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci// fs.readv with array of buffers without position
501cb0ef41Sopenharmony_ci{
511cb0ef41Sopenharmony_ci  const filename = getFileName();
521cb0ef41Sopenharmony_ci  fs.writeFileSync(filename, exptectedBuff);
531cb0ef41Sopenharmony_ci  const fd = fs.openSync(filename, 'r');
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  const bufferArr = allocateEmptyBuffers(exptectedBuff.length);
561cb0ef41Sopenharmony_ci  const callback = getCallback(fd, bufferArr);
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  fs.readv(fd, bufferArr, callback);
591cb0ef41Sopenharmony_ci}
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci/**
621cb0ef41Sopenharmony_ci * Testing with incorrect arguments
631cb0ef41Sopenharmony_ci */
641cb0ef41Sopenharmony_ciconst wrongInputs = [false, 'test', {}, [{}], ['sdf'], null, undefined];
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci{
671cb0ef41Sopenharmony_ci  const filename = getFileName(2);
681cb0ef41Sopenharmony_ci  fs.writeFileSync(filename, exptectedBuff);
691cb0ef41Sopenharmony_ci  const fd = fs.openSync(filename, 'r');
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  wrongInputs.forEach((wrongInput) => {
731cb0ef41Sopenharmony_ci    assert.throws(
741cb0ef41Sopenharmony_ci      () => fs.readv(fd, wrongInput, null, common.mustNotCall()), {
751cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_ARG_TYPE',
761cb0ef41Sopenharmony_ci        name: 'TypeError'
771cb0ef41Sopenharmony_ci      }
781cb0ef41Sopenharmony_ci    );
791cb0ef41Sopenharmony_ci  });
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  fs.closeSync(fd);
821cb0ef41Sopenharmony_ci}
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci{
851cb0ef41Sopenharmony_ci  // fs.readv with wrong fd argument
861cb0ef41Sopenharmony_ci  wrongInputs.forEach((wrongInput) => {
871cb0ef41Sopenharmony_ci    assert.throws(
881cb0ef41Sopenharmony_ci      () => fs.readv(wrongInput, common.mustNotCall()),
891cb0ef41Sopenharmony_ci      {
901cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_ARG_TYPE',
911cb0ef41Sopenharmony_ci        name: 'TypeError'
921cb0ef41Sopenharmony_ci      }
931cb0ef41Sopenharmony_ci    );
941cb0ef41Sopenharmony_ci  });
951cb0ef41Sopenharmony_ci}
96