mirror of
https://github.com/ncruces/go-sqlite3.git
synced 2026-01-12 05:59:14 +00:00
22
internal/util/math.go
Normal file
22
internal/util/math.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package util
|
||||
|
||||
func abs(n int) int {
|
||||
if n < 0 {
|
||||
return -n
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func GCD(m, n int) int {
|
||||
for n != 0 {
|
||||
m, n = n, m%n
|
||||
}
|
||||
return abs(m)
|
||||
}
|
||||
|
||||
func LCM(m, n int) int {
|
||||
if n == 0 {
|
||||
return 0
|
||||
}
|
||||
return abs(n) * (abs(m) / GCD(m, n))
|
||||
}
|
||||
82
internal/util/math_test.go
Normal file
82
internal/util/math_test.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_abs(t *testing.T) {
|
||||
tests := []struct {
|
||||
arg int
|
||||
want int
|
||||
}{
|
||||
{0, 0},
|
||||
{1, 1},
|
||||
{-1, 1},
|
||||
{math.MaxInt, math.MaxInt},
|
||||
{math.MinInt, math.MinInt},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run("", func(t *testing.T) {
|
||||
if got := abs(tt.arg); got != tt.want {
|
||||
t.Errorf("abs(%d) = %d, want %d", tt.arg, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_GCD(t *testing.T) {
|
||||
tests := []struct {
|
||||
arg1 int
|
||||
arg2 int
|
||||
want int
|
||||
}{
|
||||
{0, 0, 0},
|
||||
{0, 1, 1},
|
||||
{1, 0, 1},
|
||||
{1, 1, 1},
|
||||
{2, 3, 1},
|
||||
{42, 56, 14},
|
||||
{48, -18, 6},
|
||||
{1e9, 1e9, 1e9},
|
||||
{1e9, -1e9, 1e9},
|
||||
{-1e9, -1e9, 1e9},
|
||||
{math.MaxInt, math.MaxInt, math.MaxInt},
|
||||
{math.MinInt, math.MinInt, math.MinInt},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run("", func(t *testing.T) {
|
||||
if got := GCD(tt.arg1, tt.arg2); got != tt.want {
|
||||
t.Errorf("gcd(%d, %d) = %d, want %d", tt.arg1, tt.arg2, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_LCM(t *testing.T) {
|
||||
tests := []struct {
|
||||
arg1 int
|
||||
arg2 int
|
||||
want int
|
||||
}{
|
||||
{0, 0, 0},
|
||||
{0, 1, 0},
|
||||
{1, 0, 0},
|
||||
{1, 1, 1},
|
||||
{2, 3, 6},
|
||||
{42, 56, 168},
|
||||
{48, -18, 144},
|
||||
{1e9, 1e9, 1e9},
|
||||
{1e9, -1e9, 1e9},
|
||||
{-1e9, -1e9, 1e9},
|
||||
{math.MaxInt, math.MaxInt, math.MaxInt},
|
||||
{math.MinInt, math.MinInt, math.MinInt},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run("", func(t *testing.T) {
|
||||
if got := LCM(tt.arg1, tt.arg2); got != tt.want {
|
||||
t.Errorf("lcm(%d, %d) = %d, want %d", tt.arg1, tt.arg2, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user