Skip to content

Commit

Permalink
Merge pull request #72 from terra-project/bugfix/ballot-precision
Browse files Browse the repository at this point in the history
HOTFIX: ballot tally precision issue breaking CI;
  • Loading branch information
ziwon authored Apr 8, 2019
2 parents 38562c5 + ec70620 commit 4be2a91
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions x/oracle/ballot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})

Expand All @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 4be2a91

Please sign in to comment.