Changes to be committed:

modified:   pkg/obioptions/version.go
	modified:   pkg/obitools/obitagpcr/pcrtag.go
	modified:   pkg/obiutils/abs.go
	new file:   pkg/obiutils/abs_test.go
This commit is contained in:
Eric Coissac
2024-10-28 21:51:21 +01:00
parent 3e00d39d47
commit fefc360f80
4 changed files with 102 additions and 40 deletions

View File

@ -2,9 +2,13 @@ package obiutils
import "golang.org/x/exp/constraints"
// Abs returns the absolute value of x.
//
// It is a generic function that can be used on any signed type.
func Abs[T constraints.Signed](x T) T {
if x < 0 {
return -x
}
return x
}

44
pkg/obiutils/abs_test.go Normal file
View File

@ -0,0 +1,44 @@
package obiutils_test
import (
"testing"
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiutils"
)
func TestAbs(t *testing.T) {
// Test cases for positive integers
testCasesPositive := []struct {
input int
expected int
}{
{0, 0},
{1, 1},
{5, 5},
{10, 10},
}
for _, tc := range testCasesPositive {
result := obiutils.Abs(tc.input)
if result != tc.expected {
t.Errorf("Abs(%d) = %d; want %d", tc.input, result, tc.expected)
}
}
// Test cases for negative integers
testCasesNegative := []struct {
input int
expected int
}{
{-1, 1},
{-5, 5},
{-10, 10},
}
for _, tc := range testCasesNegative {
result := obiutils.Abs(tc.input)
if result != tc.expected {
t.Errorf("Abs(%d) = %d; want %d", tc.input, result, tc.expected)
}
}
}