Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

test-buffer-swap.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. 'use strict';
  2. var Buffer = require('../../').Buffer;
  3. var assert = require('assert');
  4. // Test buffers small enough to use the JS implementation
  5. var buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
  6. 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]);
  7. assert.strictEqual(buf, buf.swap16());
  8. assert.deepStrictEqual(buf, Buffer.from([0x02, 0x01, 0x04, 0x03, 0x06, 0x05,
  9. 0x08, 0x07, 0x0a, 0x09, 0x0c, 0x0b,
  10. 0x0e, 0x0d, 0x10, 0x0f]));
  11. buf.swap16(); // restore
  12. assert.strictEqual(buf, buf.swap32());
  13. assert.deepStrictEqual(buf, Buffer.from([0x04, 0x03, 0x02, 0x01, 0x08, 0x07,
  14. 0x06, 0x05, 0x0c, 0x0b, 0x0a, 0x09,
  15. 0x10, 0x0f, 0x0e, 0x0d]));
  16. buf.swap32(); // restore
  17. assert.strictEqual(buf, buf.swap64());
  18. assert.deepStrictEqual(buf, Buffer.from([0x08, 0x07, 0x06, 0x05, 0x04, 0x03,
  19. 0x02, 0x01, 0x10, 0x0f, 0x0e, 0x0d,
  20. 0x0c, 0x0b, 0x0a, 0x09]));
  21. // Operates in-place
  22. var buf3 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7]);
  23. buf3.slice(1, 5).swap32();
  24. assert.deepStrictEqual(buf3, Buffer.from([0x1, 0x5, 0x4, 0x3, 0x2, 0x6, 0x7]));
  25. buf3.slice(1, 5).swap16();
  26. assert.deepStrictEqual(buf3, Buffer.from([0x1, 0x4, 0x5, 0x2, 0x3, 0x6, 0x7]));
  27. var buf3_64 = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  28. 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
  29. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  30. 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]);
  31. buf3_64.slice(2, 18).swap64();
  32. assert.deepStrictEqual(buf3_64, Buffer.from([0x01, 0x02, 0x0a, 0x09, 0x08, 0x07,
  33. 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
  34. 0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b,
  35. 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  36. 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
  37. 0x0f, 0x10]));
  38. // Force use of native code (Buffer size above threshold limit for js impl)
  39. var buf4A = new Uint32Array(256).fill(0x04030201);
  40. var buf4 = Buffer.from(buf4A.buffer, buf4A.byteOffset);
  41. var buf5A = new Uint32Array(256).fill(0x03040102);
  42. var buf5 = Buffer.from(buf5A.buffer, buf5A.byteOffset);
  43. buf4.swap16();
  44. assert.deepStrictEqual(buf4, buf5);
  45. var buf6A = new Uint32Array(256).fill(0x04030201);
  46. var buf6 = Buffer.from(buf6A.buffer);
  47. var bu7A = new Uint32Array(256).fill(0x01020304);
  48. var buf7 = Buffer.from(bu7A.buffer, bu7A.byteOffset);
  49. buf6.swap32();
  50. assert.deepStrictEqual(buf6, buf7);
  51. var buf8A = new Uint8Array(256 * 8);
  52. var buf9A = new Uint8Array(256 * 8);
  53. for (var i = 0; i < buf8A.length; i++) {
  54. buf8A[i] = i % 8;
  55. buf9A[buf9A.length - i - 1] = i % 8;
  56. }
  57. var buf8 = Buffer.from(buf8A.buffer, buf8A.byteOffset);
  58. var buf9 = Buffer.from(buf9A.buffer, buf9A.byteOffset);
  59. buf8.swap64();
  60. assert.deepStrictEqual(buf8, buf9);
  61. // Test native code with buffers that are not memory-aligned
  62. var buf10A = new Uint8Array(256 * 8);
  63. var buf11A = new Uint8Array(256 * 8 - 2);
  64. for (var i = 0; i < buf10A.length; i++) {
  65. buf10A[i] = i % 2;
  66. }
  67. for (var i = 1; i < buf11A.length; i++) {
  68. buf11A[buf11A.length - i] = (i + 1) % 2;
  69. }
  70. var buf10 = Buffer.from(buf10A.buffer, buf10A.byteOffset);
  71. // 0|1 0|1 0|1...
  72. var buf11 = Buffer.from(buf11A.buffer, buf11A.byteOffset);
  73. // 0|0 1|0 1|0...
  74. buf10.slice(1, buf10.length - 1).swap16();
  75. assert.deepStrictEqual(buf10.slice(0, buf11.length), buf11);
  76. var buf12A = new Uint8Array(256 * 8);
  77. var buf13A = new Uint8Array(256 * 8 - 4);
  78. for (var i = 0; i < buf12A.length; i++) {
  79. buf12A[i] = i % 4;
  80. }
  81. for (var i = 1; i < buf13A.length; i++) {
  82. buf13A[buf13A.length - i] = (i + 1) % 4;
  83. }
  84. var buf12 = Buffer.from(buf12A.buffer, buf12A.byteOffset);
  85. // 0|1 2 3 0|1 2 3...
  86. var buf13 = Buffer.from(buf13A.buffer, buf13A.byteOffset);
  87. // 0|0 3 2 1|0 3 2...
  88. buf12.slice(1, buf12.length - 3).swap32();
  89. assert.deepStrictEqual(buf12.slice(0, buf13.length), buf13);
  90. var buf14A = new Uint8Array(256 * 8);
  91. var buf15A = new Uint8Array(256 * 8 - 8);
  92. for (var i = 0; i < buf14A.length; i++) {
  93. buf14A[i] = i % 8;
  94. }
  95. for (var i = 1; i < buf15A.length; i++) {
  96. buf15A[buf15A.length - i] = (i + 1) % 8;
  97. }
  98. var buf14 = Buffer.from(buf14A.buffer, buf14A.byteOffset);
  99. // 0|1 2 3 4 5 6 7 0|1 2 3 4...
  100. var buf15 = Buffer.from(buf15A.buffer, buf15A.byteOffset);
  101. // 0|0 7 6 5 4 3 2 1|0 7 6 5...
  102. buf14.slice(1, buf14.length - 7).swap64();
  103. assert.deepStrictEqual(buf14.slice(0, buf15.length), buf15);
  104. // Length assertions
  105. var re16 = /Buffer size must be a multiple of 16-bits/;
  106. var re32 = /Buffer size must be a multiple of 32-bits/;
  107. var re64 = /Buffer size must be a multiple of 64-bits/;
  108. assert.throws(() => Buffer.from(buf3).swap16(), re16);
  109. assert.throws(() => Buffer.alloc(1025).swap16(), re16);
  110. assert.throws(() => Buffer.from(buf3).swap32(), re32);
  111. assert.throws(() => buf3.slice(1, 3).swap32(), re32);
  112. assert.throws(() => Buffer.alloc(1025).swap32(), re32);
  113. assert.throws(() => buf3.slice(1, 3).swap64(), re64);
  114. assert.throws(() => Buffer.alloc(1025).swap64(), re64);