1'use strict'; 2 3const common = require('../common'); 4const assert = require('assert'); 5const path = require('path'); 6const fs = require('fs'); 7const tmpdir = require('../common/tmpdir'); 8 9tmpdir.refresh(); 10 11const expected = 'ümlaut. Лорем 運務ホソモ指及 आपको करने विकास 紙読決多密所 أضف'; 12 13let cnt = 0; 14const getFileName = () => path.join(tmpdir.path, `readv_${++cnt}.txt`); 15const exptectedBuff = Buffer.from(expected); 16 17const allocateEmptyBuffers = (combinedLength) => { 18 const bufferArr = []; 19 // Allocate two buffers, each half the size of exptectedBuff 20 bufferArr[0] = Buffer.alloc(Math.floor(combinedLength / 2)); 21 bufferArr[1] = Buffer.alloc(combinedLength - bufferArr[0].length); 22 23 return bufferArr; 24}; 25 26const getCallback = (fd, bufferArr) => { 27 return common.mustSucceed((bytesRead, buffers) => { 28 assert.deepStrictEqual(bufferArr, buffers); 29 const expectedLength = exptectedBuff.length; 30 assert.deepStrictEqual(bytesRead, expectedLength); 31 fs.closeSync(fd); 32 33 assert(Buffer.concat(bufferArr).equals(exptectedBuff)); 34 }); 35}; 36 37// fs.readv with array of buffers with all parameters 38{ 39 const filename = getFileName(); 40 const fd = fs.openSync(filename, 'w+'); 41 fs.writeSync(fd, exptectedBuff); 42 43 const bufferArr = allocateEmptyBuffers(exptectedBuff.length); 44 const callback = getCallback(fd, bufferArr); 45 46 fs.readv(fd, bufferArr, 0, callback); 47} 48 49// fs.readv with array of buffers without position 50{ 51 const filename = getFileName(); 52 fs.writeFileSync(filename, exptectedBuff); 53 const fd = fs.openSync(filename, 'r'); 54 55 const bufferArr = allocateEmptyBuffers(exptectedBuff.length); 56 const callback = getCallback(fd, bufferArr); 57 58 fs.readv(fd, bufferArr, callback); 59} 60 61/** 62 * Testing with incorrect arguments 63 */ 64const wrongInputs = [false, 'test', {}, [{}], ['sdf'], null, undefined]; 65 66{ 67 const filename = getFileName(2); 68 fs.writeFileSync(filename, exptectedBuff); 69 const fd = fs.openSync(filename, 'r'); 70 71 72 wrongInputs.forEach((wrongInput) => { 73 assert.throws( 74 () => fs.readv(fd, wrongInput, null, common.mustNotCall()), { 75 code: 'ERR_INVALID_ARG_TYPE', 76 name: 'TypeError' 77 } 78 ); 79 }); 80 81 fs.closeSync(fd); 82} 83 84{ 85 // fs.readv with wrong fd argument 86 wrongInputs.forEach((wrongInput) => { 87 assert.throws( 88 () => fs.readv(wrongInput, common.mustNotCall()), 89 { 90 code: 'ERR_INVALID_ARG_TYPE', 91 name: 'TypeError' 92 } 93 ); 94 }); 95} 96