Skip to content

Commit

Permalink
more testing coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
piux2 committed Oct 11, 2024
1 parent ae25c58 commit 6da36bb
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions tm2/pkg/sdk/auth/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package auth

import (
"math/big"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -97,3 +98,81 @@ func TestGasPrice(t *testing.T) {
gp2 := env.gk.LastGasPrice(env.ctx)
require.True(t, gp == gp2)
}

func TestMax(t *testing.T) {
tests := []struct {
name string
x, y *big.Int
expected *big.Int
}{
{
name: "X is less than Y",
x: big.NewInt(5),
y: big.NewInt(10),
expected: big.NewInt(10),
},
{
name: "X is greater than Y",
x: big.NewInt(15),
y: big.NewInt(10),
expected: big.NewInt(15),
},
{
name: "X is equal to Y",
x: big.NewInt(10),
y: big.NewInt(10),
expected: big.NewInt(10),
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := max(tc.x, tc.y)
require.Equal(t, tc.expected, result)
})
}
}

func TestCalcBlockGasPrice(t *testing.T) {
gk := GasPriceKeeper{}

lastGasPrice := std.GasPrice{
Price: std.Coin{
Amount: 100,
Denom: "atom",
},
}
gasUsed := int64(5000)
maxGas := int64(10000)
params := Params{
TargetGasRatio: 50,
GasPricesChangeCompressor: 2,
}

// Test with normal parameters
newGasPrice := gk.calcBlockGasPrice(lastGasPrice, gasUsed, maxGas, params)
expectedAmount := big.NewInt(100)
num := big.NewInt(gasUsed - maxGas*params.TargetGasRatio/100)
num.Mul(num, expectedAmount)
num.Div(num, big.NewInt(maxGas*params.TargetGasRatio/100))
num.Div(num, big.NewInt(params.GasPricesChangeCompressor))
expectedAmount.Add(expectedAmount, num)
require.Equal(t, expectedAmount.Int64(), newGasPrice.Price.Amount)

// Test with lastGasPrice amount as 0
lastGasPrice.Price.Amount = 0
newGasPrice = gk.calcBlockGasPrice(lastGasPrice, gasUsed, maxGas, params)
require.Equal(t, int64(0), newGasPrice.Price.Amount)

// Test with TargetGasRatio as 0 (should not change the last price)
params.TargetGasRatio = 0
newGasPrice = gk.calcBlockGasPrice(lastGasPrice, gasUsed, maxGas, params)
require.Equal(t, int64(0), newGasPrice.Price.Amount)

// Test with gasUsed as 0 (should not change the last price)
params.TargetGasRatio = 50
lastGasPrice.Price.Amount = 100
gasUsed = 0
newGasPrice = gk.calcBlockGasPrice(lastGasPrice, gasUsed, maxGas, params)
require.Equal(t, int64(100), newGasPrice.Price.Amount)
}

0 comments on commit 6da36bb

Please sign in to comment.