Skip to content
This repository has been archived by the owner on Dec 18, 2020. It is now read-only.

Commit

Permalink
Added method equals; Added unit tests for instanceof, compare, and eq…
Browse files Browse the repository at this point in the history
…uals
  • Loading branch information
Doug Judd committed Apr 4, 2015
1 parent 2014c01 commit bfdf700
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Int64.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,13 @@ Int64.prototype = {
* @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];
Expand All @@ -243,6 +250,15 @@ Int64.prototype = {
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
*/
Expand Down
37 changes: 37 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,40 @@ exports.testBufferOffsets = function(test) {

test.done();
};

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();
};

0 comments on commit bfdf700

Please sign in to comment.