| 7 | ) |
| 8 | |
| 9 | func TestMatrixCopy(t *testing.T) { |
| 10 | // Create a sample matrix |
| 11 | data := [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} |
| 12 | // Ensure that the copy is not the same as the original |
| 13 | matrix, err := matrix.NewFromElements(data) |
| 14 | if err != nil { |
| 15 | t.Fatalf("Failed to copy matrix: %v", err) |
| 16 | } |
| 17 | copyMatrix, err := matrix.Copy() |
| 18 | if err != nil { |
| 19 | t.Fatalf("Failed to copy matrix: %v", err) |
| 20 | } |
| 21 | |
| 22 | // Ensure that the copy is not the same as the original |
| 23 | if &matrix == ©Matrix { |
| 24 | t.Errorf("Copy did not create a new matrix.") |
| 25 | } |
| 26 | |
| 27 | for i := 0; i < matrix.Rows(); i++ { |
| 28 | for j := 0; j < matrix.Columns(); j++ { |
| 29 | val1, err := matrix.Get(i, j) |
| 30 | if err != nil { |
| 31 | t.Fatalf("Failed to copy matrix: %v", err) |
| 32 | } |
| 33 | val2, err := copyMatrix.Get(i, j) |
| 34 | if err != nil { |
| 35 | t.Fatalf("Failed to copy matrix: %v", err) |
| 36 | } |
| 37 | if val1 != val2 { |
| 38 | t.Errorf("Copy did not correctly copy element (%d, %d).", i, j) |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | } |
| 44 | |
| 45 | func TestMatrixCopyEmpty(t *testing.T) { |
| 46 | // Create an empty matrix |