node-int64
Advanced tools
Comparing version 0.3.3 to 0.4.0
34
Int64.js
@@ -79,2 +79,5 @@ // Int64.js | ||
Int64.prototype = { | ||
constructor: Int64, | ||
/** | ||
@@ -230,2 +233,33 @@ * Do in-place 2's compliment. See | ||
/** | ||
* Returns a number indicating whether this comes before or after or is the | ||
* same as the other in sort order. | ||
* | ||
* @param {Int64} other Other Int64 to compare. | ||
*/ | ||
compare: function(other) { | ||
// If sign bits differ ... | ||
if ((this.buffer[this.offset] & 0x80) != (other.buffer[other.offset] & 0x80)) { | ||
return other.buffer[other.offset] - this.buffer[this.offset]; | ||
} | ||
// otherwise, compare bytes lexicographically | ||
for (var i = 0; i < 8; i++) { | ||
if (this.buffer[this.offset+i] !== other.buffer[other.offset+i]) { | ||
return this.buffer[this.offset+i] - other.buffer[other.offset+i]; | ||
} | ||
} | ||
return 0; | ||
}, | ||
/** | ||
* Returns a boolean indicating if this integer is equal to other. | ||
* | ||
* @param {Int64} other Other Int64 to compare. | ||
*/ | ||
equals: function(other) { | ||
return this.compare(other) === 0; | ||
}, | ||
/** | ||
* Pretty output in console.log | ||
@@ -232,0 +266,0 @@ */ |
@@ -16,3 +16,3 @@ { | ||
"main": "./Int64.js", | ||
"version": "0.3.3", | ||
"version": "0.4.0", | ||
"scripts": { | ||
@@ -19,0 +19,0 @@ "test": "nodeunit test.js" |
37
test.js
@@ -84,1 +84,38 @@ var assert = require('assert'); | ||
}; | ||
exports.testInstanceOf = function(test) { | ||
var x = new Int64(); | ||
assert(x instanceof Int64, 'Variable is not instance of Int64'); | ||
var y = {}; | ||
assert(!(y instanceof Int64), 'Object is an instance of Int64'); | ||
test.done(); | ||
}; | ||
exports.testCompare = function(test) { | ||
var intMin = new Int64(2147483648, 0); | ||
var intMinPlusOne = new Int64(2147483648, 1); | ||
var zero = new Int64(0, 0); | ||
var intMaxMinusOne = new Int64(2147483647, 4294967294); | ||
var intMax = new Int64(2147483647, 4294967295); | ||
assert(intMin.compare(intMinPlusOne) < 0, "INT64_MIN is not less than INT64_MIN+1"); | ||
assert(intMin.compare(zero) < 0, "INT64_MIN is not less than 0"); | ||
assert(intMin.compare(zero) < intMax, "INT64_MIN is not less than INT64_MAX"); | ||
assert(intMax.compare(intMaxMinusOne) > 0, "INT64_MAX is not greater than INT64_MAX-1"); | ||
assert(intMax.compare(zero) > 0, "INT64_MAX is not greater than 0"); | ||
assert(intMax.compare(intMin) > 0, "INT64_MAX is not greater than INT_MIN"); | ||
test.done(); | ||
}; | ||
exports.testEquals = function(test) { | ||
var intMin = new Int64(2147483648, 0); | ||
var zero = new Int64(0, 0); | ||
var intMax = new Int64(2147483647, 4294967295); | ||
assert(intMin.equals(intMin), "INT64_MIN !== INT64_MIN"); | ||
assert(intMax.equals(intMax), "INT64_MAX !== INT64_MAX"); | ||
assert(zero.equals(zero), "0 !== 0"); | ||
assert(!intMin.equals(zero), "INT64_MIN === 0"); | ||
assert(!intMin.equals(intMax), "INT64_MIN === INT64_MAX"); | ||
assert(!intMax.equals(zero), "INT64_MAX === 0"); | ||
assert(!intMax.equals(intMin), "INT64_MAX === INT64_MIN"); | ||
test.done(); | ||
}; |
16268
337