MCPcopy Index your code
hub / github.com/TheAlgorithms/Go / TestMatrixCopyWithDefaultValues

Function TestMatrixCopyWithDefaultValues

math/matrix/copy_test.go:66–99  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

64}
65
66func TestMatrixCopyWithDefaultValues(t *testing.T) {
67 // Create a matrix with default values (zeroes)
68 rows, columns := 3, 3
69 defaultValue := 0
70 defaultMatrix := matrix.New(rows, columns, defaultValue)
71
72 // Make a copy of the matrix
73 copyMatrix, err := defaultMatrix.Copy()
74 if err != nil {
75 t.Fatalf("Failed to copy matrix: %v", err)
76 }
77
78 // Ensure that the copy is not the same as the original by comparing their addresses
79 if &defaultMatrix == &copyMatrix {
80 t.Errorf("Copy did not create a new matrix for default values.")
81 }
82
83 // Check if the copy has the same values as the original (all zeroes)
84 for i := 0; i < defaultMatrix.Rows(); i++ {
85 for j := 0; j < defaultMatrix.Columns(); j++ {
86 val1, err := defaultMatrix.Get(i, j)
87 if err != nil {
88 t.Fatalf("Failed to copy matrix: %v", err)
89 }
90 val2, err := copyMatrix.Get(i, j)
91 if err != nil {
92 t.Fatalf("Failed to copy matrix: %v", err)
93 }
94 if val1 != val2 || val1 != defaultValue || val2 != defaultValue {
95 t.Errorf("Copy did not preserve default values at row %d, column %d. Expected %v, got %v", i, j, defaultValue, val2)
96 }
97 }
98 }
99}
100
101func BenchmarkCopyMatrix(b *testing.B) {
102 // Create a matrix for benchmarking

Callers

nothing calls this directly

Calls 5

NewFunction · 0.92
CopyMethod · 0.80
RowsMethod · 0.80
ColumnsMethod · 0.80
GetMethod · 0.65

Tested by

no test coverage detected