mirror of
https://github.com/cerc-io/watcher-ts
synced 2024-11-19 20:36:19 +00:00
Add more test cases for numeric host APIs (#66)
* Add test cases for bigInt host APIs * Add test cases for bigDecimal fromString, plus and minus host APIs * Add test cases for bigDecimal times and divideBy host APIs * Throw an error on underflow or overflow in bigDecimal toString host API * Avoid rounding of out of range bigDecimal values * Use big number's byte size to set storage size of bigInt * Add test cases for errors in bigDecimal host API
This commit is contained in:
parent
c75be57146
commit
c79647548e
@ -19,18 +19,20 @@ import loader from '@vulcanize/assemblyscript/lib/loader';
|
||||
import { IndexerInterface } from '@vulcanize/util';
|
||||
|
||||
import { TypeId, Level } from './types';
|
||||
import { Block, fromEthereumValue, toEthereumValue, resolveEntityFieldConflicts, GraphDecimal, digitsToString } from './utils';
|
||||
import {
|
||||
Block,
|
||||
fromEthereumValue,
|
||||
toEthereumValue,
|
||||
resolveEntityFieldConflicts,
|
||||
GraphDecimal,
|
||||
digitsToString,
|
||||
MIN_EXP,
|
||||
MAX_EXP
|
||||
} from './utils';
|
||||
import { Database } from './database';
|
||||
|
||||
const NETWORK_URL = 'http://127.0.0.1:8081';
|
||||
|
||||
// Size (in bytes) of the BN used in bigInt store host API.
|
||||
// The BN is being stored as a byte array in wasm memory in 2's compliment representation and interpreted as such in other APIs.
|
||||
// 33 bytes is chosen so that it can support:
|
||||
// - Int256 (32 bytes sufficient)
|
||||
// - UInt256 (33 bytes required as we are storing the 2's compliment)
|
||||
const BN_SIZE = 33;
|
||||
|
||||
// Endianness of BN used in bigInt store host API.
|
||||
// Negative bigInt is being stored in wasm in 2's compliment, 'le' representation.
|
||||
// (for eg. bigInt.fromString(negativeI32Value))
|
||||
@ -282,6 +284,10 @@ export const instantiate = async (
|
||||
const expStringPtr = await expBigInt.toString();
|
||||
const exp = __getString(expStringPtr);
|
||||
|
||||
if (parseInt(exp) < MIN_EXP || parseInt(exp) > MAX_EXP) {
|
||||
throw new Error(`Big decimal exponent '${exp}' is outside the '${MIN_EXP}' to '${MAX_EXP}' range`);
|
||||
}
|
||||
|
||||
const decimal = new GraphDecimal(`${digits}e${exp}`);
|
||||
const ptr = __newString(decimal.toFixed());
|
||||
|
||||
@ -368,15 +374,20 @@ export const instantiate = async (
|
||||
'bigInt.fromString': async (s: number) => {
|
||||
const string = __getString(s);
|
||||
|
||||
// The BN is being stored as a byte array in wasm memory in 2's compliment representation and interpreted as such in other APIs.
|
||||
// Create a BN in 2's compliment representation.
|
||||
// Need to use BN as ethers.BigNumber:
|
||||
// Doesn't store -ve numbers in 2's compilment form
|
||||
// Stores in big endian form.
|
||||
let bigNumber = new BN(string);
|
||||
bigNumber = bigNumber.toTwos(BN_SIZE * 8);
|
||||
|
||||
// Create an array out of BN in 'le' endianness.
|
||||
const bytes = bigNumber.toArray(BN_ENDIANNESS, BN_SIZE);
|
||||
// Size (in bytes) of the BN stored.
|
||||
// Add an extra byte to the BNs byte length to allow for 2's compiment.
|
||||
const bnSize = bigNumber.byteLength() + 1;
|
||||
bigNumber = bigNumber.toTwos(bnSize * 8);
|
||||
|
||||
// Create a byte array out of BN in 'le' endianness.
|
||||
const bytes = bigNumber.toArray(BN_ENDIANNESS, bnSize);
|
||||
|
||||
const uint8ArrayId = await getIdOfType(TypeId.Uint8Array);
|
||||
const ptr = await __newArray(uint8ArrayId, bytes);
|
||||
|
@ -4,11 +4,24 @@
|
||||
|
||||
import path from 'path';
|
||||
import { expect } from 'chai';
|
||||
import Decimal from 'decimal.js';
|
||||
import BN from 'bn.js';
|
||||
|
||||
import { instantiate } from './loader';
|
||||
import { getTestDatabase, getTestIndexer } from '../test/utils';
|
||||
import { Database } from './database';
|
||||
import { Indexer } from '../test/utils/indexer';
|
||||
import {
|
||||
PRECISION,
|
||||
UINT128_MAX,
|
||||
UINT256_MAX,
|
||||
INT256_MIN,
|
||||
INT256_MAX,
|
||||
DECIMAL128_MIN,
|
||||
DECIMAL128_MAX,
|
||||
DECIMAL128_PMIN,
|
||||
DECIMAL128_NMAX
|
||||
} from './utils';
|
||||
|
||||
const EXAMPLE_WASM_FILE_PATH = '../test/subgraph/example1/build/Example1/Example1.wasm';
|
||||
|
||||
@ -60,34 +73,169 @@ describe('numbers wasm tests', () => {
|
||||
expect(__getString(ptrs[0])).to.equal(__getString(ptrs[1]));
|
||||
expect(__getString(ptrs[2])).to.equal('0');
|
||||
});
|
||||
|
||||
it('should get bigInt for INT256_MIN', async () => {
|
||||
const ptr = await testBigIntFromString(await __newString(INT256_MIN));
|
||||
expect(__getString(ptr)).to.equal(INT256_MIN);
|
||||
});
|
||||
|
||||
it('should get bigInt for INT256_MAX', async () => {
|
||||
const ptr = await testBigIntFromString(await __newString(INT256_MAX));
|
||||
expect(__getString(ptr)).to.equal(INT256_MAX);
|
||||
});
|
||||
|
||||
it('should get bigInt for UINT256_MAX', async () => {
|
||||
const ptr = await testBigIntFromString(await __newString(UINT256_MAX));
|
||||
expect(__getString(ptr)).to.equal(UINT256_MAX);
|
||||
});
|
||||
});
|
||||
|
||||
it('should execute bigInt plus API', async () => {
|
||||
const { testBigIntPlus, __getString } = exports;
|
||||
describe('should execute bigInt plus API', () => {
|
||||
let testBigIntPlus: any, __getString: any, __newString: any;
|
||||
|
||||
const ptr = await testBigIntPlus();
|
||||
expect(__getString(ptr)).to.equal('200');
|
||||
before(() => {
|
||||
({ testBigIntPlus, __getString, __newString } = exports);
|
||||
});
|
||||
|
||||
it('should execute bigInt plus for positive numbers', async () => {
|
||||
const ptr = await testBigIntPlus(await __newString('923567899898'), await __newString('89456153132132'));
|
||||
expect(__getString(ptr)).to.equal('90379721032030');
|
||||
});
|
||||
|
||||
it('should execute bigInt plus for INT256_MAX and INT256_MAX', async () => {
|
||||
const ptr = await testBigIntPlus(await __newString(INT256_MAX), await __newString(INT256_MAX));
|
||||
expect(__getString(ptr)).to.equal('115792089237316195423570985008687907853269984665640564039457584007913129639934');
|
||||
});
|
||||
|
||||
it('should execute bigInt plus for INT256_MIN and INT256_MIN', async () => {
|
||||
const ptr = await testBigIntPlus(await __newString(INT256_MIN), await __newString(INT256_MIN));
|
||||
expect(__getString(ptr)).to.equal('-115792089237316195423570985008687907853269984665640564039457584007913129639936');
|
||||
});
|
||||
|
||||
it('should execute bigInt plus for INT256_MAX and INT256_MIN', async () => {
|
||||
const ptr = await testBigIntPlus(await __newString(INT256_MAX), await __newString(INT256_MIN));
|
||||
expect(__getString(ptr)).to.equal('-1');
|
||||
});
|
||||
});
|
||||
|
||||
it('should execute bigInt minus API', async () => {
|
||||
const { testBigIntMinus, __getString } = exports;
|
||||
describe('should execute bigInt minus API', () => {
|
||||
let testBigIntMinus: any, __getString: any, __newString: any;
|
||||
|
||||
const ptr = await testBigIntMinus();
|
||||
expect(__getString(ptr)).to.equal('100');
|
||||
before(() => {
|
||||
({ testBigIntMinus, __getString, __newString } = exports);
|
||||
});
|
||||
|
||||
it('should execute bigInt minus for positive numbers', async () => {
|
||||
const ptr = await testBigIntMinus(await __newString('923567899898'), await __newString('89456153132132'));
|
||||
expect(__getString(ptr)).to.equal('-88532585232234');
|
||||
});
|
||||
|
||||
it('should execute bigInt minus for UINT256_MAX and UINT256_MAX', async () => {
|
||||
const ptr = await testBigIntMinus(await __newString(UINT256_MAX), await __newString(UINT256_MAX));
|
||||
expect(__getString(ptr)).to.equal('0');
|
||||
});
|
||||
|
||||
it('should execute bigInt minus for INT256_MIN and INT256_MIN', async () => {
|
||||
const ptr = await testBigIntMinus(await __newString(INT256_MIN), await __newString(INT256_MIN));
|
||||
expect(__getString(ptr)).to.equal('0');
|
||||
});
|
||||
|
||||
it('should execute bigInt minus for INT256_MAX and INT256_MIN', async () => {
|
||||
const ptr = await testBigIntMinus(await __newString(INT256_MAX), await __newString(INT256_MIN));
|
||||
expect(__getString(ptr)).to.equal(UINT256_MAX);
|
||||
});
|
||||
|
||||
it('should execute bigInt minus for INT256_MIN and INT256_MAX', async () => {
|
||||
const ptr = await testBigIntMinus(await __newString(INT256_MIN), await __newString(INT256_MAX));
|
||||
expect(__getString(ptr)).to.equal(`-${UINT256_MAX}`);
|
||||
});
|
||||
});
|
||||
|
||||
it('should execute bigInt times API', async () => {
|
||||
const { testBigIntTimes, __getString } = exports;
|
||||
describe('should execute bigInt times API', () => {
|
||||
let testBigIntTimes: any, __getString: any, __newString: any;
|
||||
|
||||
const ptr = await testBigIntTimes();
|
||||
expect(__getString(ptr)).to.equal('1000');
|
||||
before(() => {
|
||||
({ testBigIntTimes, __getString, __newString } = exports);
|
||||
});
|
||||
|
||||
it('should execute bigInt times for positive numbers', async () => {
|
||||
const ptr = await testBigIntTimes(await __newString('923567899898'), await __newString('89456153132132'));
|
||||
expect(__getString(ptr)).to.equal('82618831481197046143322536');
|
||||
});
|
||||
|
||||
it('should execute bigInt times for UINT128_MAX and UINT128_MAX', async () => {
|
||||
const ptr = await testBigIntTimes(await __newString(UINT128_MAX), await __newString(UINT128_MAX));
|
||||
expect(__getString(ptr)).to.equal('115792089237316195423570985008687907852589419931798687112530834793049593217025');
|
||||
});
|
||||
|
||||
it('should execute bigInt times for -UINT128_MAX and UINT128_MAX', async () => {
|
||||
const ptr = await testBigIntTimes(await __newString(`-${UINT128_MAX}`), await __newString(UINT128_MAX));
|
||||
expect(__getString(ptr)).to.equal('-115792089237316195423570985008687907852589419931798687112530834793049593217025');
|
||||
});
|
||||
|
||||
it('should execute bigInt times for -UINT128_MAX and -UINT128_MAX', async () => {
|
||||
const ptr = await testBigIntTimes(await __newString(`-${UINT128_MAX}`), await __newString(`-${UINT128_MAX}`));
|
||||
expect(__getString(ptr)).to.equal('115792089237316195423570985008687907852589419931798687112530834793049593217025');
|
||||
});
|
||||
|
||||
it('should execute bigInt times for UINT256_MAX and 0', async () => {
|
||||
const ptr = await testBigIntTimes(await __newString(UINT256_MAX), await __newString('0'));
|
||||
expect(__getString(ptr)).to.equal('0');
|
||||
});
|
||||
|
||||
it('should execute bigInt times for 0 and 0', async () => {
|
||||
const ptr = await testBigIntTimes(await __newString('0'), await __newString('0'));
|
||||
expect(__getString(ptr)).to.equal('0');
|
||||
});
|
||||
|
||||
it('should execute bigInt times for INT256_MIN and UINT256_MAX', async () => {
|
||||
const ptr = await testBigIntTimes(await __newString(INT256_MIN), await __newString(UINT256_MAX));
|
||||
const expected = new BN(INT256_MIN).mul(new BN(UINT256_MAX)).toString();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it('should execute bigInt dividedBy API', async () => {
|
||||
const { testBigIntDividedBy, __getString } = exports;
|
||||
describe('should execute bigInt dividedBy API', () => {
|
||||
let testBigIntDividedBy: any, __getString: any, __newString: any;
|
||||
|
||||
const ptr = await testBigIntDividedBy();
|
||||
expect(__getString(ptr)).to.equal('100');
|
||||
before(() => {
|
||||
({ testBigIntDividedBy, __getString, __newString } = exports);
|
||||
});
|
||||
|
||||
it('should execute bigInt dividedBy for positive numbers', async () => {
|
||||
const ptr = await testBigIntDividedBy(await __newString('82618831481197046143322536'), await __newString('89456153132132'));
|
||||
expect(__getString(ptr)).to.equal('923567899898');
|
||||
});
|
||||
|
||||
it('should execute bigInt dividedBy for UINT256_MAX and UINT256_MAX', async () => {
|
||||
const ptr = await testBigIntDividedBy(await __newString(UINT256_MAX), await __newString(UINT256_MAX));
|
||||
expect(__getString(ptr)).to.equal('1');
|
||||
});
|
||||
|
||||
it('should execute bigInt dividedBy for -UINT256_MAX and UINT256_MAX', async () => {
|
||||
const ptr = await testBigIntDividedBy(await __newString(`-${UINT256_MAX}`), await __newString(UINT256_MAX));
|
||||
expect(__getString(ptr)).to.equal('-1');
|
||||
});
|
||||
|
||||
it('should execute bigInt dividedBy for -UINT256_MAX and -UINT256_MAX', async () => {
|
||||
const ptr = await testBigIntDividedBy(await __newString(`-${UINT256_MAX}`), await __newString(`-${UINT256_MAX}`));
|
||||
expect(__getString(ptr)).to.equal('1');
|
||||
});
|
||||
|
||||
it('should execute bigInt dividedBy for UINT256_MAX and -UINT256_MAX', async () => {
|
||||
const ptr = await testBigIntDividedBy(await __newString(UINT256_MAX), await __newString(`-${UINT256_MAX}`));
|
||||
expect(__getString(ptr)).to.equal('-1');
|
||||
});
|
||||
|
||||
it('should execute bigInt dividedBy for UINT256_MAX and INT256_MAX', async () => {
|
||||
const ptr = await testBigIntDividedBy(await __newString(UINT256_MAX), await __newString(INT256_MAX));
|
||||
expect(__getString(ptr)).to.equal('2');
|
||||
});
|
||||
|
||||
it('should execute bigInt dividedBy for 0 and UINT256_MAX', async () => {
|
||||
const ptr = await testBigIntDividedBy(await __newString('0'), await __newString(UINT256_MAX));
|
||||
expect(__getString(ptr)).to.equal('0');
|
||||
});
|
||||
});
|
||||
|
||||
describe('should execute bigInt dividedByDecimal API', async () => {
|
||||
@ -118,11 +266,27 @@ describe('numbers wasm tests', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('should execute bigInt mod API', async () => {
|
||||
const { testBigIntMod, __getString, __newString } = exports;
|
||||
describe('should execute bigInt mod API', () => {
|
||||
let testBigIntMod: any, __getString: any, __newString: any;
|
||||
|
||||
const ptr = await testBigIntMod(await __newString('2315432122132354'), await __newString('5465265645'));
|
||||
expect(__getString(ptr)).to.equal('1283174719');
|
||||
before(() => {
|
||||
({ testBigIntMod, __getString, __newString } = exports);
|
||||
});
|
||||
|
||||
it('should execute bigInt mod for positive dividend and positive divisor', async () => {
|
||||
const ptr = await testBigIntMod(await __newString('2315432122132354'), await __newString('5465265645'));
|
||||
expect(__getString(ptr)).to.equal('1283174719');
|
||||
});
|
||||
|
||||
it('should execute bigInt mod for negative dividend and positive divisor', async () => {
|
||||
const ptr = await testBigIntMod(await __newString('-2315432122132354'), await __newString('5465265645'));
|
||||
expect(__getString(ptr)).to.equal('4182090926');
|
||||
});
|
||||
|
||||
it('should execute bigInt dividedBy for UINT256_MAX and UINT256_MAX', async () => {
|
||||
const ptr = await testBigIntMod(await __newString(UINT256_MAX), await __newString(UINT256_MAX));
|
||||
expect(__getString(ptr)).to.equal('0');
|
||||
});
|
||||
});
|
||||
|
||||
it('should execute bigInt bitOr API', async () => {
|
||||
@ -179,37 +343,292 @@ describe('numbers wasm tests', () => {
|
||||
expect(__getString(ptr)).to.equal('43210');
|
||||
});
|
||||
|
||||
it('should get bigDecimal for numbers with decimals', async () => {
|
||||
it('should get bigDecimal for number with negative exponent', async () => {
|
||||
const ptr = await testBigDecimalFromString(await __newString('4.32184561e-30'));
|
||||
expect(__getString(ptr)).to.equal('0.00000000000000000000000000000432184561');
|
||||
});
|
||||
|
||||
it('should get bigDecimal for decimal number having more than 34 digits', async () => {
|
||||
const ptr = await testBigDecimalFromString(await __newString('-5032485723458348569331745849735.3343434634691214453454356561'));
|
||||
expect(__getString(ptr)).to.equal('-5032485723458348569331745849735.334');
|
||||
});
|
||||
|
||||
it('should get bigDecimal for decimal number with whole part having more than 34 digits', async () => {
|
||||
const ptr = await testBigDecimalFromString(await __newString('1157920892373161954235709850086879078532699846.65640564039457584007913129639935'));
|
||||
expect(__getString(ptr)).to.equal('1157920892373161954235709850086879000000000000');
|
||||
});
|
||||
|
||||
it('should get bigDecimal for UINT256_MAX', async () => {
|
||||
const ptr = await testBigDecimalFromString(await __newString(UINT256_MAX));
|
||||
expect(__getString(ptr)).to.equal('115792089237316195423570985008687900000000000000000000000000000000000000000000');
|
||||
});
|
||||
|
||||
it('should get bigDecimal for 0000.000000000', async () => {
|
||||
const ptr = await testBigDecimalFromString(await __newString('0000.000000000'));
|
||||
expect(__getString(ptr)).to.equal('0');
|
||||
});
|
||||
|
||||
it('should get bigDecimal for DECIMAL128_MAX', async () => {
|
||||
const ptr = await testBigDecimalFromString(await __newString(DECIMAL128_MAX));
|
||||
const expected = new Decimal(DECIMAL128_MAX).toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should get bigDecimal for DECIMAL128_MIN', async () => {
|
||||
const ptr = await testBigDecimalFromString(await __newString(DECIMAL128_MIN));
|
||||
const expected = new Decimal(DECIMAL128_MIN).toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should get bigDecimal for DECIMAL128_NMAX', async () => {
|
||||
const ptr = await testBigDecimalFromString(await __newString(DECIMAL128_NMAX));
|
||||
const expected = new Decimal(DECIMAL128_NMAX).toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should get bigDecimal for DECIMAL128_PMIN', async () => {
|
||||
const ptr = await testBigDecimalFromString(await __newString(DECIMAL128_PMIN));
|
||||
const expected = new Decimal(DECIMAL128_PMIN).toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it('should execute bigDecimal plus API', async () => {
|
||||
const { testBigDecimalPlus, __getString, __newString } = exports;
|
||||
describe('should execute bigDecimal plus API', () => {
|
||||
let testBigDecimalPlus: any, __getString: any, __newString: any;
|
||||
|
||||
const ptr = await testBigDecimalPlus(await __newString('231543212.2132354'), await __newString('54652.65645'));
|
||||
expect(__getString(ptr)).to.equal('231597864.8696854');
|
||||
before(() => {
|
||||
({ testBigDecimalPlus, __getString, __newString } = exports);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal plus for positive decimals', async () => {
|
||||
const ptr = await testBigDecimalPlus(await __newString('231543212.2132354'), await __newString('54652.65645'));
|
||||
expect(__getString(ptr)).to.equal('231597864.8696854');
|
||||
});
|
||||
|
||||
it('should execute bigDecimal plus for DECIMAL128_MAX and 0', async () => {
|
||||
const ptr = await testBigDecimalPlus(await __newString(DECIMAL128_MAX), await __newString('0'));
|
||||
const expected = new Decimal(DECIMAL128_MAX).toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal plus for DECIMAL128_MIN and 0', async () => {
|
||||
const ptr = await testBigDecimalPlus(await __newString(DECIMAL128_MIN), await __newString('0'));
|
||||
const expected = new Decimal(DECIMAL128_MIN).toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal plus for DECIMAL128_PMIN and 0', async () => {
|
||||
const ptr = await testBigDecimalPlus(await __newString(DECIMAL128_PMIN), await __newString('0'));
|
||||
const expected = new Decimal(DECIMAL128_PMIN).toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal plus for DECIMAL128_NMAX and 0', async () => {
|
||||
const ptr = await testBigDecimalPlus(await __newString(DECIMAL128_NMAX), await __newString('0'));
|
||||
const expected = new Decimal(DECIMAL128_NMAX).toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal plus for DECIMAL128_MAX and DECIMAL128_MIN', async () => {
|
||||
const ptr = await testBigDecimalPlus(await __newString(DECIMAL128_MAX), await __newString(DECIMAL128_MIN));
|
||||
const expected = new Decimal('0').toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal plus for DECIMAL128_PMIN and DECIMAL128_NMAX', async () => {
|
||||
const ptr = await testBigDecimalPlus(await __newString(DECIMAL128_PMIN), await __newString(DECIMAL128_NMAX));
|
||||
const expected = new Decimal('0').toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it('should execute bigDecimal minus API', async () => {
|
||||
const { testBigDecimalMinus, __getString, __newString } = exports;
|
||||
describe('should execute bigDecimal minus API', () => {
|
||||
let testBigDecimalMinus: any, __getString: any, __newString: any;
|
||||
|
||||
const ptr = await testBigDecimalMinus(await __newString('231543212.2132354'), await __newString('54652.65645'));
|
||||
expect(__getString(ptr)).to.equal('231488559.5567854');
|
||||
before(() => {
|
||||
({ testBigDecimalMinus, __getString, __newString } = exports);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal minus for positive decimals', async () => {
|
||||
const ptr = await testBigDecimalMinus(await __newString('231543212.2132354'), await __newString('54652.65645'));
|
||||
expect(__getString(ptr)).to.equal('231488559.5567854');
|
||||
});
|
||||
|
||||
it('should execute bigDecimal minus for DECIMAL128_MAX and 0', async () => {
|
||||
const ptr = await testBigDecimalMinus(await __newString(DECIMAL128_MAX), await __newString('0'));
|
||||
const expected = new Decimal(DECIMAL128_MAX).toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal minus for 0 and DECIMAL128_MAX', async () => {
|
||||
const ptr = await testBigDecimalMinus(await __newString('0'), await __newString(DECIMAL128_MAX));
|
||||
const expected = new Decimal(DECIMAL128_MIN).toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal minus for DECIMAL128_MIN and 0', async () => {
|
||||
const ptr = await testBigDecimalMinus(await __newString(DECIMAL128_MIN), await __newString('0'));
|
||||
const expected = new Decimal(DECIMAL128_MIN).toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal minus for 0 and DECIMAL128_MIN', async () => {
|
||||
const ptr = await testBigDecimalMinus(await __newString('0'), await __newString(DECIMAL128_MIN));
|
||||
const expected = new Decimal(DECIMAL128_MAX).toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal minus for DECIMAL128_PMIN and 0', async () => {
|
||||
const ptr = await testBigDecimalMinus(await __newString(DECIMAL128_PMIN), await __newString('0'));
|
||||
const expected = new Decimal(DECIMAL128_PMIN).toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal minus for 0 and DECIMAL128_PMIN', async () => {
|
||||
const ptr = await testBigDecimalMinus(await __newString('0'), await __newString(DECIMAL128_PMIN));
|
||||
const expected = new Decimal(DECIMAL128_NMAX).toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal minus for DECIMAL128_NMAX and 0', async () => {
|
||||
const ptr = await testBigDecimalMinus(await __newString(DECIMAL128_NMAX), await __newString('0'));
|
||||
const expected = new Decimal(DECIMAL128_NMAX).toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal minus for 0 and DECIMAL128_NMAX', async () => {
|
||||
const ptr = await testBigDecimalMinus(await __newString('0'), await __newString(DECIMAL128_NMAX));
|
||||
const expected = new Decimal(DECIMAL128_PMIN).toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal minus for DECIMAL128_MIN and DECIMAL128_MIN', async () => {
|
||||
const ptr = await testBigDecimalMinus(await __newString(DECIMAL128_MIN), await __newString(DECIMAL128_MIN));
|
||||
const expected = new Decimal('0').toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal minus for DECIMAL128_PMIN and DECIMAL128_PMIN', async () => {
|
||||
const ptr = await testBigDecimalMinus(await __newString(DECIMAL128_PMIN), await __newString(DECIMAL128_PMIN));
|
||||
const expected = new Decimal('0').toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it('should execute bigDecimal times API', async () => {
|
||||
const { testBigDecimalTimes, __getString, __newString } = exports;
|
||||
describe('should execute bigDecimal times API', () => {
|
||||
let testBigDecimalTimes: any, __getString: any, __newString: any;
|
||||
|
||||
const ptr = await testBigDecimalTimes(await __newString('231543212.2132354'), await __newString('54652.65645'));
|
||||
expect(__getString(ptr)).to.equal('12654451630419.39845917833');
|
||||
before(() => {
|
||||
({ testBigDecimalTimes, __getString, __newString } = exports);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal times for positive decimals', async () => {
|
||||
const ptr = await testBigDecimalTimes(await __newString('231543212.2132354'), await __newString('54652.65645'));
|
||||
expect(__getString(ptr)).to.equal('12654451630419.39845917833');
|
||||
});
|
||||
|
||||
it('should execute bigDecimal times for positive and negative decimal', async () => {
|
||||
const ptr = await testBigDecimalTimes(await __newString('231543212.2132354'), await __newString('-54652.65645'));
|
||||
expect(__getString(ptr)).to.equal('-12654451630419.39845917833');
|
||||
});
|
||||
|
||||
it('should execute bigDecimal times for positive decimal and 0', async () => {
|
||||
const ptr = await testBigDecimalTimes(await __newString('231543212.2132354'), await __newString('0'));
|
||||
expect(__getString(ptr)).to.equal('0');
|
||||
});
|
||||
|
||||
it('should execute bigDecimal times for 0 and 0', async () => {
|
||||
const ptr = await testBigDecimalTimes(await __newString('0'), await __newString('0'));
|
||||
expect(__getString(ptr)).to.equal('0');
|
||||
});
|
||||
|
||||
it('should execute bigDecimal times for DECIMAL128_MAX and 1', async () => {
|
||||
const ptr = await testBigDecimalTimes(await __newString(DECIMAL128_MAX), await __newString('1'));
|
||||
const expected = new Decimal(DECIMAL128_MAX).toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal times for DECIMAL128_MAX and -1', async () => {
|
||||
const ptr = await testBigDecimalTimes(await __newString(DECIMAL128_MAX), await __newString('-1'));
|
||||
const expected = new Decimal(DECIMAL128_MIN).toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal times for DECIMAL128_PMIN and 1', async () => {
|
||||
const ptr = await testBigDecimalTimes(await __newString(DECIMAL128_PMIN), await __newString('1'));
|
||||
const expected = new Decimal(DECIMAL128_PMIN).toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal times for DECIMAL128_PMIN and -1', async () => {
|
||||
const ptr = await testBigDecimalTimes(await __newString(DECIMAL128_PMIN), await __newString('-1'));
|
||||
const expected = new Decimal(DECIMAL128_NMAX).toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should throw an error for DECIMAL128_PMIN times DECIMAL128_NMAX', async () => {
|
||||
try {
|
||||
await testBigDecimalTimes(await __newString(DECIMAL128_PMIN), await __newString(DECIMAL128_NMAX));
|
||||
} catch (error) {
|
||||
expect(error.message).to.be.equal('Big decimal exponent \'-12286\' is outside the \'-6143\' to \'6144\' range');
|
||||
}
|
||||
});
|
||||
|
||||
it('should execute bigDecimal times for DECIMAL128_MAX and DECIMAL128_NMAX', async () => {
|
||||
const ptr = await testBigDecimalTimes(await __newString(DECIMAL128_MAX), await __newString(DECIMAL128_NMAX));
|
||||
const expected = new Decimal('-99.99999999999999999999999999999999').toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it('should execute bigDecimal dividedBy API', async () => {
|
||||
const { testBigDecimalDividedBy, __getString, __newString } = exports;
|
||||
describe('should execute bigDecimal dividedBy API', () => {
|
||||
let testBigDecimalDividedBy: any, __getString: any, __newString: any;
|
||||
|
||||
const ptr = await testBigDecimalDividedBy(await __newString('231543212.2132354'), await __newString('54652.65645'));
|
||||
expect(__getString(ptr)).to.equal('4236.632347872550667205491344419362');
|
||||
before(() => {
|
||||
({ testBigDecimalDividedBy, __getString, __newString } = exports);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal dividedBy for positive decimals', async () => {
|
||||
const ptr = await testBigDecimalDividedBy(await __newString('231543212.2132354'), await __newString('54652.65645'));
|
||||
expect(__getString(ptr)).to.equal('4236.632347872550667205491344419362');
|
||||
});
|
||||
|
||||
it('should execute bigDecimal dividedBy for negative decimal and DECIMAL128_MAX', async () => {
|
||||
const ptr = await testBigDecimalDividedBy(await __newString('-10000.00'), await __newString(DECIMAL128_MAX));
|
||||
const expected = new Decimal('-1e-6141').toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal dividedBy for DECIMAL128_MAX and DECIMAL128_MAX', async () => {
|
||||
const ptr = await testBigDecimalDividedBy(await __newString(DECIMAL128_MAX), await __newString(DECIMAL128_MAX));
|
||||
const expected = new Decimal('1').toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal dividedBy for DECIMAL128_MAX and DECIMAL128_MIN', async () => {
|
||||
const ptr = await testBigDecimalDividedBy(await __newString(DECIMAL128_MAX), await __newString(DECIMAL128_MIN));
|
||||
const expected = new Decimal('-1').toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should throw an error for DECIMAL128_PMIN divideBy DECIMAL128_MAX', async () => {
|
||||
try {
|
||||
await testBigDecimalDividedBy(await __newString(DECIMAL128_PMIN), await __newString(DECIMAL128_MAX));
|
||||
} catch (error) {
|
||||
expect(error.message).to.be.equal('Big decimal exponent \'-12288\' is outside the \'-6143\' to \'6144\' range');
|
||||
}
|
||||
});
|
||||
|
||||
it('should execute bigDecimal dividedBy for 0 and DECIMAL128_MAX', async () => {
|
||||
const ptr = await testBigDecimalDividedBy(await __newString('0'), await __newString(DECIMAL128_MAX));
|
||||
const expected = new Decimal('0').toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
|
||||
it('should execute bigDecimal dividedBy for DECIMAL128_PMIN and DECIMAL128_NMAX', async () => {
|
||||
const ptr = await testBigDecimalDividedBy(await __newString(DECIMAL128_PMIN), await __newString(DECIMAL128_NMAX));
|
||||
const expected = new Decimal('-1').toSignificantDigits(PRECISION).toFixed();
|
||||
expect(__getString(ptr)).to.equal(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -13,7 +13,25 @@ const log = debug('vulcanize:utils');
|
||||
|
||||
// Customize Decimal according the limits of IEEE-754 decimal128.
|
||||
// Reference: https://github.com/graphprotocol/graph-node/blob/v0.24.2/graph/src/data/store/scalar.rs#L42
|
||||
export const GraphDecimal = Decimal.clone({ minE: -6143, maxE: 6144, precision: 34 });
|
||||
export const MIN_EXP = -6143;
|
||||
export const MAX_EXP = 6144;
|
||||
export const PRECISION = 34;
|
||||
export const GraphDecimal = Decimal.clone({ precision: PRECISION });
|
||||
|
||||
export const INT256_MIN = '-57896044618658097711785492504343953926634992332820282019728792003956564819968';
|
||||
export const INT256_MAX = '57896044618658097711785492504343953926634992332820282019728792003956564819967';
|
||||
export const UINT128_MAX = '340282366920938463463374607431768211455';
|
||||
export const UINT256_MAX = '115792089237316195423570985008687907853269984665640564039457584007913129639935';
|
||||
|
||||
// Maximum decimal value.
|
||||
export const DECIMAL128_MAX = '9.999999999999999999999999999999999e+6144';
|
||||
// Minimum decimal value.
|
||||
export const DECIMAL128_MIN = '-9.999999999999999999999999999999999e+6144';
|
||||
|
||||
// Minimum +ve decimal value.
|
||||
export const DECIMAL128_PMIN = '1e-6143';
|
||||
// Maximum -ve decimal value.
|
||||
export const DECIMAL128_NMAX = '-1e-6143';
|
||||
|
||||
// Constant used in function digitsToString.
|
||||
const LOG_BASE = 7;
|
||||
|
@ -127,6 +127,7 @@ export function testGetEthCall (): void {
|
||||
export function testAddEthCall (): void {
|
||||
log.debug('In test add eth call', []);
|
||||
|
||||
// Bind the contract to the address that emitted the event.
|
||||
const contractAddress = dataSource.address();
|
||||
const contract = Example1.bind(contractAddress);
|
||||
|
||||
@ -224,7 +225,6 @@ export function testBigDecimalFromString (value: string): string {
|
||||
|
||||
const bigDecimal = BigDecimal.fromString(value);
|
||||
const res = bigDecimal.toString();
|
||||
log.debug('bigDecimal.FromString result: {}', [res]);
|
||||
|
||||
return res;
|
||||
}
|
||||
@ -236,7 +236,6 @@ export function testBigDecimalDividedBy (value1: string, value2: string): string
|
||||
const bigDecimal2 = BigDecimal.fromString(value2);
|
||||
|
||||
const res = bigDecimal1 / bigDecimal2;
|
||||
log.debug('bigDecimal.dividedBy result: {}', [res.toString()]);
|
||||
|
||||
return res.toString();
|
||||
}
|
||||
@ -248,7 +247,6 @@ export function testBigDecimalPlus (value1: string, value2: string): string {
|
||||
const bigDecimal2 = BigDecimal.fromString(value2);
|
||||
|
||||
const res = bigDecimal1 + bigDecimal2;
|
||||
log.debug('bigDecimal.plus result: {}', [res.toString()]);
|
||||
|
||||
return res.toString();
|
||||
}
|
||||
@ -260,7 +258,6 @@ export function testBigDecimalMinus (value1: string, value2: string): string {
|
||||
const bigDecimal2 = BigDecimal.fromString(value2);
|
||||
|
||||
const res = bigDecimal1 - bigDecimal2;
|
||||
log.debug('bigDecimal.minus result: {}', [res.toString()]);
|
||||
|
||||
return res.toString();
|
||||
}
|
||||
@ -272,49 +269,48 @@ export function testBigDecimalTimes (value1: string, value2: string): string {
|
||||
const bigDecimal2 = BigDecimal.fromString(value2);
|
||||
|
||||
const res = bigDecimal1 * bigDecimal2;
|
||||
log.debug('bigDecimal.times result: {}', [res.toString()]);
|
||||
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
export function testBigIntPlus (): string {
|
||||
export function testBigIntPlus (value1: string, value2: string): string {
|
||||
log.debug('In test bigInt.plus', []);
|
||||
|
||||
const bigInt1 = BigInt.fromString('100');
|
||||
const bigInt2 = BigInt.fromString('100');
|
||||
const bigInt1 = BigInt.fromString(value1);
|
||||
const bigInt2 = BigInt.fromString(value2);
|
||||
|
||||
const res = bigInt1 + bigInt2;
|
||||
log.debug('bigInt.plus result: {}', [res.toString()]);
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
export function testBigIntMinus (): string {
|
||||
export function testBigIntMinus (value1: string, value2: string): string {
|
||||
log.debug('In test bigInt.minus', []);
|
||||
|
||||
const bigInt1 = BigInt.fromString('200');
|
||||
const bigInt2 = BigInt.fromString('100');
|
||||
const bigInt1 = BigInt.fromString(value1);
|
||||
const bigInt2 = BigInt.fromString(value2);
|
||||
|
||||
const res = bigInt1 - bigInt2;
|
||||
log.debug('bigInt.minus result: {}', [res.toString()]);
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
export function testBigIntTimes (): string {
|
||||
export function testBigIntTimes (value1: string, value2: string): string {
|
||||
log.debug('In test bigInt.times', []);
|
||||
|
||||
const bigInt1 = BigInt.fromString('100');
|
||||
const bigInt2 = BigInt.fromString('10');
|
||||
const bigInt1 = BigInt.fromString(value1);
|
||||
const bigInt2 = BigInt.fromString(value2);
|
||||
|
||||
const res = bigInt1 * bigInt2;
|
||||
log.debug('bigInt.times result: {}', [res.toString()]);
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
export function testBigIntDividedBy (): string {
|
||||
export function testBigIntDividedBy (value1: string, value2: string): string {
|
||||
log.debug('In test bigInt.dividedBy', []);
|
||||
|
||||
const bigInt1 = BigInt.fromString('1000');
|
||||
const bigInt2 = BigInt.fromString('10');
|
||||
const bigInt1 = BigInt.fromString(value1);
|
||||
const bigInt2 = BigInt.fromString(value2);
|
||||
|
||||
const res = bigInt1 / bigInt2;
|
||||
log.debug('bigInt.dividedBy result: {}', [res.toString()]);
|
||||
|
@ -22,7 +22,8 @@ export const getDummyEventData = (): EventData => {
|
||||
uncleHash: ZERO_HASH,
|
||||
difficulty: '0',
|
||||
gasLimit: '0',
|
||||
gasUsed: '0'
|
||||
gasUsed: '0',
|
||||
author: ZERO_ADDRESS
|
||||
};
|
||||
|
||||
const tx = {
|
||||
|
Loading…
Reference in New Issue
Block a user