-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(math): update math docs & move test code to test file #19604
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,27 +24,39 @@ func TestDecimalTestSuite(t *testing.T) { | |
suite.Run(t, new(decimalTestSuite)) | ||
} | ||
|
||
// LegacyDecEq intended to be used with require/assert: require.True(DecEq(...)) | ||
func legacyDecEq(t *testing.T, exp, got math.LegacyDec) (*testing.T, bool, string, string, string) { | ||
t.Helper() | ||
return t, exp.Equal(got), "expected:\t%v\ngot:\t\t%v", exp.String(), got.String() | ||
} | ||
|
||
func legacyDecApproxEq(t *testing.T, d1, d2, tol math.LegacyDec) (*testing.T, bool, string, string, string) { | ||
t.Helper() | ||
diff := d1.Sub(d2).Abs() | ||
return t, diff.LTE(tol), "expected |d1 - d2| <:\t%v\ngot |d1 - d2| = \t\t%v", tol.String(), diff.String() | ||
} | ||
|
||
func TestDecApproxEq(t *testing.T) { | ||
// d1 = 0.55, d2 = 0.6, tol = 0.1 | ||
d1 := math.LegacyNewDecWithPrec(55, 2) | ||
d2 := math.LegacyNewDecWithPrec(6, 1) | ||
tol := math.LegacyNewDecWithPrec(1, 1) | ||
|
||
require.True(math.LegacyDecApproxEq(t, d1, d2, tol)) | ||
require.True(legacyDecApproxEq(t, d1, d2, tol)) | ||
|
||
// d1 = 0.55, d2 = 0.6, tol = 1E-5 | ||
d1 = math.LegacyNewDecWithPrec(55, 2) | ||
d2 = math.LegacyNewDecWithPrec(6, 1) | ||
tol = math.LegacyNewDecWithPrec(1, 5) | ||
|
||
require.False(math.LegacyDecApproxEq(t, d1, d2, tol)) | ||
require.False(legacyDecApproxEq(t, d1, d2, tol)) | ||
|
||
// d1 = 0.6, d2 = 0.61, tol = 0.01 | ||
d1 = math.LegacyNewDecWithPrec(6, 1) | ||
d2 = math.LegacyNewDecWithPrec(61, 2) | ||
tol = math.LegacyNewDecWithPrec(1, 2) | ||
|
||
require.True(math.LegacyDecApproxEq(t, d1, d2, tol)) | ||
require.True(legacyDecApproxEq(t, d1, d2, tol)) | ||
} | ||
|
||
// create a decimal from a decimal string (ex. "1234.5678") | ||
Comment on lines
24
to
62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For benchmark tests, it's beneficial to include a brief comment explaining what each benchmark aims to measure. This helps maintainers and contributors understand the purpose of the benchmarks and can guide future optimizations. + // BenchmarkMarshalTo measures the performance of the MarshalTo function.
func BenchmarkMarshalTo(b *testing.B) {
...
}
+ // BenchmarkLegacyQuoMut measures the performance of the QuoMut function on LegacyDec.
func BenchmarkLegacyQuoMut(b *testing.B) {
...
} |
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -9,7 +9,6 @@ import ( | |||||
"math/bits" | ||||||
"strings" | ||||||
"sync" | ||||||
"testing" | ||||||
) | ||||||
|
||||||
// MaxBitLen defines the maximum bit length supported bit Int and Uint types. | ||||||
|
@@ -333,7 +332,7 @@ func (i Int) Mul(i2 Int) (res Int) { | |||||
return x | ||||||
} | ||||||
|
||||||
// MulRaw multipies Int and int64 | ||||||
// MulRaw multiples Int and int64 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks👍 |
||||||
func (i Int) MulRaw(i2 int64) Int { | ||||||
return i.Mul(NewInt(i2)) | ||||||
} | ||||||
|
@@ -414,7 +413,7 @@ func MaxInt(i, i2 Int) Int { | |||||
return Int{max(i.BigInt(), i2.BigInt())} | ||||||
} | ||||||
|
||||||
// Human readable string | ||||||
// String Human readable string | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. u mean String returns human readable string There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks, i'll fix it |
||||||
func (i Int) String() string { | ||||||
return i.i.String() | ||||||
} | ||||||
|
@@ -521,12 +520,6 @@ func (i *Int) Size() int { | |||||
func (i Int) MarshalAmino() ([]byte, error) { return i.Marshal() } | ||||||
func (i *Int) UnmarshalAmino(bz []byte) error { return i.Unmarshal(bz) } | ||||||
|
||||||
// intended to be used with require/assert: require.True(IntEq(...)) | ||||||
func IntEq(t *testing.T, exp, got Int) (*testing.T, bool, string, string, string) { | ||||||
t.Helper() | ||||||
return t, exp.Equal(got), "expected:\t%v\ngot:\t\t%v", exp.String(), got.String() | ||||||
} | ||||||
|
||||||
func hasOnlyDigits(s string) bool { | ||||||
if s == "" { | ||||||
return false | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing this is api breaking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok,i'll fix it