(t *testing.T)
| 11 | ) |
| 12 | |
| 13 | func TestMatrixString(t *testing.T) { |
| 14 | // Create a sample matrix for testing |
| 15 | m1, err := matrix.NewFromElements([][]int{{1, 2}, {3, 4}}) |
| 16 | if err != nil { |
| 17 | t.Errorf("Error creating matrix: %v", err) |
| 18 | } |
| 19 | |
| 20 | // Redirect stdout to capture Stringed output |
| 21 | old := os.Stdout |
| 22 | r, w, err := os.Pipe() |
| 23 | if err != nil { |
| 24 | t.Fatalf("Failed to copy matrix: %v", err) |
| 25 | } |
| 26 | os.Stdout = w |
| 27 | |
| 28 | // Call the String method |
| 29 | fmt.Print(m1) |
| 30 | |
| 31 | // Reset stdout |
| 32 | w.Close() |
| 33 | os.Stdout = old |
| 34 | |
| 35 | // Read the captured output |
| 36 | var buf bytes.Buffer |
| 37 | _, err = io.Copy(&buf, r) |
| 38 | if err != nil { |
| 39 | t.Errorf("Error copying output: %v", err) |
| 40 | } |
| 41 | capturedOutput := buf.String() |
| 42 | |
| 43 | // Define the expected output |
| 44 | expectedOutput := "1 2 \n3 4 \n" |
| 45 | |
| 46 | // Compare the captured output with the expected output |
| 47 | if capturedOutput != expectedOutput { |
| 48 | t.Errorf("Matrix.Print() produced incorrect output:\n%s\nExpected:\n%s", capturedOutput, expectedOutput) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestNullMatrixString(t *testing.T) { |
| 53 |
nothing calls this directly
no test coverage detected