test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. var assert = require('assert');
  2. var Int64 = require('./Int64');
  3. exports.setUp = function(done) {
  4. done();
  5. };
  6. exports.testBufferToString = function(test) {
  7. var int = new Int64(0xfffaffff, 0xfffff700);
  8. test.equal(
  9. int.toBuffer().toString('hex'),
  10. 'fffafffffffff700',
  11. 'Buffer to string conversion'
  12. );
  13. test.done();
  14. };
  15. exports.testBufferCopy = function(test) {
  16. var src = new Int64(0xfffaffff, 0xfffff700);
  17. var dst = new Buffer(8);
  18. src.copy(dst);
  19. test.deepEqual(
  20. dst,
  21. new Buffer([0xff, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xf7, 0x00]),
  22. 'Copy to buffer'
  23. );
  24. test.done();
  25. };
  26. exports.testValueRepresentation = function(test) {
  27. var args = [
  28. [0], '0000000000000000', 0,
  29. [1], '0000000000000001', 1,
  30. [-1], 'ffffffffffffffff', -1,
  31. [1e18], '0de0b6b3a7640000', 1e18,
  32. ['0001234500654321'], '0001234500654321', 0x1234500654321,
  33. ['0ff1234500654321'], '0ff1234500654321', 0xff1234500654300, // Imprecise!
  34. [0xff12345, 0x654321], '0ff1234500654321', 0xff1234500654300, // Imprecise!
  35. [0xfffaffff, 0xfffff700],'fffafffffffff700', -0x5000000000900,
  36. [0xafffffff, 0xfffff700],'affffffffffff700', -0x5000000000000800, // Imprecise!
  37. ['0x0000123450654321'], '0000123450654321', 0x123450654321,
  38. ['0xFFFFFFFFFFFFFFFF'], 'ffffffffffffffff', -1
  39. ];
  40. // Test constructor argments
  41. for (var i = 0; i < args.length; i += 3) {
  42. var a = args[i], octets = args[i+1], number = args[i+2];
  43. // Create instance
  44. var x = new Int64();
  45. Int64.apply(x, a);
  46. test.equal(x.toOctetString(), octets, 'Constuctor with ' + args.join(', '));
  47. test.equal(x.toNumber(true), number);
  48. }
  49. test.done();
  50. };
  51. exports.testBufferOffsets = function(test) {
  52. var sourceBuffer = new Buffer(16);
  53. sourceBuffer.writeUInt32BE(0xfffaffff, 2);
  54. sourceBuffer.writeUInt32BE(0xfffff700, 6);
  55. var int = new Int64(sourceBuffer, 2);
  56. assert.equal(
  57. int.toBuffer().toString('hex'), 'fffafffffffff700',
  58. 'Construct from offset'
  59. );
  60. var targetBuffer = new Buffer(16);
  61. int.copy(targetBuffer, 4);
  62. assert.equal(
  63. targetBuffer.slice(4, 12).toString('hex'), 'fffafffffffff700',
  64. 'Copy to offset'
  65. );
  66. test.done();
  67. };
  68. exports.testInstanceOf = function(test) {
  69. var x = new Int64();
  70. assert(x instanceof Int64, 'Variable is not instance of Int64');
  71. var y = {};
  72. assert(!(y instanceof Int64), 'Object is an instance of Int64');
  73. test.done();
  74. };
  75. exports.testCompare = function(test) {
  76. var intMin = new Int64(2147483648, 0);
  77. var intMinPlusOne = new Int64(2147483648, 1);
  78. var zero = new Int64(0, 0);
  79. var intMaxMinusOne = new Int64(2147483647, 4294967294);
  80. var intMax = new Int64(2147483647, 4294967295);
  81. assert(intMin.compare(intMinPlusOne) < 0, "INT64_MIN is not less than INT64_MIN+1");
  82. assert(intMin.compare(zero) < 0, "INT64_MIN is not less than 0");
  83. assert(intMin.compare(zero) < intMax, "INT64_MIN is not less than INT64_MAX");
  84. assert(intMax.compare(intMaxMinusOne) > 0, "INT64_MAX is not greater than INT64_MAX-1");
  85. assert(intMax.compare(zero) > 0, "INT64_MAX is not greater than 0");
  86. assert(intMax.compare(intMin) > 0, "INT64_MAX is not greater than INT_MIN");
  87. test.done();
  88. };
  89. exports.testEquals = function(test) {
  90. var intMin = new Int64(2147483648, 0);
  91. var zero = new Int64(0, 0);
  92. var intMax = new Int64(2147483647, 4294967295);
  93. assert(intMin.equals(intMin), "INT64_MIN !== INT64_MIN");
  94. assert(intMax.equals(intMax), "INT64_MAX !== INT64_MAX");
  95. assert(zero.equals(zero), "0 !== 0");
  96. assert(!intMin.equals(zero), "INT64_MIN === 0");
  97. assert(!intMin.equals(intMax), "INT64_MIN === INT64_MAX");
  98. assert(!intMax.equals(zero), "INT64_MAX === 0");
  99. assert(!intMax.equals(intMin), "INT64_MAX === INT64_MIN");
  100. test.done();
  101. };