2024-10-17 23:53:39 +01:00
|
|
|
package util
|
2024-05-06 11:45:40 +01:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 23:53:39 +01:00
|
|
|
func Test_GCD(t *testing.T) {
|
2024-05-06 11:45:40 +01:00
|
|
|
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) {
|
2024-10-17 23:53:39 +01:00
|
|
|
if got := GCD(tt.arg1, tt.arg2); got != tt.want {
|
2024-05-06 11:45:40 +01:00
|
|
|
t.Errorf("gcd(%d, %d) = %d, want %d", tt.arg1, tt.arg2, got, tt.want)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 23:53:39 +01:00
|
|
|
func Test_LCM(t *testing.T) {
|
2024-05-06 11:45:40 +01:00
|
|
|
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) {
|
2024-10-17 23:53:39 +01:00
|
|
|
if got := LCM(tt.arg1, tt.arg2); got != tt.want {
|
2024-05-06 11:45:40 +01:00
|
|
|
t.Errorf("lcm(%d, %d) = %d, want %d", tt.arg1, tt.arg2, got, tt.want)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|