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

Function TestMatrixString

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

Source from the content-addressed store, hash-verified

11)
12
13func 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
52func TestNullMatrixString(t *testing.T) {
53

Callers

nothing calls this directly

Calls 3

NewFromElementsFunction · 0.92
CopyMethod · 0.80
StringMethod · 0.80

Tested by

no test coverage detected