Skip to content

Commit c1707f9

Browse files
authored
Merge pull request #351 from NEMStudios/task/g347_uint64_comparison
Added Uint64.compare
2 parents a8cc6cc + 9d16d1b commit c1707f9

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/model/UInt64.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export class UInt64 {
7979
}
8080
return true;
8181
}
82+
8283
/**
8384
* Constructor
8485
* @param uintArray
@@ -135,4 +136,15 @@ export class UInt64 {
135136
public equals(other: UInt64): boolean {
136137
return this.lower === other.lower && this.higher === other.higher;
137138
}
139+
140+
/**
141+
* Compares two UInt64
142+
* @param other
143+
* @returns {number} - -1, 0, 1
144+
*/
145+
public compare(other: UInt64): number {
146+
const long_a = Long.fromBits(this.lower, this.higher, true);
147+
const long_b = Long.fromBits(other.lower, other.higher, true);
148+
return long_a.compare(long_b);
149+
}
138150
}

test/model/UInt64.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,26 @@ describe('Uint64', () => {
9393
});
9494
});
9595

96+
describe('compare', () => {
97+
it('should return -1 - unsigned', () => {
98+
const value = UInt64.fromNumericString('1');
99+
const other = UInt64.fromNumericString('2');
100+
expect(value.compare(other)).to.be.equal(-1);
101+
});
102+
103+
it('should return 0 - unsigned', () => {
104+
const value = UInt64.fromNumericString('1');
105+
const other = UInt64.fromNumericString('1');
106+
expect(value.compare(other)).to.be.equal(0);
107+
});
108+
109+
it('should return 1 - unsigned', () => {
110+
const value = UInt64.fromNumericString('2');
111+
const other = UInt64.fromNumericString('1');
112+
expect(value.compare(other)).to.be.equal(1);
113+
});
114+
});
115+
96116
describe('fromHex', () => {
97117
it('should create from hexadecimal notation', () => {
98118
hexTestCases.forEach((testCase) => {

0 commit comments

Comments
 (0)