You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
272 lines
3.8 KiB
Go
272 lines
3.8 KiB
Go
package math
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestIsPrime(t *testing.T) {
|
|
tests := []struct {
|
|
n int
|
|
expected bool
|
|
}{
|
|
{
|
|
n: 0,
|
|
expected: false,
|
|
},
|
|
{
|
|
n: 1,
|
|
expected: false,
|
|
},
|
|
{
|
|
n: 2,
|
|
expected: true,
|
|
},
|
|
{
|
|
n: 3,
|
|
expected: true,
|
|
},
|
|
{
|
|
n: 4,
|
|
expected: false,
|
|
},
|
|
{
|
|
n: 5,
|
|
expected: true,
|
|
},
|
|
{
|
|
n: 6,
|
|
expected: false,
|
|
},
|
|
// Add more test cases as needed
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
result := IsPrime(tt.n)
|
|
if result != tt.expected {
|
|
t.Errorf("IsPrime(%d) = %v, expected %v", tt.n, result, tt.expected)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestIntPow(t *testing.T) {
|
|
tests := []struct {
|
|
x int
|
|
y int
|
|
expected int
|
|
}{
|
|
{
|
|
x: 2,
|
|
y: 3,
|
|
expected: 8,
|
|
},
|
|
{
|
|
x: 5,
|
|
y: 0,
|
|
expected: 1,
|
|
},
|
|
{
|
|
x: 10,
|
|
y: 1,
|
|
expected: 10,
|
|
},
|
|
{
|
|
x: 3,
|
|
y: 4,
|
|
expected: 81,
|
|
},
|
|
{
|
|
x: 2,
|
|
y: 0,
|
|
expected: 1,
|
|
},
|
|
{
|
|
x: 0,
|
|
y: 5,
|
|
expected: 0,
|
|
},
|
|
{
|
|
x: -2,
|
|
y: 3,
|
|
expected: -8,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
result := IntPow(tt.x, tt.y)
|
|
if result != tt.expected {
|
|
t.Errorf("IntPow(%d, %d) = %d, expected %d", tt.x, tt.y, result, tt.expected)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPow2(t *testing.T) {
|
|
tests := []struct {
|
|
x int
|
|
expected int
|
|
}{
|
|
{
|
|
x: 0,
|
|
expected: 1,
|
|
},
|
|
{
|
|
x: 1,
|
|
expected: 2,
|
|
},
|
|
{
|
|
x: 2,
|
|
expected: 4,
|
|
},
|
|
{
|
|
x: 3,
|
|
expected: 8,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
result := Pow2(tt.x)
|
|
if result != tt.expected {
|
|
t.Errorf("Pow2(%d) = %d, expected %d", tt.x, result, tt.expected)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGCD(t *testing.T) {
|
|
tests := []struct {
|
|
a int
|
|
b int
|
|
expected int
|
|
}{
|
|
{
|
|
a: 0,
|
|
b: 0,
|
|
expected: 0,
|
|
},
|
|
{
|
|
a: 0,
|
|
b: 5,
|
|
expected: 5,
|
|
},
|
|
{
|
|
a: 10,
|
|
b: 5,
|
|
expected: 5,
|
|
},
|
|
{
|
|
a: 12,
|
|
b: 8,
|
|
expected: 4,
|
|
},
|
|
{
|
|
a: 15,
|
|
b: 25,
|
|
expected: 5,
|
|
},
|
|
{
|
|
a: 21,
|
|
b: 14,
|
|
expected: 7,
|
|
},
|
|
{
|
|
a: 100,
|
|
b: 75,
|
|
expected: 25,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
result := GCD(tt.a, tt.b)
|
|
if result != tt.expected {
|
|
t.Errorf("GCD(%d, %d) = %d, expected %d", tt.a, tt.b, result, tt.expected)
|
|
}
|
|
}
|
|
}
|
|
func TestLCM(t *testing.T) {
|
|
tests := []struct {
|
|
a int
|
|
b int
|
|
integers []int
|
|
expected int
|
|
}{
|
|
{
|
|
a: 2,
|
|
b: 3,
|
|
integers: []int{4, 5},
|
|
expected: 60,
|
|
},
|
|
{
|
|
a: 5,
|
|
b: 7,
|
|
integers: []int{10, 15},
|
|
expected: 210,
|
|
},
|
|
{
|
|
a: 12,
|
|
b: 18,
|
|
integers: []int{24, 36},
|
|
expected: 72,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
result := LCM(tt.a, tt.b, tt.integers...)
|
|
if result != tt.expected {
|
|
t.Errorf("LCM(%d, %d, %v) = %d, expected %d", tt.a, tt.b, tt.integers, result, tt.expected)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAbsInt(t *testing.T) {
|
|
tests := []struct {
|
|
x int
|
|
expected int
|
|
}{
|
|
{
|
|
x: 0,
|
|
expected: 0,
|
|
},
|
|
{
|
|
x: 5,
|
|
expected: 5,
|
|
},
|
|
{
|
|
x: -10,
|
|
expected: 10,
|
|
},
|
|
{
|
|
x: 100,
|
|
expected: 100,
|
|
},
|
|
{
|
|
x: -50,
|
|
expected: 50,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
result := AbsInt(tt.x)
|
|
if result != tt.expected {
|
|
t.Errorf("AbsInt(%d) = %d, expected %d", tt.x, result, tt.expected)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGaussianElimination(t *testing.T) {
|
|
coefficients := [][]float64{
|
|
{2, 1, -1},
|
|
{-3, -1, 2},
|
|
{-2, 1, 2},
|
|
}
|
|
rhs := []float64{8, -11, -3}
|
|
|
|
expected := []float64{2, 3, -1}
|
|
|
|
gaussianElimination(coefficients, rhs)
|
|
|
|
for i := 0; i < len(rhs); i++ {
|
|
if rhs[i] != expected[i] {
|
|
t.Errorf("GaussianElimination failed. Expected %v, got %v", expected, rhs)
|
|
break
|
|
}
|
|
}
|
|
}
|