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