buffer-more-ints-tests.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. 'use strict';
  2. require('./polyfill');
  3. // A simple abbreviation to obtain a buffer from a hex string
  4. function h2b(str) {
  5. return new Buffer(str, "hex");
  6. }
  7. // Reverse a buffer
  8. function reverse(buf) {
  9. var res = new Buffer(buf.length);
  10. for (var i = 0, j = buf.length - 1; j >= 0; i++, j--) {
  11. res[i] = buf[j];
  12. }
  13. return res;
  14. }
  15. // Fill a buffer with distinctive data
  16. function scrub(buf) {
  17. var pos = 0;
  18. var written;
  19. while ((written = buf.write("deadbeef", pos, 4, "hex")) == 4) {
  20. pos += written;
  21. }
  22. return buf;
  23. }
  24. function xint_case(xint) {
  25. return function (assert, hex, val, inexact) {
  26. var readBE = "read" + xint + "BE";
  27. var writeBE = "write" + xint + "BE";
  28. var readLE = "read" + xint + "LE";
  29. var writeLE = "write" + xint + "LE";
  30. var len = hex.length / 2;
  31. // Straightforward read cases
  32. assert.equal(h2b(hex)[readBE](len, 0), val);
  33. assert.equal(reverse(h2b(hex))[readLE](len, 0), val);
  34. // Test straightforward writes and noAssert writes off the ends of
  35. // the buffer
  36. var buf = scrub(new Buffer(len));
  37. buf[writeBE](len, val, 0);
  38. if (!inexact) {
  39. assert.equal(buf.toString("hex"), hex);
  40. } else {
  41. assert.equal(buf[readBE](len, 0), val);
  42. }
  43. scrub(buf);
  44. buf[writeLE](len, val, 0);
  45. if (!inexact) {
  46. assert.equal(reverse(buf).toString("hex"), hex);
  47. } else {
  48. assert.equal(buf[readLE](len, 0), val);
  49. }
  50. // Accesses off the ends of the buffer should throw.
  51. assert.throws(function () { h2b(hex)[readBE](len, -1); });
  52. assert.throws(function () { h2b(hex)[readBE](len, 1); });
  53. assert.throws(function () { reverse(h2b(hex))[readLE](len, 1); });
  54. assert.throws(function () { reverse(h2b(hex))[readLE](len, -1); });
  55. assert.throws(function () { buf[writeBE](len, val, 1); });
  56. assert.throws(function () { buf[writeBE](len, val, -1); });
  57. assert.throws(function () { buf[writeLE](len, val, 1); });
  58. assert.throws(function () { buf[writeLE](len, val, -1); });
  59. };
  60. }
  61. var uint_case = xint_case("UInt");
  62. var int_case = xint_case("Int");
  63. module.exports.uint = function (assert) {
  64. uint_case(assert, "00", 0x00);
  65. uint_case(assert, "01", 0x01);
  66. uint_case(assert, "ff", 0xff);
  67. uint_case(assert, "0000", 0x0000);
  68. uint_case(assert, "0102", 0x0102);
  69. uint_case(assert, "ffff", 0xffff);
  70. uint_case(assert, "000000", 0x000000);
  71. uint_case(assert, "010203", 0x010203);
  72. uint_case(assert, "ffffff", 0xffffff);
  73. uint_case(assert, "00000000", 0x00000000);
  74. uint_case(assert, "01020304", 0x01020304);
  75. uint_case(assert, "ffffffff", 0xffffffff);
  76. uint_case(assert, "0000000000", 0x0000000000);
  77. uint_case(assert, "0102030405", 0x0102030405);
  78. uint_case(assert, "ffffffffff", 0xffffffffff);
  79. uint_case(assert, "000000000000", 0x000000000000);
  80. uint_case(assert, "010203040506", 0x010203040506);
  81. uint_case(assert, "ffffffffffff", 0xffffffffffff);
  82. uint_case(assert, "00000000000000", 0x00000000000000);
  83. uint_case(assert, "01020304050607", 0x01020304050607);
  84. uint_case(assert, "ffffffffffffff", 0xffffffffffffff);
  85. uint_case(assert, "0000000000000000", 0x0000000000000000);
  86. uint_case(assert, "0102030405060708", 0x0102030405060708, true);
  87. uint_case(assert, "ffffffffffffffff", 0xffffffffffffffff);
  88. assert.done();
  89. };
  90. module.exports.int = function (assert) {
  91. int_case(assert, "00", 0x00);
  92. int_case(assert, "01", 0x01);
  93. int_case(assert, "7f", 0x7f);
  94. int_case(assert, "80", -0x80);
  95. int_case(assert, "ff", -0x01);
  96. int_case(assert, "0000", 0x0000);
  97. int_case(assert, "0102", 0x0102);
  98. int_case(assert, "7fff", 0x7fff);
  99. int_case(assert, "8000", -0x8000);
  100. int_case(assert, "ffff", -0x0001);
  101. int_case(assert, "000000", 0x000000);
  102. int_case(assert, "010203", 0x010203);
  103. int_case(assert, "7fffff", 0x7fffff);
  104. int_case(assert, "800000", -0x800000);
  105. int_case(assert, "ffffff", -0x000001);
  106. int_case(assert, "00000000", 0x00000000);
  107. int_case(assert, "01020304", 0x01020304);
  108. int_case(assert, "7fffffff", 0x7fffffff);
  109. int_case(assert, "80000000", -0x80000000);
  110. int_case(assert, "ffffffff", -0x00000001);
  111. int_case(assert, "0000000000", 0x0000000000);
  112. int_case(assert, "0102030405", 0x0102030405);
  113. int_case(assert, "7fffffffff", 0x7fffffffff);
  114. int_case(assert, "8000000000", -0x8000000000);
  115. int_case(assert, "ffffffffff", -0x0000000001);
  116. int_case(assert, "000000000000", 0x000000000000);
  117. int_case(assert, "010203040506", 0x010203040506);
  118. int_case(assert, "7fffffffffff", 0x7fffffffffff);
  119. int_case(assert, "800000000000", -0x800000000000);
  120. int_case(assert, "ffffffffffff", -0x000000000001);
  121. int_case(assert, "00000000000000", 0x00000000000000);
  122. int_case(assert, "01020304050607", 0x01020304050607);
  123. int_case(assert, "7fffffffffffff", 0x7fffffffffffff);
  124. int_case(assert, "80000000000000", -0x80000000000000);
  125. int_case(assert, "ffffffffffffff", -0x00000000000001);
  126. int_case(assert, "0000000000000000", 0x0000000000000000);
  127. int_case(assert, "0102030405060708", 0x0102030405060708, true);
  128. int_case(assert, "7fffffffffffffff", 0x7fffffffffffffff);
  129. int_case(assert, "8000000000000000", -0x8000000000000000);
  130. int_case(assert, "ffffffffffffffff", -0x0000000000000001);
  131. assert.done();
  132. };
  133. module.exports.isContiguousInt = function (assert) {
  134. assert.equal(Buffer.isContiguousInt(0x1fffffffffffff), true);
  135. assert.equal(Buffer.isContiguousInt(0x20000000000000), false);
  136. assert.equal(Buffer.isContiguousInt(-0x1fffffffffffff), true);
  137. assert.equal(Buffer.isContiguousInt(-0x20000000000000), false);
  138. assert.doesNotThrow(function () {
  139. Buffer.assertContiguousInt(0x1fffffffffffff);
  140. });
  141. assert.throws(function () {
  142. Buffer.assertContiguousInt(0x20000000000000);
  143. });
  144. assert.doesNotThrow(function () {
  145. Buffer.assertContiguousInt(-0x1fffffffffffff);
  146. });
  147. assert.throws(function () {
  148. Buffer.assertContiguousInt(-0x20000000000000);
  149. });
  150. assert.done();
  151. };