11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_cifunction test(arrayBuffer, offset, length) {
71cb0ef41Sopenharmony_ci  const uint8Array = new Uint8Array(arrayBuffer, offset, length);
81cb0ef41Sopenharmony_ci  for (let i = 0; i < length; i += 1) {
91cb0ef41Sopenharmony_ci    uint8Array[i] = 1;
101cb0ef41Sopenharmony_ci  }
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci  const buffer = Buffer.from(arrayBuffer, offset, length);
131cb0ef41Sopenharmony_ci  for (let i = 0; i < length; i += 1) {
141cb0ef41Sopenharmony_ci    assert.strictEqual(buffer[i], 1);
151cb0ef41Sopenharmony_ci  }
161cb0ef41Sopenharmony_ci}
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst acceptableOOMErrors = [
191cb0ef41Sopenharmony_ci  'Array buffer allocation failed',
201cb0ef41Sopenharmony_ci  'Invalid array buffer length',
211cb0ef41Sopenharmony_ci];
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciconst length = 1000;
241cb0ef41Sopenharmony_ciconst offset = 4294967296; /* 1 << 32 */
251cb0ef41Sopenharmony_ciconst size = offset + length;
261cb0ef41Sopenharmony_cilet arrayBuffer;
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_citry {
291cb0ef41Sopenharmony_ci  arrayBuffer = new ArrayBuffer(size);
301cb0ef41Sopenharmony_ci} catch (e) {
311cb0ef41Sopenharmony_ci  if (e instanceof RangeError && acceptableOOMErrors.includes(e.message))
321cb0ef41Sopenharmony_ci    common.skip(`Unable to allocate ${size} bytes for ArrayBuffer`);
331cb0ef41Sopenharmony_ci  throw e;
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_citest(arrayBuffer, offset, length);
37