| 50 | } |
| 51 | |
| 52 | func TestNullMatrixString(t *testing.T) { |
| 53 | |
| 54 | m1 := matrix.New(0, 0, 0) |
| 55 | // Redirect stdout to capture Stringed output |
| 56 | old := os.Stdout |
| 57 | r, w, err := os.Pipe() |
| 58 | if err != nil { |
| 59 | t.Fatalf("Failed to copy matrix: %v", err) |
| 60 | } |
| 61 | os.Stdout = w |
| 62 | |
| 63 | // Call the String method |
| 64 | fmt.Print(m1) |
| 65 | |
| 66 | // Reset stdout |
| 67 | w.Close() |
| 68 | os.Stdout = old |
| 69 | |
| 70 | // Read the captured output |
| 71 | var buf bytes.Buffer |
| 72 | _, err = io.Copy(&buf, r) |
| 73 | if err != nil { |
| 74 | t.Errorf("Error copying output: %v", err) |
| 75 | } |
| 76 | capturedOutput := buf.String() |
| 77 | |
| 78 | // Define the expected output |
| 79 | expectedOutput := "" |
| 80 | |
| 81 | // Compare the captured output with the expected output |
| 82 | if capturedOutput != expectedOutput { |
| 83 | t.Errorf("Matrix.Print() produced incorrect output:\n%s\nExpected:\n%s", capturedOutput, expectedOutput) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func BenchmarkString(b *testing.B) { |
| 88 | // Create a sample matrix for benchmarking |