-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathdeterminant_test.go
142 lines (116 loc) · 3.61 KB
/
determinant_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package matrix_test
import (
"errors"
"math"
"math/rand"
"testing"
"github.com/TheAlgorithms/Go/math/matrix"
)
// Test different matrix contents
func TestMatrixDeterminant(t *testing.T) {
// Find Determinant of a 2 by 2 matrix.
matrix1, err := matrix.NewFromElements([][]int{
{3, 8},
{4, 6},
})
if err != nil {
t.Fatalf("Error creating 3 by 3 matrix: %v", err)
}
determinant, err := matrix1.Determinant()
if err != nil {
t.Fatalf("Error returned from 3 by 3 matrix: %v", err)
}
if determinant != -14 {
t.Fatalf("Determinant returned for a 3 by 3 matrix was %d; wanted -14", determinant)
}
// Find Dertminant of a 1 by 1 matrix
expectedValue := rand.Intn(math.MaxInt)
matrix2, err := matrix.NewFromElements([][]int{
{expectedValue},
})
if err != nil {
t.Fatalf("Error creating 1 by 1 matrix: %v", err)
}
determinant, err = matrix2.Determinant()
if err != nil {
t.Fatalf("Error returned from 1 by 1 matrix: %v", err)
}
if determinant != expectedValue {
t.Fatalf("Determinant returned for a 1 by 1 matrix was %d; wanted %d", determinant, expectedValue)
}
}
func TestEmptyMatrix(t *testing.T) {
emptyElements := [][]int{}
matrix, err := matrix.NewFromElements(emptyElements)
if err != nil {
t.Fatalf("Error creating Matrix with empty elements: %v", err)
}
determinant, err := matrix.Determinant()
if err != nil {
t.Fatalf("Determinant returned an error for empty matrix: %v", err)
}
// Check that 0 is returned from an empty matrix.
expectedValue := 0
if determinant != expectedValue {
t.Errorf("Determinant returned from empty matrix was %d; wanted %d", determinant, expectedValue)
}
}
func TestNonSquareMatrix(t *testing.T) {
// Creating non-square matrix for testing.
initialValue := 0
initialRows := 4
initialCols := 2
nonSquareMatrix := matrix.New(initialRows, initialCols, initialValue)
determinant, err := nonSquareMatrix.Determinant()
// Check if non square matrix returns an error.
if err == nil {
t.Fatalf("No error was returned for a non-square matrix")
}
// Check if the correct error was returned.
expectedError := errors.New("Matrix rows and columns must equal in order to find the determinant.")
if err.Error() != expectedError.Error() {
t.Errorf("Error returned from non-square matrix was \n\"%v\"; \nwanted \n\"%v\"", err, expectedError)
}
// Check if the determinant of the non-square matrix is 0.
if determinant != 0 {
t.Errorf("Determinant of non-square matrix was not 0 but was %d", determinant)
}
}
// Test matrix returned from matrix.New
func TestDefaultMatrix(t *testing.T) {
initialValue := 0
initialRows := 3
initialCols := 3
defaultMatrix := matrix.New(initialRows, initialCols, initialValue)
determinant, err := defaultMatrix.Determinant()
if err != nil {
t.Fatalf("Error finding the determinant of 3 by 3 default matrix: %v.", err)
}
expectedValue := 0
if determinant != expectedValue {
t.Errorf("Determinant of the default matrix with an initial value 0 was %d; wanted %d.", initialValue, expectedValue)
}
}
// Benchmark a 3 by 3 matrix for computational throughput
func BenchmarkSmallMatrixDeterminant(b *testing.B) {
// Create a 3 by 3 matrix for benchmarking
rows := 3
columns := 3
initialValue := 0
matrix := matrix.New(rows, columns, initialValue)
for i := 0; i < b.N; i++ {
_, _ = matrix.Determinant()
}
}
// Benchmark a 10 by 10 matrix for computational throughput.
func BenchmarkMatrixDeterminant(b *testing.B) {
// Create a 10 by 10 matrix for benchmarking
rows := 10
columns := 10
initialValue := 0
matrix := matrix.New(rows, columns, initialValue)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = matrix.Determinant()
}
}