/** * Migration Test Suite * * Tests to verify BigInt implementations produce correct results * matching the original BigNumber behavior from ethers v5. */ import { assertEqual, toBigInt, X128, testFixtures, } from '../../src/test-utils/migration-harness'; describe('Migration Test Harness - BigInt Arithmetic', () => { describe('Basic Arithmetic Operations', () => { test('addition should work correctly', () => { testFixtures.arithmetic.addition.forEach(({ a, b, expected }) => { const result = BigInt(a) + BigInt(b); assertEqual(result, expected, `Addition: ${a} + ${b}`); }); }); test('subtraction should work correctly', () => { testFixtures.arithmetic.subtraction.forEach(({ a, b, expected }) => { const result = BigInt(a) - BigInt(b); assertEqual(result, expected, `Subtraction: ${a} - ${b}`); }); }); test('multiplication should work correctly', () => { testFixtures.arithmetic.multiplication.forEach(({ a, b, expected }) => { const result = BigInt(a) * BigInt(b); assertEqual(result, expected, `Multiplication: ${a} * ${b}`); }); }); test('division should work correctly with floor behavior', () => { testFixtures.arithmetic.division.forEach(({ a, b, expected }) => { const result = BigInt(a) / BigInt(b); assertEqual(result, expected, `Division: ${a} / ${b}`); }); }); test('power operations should work correctly', () => { testFixtures.arithmetic.power.forEach(({ a, b, expected }) => { const result = BigInt(a) ** BigInt(b); assertEqual(result, expected, `Power: ${a} ** ${b}`); }); }); }); describe('X128 Precision Math', () => { test('X128 constant should be 2^128', () => { const expectedX128 = BigInt(2) ** BigInt(128); expect(X128).toBe(expectedX128); expect(X128.toString()).toBe('340282366920938463463374607431768211456'); }); test('sqrtPriceX96 to priceX128 conversion', () => { // Test basic conversion: priceX128 = (sqrtPriceX96^2) / 2^64 const sqrtPriceX96 = BigInt('79228162514264337593543950336'); // 2^96 (price = 1.0) const expectedPriceX128 = BigInt( '340282366920938463463374607431768211456', ); // 2^128 (price = 1.0 in X128) const priceX128 = (sqrtPriceX96 * sqrtPriceX96) / BigInt(2) ** BigInt(64); expect(priceX128).toBe(expectedPriceX128); }); test('token ratio calculations in X128', () => { testFixtures.x128.tokenRatios.forEach( ({ numerator, denominator, expected }) => { const ratioX128 = (BigInt(numerator) * X128) / BigInt(denominator); assertEqual( ratioX128, expected, `Token ratio ${numerator}/${denominator}`, ); }, ); }); }); describe('Slippage Calculations', () => { test('basis points calculation', () => { testFixtures.slippage.basisPoints.forEach( ({ slippagePercent, expected }) => { // Slippage: 10000 - (slippagePercent * 100) const basisPoints = BigInt(10000 - Math.round(slippagePercent * 100)); assertEqual(basisPoints, expected, `Slippage ${slippagePercent}%`); }, ); }); test('slippage boundaries', () => { // 0% slippage = 10000 bps expect(BigInt(10000)).toBe(10000n); // 100% slippage = 0 bps (extreme case) expect(BigInt(10000 - 10000)).toBe(0n); // Small slippage 0.01% expect(BigInt(10000 - 1)).toBe(9999n); }); }); describe('Edge Cases and Large Numbers', () => { test('zero handling', () => { testFixtures.edgeCases.zero.forEach((zeroValue) => { const zero = BigInt(zeroValue); expect(zero).toBe(0n); // Arithmetic with zero expect(zero + BigInt(1)).toBe(1n); expect(BigInt(1) - zero).toBe(1n); expect(zero * BigInt(100)).toBe(0n); }); }); test('max uint256 value', () => { const maxUint256 = BigInt(testFixtures.edgeCases.maxUint256); expect(maxUint256 > BigInt(0)).toBe(true); expect(maxUint256.toString()).toBe(testFixtures.edgeCases.maxUint256); }); test('large number arithmetic', () => { testFixtures.edgeCases.largeNumbers.forEach((num) => { const bigNum = BigInt(num); // Test that we can perform operations without overflow const doubled = bigNum * BigInt(2); const halved = bigNum / BigInt(2); expect(doubled / BigInt(2)).toBe(bigNum); expect(halved * BigInt(2)).toBeLessThanOrEqual(bigNum); }); }); test('comparison operations', () => { const a = BigInt('1000000000000000000'); const b = BigInt('500000000000000000'); const c = BigInt('1000000000000000000'); expect(a > b).toBe(true); expect(b < a).toBe(true); expect(a === c).toBe(true); expect(a >= c).toBe(true); expect(a <= c).toBe(true); expect(a !== b).toBe(true); }); }); describe('String Conversions', () => { test('BigInt to string conversion', () => { const values = [ '0', '1', '1000000000000000000', testFixtures.edgeCases.maxUint256, ]; values.forEach((val) => { const bigInt = BigInt(val); expect(bigInt.toString()).toBe(val); }); }); test('hex string conversion', () => { const hexValue = '0x' + BigInt(1000000).toString(16); const fromHex = BigInt(hexValue); expect(fromHex).toBe(1000000n); }); }); }); describe('Migration Test Harness - Helper Functions', () => { test('toBigInt converts various types correctly', () => { expect(toBigInt(100)).toBe(100n); expect(toBigInt('100')).toBe(100n); expect(toBigInt(100n)).toBe(100n); }); test('assertEqual throws on mismatch', () => { expect(() => assertEqual(100n, '200')).toThrow(); expect(() => assertEqual(100n, '100')).not.toThrow(); }); });