Skip to content

Commit

Permalink
table-driven log tests
Browse files Browse the repository at this point in the history
  • Loading branch information
p0mvn committed Dec 23, 2022
1 parent 598c53a commit a15947d
Showing 1 changed file with 42 additions and 24 deletions.
66 changes: 42 additions & 24 deletions x/twap/logic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1339,33 +1339,51 @@ func (s *TestSuite) TestTwapLog_CorrectBase() {
s.Require().Equal(expectedValue, result)
}

func (s *TestSuite) TestTwapLog_MaxSpotPrice() {
price := gammtypes.MaxSpotPrice
// log_2{2^128 - 1} = 128
expectedResult := sdk.MustNewDecFromStr("127.999999999999999999")
result := twap.TwapLog(price)

s.Require().Equal(expectedResult, result)
}
func (s *TestSuite) TestTwapLog() {
smallestAdditiveTolerance := osmomath.ErrTolerance{
AdditiveTolerance: sdk.SmallestDec(),
}

func (s *TestSuite) TestTwapLog_ZeroPanic() {
osmoassert.ConditionalPanic(s.T(), true, func() {
twap.TwapLog(sdk.ZeroDec())
})
}
testcases := []struct {
name string
price sdk.Dec
expected sdk.Dec
expectPanic bool
}{
{
"max spot price",
gammtypes.MaxSpotPrice,
// log_2{2^128 - 1} = 128
sdk.MustNewDecFromStr("127.999999999999999999"),
false,
},
{
"zero price - panic",
sdk.ZeroDec(),
sdk.Dec{},
true,
},
{
"smallest dec",
sdk.SmallestDec(),
// https://www.wolframalpha.com/input?i=log+base+2+of+%2810%5E-18%29+with+20+digits
sdk.MustNewDecFromStr("59.794705707972522262").Neg(),
false,
},
}

func (s *TestSuite) TestTwapLog_SmallestDec() {
zero := sdk.SmallestDec()
// https://www.wolframalpha.com/input?i=log+base+2+of+%2810%5E-18%29+with+20+digits
expectedResult := sdk.MustNewDecFromStr("59.794705707972522262").Neg()
result := twap.TwapLog(zero)
for _, tc := range testcases {
s.Run(tc.name, func() {
osmoassert.ConditionalPanic(s.T(), tc.expectPanic, func() {
result := twap.TwapLog(tc.price)

osmomath.ErrTolerance{
AdditiveTolerance: sdk.SmallestDec(),
}.CompareBigDec(
osmomath.BigDecFromSDKDec(expectedResult),
osmomath.BigDecFromSDKDec(result),
)
smallestAdditiveTolerance.CompareBigDec(
osmomath.BigDecFromSDKDec(tc.expected),
osmomath.BigDecFromSDKDec(result),
)
})
})
}
}

// TestTwapPow_CorrectBase tests that the base of 2 is used for the twap power function.
Expand Down

0 comments on commit a15947d

Please sign in to comment.