package uniswap import ( "math/big" "testing" "github.com/stretchr/testify/assert" ) func TestSqrtPriceX96ToPriceOptimizedCached(t *testing.T) { // Test with sqrtPriceX96 = 2^96 (should give price = 1.0) sqrtPriceX96 := new(big.Int) sqrtPriceX96.SetString("79228162514264337593543950336", 10) // 2^96 result := SqrtPriceX96ToPriceOptimizedCached(sqrtPriceX96) expected := new(big.Float).SetFloat64(1.0) resultFloat, _ := result.Float64() expectedFloat, _ := expected.Float64() assert.InDelta(t, expectedFloat, resultFloat, 0.0001, "Result should be close to 1.0") } func TestPriceToSqrtPriceX96OptimizedCached(t *testing.T) { // Test with price = 1.0 (should give sqrtPriceX96 = 2^96) price := new(big.Float).SetFloat64(1.0) result := PriceToSqrtPriceX96OptimizedCached(price) expected, _ := new(big.Int).SetString("79228162514264337593543950336", 10) // 2^96 // Allow for small differences due to floating point precision diff := new(big.Int).Sub(expected, result) assert.True(t, diff.Cmp(big.NewInt(1000000000000)) < 0, "Result should be close to 2^96") } func TestTickToSqrtPriceX96OptimizedCached(t *testing.T) { // Test with tick = 0 (should give sqrtPriceX96 = 2^96) tick := 0 result := TickToSqrtPriceX96OptimizedCached(tick) expected, _ := new(big.Int).SetString("79228162514264337593543950336", 10) // 2^96 // Allow for small differences due to floating point precision diff := new(big.Int).Sub(expected, result) assert.True(t, diff.Cmp(big.NewInt(1000000000000)) < 0, "Result should be close to 2^96 for tick 0") } func TestSqrtPriceX96ToTickOptimizedCached(t *testing.T) { // Test with sqrtPriceX96 = 2^96 (should give tick = 0) sqrtPriceX96 := new(big.Int) sqrtPriceX96.SetString("79228162514264337593543950336", 10) // 2^96 result := SqrtPriceX96ToTickOptimizedCached(sqrtPriceX96) expected := 0 assert.Equal(t, expected, result, "Result should be 0 for sqrtPriceX96 = 2^96") } func TestOptimizedCachedRoundTripConversions(t *testing.T) { // Test sqrtPriceX96 -> price -> sqrtPriceX96 round trip with optimized cached functions // Since these functions are aliases to the Advanced versions, this tests the same functionality sqrtPriceX96 := new(big.Int) sqrtPriceX96.SetString("79228162514264337593543950336", 10) // 2^96 (price = 1.0) price := SqrtPriceX96ToPriceOptimizedCached(sqrtPriceX96) resultSqrtPriceX96 := PriceToSqrtPriceX96OptimizedCached(price) // Allow for small differences due to floating point precision diff := new(big.Int).Sub(sqrtPriceX96, resultSqrtPriceX96) assert.True(t, diff.Cmp(big.NewInt(1000000000000)) < 0, "Round trip conversion should be accurate") // Test tick -> sqrtPriceX96 -> tick round trip with optimized cached functions tick := 100000 sqrtPrice := TickToSqrtPriceX96OptimizedCached(tick) resultTick := SqrtPriceX96ToTickOptimizedCached(sqrtPrice) // Allow for small differences due to floating point precision assert.InDelta(t, tick, resultTick, 1, "Round trip tick conversion should be accurate") }