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