-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathstring_test.go
96 lines (78 loc) · 1.98 KB
/
string_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package matrix_test
import (
"bytes"
"fmt"
"io"
"os"
"testing"
"github.com/TheAlgorithms/Go/math/matrix"
)
func TestMatrixString(t *testing.T) {
// Create a sample matrix for testing
m1, err := matrix.NewFromElements([][]int{{1, 2}, {3, 4}})
if err != nil {
t.Errorf("Error creating matrix: %v", err)
}
// Redirect stdout to capture Stringed output
old := os.Stdout
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
os.Stdout = w
// Call the String method
fmt.Print(m1)
// Reset stdout
w.Close()
os.Stdout = old
// Read the captured output
var buf bytes.Buffer
_, err = io.Copy(&buf, r)
if err != nil {
t.Errorf("Error copying output: %v", err)
}
capturedOutput := buf.String()
// Define the expected output
expectedOutput := "1 2 \n3 4 \n"
// Compare the captured output with the expected output
if capturedOutput != expectedOutput {
t.Errorf("Matrix.Print() produced incorrect output:\n%s\nExpected:\n%s", capturedOutput, expectedOutput)
}
}
func TestNullMatrixString(t *testing.T) {
m1 := matrix.New(0, 0, 0)
// Redirect stdout to capture Stringed output
old := os.Stdout
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
os.Stdout = w
// Call the String method
fmt.Print(m1)
// Reset stdout
w.Close()
os.Stdout = old
// Read the captured output
var buf bytes.Buffer
_, err = io.Copy(&buf, r)
if err != nil {
t.Errorf("Error copying output: %v", err)
}
capturedOutput := buf.String()
// Define the expected output
expectedOutput := ""
// Compare the captured output with the expected output
if capturedOutput != expectedOutput {
t.Errorf("Matrix.Print() produced incorrect output:\n%s\nExpected:\n%s", capturedOutput, expectedOutput)
}
}
func BenchmarkString(b *testing.B) {
// Create a sample matrix for benchmarking
rows := 100
columns := 100
m := matrix.New(rows, columns, 0) // Replace with appropriate values
for i := 0; i < b.N; i++ {
_ = m.String()
}
}