github.com/0xsequence/ethkit@v1.25.0/ethgas/ema_test.go (about)

     1  package ethgas_test
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	"github.com/0xsequence/ethkit/ethgas"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestEMA(t *testing.T) {
    12  	tt := []struct {
    13  		decay    float64
    14  		values   []int64
    15  		expected int64
    16  	}{
    17  		{
    18  			decay:    0.1,
    19  			values:   []int64{1, 1, 1},
    20  			expected: 1,
    21  		},
    22  		{
    23  			decay:    0.5,
    24  			values:   []int64{100, 200},
    25  			expected: 150,
    26  		},
    27  		{
    28  			decay:    0.5,
    29  			values:   []int64{100, 200, 300},
    30  			expected: 225,
    31  		},
    32  		{
    33  			decay:    0.5,
    34  			values:   []int64{300, 200, 100},
    35  			expected: 175,
    36  		},
    37  		{
    38  			decay:    0.1818,
    39  			values:   []int64{2227, 2219, 2208, 2217, 2218, 2213, 2223, 2243, 2224},
    40  			expected: 2222,
    41  		},
    42  	}
    43  	for _, tc := range tt {
    44  		ema := ethgas.NewEMA(tc.decay)
    45  		for _, v := range tc.values {
    46  			ema.Tick(big.NewInt(v))
    47  		}
    48  		assert.Equal(t, tc.expected, ema.Value().Int64())
    49  	}
    50  }