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

Function TestNullMatrixString

math/matrix/string_test.go:52–85  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

50}
51
52func 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
87func BenchmarkString(b *testing.B) {
88 // Create a sample matrix for benchmarking

Callers

nothing calls this directly

Calls 3

NewFunction · 0.92
CopyMethod · 0.80
StringMethod · 0.80

Tested by

no test coverage detected