Skip to content

Commit

Permalink
add amount tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentine1898 committed Mar 18, 2024
1 parent 7e2ddeb commit 8b85b79
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
64 changes: 64 additions & 0 deletions packages/types/src/amount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import {
addAmounts,
displayAmount,
displayUsd,
divideAmounts,
fromBaseUnitAmount,
fromValueView,
isZero,
joinLoHiAmount,
subtractAmounts,
} from './amount';
import { Amount } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/num/v1/num_pb';
import {
Expand Down Expand Up @@ -114,6 +116,68 @@ describe('addAmounts', () => {
});
});

describe('subtractAmounts', () => {
it('should return an Amount with lo and hi equal to 0 when both inputs are 0', () => {
const a = new Amount({ lo: 0n, hi: 0n });
const b = new Amount({ lo: 0n, hi: 0n });

const result = subtractAmounts(a, b);

expect(result.lo).toBe(0n);
expect(result.hi).toBe(0n);
});

it('should correctly subtract two Amounts', () => {
const a = new Amount({ lo: 2n, hi: 2n });
const b = new Amount({ lo: 1n, hi: 1n });

const result = subtractAmounts(a, b);

expect(result.lo).toBe(1n);
expect(result.hi).toBe(1n);
});

it('should throw an error if minuend is less than subtrahend', () => {
const a = new Amount({ lo: 1n, hi: 1n });
const b = new Amount({ lo: 2n, hi: 2n });

expect(() => subtractAmounts(a, b)).toThrow('Amount cannot be negative');
});
});

describe('divideAmounts', () => {
it('should throw an error when dividing by zero', () => {
const a = new Amount({ lo: 1n, hi: 1n });
const b = new Amount({ lo: 0n, hi: 0n });

expect(() => divideAmounts(a, b)).toThrow('Division by zero');
});

it('should return 0n if dividend is zero', () => {
const a = new Amount({ lo: 0n, hi: 0n });
const b = new Amount({ lo: 18446744073709551615n, hi: 1n });
const result = divideAmounts(a, b);

expect(result.isZero()).toBeTruthy();
});

it('should return a number without fractions when dividing without remainder', () => {
const a = new Amount({ lo: 6n, hi: 0n });
const b = new Amount({ lo: 2n, hi: 0n });
const result = divideAmounts(a, b);

expect(result.toNumber()).toBe(3);
});

it('should return a number with specified precision', () => {
const a = new Amount({ lo: 10n, hi: 0n });
const b = new Amount({ lo: 3n, hi: 0n });
const result = divideAmounts(a, b);

expect(result.toFixed(3)).toEqual('3.333');
});
});

describe('Formatting', () => {
describe('displayAmount()', () => {
it('no decimals', () => {
Expand Down
6 changes: 2 additions & 4 deletions packages/types/src/amount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const subtractAmounts = (minuend: Amount, subtrahend: Amount): Amount =>
const joinedMinuend = joinLoHiAmount(minuend);
const joinedSubtrahend = joinLoHiAmount(subtrahend);

if (joinedSubtrahend > joinedMinuend) throw new Error('Amount cannot be negative ');
if (joinedSubtrahend > joinedMinuend) throw new Error('Amount cannot be negative');

const joined = joinedMinuend - joinedSubtrahend;
const { lo, hi } = splitLoHi(joined);
Expand All @@ -46,9 +46,7 @@ export const divideAmounts = (dividend: Amount, divider: Amount): BigNumber => {
const joinedDividend = new BigNumber(joinLoHiAmount(dividend).toString());
const joinedDivider = new BigNumber(joinLoHiAmount(divider).toString());

const bigNumber = joinedDividend.dividedBy(joinedDivider);
console.log(bigNumber);
return bigNumber;
return joinedDividend.dividedBy(joinedDivider);
};

// This function takes a number and formats it in a display-friendly way (en-US locale)
Expand Down

0 comments on commit 8b85b79

Please sign in to comment.