diff --git a/x/oracle/ballot_test.go b/x/oracle/ballot_test.go index 2aa17d6df..c5c8224e5 100644 --- a/x/oracle/ballot_test.go +++ b/x/oracle/ballot_test.go @@ -31,6 +31,15 @@ func generateRandomTestCase() (prices []float64, weights []float64) { return } +func checkFloatEquality(a sdk.Dec, b float64, precision int) bool { + base := math.Pow10(precision) + + a2 := a.MulInt64(int64(base)).TruncateInt64() + b2 := int64(b * base) + + return a2 == b2 +} + func TestPBStdDev(t *testing.T) { _, addrs, _, _ := mock.CreateGenAccounts(1, sdk.Coins{}) @@ -42,15 +51,9 @@ func TestPBStdDev(t *testing.T) { vote := NewPriceVote(sdk.NewDecWithPrec(int64(price*10000), 4), "", weight, addrs[0]) pb = append(pb, vote) } - - precBase := math.Pow10(2) - statAnswerRaw := stat.StdDev(prices, weights) - statAnswerRounded := float64(int64(statAnswerRaw*precBase)) / precBase - ballotAnswerRaw := pb.stdDev() - ballotAnswerRounded := float64(ballotAnswerRaw.MulInt64(int64(precBase)).TruncateInt64()) / precBase - - require.Equal(t, statAnswerRounded, ballotAnswerRounded) - } + + match := checkFloatEquality(pb.stdDev(), stat.StdDev(prices, weights), 2) + require.True(t, match) } func TestPBMean(t *testing.T) { @@ -65,9 +68,8 @@ func TestPBMean(t *testing.T) { pb = append(pb, vote) } - statAnswerRounded := float64(int64(stat.Mean(prices, weights)*10000)) / 10000 - ballotAnswerRounded := float64(pb.mean().Mul(sdk.NewDec(10000)).TruncateInt64()) / 10000 - require.Equal(t, ballotAnswerRounded, statAnswerRounded) + match := checkFloatEquality(pb.mean(), stat.Mean(prices, weights), 2) + require.True(t, match) } func TestPBWeightedMedian(t *testing.T) {