Best JavaScript code snippet using wpt
read_write_async_buffer_offset.tentative.https.any.js
Source:read_write_async_buffer_offset.tentative.https.any.js
1// META: title=NativeIO API: Read/write correctly offsets into the source buffer2// META: global=window,worker3// META: script=resources/support.js4'use strict';5promise_test(async testCase => {6 await reserveAndCleanupCapacity(testCase);7 const file = await storageFoundation.open('test_file');8 testCase.add_cleanup(async () => {9 await file.close();10 await storageFoundation.delete('test_file');11 });12 const writeBufferData = new Uint8Array([64, 65, 66, 67, 68, 69, 70, 71]);13 const writeBufferSource = new Uint8Array(writeBufferData.buffer, 3, 4);14 assert_equals(writeBufferSource.byteOffset, 3, 'Test setup error');15 assert_equals(writeBufferSource.byteLength, 4, 'Test setup error');16 assert_equals(writeBufferSource.buffer.byteLength, 8, 'Test setup error');17 const {buffer: writeBuffer, writtenBytes} =18 await file.write(writeBufferSource, 0);19 assert_equals(20 writtenBytes, 4,21 'NativeIOFile.write() should resolve with the number of bytes written');22 assert_equals(23 writeBuffer.byteOffset, 3,24 'NativeIOFile.write() should produce the same view offset as its input');25 assert_equals(26 writeBuffer.byteLength, 4,27 'NativeIOFile.write() should produce the same view size as its input');28 assert_equals(29 writeBuffer.buffer.byteLength, 8,30 'NativeIOFile.write() should use the same buffer as its input');31 const {buffer: readBuffer, readBytes} = await file.read(new Uint8Array(4), 0);32 assert_equals(33 readBytes, 4,34 'NativeIOFile.write() should write the entire view size');35 assert_array_equals(36 readBuffer, [67, 68, 69, 70],37 'NativeIOFile.write() should account for the buffer offset');38}, 'NativeIOFile.write with a Uint8Array accounts for the buffer offset');39promise_test(async testCase => {40 await reserveAndCleanupCapacity(testCase);41 const file = await storageFoundation.open('test_file');42 testCase.add_cleanup(async () => {43 await file.close();44 await storageFoundation.delete('test_file');45 });46 const writeBufferData = new Uint16Array(47 [0x4041, 0x4243, 0x4445, 0x4647, 0x4849, 0x4a4b, 0x4c4d, 0x4e4f]);48 const writeBufferSource = new Uint16Array(writeBufferData.buffer, 6, 4);49 assert_equals(writeBufferSource.byteOffset, 6, 'Test setup error');50 assert_equals(writeBufferSource.byteLength, 8, 'Test setup error');51 assert_equals(writeBufferSource.buffer.byteLength, 16, 'Test setup error');52 const {buffer: writeBuffer, writtenBytes} =53 await file.write(writeBufferSource, 0);54 assert_equals(55 writtenBytes, 8,56 'NativeIOFile.write() should resolve with the number of bytes written');57 assert_equals(58 writeBuffer.byteOffset, 6,59 'NativeIOFile.write() should produce the same view offset as its input');60 assert_equals(61 writeBuffer.byteLength, 8,62 'NativeIOFile.write() should produce the same view size as its input');63 assert_equals(64 writeBuffer.buffer.byteLength, 16,65 'NativeIOFile.write() should use the same buffer as its input');66 const {buffer: readBuffer, readBytes} =67 await file.read(new Uint16Array(4), 0);68 assert_equals(69 readBytes, 8,70 'NativeIOFile.write() should write the entire view size');71 assert_array_equals(72 readBuffer, [0x4647, 0x4849, 0x4a4b, 0x4c4d],73 'NativeIOFile.write() should account for the buffer offset');74}, 'NativeIOFile.write with a Uint16Array accounts for the buffer offset');75promise_test(async testCase => {76 await reserveAndCleanupCapacity(testCase);77 const file = await storageFoundation.open('test_file');78 testCase.add_cleanup(async () => {79 await file.close();80 await storageFoundation.delete('test_file');81 });82 const {buffer: writeBuffer, writtenBytes} =83 await file.write(new Uint8Array([64, 65, 66, 67]), 0);84 assert_equals(85 writtenBytes, 4,86 'NativeIOFile.write() should resolve with the number of bytes written');87 const readBufferData = new Uint8Array(8);88 const readBufferSource = new Uint8Array(readBufferData.buffer, 3, 4);89 assert_equals(readBufferSource.byteOffset, 3, 'Test setup error');90 assert_equals(readBufferSource.byteLength, 4, 'Test setup error');91 assert_equals(readBufferSource.buffer.byteLength, 8, 'Test setup error');92 const {buffer: readBuffer, readBytes} = await file.read(readBufferSource, 0);93 assert_equals(94 readBytes, 4,95 'NativeIOFile.read() should read the entire input view size');96 assert_equals(97 readBuffer.byteOffset, 3,98 'NativeIOFile.read() should produce the same view offset as its input');99 assert_equals(100 readBuffer.byteLength, 4,101 'NativeIOFile.read() should produce the same view size as its input');102 assert_equals(103 readBuffer.buffer.byteLength, 8,104 'NativeIOFile.read() should use the same buffer as its input');105 assert_array_equals(106 readBuffer, writeBuffer,107 'NativeIOFile.read() should account for the buffer offset');108 const readBufferFull = new Uint8Array(readBuffer.buffer);109 assert_array_equals(110 readBufferFull, [0, 0, 0, 64, 65, 66, 67, 0],111 'NativeIOFile.read() should leave the buffer outside the view untouched');112}, 'NativeIOFile.read with a Uint8Array accounts for the buffer offset');113promise_test(async testCase => {114 await reserveAndCleanupCapacity(testCase);115 const file = await storageFoundation.open('test_file');116 testCase.add_cleanup(async () => {117 await file.close();118 await storageFoundation.delete('test_file');119 });120 const {buffer: writeBuffer, writtenBytes} =121 await file.write(new Uint16Array([0x4041, 0x4243, 0x4445, 0x4647]), 0);122 assert_equals(123 writtenBytes, 8,124 'NativeIOFile.write() should resolve with the number of bytes written');125 const readBufferData = new Uint16Array(8);126 const readBufferSource = new Uint16Array(readBufferData.buffer, 6, 4);127 assert_equals(readBufferSource.byteOffset, 6, 'Test setup error');128 assert_equals(readBufferSource.byteLength, 8, 'Test setup error');129 assert_equals(readBufferSource.buffer.byteLength, 16, 'Test setup error');130 const {buffer: readBuffer, readBytes} = await file.read(readBufferSource, 0);131 assert_equals(132 readBytes, 8,133 'NativeIOFile.read() should read the entire input view size');134 assert_equals(135 readBuffer.byteOffset, 6,136 'NativeIOFile.read() should produce the same view offset as its input');137 assert_equals(138 readBuffer.byteLength, 8,139 'NativeIOFile.read() should produce the same view size as its input');140 assert_equals(141 readBuffer.buffer.byteLength, 16,142 'NativeIOFile.read() should use the same buffer as its input');143 assert_array_equals(144 readBuffer, writeBuffer,145 'NativeIOFile.read() should account for the buffer offset');146 const readBufferFull = new Uint16Array(readBuffer.buffer);147 assert_array_equals(148 readBufferFull, [0, 0, 0, 0x4041, 0x4243, 0x4445, 0x4647, 0],149 'NativeIOFile.read() should leave the buffer outside the view untouched');...
read_write_sync_buffer_offset.tentative.https.any.js
Source:read_write_sync_buffer_offset.tentative.https.any.js
1// META: title=NativeIO API: Read/write correctly offsets into the source buffer2// META: global=dedicatedworker3// META: script=resources/support.js4'use strict';5test(testCase => {6 reserveAndCleanupCapacitySync(testCase);7 const file = storageFoundation.openSync('test_file');8 testCase.add_cleanup(() => {9 file.close();10 storageFoundation.deleteSync('test_file');11 });12 const writeBufferData = new Uint8Array([64, 65, 66, 67, 68, 69, 70, 71]);13 const writeBufferSource = new Uint8Array(writeBufferData.buffer, 3, 4);14 assert_equals(writeBufferSource.byteOffset, 3, 'Test setup error');15 assert_equals(writeBufferSource.byteLength, 4, 'Test setup error');16 assert_equals(writeBufferSource.buffer.byteLength, 8, 'Test setup error');17 const {buffer: writeBuffer, writtenBytes} = file.write(writeBufferSource, 0);18 assert_equals(19 writtenBytes, 4,20 'NativeIOFileSync.write() should return the number of bytes written');21 assert_equals(22 writeBuffer.byteOffset, 3,23 'NativeIOFileSync.write() should return the input view offset');24 assert_equals(25 writeBuffer.byteLength, 4,26 'NativeIOFileSync.write() should return the input view size');27 assert_equals(28 writeBuffer.buffer.byteLength, 8,29 'NativeIOFileSync.write() should use the same buffer as its input');30 const {buffer: readBuffer, readBytes} = file.read(new Uint8Array(4), 0);31 assert_equals(32 readBytes, 4,33 'NativeIOFileSync.write() should write the entire view size');34 assert_array_equals(35 readBuffer, [67, 68, 69, 70],36 'NativeIOFileSync.write() should account for the buffer offset');37}, 'NativeIOFileSync.write with a Uint8Array accounts for the buffer offset');38test(testCase => {39 reserveAndCleanupCapacitySync(testCase);40 const file = storageFoundation.openSync('test_file');41 testCase.add_cleanup(() => {42 file.close();43 storageFoundation.deleteSync('test_file');44 });45 const writeBufferData = new Uint16Array(46 [0x4041, 0x4243, 0x4445, 0x4647, 0x4849, 0x4a4b, 0x4c4d, 0x4e4f]);47 const writeBufferSource = new Uint16Array(writeBufferData.buffer, 6, 4);48 assert_equals(writeBufferSource.byteOffset, 6, 'Test setup error');49 assert_equals(writeBufferSource.byteLength, 8, 'Test setup error');50 assert_equals(writeBufferSource.buffer.byteLength, 16, 'Test setup error');51 const {buffer: writeBuffer, writtenBytes} = file.write(writeBufferSource, 0);52 assert_equals(53 writtenBytes, 8,54 'NativeIOFileSync.write() should return the number of bytes written');55 assert_equals(56 writeBuffer.byteOffset, 6,57 'NativeIOFileSync.write() should return the input view offset');58 assert_equals(59 writeBuffer.byteLength, 8,60 'NativeIOFileSync.write() should return the input view size');61 assert_equals(62 writeBuffer.buffer.byteLength, 16,63 'NativeIOFileSync.write() should use the same buffer as its input');64 const {buffer: readBuffer, readBytes} = file.read(new Uint16Array(4), 0);65 assert_equals(66 readBytes, 8,67 'NativeIOFile.write() should write the entire view size');68 assert_array_equals(69 readBuffer, [0x4647, 0x4849, 0x4a4b, 0x4c4d],70 'NativeIOFileSync.write() should account for the buffer offset');71}, 'NativeIOFileSync.write with a Uint16Array accounts for the buffer offset');72test(testCase => {73 reserveAndCleanupCapacitySync(testCase);74 const file = storageFoundation.openSync('test_file');75 testCase.add_cleanup(() => {76 file.close();77 storageFoundation.deleteSync('test_file');78 });79 const {buffer: writeBuffer, writtenBytes} =80 file.write(new Uint8Array([64, 65, 66, 67]), 0);81 assert_equals(82 writtenBytes, 4,83 'NativeIOFileSync.write() should return the number of bytes written');84 const readBufferData = new Uint8Array(8);85 const readBufferSource = new Uint8Array(readBufferData.buffer, 3, 4);86 assert_equals(readBufferSource.byteOffset, 3, 'Test setup error');87 assert_equals(readBufferSource.byteLength, 4, 'Test setup error');88 assert_equals(readBufferSource.buffer.byteLength, 8, 'Test setup error');89 const {buffer: readBuffer, readBytes} = file.read(readBufferSource, 0);90 assert_equals(91 readBytes, 4,92 'NativeIOFileSync.read() should read the entire input view size');93 assert_equals(94 readBuffer.byteOffset, 3,95 'NativeIOFileSync.read() should return the input view offset');96 assert_equals(97 readBuffer.byteLength, 4,98 'NativeIOFileSync.read() should return the input view size');99 assert_equals(100 readBuffer.buffer.byteLength, 8,101 'NativeIOFileSync.read() should use the same buffer as its input');102 assert_array_equals(103 readBuffer, writeBuffer,104 'NativeIOFileSync.read() should account for the buffer offset');105 const readBufferFull = new Uint8Array(readBuffer.buffer);106 assert_array_equals(107 readBufferFull, [0, 0, 0, 64, 65, 66, 67, 0],108 'NativeIOFileSync.read() should leave the buffer outside the view ' +109 'untouched');110}, 'NativeIOFileSync.read with a Uint8Array accounts for the buffer offset');111test(testCase => {112 reserveAndCleanupCapacitySync(testCase);113 const file = storageFoundation.openSync('test_file');114 testCase.add_cleanup(() => {115 file.close();116 storageFoundation.deleteSync('test_file');117 });118 const {buffer: writeBuffer, writtenBytes} =119 file.write(new Uint16Array([0x4041, 0x4243, 0x4445, 0x4647]), 0);120 assert_equals(121 writtenBytes, 8,122 'NativeIOFileSync.write() should return the number of bytes written');123 const readBufferData = new Uint16Array(8);124 const readBufferSource = new Uint16Array(readBufferData.buffer, 6, 4);125 assert_equals(readBufferSource.byteOffset, 6, 'Test setup error');126 assert_equals(readBufferSource.byteLength, 8, 'Test setup error');127 assert_equals(readBufferSource.buffer.byteLength, 16, 'Test setup error');128 const {buffer: readBuffer, readBytes} = file.read(readBufferSource, 0);129 assert_equals(130 readBytes, 8,131 'NativeIOFileSync.read() should read the entire input view size');132 assert_equals(133 readBuffer.byteOffset, 6,134 'NativeIOFileSync.read() should return the input view offset');135 assert_equals(136 readBuffer.byteLength, 8,137 'NativeIOFileSync.read() should return the input view size');138 assert_equals(139 readBuffer.buffer.byteLength, 16,140 'NativeIOFileSync.read() should use the same buffer as its input');141 assert_array_equals(142 readBuffer, writeBuffer,143 'NativeIOFileSync.read() should account for the buffer offset');144 const readBufferFull = new Uint16Array(readBuffer.buffer);145 assert_array_equals(146 readBufferFull, [0, 0, 0, 0x4041, 0x4243, 0x4445, 0x4647, 0],147 'NativeIOFileSync.read() should leave the buffer outside the view ' +148 'untouched');...
i2c-addressed.ts
Source:i2c-addressed.ts
1/* eslint-disable fp/no-unused-expression */2/* eslint-disable no-undefined */3import { I2CAddress, I2CBufferSource, I2CBus } from './i2c'4const BASE_16 = 165export type ABOptions = {6 sharedReadBuffer?: I2CBufferSource,7 allocOnRead: boolean,8 allowMixedReadBuffers: boolean,9 maxReadLength: number10 maxWriteLength: number,11 validateReadWriteLengths: boolean12}13const WARN_READ_LENGTH = 3214const WARN_WRITE_LENGTH = 3215function assertBufferSource(bs: I2CBufferSource) {16 if(bs === undefined) {17 throw new Error('bufferSource undefined')18 }19 const isView = ArrayBuffer.isView(bs)20 const isAB = bs instanceof ArrayBuffer21 if(!isView && !isAB) {22 throw new Error('bufferSource is not ArrayBuffer or ArrayBufferView')23 }24}25/**26 * I2CBus layer providing address encapsulation.27 **/28export class I2CAddressedBus {29 private readonly address: I2CAddress30 private readonly bus: I2CBus31 private readonly options: ABOptions32 static from(bus: I2CBus, address: I2CAddress, options?: Partial<ABOptions>): I2CAddressedBus {33 return new I2CAddressedBus(bus, address, options)34 }35 constructor(bus: I2CBus, address: I2CAddress, options: Partial<ABOptions> = { allocOnRead: true }) {36 this.address = address37 this.bus = bus38 this.options = {39 sharedReadBuffer: options.sharedReadBuffer ?? undefined,40 allocOnRead: options.allocOnRead === true,41 allowMixedReadBuffers: false,42 maxReadLength: WARN_READ_LENGTH,43 maxWriteLength: WARN_WRITE_LENGTH,44 validateReadWriteLengths: true45 }46 }47 get name(): string {48 return this.bus.name + ':0x' + this.address.toString(BASE_16)49 }50 private _getReadBuffer(length: number, readBufferSource?: I2CBufferSource): I2CBufferSource {51 const hasOptionsBuffer = this.options.sharedReadBuffer !== undefined52 if(readBufferSource !== undefined && !(hasOptionsBuffer && !this.options.allowMixedReadBuffers)) {53 return readBufferSource54 }55 if(this.options.sharedReadBuffer !== undefined) {56 return this.options.sharedReadBuffer57 }58 if(this.options.allocOnRead) {59 return new ArrayBuffer(length)60 }61 throw new Error('no provided read buffer and allocation disabled')62 }63 close(): void { return this.bus.close() }64 async readI2cBlock(cmd: number, length: number, readBufferSource?: I2CBufferSource): Promise<ArrayBuffer> {65 if(length > this.options.maxReadLength) {66 throw new Error('read length greater then max configured')67 }68 const readBuffer = this._getReadBuffer(length, readBufferSource)69 const { bytesRead, buffer } = await this.bus.readI2cBlock(this.address, cmd, length, readBuffer)70 if(this.options.validateReadWriteLengths && bytesRead !== length) {71 throw new Error('read length mismatch - ' + bytesRead + ' / ' + length)72 }73 //74 return buffer.slice(0, bytesRead)75 }76 async writeI2cBlock(cmd: number, bufferSource: I2CBufferSource): Promise<void> {77 assertBufferSource(bufferSource)78 if(bufferSource.byteLength > this.options.maxWriteLength) {79 throw new Error('write length greater then max configured')80 }81 const { bytesWritten } = await this.bus.writeI2cBlock(this.address, cmd, bufferSource.byteLength, bufferSource)82 if(this.options.validateReadWriteLengths && bytesWritten !== bufferSource.byteLength) {83 throw new Error('write length mismatch: ' + bytesWritten + '/' + bufferSource.byteLength)84 }85 }86 async sendByte(value: number): Promise<void> {87 await this.bus.sendByte(this.address, value)88 }89 async i2cRead(length: number, readBufferSource?: I2CBufferSource): Promise<ArrayBuffer> {90 const readBuffer = this._getReadBuffer(length, readBufferSource)91 const { bytesRead, buffer } = await this.bus.i2cRead(this.address, length, readBuffer)92 if(this.options.validateReadWriteLengths && bytesRead !== length) {93 throw new Error('read length mismatch: ' + bytesRead + '/' + length)94 }95 //96 return buffer.slice(0, bytesRead)97 }98 async i2cWrite(bufferSource: I2CBufferSource): Promise<void> {99 assertBufferSource(bufferSource)100 const { bytesWritten } = await this.bus.i2cWrite(this.address, bufferSource.byteLength, bufferSource)101 if(this.options.validateReadWriteLengths && bytesWritten !== bufferSource.byteLength) {102 throw new Error('write length mismatch')103 }104 }...
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var buffer = new Buffer(100);4var fd = fs.openSync('test.txt', 'r');5wptools.readBufferSource(fd, buffer, 0, 100, 0, function(err, bytesRead, buffer) {6 if (err) {7 console.log(err);8 } else {9 console.log(buffer.toString());10 }11});12wptools.readBufferSource(source, buffer, offset, length, position, callback)13var wptools = require('wptools');14var fs = require('fs');15var buffer = new Buffer(100);16var fd = fs.openSync('test.txt', 'r');17wptools.readBufferSource(fd, buffer, 0, 100, 0, function(err, bytesRead, buffer) {18 if (err) {19 console.log(err);20 } else {21 console.log(buffer.toString());22 }23});24wptools.readFile(filename, options, callback)25var wptools = require('wptools');26wptools.readFile('test.txt
Using AI Code Generation
1const wptools = require('wp-tools');2const fs = require('fs');3var data = fs.readFileSync('test.docx');4wptools.readBufferSource(data).then((result) => {5 console.log(result);6}).catch((err) => {7 console.log(err);8});9const wptools = require('wp-tools');10const fs = require('fs');11var data = fs.readFileSync('test.docx');12var result = wptools.readBufferSourceSync(data);13console.log(result);14const wptools = require('wp-tools');15const fs = require('fs');16var data = fs.createReadStream('test.docx');17wptools.readStreamSource(data).then((result) => {18 console.log(result);19}).catch((err) => {20 console.log(err);21});22const wptools = require('wp-tools');23const fs = require('fs');24var data = fs.createReadStream('test.docx');25var result = wptools.readStreamSourceSync(data);26console.log(result);27[MIT](LICENSE)
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!