Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/model/UInt64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,37 @@ export class UInt64 {
const long_b = Long.fromBits(other.lower, other.higher, true);
return long_a.compare(long_b);
}

/**
* UInt64 add operation
* @param other
* @returns {UInt64}
*/
public add(other: UInt64): UInt64 {
const long_value = Long.fromBits(this.lower, this.higher, true);
const long_b = Long.fromBits(other.lower, other.higher, true);
return this.longToUint64(long_value.add(long_b));
}

/**
* UInt64 add operation
* @param other
* @returns {UInt64}
*/
public subtract(other: UInt64): UInt64 {
const long_value = Long.fromBits(this.lower, this.higher, true);
const long_b = Long.fromBits(other.lower, other.higher, true);
if (long_value.compare(long_b) < 0) {
throw new Error('Unsigned substraction result cannot be negative.');
}
return this.longToUint64(long_value.subtract(long_b));
}

/**
* Convert long value to UInt64
* @param longValue long value
*/
private longToUint64(longValue: Long): UInt64 {
return new UInt64([longValue.getLowBitsUnsigned(), longValue.getHighBitsUnsigned()]);
}
}
37 changes: 37 additions & 0 deletions test/model/UInt64.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,41 @@ describe('Uint64', () => {
expect(result).to.be.true;
});
});

describe('Operation', () => {
it('should return added value', () => {
const value = UInt64.fromUint(100);
const other = UInt64.fromUint(1);
const result = value.add(other);
expect(result.compact()).to.be.equal(101);
});
it('should return added value', () => {
const value = UInt64.fromUint(0);
const other = UInt64.fromUint(0);
const result = value.add(other);
expect(result.compact()).to.be.equal(0);
});

it('should return substract value', () => {
const value = UInt64.fromUint(100);
const other = UInt64.fromUint(1);
const result = value.subtract(other);
expect(result.compact()).to.be.equal(99);
});

it('should return substract value', () => {
const value = UInt64.fromUint(1);
const other = UInt64.fromUint(1);
const result = value.subtract(other);
expect(result.compact()).to.be.equal(0);
});

it('should return substract value', () => {
const value = UInt64.fromUint(100);
const other = UInt64.fromUint(1);
expect(() => {
other.subtract(value);
}).to.throw(Error, 'Unsigned substraction result cannot be negative.');
});
});
});