test: add example for mathutil package

This commit is contained in:
dudaodong
2023-01-02 14:59:49 +08:00
parent 31fdbee0b5
commit 927245e47f
3 changed files with 211 additions and 13 deletions

View File

@@ -13,7 +13,8 @@ import (
"golang.org/x/exp/constraints"
)
// Exponent calculate x^n
// Exponent calculate x^n.
// Play: https://go.dev/play/p/uF3HGNPk8wr
func Exponent(x, n int64) int64 {
if n == 0 {
return 1
@@ -28,7 +29,8 @@ func Exponent(x, n int64) int64 {
return t * t
}
// Fibonacci calculate fibonacci number before n
// Fibonacci calculate fibonacci number before n.
// Play: https://go.dev/play/p/IscseUNMuUc
func Fibonacci(first, second, n int) int {
if n <= 0 {
return 0
@@ -42,7 +44,8 @@ func Fibonacci(first, second, n int) int {
}
}
// Factorial calculate x!
// Factorial calculate x!.
// Play: https://go.dev/play/p/tt6LdOK67Nx
func Factorial(x uint) uint {
var f uint = 1
for ; x > 1; x-- {
@@ -51,18 +54,19 @@ func Factorial(x uint) uint {
return f
}
// Percent calculate the percentage of val to total
// Percent calculate the percentage of val to total.
func Percent(val, total float64, n int) float64 {
if total == 0 {
return float64(0)
}
tmp := val / total * 100
tmp := val / total
result := RoundToFloat(tmp, n)
return result
}
// RoundToString round up to n decimal places
// RoundToString round up to n decimal places.
// Play: https://go.dev/play/p/kZwpBRAcllO
func RoundToString(x float64, n int) string {
tmp := math.Pow(10.0, float64(n))
x *= tmp
@@ -71,7 +75,8 @@ func RoundToString(x float64, n int) string {
return result
}
// RoundToFloat round up to n decimal places
// RoundToFloat round up to n decimal places.
// Play: https://go.dev/play/p/ghyb528JRJL
func RoundToFloat(x float64, n int) float64 {
tmp := math.Pow(10.0, float64(n))
x *= tmp
@@ -79,7 +84,8 @@ func RoundToFloat(x float64, n int) float64 {
return x / tmp
}
// TruncRound round off n decimal places
// TruncRound round off n decimal places.
// Play: https://go.dev/play/p/aumarSHIGzP
func TruncRound(x float64, n int) float64 {
floatStr := fmt.Sprintf("%."+strconv.Itoa(n+1)+"f", x)
temp := strings.Split(floatStr, ".")
@@ -93,7 +99,8 @@ func TruncRound(x float64, n int) float64 {
return result
}
// Max return max value of params
// Max return max value of numbers.
// Play: https://go.dev/play/p/cN8DHI0rTkH
func Max[T constraints.Integer | constraints.Float](numbers ...T) T {
max := numbers[0]
@@ -107,6 +114,7 @@ func Max[T constraints.Integer | constraints.Float](numbers ...T) T {
}
// MaxBy search the maximum value of a slice using the given comparator function.
// Play: https://go.dev/play/p/pbe2MT-7DV2
func MaxBy[T any](slice []T, comparator func(T, T) bool) T {
var max T
@@ -127,7 +135,8 @@ func MaxBy[T any](slice []T, comparator func(T, T) bool) T {
return max
}
// Min return min value of params
// Min return min value of numbers.
// Play: https://go.dev/play/p/21BER_mlGUj
func Min[T constraints.Integer | constraints.Float](numbers ...T) T {
min := numbers[0]
@@ -141,6 +150,7 @@ func Min[T constraints.Integer | constraints.Float](numbers ...T) T {
}
// MinBy search the minimum value of a slice using the given comparator function.
// Play: https://go.dev/play/p/XuJDKrDdglW
func MinBy[T any](slice []T, comparator func(T, T) bool) T {
var min T
@@ -161,7 +171,8 @@ func MinBy[T any](slice []T, comparator func(T, T) bool) T {
return min
}
// Average return average value of numbers
// Average return average value of numbers.
// Play: https://go.dev/play/p/Vv7LBwER-pz
func Average[T constraints.Integer | constraints.Float](numbers ...T) T {
var sum T
n := T(len(numbers))

View File

@@ -0,0 +1,187 @@
package mathutil
import "fmt"
func ExampleExponent() {
result1 := Exponent(10, 0)
result2 := Exponent(10, 1)
result3 := Exponent(10, 2)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 1
// 10
// 100
}
func ExampleFibonacci() {
result1 := Fibonacci(1, 1, 1)
result2 := Fibonacci(1, 1, 2)
result3 := Fibonacci(1, 1, 5)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 1
// 1
// 5
}
func ExampleFactorial() {
result1 := Factorial(1)
result2 := Factorial(2)
result3 := Factorial(3)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 1
// 2
// 6
}
func ExamplePercent() {
result1 := Percent(1, 2, 2)
result2 := Percent(0.1, 0.3, 2)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 0.5
// 0.33
}
func ExampleRoundToFloat() {
result1 := RoundToFloat(0.124, 2)
result2 := RoundToFloat(0.125, 2)
result3 := RoundToFloat(0.125, 3)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 0.12
// 0.13
// 0.125
}
func ExampleRoundToString() {
result1 := RoundToString(0.124, 2)
result2 := RoundToString(0.125, 2)
result3 := RoundToString(0.125, 3)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 0.12
// 0.13
// 0.125
}
func ExampleTruncRound() {
result1 := TruncRound(0.124, 2)
result2 := TruncRound(0.125, 2)
result3 := TruncRound(0.125, 3)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 0.12
// 0.12
// 0.125
}
func ExampleAverage() {
result1 := Average(1, 2)
result2 := RoundToFloat(Average(1.2, 1.4), 1)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 1
// 1.3
}
func ExampleMax() {
result1 := Max(1, 2, 3)
result2 := Max(1.2, 1.4, 1.1, 1.4)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 3
// 1.4
}
func ExampleMaxBy() {
result1 := MaxBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool {
return len(v1) > len(v2)
})
result2 := MaxBy([]string{"abd", "abc", "ab"}, func(v1, v2 string) bool {
return len(v1) > len(v2)
})
result3 := MaxBy([]string{}, func(v1, v2 string) bool {
return len(v1) > len(v2)
})
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// abc
// abd
//
}
func ExampleMin() {
result1 := Min(1, 2, 3)
result2 := Min(1.2, 1.4, 1.1, 1.4)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 1
// 1.1
}
func ExampleMinBy() {
result1 := MinBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool {
return len(v1) < len(v2)
})
result2 := MinBy([]string{"ab", "ac", "abc"}, func(v1, v2 string) bool {
return len(v1) < len(v2)
})
result3 := MinBy([]string{}, func(v1, v2 string) bool {
return len(v1) < len(v2)
})
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// a
// ab
//
}

View File

@@ -35,8 +35,8 @@ func TestFactorial(t *testing.T) {
func TestPercent(t *testing.T) {
assert := internal.NewAssert(t, "TestPercent")
assert.Equal(float64(50), Percent(1, 2, 2))
assert.Equal(float64(33.33), Percent(0.1, 0.3, 2))
assert.Equal(0.5, Percent(1, 2, 2))
assert.Equal(0.33, Percent(0.1, 0.3, 2))
}
func TestRoundToFloat(t *testing.T) {