Replace MinInt/MaxInt with generic versions

This commit is contained in:
Deluan
2022-12-19 00:47:42 -05:00
committed by Deluan Quintão
parent 6f5aaa1ec4
commit e03ccb3166
9 changed files with 82 additions and 74 deletions
+2 -2
View File
@@ -5,7 +5,7 @@ import (
"fmt"
"strings"
"github.com/navidrome/navidrome/utils"
"github.com/navidrome/navidrome/utils/math2"
)
const baseUrl = "https://www.gravatar.com/avatar"
@@ -19,7 +19,7 @@ func Url(email string, size int) string {
if size < 1 {
size = defaultSize
}
size = utils.MinInt(maxSize, size)
size = math2.Min(maxSize, size)
return fmt.Sprintf("%s/%x?s=%d", baseUrl, hash, size)
}
-24
View File
@@ -1,24 +0,0 @@
package utils
func MinInt(x, y int) int {
if x < y {
return x
}
return y
}
func MaxInt(x, y int) int {
if x > y {
return x
}
return y
}
func IntInSlice(a int, list []int) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
-40
View File
@@ -1,40 +0,0 @@
package utils
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Int utils", func() {
Describe("MinInt", func() {
It("returns the first value if it is the smallest", func() {
Expect(MinInt(1, 2)).To(Equal(1))
})
It("returns the second value if it is the smallest", func() {
Expect(MinInt(-4, -6)).To(Equal(-6))
})
})
Describe("MaxInt", func() {
It("returns the first value if it is the biggest", func() {
Expect(MaxInt(1, 2)).To(Equal(2))
})
It("returns the second value if it is the smallest", func() {
Expect(MaxInt(-4, -6)).To(Equal(-4))
})
})
Describe("IntInSlice", func() {
It("returns false if slice is empty", func() {
Expect(IntInSlice(1, nil)).To(BeFalse())
})
It("returns false if number is not in slice", func() {
Expect(IntInSlice(1, []int{3, 4, 5})).To(BeFalse())
})
It("returns true if number is in slice", func() {
Expect(IntInSlice(4, []int{3, 4, 5})).To(BeTrue())
})
})
})
+31
View File
@@ -0,0 +1,31 @@
package math2
import "golang.org/x/exp/constraints"
func Min[T constraints.Ordered](vs ...T) T {
if len(vs) == 0 {
var zero T
return zero
}
min := vs[0]
for _, v := range vs[1:] {
if v < min {
min = v
}
}
return min
}
func Max[T constraints.Ordered](vs ...T) T {
if len(vs) == 0 {
var zero T
return zero
}
max := vs[0]
for _, v := range vs[1:] {
if v > max {
max = v
}
}
return max
}
+38
View File
@@ -0,0 +1,38 @@
package math2_test
import (
"testing"
"github.com/navidrome/navidrome/utils/math2"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestMath2(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Math2 Suite")
}
var _ = Describe("Min", func() {
It("returns zero value if no arguments are passed", func() {
Expect(math2.Min[int]()).To(BeZero())
})
It("returns the smallest int", func() {
Expect(math2.Min(1, 2)).To(Equal(1))
})
It("returns the smallest float", func() {
Expect(math2.Min(-4.1, -4.2, -4.0)).To(Equal(-4.2))
})
})
var _ = Describe("Max", func() {
It("returns zero value if no arguments are passed", func() {
Expect(math2.Max[int]()).To(BeZero())
})
It("returns the biggest int", func() {
Expect(math2.Max(1, 2)).To(Equal(2))
})
It("returns the biggest float", func() {
Expect(math2.Max(-4.1, -4.2, -4.0)).To(Equal(-4.0))
})
})