-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfmt_fprintf_test.go
More file actions
185 lines (161 loc) · 3.66 KB
/
fmt_fprintf_test.go
File metadata and controls
185 lines (161 loc) · 3.66 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package fmt
import (
"bytes"
"strings"
"testing"
)
func TestFprintf(t *testing.T) {
tests := []struct {
name string
format string
args []any
expected string
}{
{
name: "simple string",
format: "Hello %s",
args: []any{"world"},
expected: "Hello world",
},
{
name: "integer formatting",
format: "Number: %d",
args: []any{42},
expected: "Number: 42",
},
{
name: "multiple args",
format: "Hello %s, you have %d messages",
args: []any{"John", 5},
expected: "Hello John, you have 5 messages",
},
{
name: "float formatting",
format: "Value: %.2f",
args: []any{3.14159},
expected: "Value: 3.14",
},
{
name: "boolean formatting",
format: "Active: %t",
args: []any{true},
expected: "Active: true",
},
{
name: "hex formatting",
format: "Hex: %x",
args: []any{255},
expected: "Hex: ff",
},
{
name: "no args",
format: "Simple text",
args: []any{},
expected: "Simple text",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
n, err := Fprintf(&buf, tt.format, tt.args...)
if err != nil {
t.Errorf("Fprintf() error = %v", err)
return
}
result := buf.String()
if result != tt.expected {
t.Errorf("Fprintf() = %q, want %q", result, tt.expected)
}
if n != len(tt.expected) {
t.Errorf("Fprintf() returned n = %d, want %d", n, len(tt.expected))
}
})
}
}
func TestFprintf_Errors(t *testing.T) {
tests := []struct {
name string
format string
args []any
}{
{
name: "missing argument",
format: "Hello %s %d",
args: []any{"world"}, // Missing second argument
},
{
name: "invalid format",
format: "Hello %z", // %z is not supported
args: []any{"world"},
},
{
name: "wrong type for %d",
format: "Number: %d",
args: []any{"not a number"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
n, err := Fprintf(&buf, tt.format, tt.args...)
if err == nil {
t.Errorf("Fprintf() expected error but got none, result: %q", buf.String())
}
if n != 0 {
t.Errorf("Fprintf() on error returned n = %d, want 0", n)
}
})
}
}
func TestFprintf_WriterError(t *testing.T) {
// Test with a writer that always returns an error
errorWriter := &errorOnlyWriter{}
n, err := Fprintf(errorWriter, "Hello %s", "world")
if err == nil {
t.Error("Fprintf() expected write error but got none")
}
if n != 0 {
t.Errorf("Fprintf() on write error returned n = %d, want 0", n)
}
// Error should be from the writer, not from formatting
if !strings.Contains(err.Error(), "write error") {
t.Errorf("Fprintf() error = %v, want write error", err)
}
}
// Helper writer that always returns an error
type errorOnlyWriter struct{}
func (w *errorOnlyWriter) Write(p []byte) (n int, err error) {
return 0, &writeError{"write error"}
}
type writeError struct {
msg string
}
func (e *writeError) Error() string {
return e.msg
}
func BenchmarkFprintf(b *testing.B) {
var buf bytes.Buffer
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf.Reset()
Fprintf(&buf, "Hello %s, number %d, float %.2f", "world", 42, 3.14159)
}
}
func BenchmarkFprintf_vs_Fmt(b *testing.B) {
var buf bytes.Buffer
format := "Hello %s, number %d, float %.2f"
args := []any{"world", 42, 3.14159}
b.Run("Fprintf", func(b *testing.B) {
for i := 0; i < b.N; i++ {
buf.Reset()
Fprintf(&buf, format, args...)
}
})
b.Run("Fmt+Write", func(b *testing.B) {
for i := 0; i < b.N; i++ {
buf.Reset()
result := Sprintf(format, args...)
buf.WriteString(result)
}
})
}