11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_cirequire('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst buffer = require('buffer');
61cb0ef41Sopenharmony_ciconst SlowBuffer = buffer.SlowBuffer;
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst ones = [1, 1, 1, 1];
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci// Should create a Buffer
111cb0ef41Sopenharmony_cilet sb = SlowBuffer(4);
121cb0ef41Sopenharmony_ciassert(sb instanceof Buffer);
131cb0ef41Sopenharmony_ciassert.strictEqual(sb.length, 4);
141cb0ef41Sopenharmony_cisb.fill(1);
151cb0ef41Sopenharmony_cifor (const [key, value] of sb.entries()) {
161cb0ef41Sopenharmony_ci  assert.deepStrictEqual(value, ones[key]);
171cb0ef41Sopenharmony_ci}
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci// underlying ArrayBuffer should have the same length
201cb0ef41Sopenharmony_ciassert.strictEqual(sb.buffer.byteLength, 4);
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci// Should work without new
231cb0ef41Sopenharmony_cisb = SlowBuffer(4);
241cb0ef41Sopenharmony_ciassert(sb instanceof Buffer);
251cb0ef41Sopenharmony_ciassert.strictEqual(sb.length, 4);
261cb0ef41Sopenharmony_cisb.fill(1);
271cb0ef41Sopenharmony_cifor (const [key, value] of sb.entries()) {
281cb0ef41Sopenharmony_ci  assert.deepStrictEqual(value, ones[key]);
291cb0ef41Sopenharmony_ci}
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci// Should work with edge cases
321cb0ef41Sopenharmony_ciassert.strictEqual(SlowBuffer(0).length, 0);
331cb0ef41Sopenharmony_citry {
341cb0ef41Sopenharmony_ci  assert.strictEqual(
351cb0ef41Sopenharmony_ci    SlowBuffer(buffer.kMaxLength).length, buffer.kMaxLength);
361cb0ef41Sopenharmony_ci} catch (e) {
371cb0ef41Sopenharmony_ci  // Don't match on message as it is from the JavaScript engine. V8 and
381cb0ef41Sopenharmony_ci  // ChakraCore provide different messages.
391cb0ef41Sopenharmony_ci  assert.strictEqual(e.name, 'RangeError');
401cb0ef41Sopenharmony_ci}
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci// Should throw with invalid length type
431cb0ef41Sopenharmony_ciconst bufferInvalidTypeMsg = {
441cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_TYPE',
451cb0ef41Sopenharmony_ci  name: 'TypeError',
461cb0ef41Sopenharmony_ci  message: /^The "size" argument must be of type number/,
471cb0ef41Sopenharmony_ci};
481cb0ef41Sopenharmony_ciassert.throws(() => SlowBuffer(), bufferInvalidTypeMsg);
491cb0ef41Sopenharmony_ciassert.throws(() => SlowBuffer({}), bufferInvalidTypeMsg);
501cb0ef41Sopenharmony_ciassert.throws(() => SlowBuffer('6'), bufferInvalidTypeMsg);
511cb0ef41Sopenharmony_ciassert.throws(() => SlowBuffer(true), bufferInvalidTypeMsg);
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci// Should throw with invalid length value
541cb0ef41Sopenharmony_ciconst bufferMaxSizeMsg = {
551cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_VALUE',
561cb0ef41Sopenharmony_ci  name: 'RangeError',
571cb0ef41Sopenharmony_ci  message: /^The argument 'size' is invalid\. Received [^"]*$/
581cb0ef41Sopenharmony_ci};
591cb0ef41Sopenharmony_ciassert.throws(() => SlowBuffer(NaN), bufferMaxSizeMsg);
601cb0ef41Sopenharmony_ciassert.throws(() => SlowBuffer(Infinity), bufferMaxSizeMsg);
611cb0ef41Sopenharmony_ciassert.throws(() => SlowBuffer(-1), bufferMaxSizeMsg);
621cb0ef41Sopenharmony_ciassert.throws(() => SlowBuffer(buffer.kMaxLength + 1), bufferMaxSizeMsg);
63