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_ciconst getFileName = (i) => path.join(tmpdir.path, `writev_${i}.txt`);
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci/**
161cb0ef41Sopenharmony_ci * Testing with a array of buffers input
171cb0ef41Sopenharmony_ci */
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci// fs.writev with array of buffers with all parameters
201cb0ef41Sopenharmony_ci{
211cb0ef41Sopenharmony_ci  const filename = getFileName(1);
221cb0ef41Sopenharmony_ci  const fd = fs.openSync(filename, 'w');
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  const buffer = Buffer.from(expected);
251cb0ef41Sopenharmony_ci  const bufferArr = [buffer, buffer];
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  const done = common.mustSucceed((written, buffers) => {
281cb0ef41Sopenharmony_ci    assert.deepStrictEqual(bufferArr, buffers);
291cb0ef41Sopenharmony_ci    const expectedLength = bufferArr.length * buffer.byteLength;
301cb0ef41Sopenharmony_ci    assert.deepStrictEqual(written, expectedLength);
311cb0ef41Sopenharmony_ci    fs.closeSync(fd);
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci    assert(Buffer.concat(bufferArr).equals(fs.readFileSync(filename)));
341cb0ef41Sopenharmony_ci  });
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  fs.writev(fd, bufferArr, null, done);
371cb0ef41Sopenharmony_ci}
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci// fs.writev with array of buffers without position
401cb0ef41Sopenharmony_ci{
411cb0ef41Sopenharmony_ci  const filename = getFileName(2);
421cb0ef41Sopenharmony_ci  const fd = fs.openSync(filename, 'w');
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  const buffer = Buffer.from(expected);
451cb0ef41Sopenharmony_ci  const bufferArr = [buffer, buffer];
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  const done = common.mustSucceed((written, buffers) => {
481cb0ef41Sopenharmony_ci    assert.deepStrictEqual(bufferArr, buffers);
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci    const expectedLength = bufferArr.length * buffer.byteLength;
511cb0ef41Sopenharmony_ci    assert.deepStrictEqual(written, expectedLength);
521cb0ef41Sopenharmony_ci    fs.closeSync(fd);
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci    assert(Buffer.concat(bufferArr).equals(fs.readFileSync(filename)));
551cb0ef41Sopenharmony_ci  });
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  fs.writev(fd, bufferArr, done);
581cb0ef41Sopenharmony_ci}
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci// fs.writev with empty array of buffers
621cb0ef41Sopenharmony_ci{
631cb0ef41Sopenharmony_ci  const filename = getFileName(3);
641cb0ef41Sopenharmony_ci  const fd = fs.openSync(filename, 'w');
651cb0ef41Sopenharmony_ci  const bufferArr = [];
661cb0ef41Sopenharmony_ci  let afterSyncCall = false;
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  const done = common.mustSucceed((written, buffers) => {
691cb0ef41Sopenharmony_ci    assert.strictEqual(buffers.length, 0);
701cb0ef41Sopenharmony_ci    assert.strictEqual(written, 0);
711cb0ef41Sopenharmony_ci    assert(afterSyncCall);
721cb0ef41Sopenharmony_ci    fs.closeSync(fd);
731cb0ef41Sopenharmony_ci  });
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci  fs.writev(fd, bufferArr, done);
761cb0ef41Sopenharmony_ci  afterSyncCall = true;
771cb0ef41Sopenharmony_ci}
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci/**
801cb0ef41Sopenharmony_ci * Testing with wrong input types
811cb0ef41Sopenharmony_ci */
821cb0ef41Sopenharmony_ci{
831cb0ef41Sopenharmony_ci  const filename = getFileName(4);
841cb0ef41Sopenharmony_ci  const fd = fs.openSync(filename, 'w');
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  [false, 'test', {}, [{}], ['sdf'], null, undefined].forEach((i) => {
871cb0ef41Sopenharmony_ci    assert.throws(
881cb0ef41Sopenharmony_ci      () => fs.writev(fd, i, null, common.mustNotCall()), {
891cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_ARG_TYPE',
901cb0ef41Sopenharmony_ci        name: 'TypeError'
911cb0ef41Sopenharmony_ci      }
921cb0ef41Sopenharmony_ci    );
931cb0ef41Sopenharmony_ci  });
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci  fs.closeSync(fd);
961cb0ef41Sopenharmony_ci}
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci// fs.writev with wrong fd types
991cb0ef41Sopenharmony_ci[false, 'test', {}, [{}], null, undefined].forEach((i) => {
1001cb0ef41Sopenharmony_ci  assert.throws(
1011cb0ef41Sopenharmony_ci    () => fs.writev(i, common.mustNotCall()),
1021cb0ef41Sopenharmony_ci    {
1031cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
1041cb0ef41Sopenharmony_ci      name: 'TypeError'
1051cb0ef41Sopenharmony_ci    }
1061cb0ef41Sopenharmony_ci  );
1071cb0ef41Sopenharmony_ci});
108