11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_cirequire('../common'); 41cb0ef41Sopenharmony_ciconst assert = require('assert'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ci// Test buffers small enough to use the JS implementation 71cb0ef41Sopenharmony_ci{ 81cb0ef41Sopenharmony_ci const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 91cb0ef41Sopenharmony_ci 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]); 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ci assert.strictEqual(buf, buf.swap16()); 121cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf, Buffer.from([0x02, 0x01, 0x04, 0x03, 0x06, 0x05, 131cb0ef41Sopenharmony_ci 0x08, 0x07, 0x0a, 0x09, 0x0c, 0x0b, 141cb0ef41Sopenharmony_ci 0x0e, 0x0d, 0x10, 0x0f])); 151cb0ef41Sopenharmony_ci buf.swap16(); // restore 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci assert.strictEqual(buf, buf.swap32()); 181cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf, Buffer.from([0x04, 0x03, 0x02, 0x01, 0x08, 0x07, 191cb0ef41Sopenharmony_ci 0x06, 0x05, 0x0c, 0x0b, 0x0a, 0x09, 201cb0ef41Sopenharmony_ci 0x10, 0x0f, 0x0e, 0x0d])); 211cb0ef41Sopenharmony_ci buf.swap32(); // restore 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci assert.strictEqual(buf, buf.swap64()); 241cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf, Buffer.from([0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 251cb0ef41Sopenharmony_ci 0x02, 0x01, 0x10, 0x0f, 0x0e, 0x0d, 261cb0ef41Sopenharmony_ci 0x0c, 0x0b, 0x0a, 0x09])); 271cb0ef41Sopenharmony_ci} 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci// Operates in-place 301cb0ef41Sopenharmony_ci{ 311cb0ef41Sopenharmony_ci const buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7]); 321cb0ef41Sopenharmony_ci buf.slice(1, 5).swap32(); 331cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf, Buffer.from([0x1, 0x5, 0x4, 0x3, 0x2, 0x6, 0x7])); 341cb0ef41Sopenharmony_ci buf.slice(1, 5).swap16(); 351cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf, Buffer.from([0x1, 0x4, 0x5, 0x2, 0x3, 0x6, 0x7])); 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci // Length assertions 381cb0ef41Sopenharmony_ci const re16 = /Buffer size must be a multiple of 16-bits/; 391cb0ef41Sopenharmony_ci const re32 = /Buffer size must be a multiple of 32-bits/; 401cb0ef41Sopenharmony_ci const re64 = /Buffer size must be a multiple of 64-bits/; 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci assert.throws(() => Buffer.from(buf).swap16(), re16); 431cb0ef41Sopenharmony_ci assert.throws(() => Buffer.alloc(1025).swap16(), re16); 441cb0ef41Sopenharmony_ci assert.throws(() => Buffer.from(buf).swap32(), re32); 451cb0ef41Sopenharmony_ci assert.throws(() => buf.slice(1, 3).swap32(), re32); 461cb0ef41Sopenharmony_ci assert.throws(() => Buffer.alloc(1025).swap32(), re32); 471cb0ef41Sopenharmony_ci assert.throws(() => buf.slice(1, 3).swap64(), re64); 481cb0ef41Sopenharmony_ci assert.throws(() => Buffer.alloc(1025).swap64(), re64); 491cb0ef41Sopenharmony_ci} 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci{ 521cb0ef41Sopenharmony_ci const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 531cb0ef41Sopenharmony_ci 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 541cb0ef41Sopenharmony_ci 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 551cb0ef41Sopenharmony_ci 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]); 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci buf.slice(2, 18).swap64(); 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf, Buffer.from([0x01, 0x02, 0x0a, 0x09, 0x08, 0x07, 601cb0ef41Sopenharmony_ci 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 611cb0ef41Sopenharmony_ci 0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 621cb0ef41Sopenharmony_ci 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 631cb0ef41Sopenharmony_ci 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 641cb0ef41Sopenharmony_ci 0x0f, 0x10])); 651cb0ef41Sopenharmony_ci} 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci// Force use of native code (Buffer size above threshold limit for js impl) 681cb0ef41Sopenharmony_ci{ 691cb0ef41Sopenharmony_ci const bufData = new Uint32Array(256).fill(0x04030201); 701cb0ef41Sopenharmony_ci const buf = Buffer.from(bufData.buffer, bufData.byteOffset); 711cb0ef41Sopenharmony_ci const otherBufData = new Uint32Array(256).fill(0x03040102); 721cb0ef41Sopenharmony_ci const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset); 731cb0ef41Sopenharmony_ci buf.swap16(); 741cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf, otherBuf); 751cb0ef41Sopenharmony_ci} 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci{ 781cb0ef41Sopenharmony_ci const bufData = new Uint32Array(256).fill(0x04030201); 791cb0ef41Sopenharmony_ci const buf = Buffer.from(bufData.buffer); 801cb0ef41Sopenharmony_ci const otherBufData = new Uint32Array(256).fill(0x01020304); 811cb0ef41Sopenharmony_ci const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset); 821cb0ef41Sopenharmony_ci buf.swap32(); 831cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf, otherBuf); 841cb0ef41Sopenharmony_ci} 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci{ 871cb0ef41Sopenharmony_ci const bufData = new Uint8Array(256 * 8); 881cb0ef41Sopenharmony_ci const otherBufData = new Uint8Array(256 * 8); 891cb0ef41Sopenharmony_ci for (let i = 0; i < bufData.length; i++) { 901cb0ef41Sopenharmony_ci bufData[i] = i % 8; 911cb0ef41Sopenharmony_ci otherBufData[otherBufData.length - i - 1] = i % 8; 921cb0ef41Sopenharmony_ci } 931cb0ef41Sopenharmony_ci const buf = Buffer.from(bufData.buffer, bufData.byteOffset); 941cb0ef41Sopenharmony_ci const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset); 951cb0ef41Sopenharmony_ci buf.swap64(); 961cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf, otherBuf); 971cb0ef41Sopenharmony_ci} 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci// Test native code with buffers that are not memory-aligned 1001cb0ef41Sopenharmony_ci{ 1011cb0ef41Sopenharmony_ci const bufData = new Uint8Array(256 * 8); 1021cb0ef41Sopenharmony_ci const otherBufData = new Uint8Array(256 * 8 - 2); 1031cb0ef41Sopenharmony_ci for (let i = 0; i < bufData.length; i++) { 1041cb0ef41Sopenharmony_ci bufData[i] = i % 2; 1051cb0ef41Sopenharmony_ci } 1061cb0ef41Sopenharmony_ci for (let i = 1; i < otherBufData.length; i++) { 1071cb0ef41Sopenharmony_ci otherBufData[otherBufData.length - i] = (i + 1) % 2; 1081cb0ef41Sopenharmony_ci } 1091cb0ef41Sopenharmony_ci const buf = Buffer.from(bufData.buffer, bufData.byteOffset); 1101cb0ef41Sopenharmony_ci // 0|1 0|1 0|1... 1111cb0ef41Sopenharmony_ci const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset); 1121cb0ef41Sopenharmony_ci // 0|0 1|0 1|0... 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ci buf.slice(1, buf.length - 1).swap16(); 1151cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf.slice(0, otherBuf.length), otherBuf); 1161cb0ef41Sopenharmony_ci} 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ci{ 1191cb0ef41Sopenharmony_ci const bufData = new Uint8Array(256 * 8); 1201cb0ef41Sopenharmony_ci const otherBufData = new Uint8Array(256 * 8 - 4); 1211cb0ef41Sopenharmony_ci for (let i = 0; i < bufData.length; i++) { 1221cb0ef41Sopenharmony_ci bufData[i] = i % 4; 1231cb0ef41Sopenharmony_ci } 1241cb0ef41Sopenharmony_ci for (let i = 1; i < otherBufData.length; i++) { 1251cb0ef41Sopenharmony_ci otherBufData[otherBufData.length - i] = (i + 1) % 4; 1261cb0ef41Sopenharmony_ci } 1271cb0ef41Sopenharmony_ci const buf = Buffer.from(bufData.buffer, bufData.byteOffset); 1281cb0ef41Sopenharmony_ci // 0|1 2 3 0|1 2 3... 1291cb0ef41Sopenharmony_ci const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset); 1301cb0ef41Sopenharmony_ci // 0|0 3 2 1|0 3 2... 1311cb0ef41Sopenharmony_ci 1321cb0ef41Sopenharmony_ci buf.slice(1, buf.length - 3).swap32(); 1331cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf.slice(0, otherBuf.length), otherBuf); 1341cb0ef41Sopenharmony_ci} 1351cb0ef41Sopenharmony_ci 1361cb0ef41Sopenharmony_ci{ 1371cb0ef41Sopenharmony_ci const bufData = new Uint8Array(256 * 8); 1381cb0ef41Sopenharmony_ci const otherBufData = new Uint8Array(256 * 8 - 8); 1391cb0ef41Sopenharmony_ci for (let i = 0; i < bufData.length; i++) { 1401cb0ef41Sopenharmony_ci bufData[i] = i % 8; 1411cb0ef41Sopenharmony_ci } 1421cb0ef41Sopenharmony_ci for (let i = 1; i < otherBufData.length; i++) { 1431cb0ef41Sopenharmony_ci otherBufData[otherBufData.length - i] = (i + 1) % 8; 1441cb0ef41Sopenharmony_ci } 1451cb0ef41Sopenharmony_ci const buf = Buffer.from(bufData.buffer, bufData.byteOffset); 1461cb0ef41Sopenharmony_ci // 0|1 2 3 4 5 6 7 0|1 2 3 4... 1471cb0ef41Sopenharmony_ci const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset); 1481cb0ef41Sopenharmony_ci // 0|0 7 6 5 4 3 2 1|0 7 6 5... 1491cb0ef41Sopenharmony_ci 1501cb0ef41Sopenharmony_ci buf.slice(1, buf.length - 7).swap64(); 1511cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf.slice(0, otherBuf.length), otherBuf); 1521cb0ef41Sopenharmony_ci} 153