1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 // These tests are too simple.
21 Rdate
= `[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]`
22 Rtime
= `[0-9][0-9]:[0-9][0-9]:[0-9][0-9]`
23 Rmicroseconds
= `\.[0-9][0-9][0-9][0-9][0-9][0-9]`
24 Rline
= `(61|63):` // must update if the calls to l.Printf / l.Print below move
25 Rlongfile
= `.*/[A-Za-z0-9_\-]+\.go:` + Rline
26 Rshortfile
= `[A-Za-z0-9_\-]+\.go:` + Rline
32 pattern
string // regexp that log output must match; we add ^ and expected_text$ always
39 {Ldate
, "", Rdate
+ " "},
40 {Ltime
, "", Rtime
+ " "},
41 {Ltime | Lmsgprefix
, "XXX", Rtime
+ " XXX"},
42 {Ltime | Lmicroseconds
, "", Rtime
+ Rmicroseconds
+ " "},
43 {Lmicroseconds
, "", Rtime
+ Rmicroseconds
+ " "}, // microsec implies time
44 {Llongfile
, "", Rlongfile
+ " "},
45 {Lshortfile
, "", Rshortfile
+ " "},
46 {Llongfile | Lshortfile
, "", Rshortfile
+ " "}, // shortfile overrides longfile
47 // everything at once:
48 {Ldate | Ltime | Lmicroseconds | Llongfile
, "XXX", "XXX" + Rdate
+ " " + Rtime
+ Rmicroseconds
+ " " + Rlongfile
+ " "},
49 {Ldate | Ltime | Lmicroseconds | Lshortfile
, "XXX", "XXX" + Rdate
+ " " + Rtime
+ Rmicroseconds
+ " " + Rshortfile
+ " "},
50 {Ldate | Ltime | Lmicroseconds | Llongfile | Lmsgprefix
, "XXX", Rdate
+ " " + Rtime
+ Rmicroseconds
+ " " + Rlongfile
+ " XXX"},
51 {Ldate | Ltime | Lmicroseconds | Lshortfile | Lmsgprefix
, "XXX", Rdate
+ " " + Rtime
+ Rmicroseconds
+ " " + Rshortfile
+ " XXX"},
54 // Test using Println("hello", 23, "world") or using Printf("hello %d world", 23)
55 func testPrint(t
*testing
.T
, flag
int, prefix
string, pattern
string, useFormat
bool) {
56 buf
:= new(bytes
.Buffer
)
61 Printf("hello %d world", 23)
63 Println("hello", 23, "world")
66 line
= line
[0 : len(line
)-1]
67 pattern
= "^" + pattern
+ "hello 23 world$"
68 matched
, err
:= regexp
.MatchString(pattern
, line
)
70 t
.Fatal("pattern did not compile:", err
)
73 t
.Errorf("log output should match %q is %q", pattern
, line
)
78 func TestDefault(t
*testing
.T
) {
79 if got
:= Default(); got
!= std
{
80 t
.Errorf("Default [%p] should be std [%p]", got
, std
)
84 func TestAll(t
*testing
.T
) {
85 for _
, testcase
:= range tests
{
86 testPrint(t
, testcase
.flag
, testcase
.prefix
, testcase
.pattern
, false)
87 testPrint(t
, testcase
.flag
, testcase
.prefix
, testcase
.pattern
, true)
91 func TestOutput(t
*testing
.T
) {
92 const testString
= "test"
96 if expect
:= testString
+ "\n"; b
.String() != expect
{
97 t
.Errorf("log output should match %q is %q", expect
, b
.String())
101 func TestOutputRace(t
*testing
.T
) {
104 for i
:= 0; i
< 100; i
++ {
112 func TestFlagAndPrefixSetting(t
*testing
.T
) {
114 l
:= New(&b
, "Test:", LstdFlags
)
117 t
.Errorf("Flags 1: expected %x got %x", LstdFlags
, f
)
119 l
.SetFlags(f | Lmicroseconds
)
121 if f
!= LstdFlags|Lmicroseconds
{
122 t
.Errorf("Flags 2: expected %x got %x", LstdFlags|Lmicroseconds
, f
)
126 t
.Errorf(`Prefix: expected "Test:" got %q`, p
)
128 l
.SetPrefix("Reality:")
131 t
.Errorf(`Prefix: expected "Reality:" got %q`, p
)
133 // Verify a log message looks right, with our prefix and microseconds present.
135 pattern
:= "^Reality:" + Rdate
+ " " + Rtime
+ Rmicroseconds
+ " hello\n"
136 matched
, err
:= regexp
.Match(pattern
, b
.Bytes())
138 t
.Fatalf("pattern %q did not compile: %s", pattern
, err
)
141 t
.Error("message did not match pattern")
145 func TestUTCFlag(t
*testing
.T
) {
147 l
:= New(&b
, "Test:", LstdFlags
)
148 l
.SetFlags(Ldate | Ltime | LUTC
)
149 // Verify a log message looks right in the right time zone. Quantize to the second only.
150 now
:= time
.Now().UTC()
152 want
:= fmt
.Sprintf("Test:%d/%.2d/%.2d %.2d:%.2d:%.2d hello\n",
153 now
.Year(), now
.Month(), now
.Day(), now
.Hour(), now
.Minute(), now
.Second())
158 // It's possible we crossed a second boundary between getting now and logging,
159 // so add a second and try again. This should very nearly always work.
160 now
= now
.Add(time
.Second
)
161 want
= fmt
.Sprintf("Test:%d/%.2d/%.2d %.2d:%.2d:%.2d hello\n",
162 now
.Year(), now
.Month(), now
.Day(), now
.Hour(), now
.Minute(), now
.Second())
166 t
.Errorf("got %q; want %q", got
, want
)
169 func TestEmptyPrintCreatesLine(t
*testing
.T
) {
171 l
:= New(&b
, "Header:", LstdFlags
)
173 l
.Println("non-empty")
175 if n
:= strings
.Count(output
, "Header"); n
!= 2 {
176 t
.Errorf("expected 2 headers, got %d", n
)
178 if n
:= strings
.Count(output
, "\n"); n
!= 2 {
179 t
.Errorf("expected 2 lines, got %d", n
)
183 func TestDiscard(t
*testing
.T
) {
184 l
:= New(io
.Discard
, "", 0)
185 s
:= strings
.Repeat("a", 102400)
186 c
:= testing
.AllocsPerRun(100, func() { l
.Printf("%s", s
) })
187 // One allocation for slice passed to Printf,
188 // but none for formatting of long string.
190 t
.Errorf("got %v allocs, want at most 1", c
)
194 func BenchmarkItoa(b
*testing
.B
) {
195 dst
:= make([]byte, 0, 64)
196 for i
:= 0; i
< b
.N
; i
++ {
198 itoa(&dst
, 2015, 4) // year
199 itoa(&dst
, 1, 2) // month
200 itoa(&dst
, 30, 2) // day
201 itoa(&dst
, 12, 2) // hour
202 itoa(&dst
, 56, 2) // minute
203 itoa(&dst
, 0, 2) // second
204 itoa(&dst
, 987654, 6) // microsecond
208 func BenchmarkPrintln(b
*testing
.B
) {
209 const testString
= "test"
211 l
:= New(&buf
, "", LstdFlags
)
212 for i
:= 0; i
< b
.N
; i
++ {
214 l
.Println(testString
)
218 func BenchmarkPrintlnNoFlags(b
*testing
.B
) {
219 const testString
= "test"
221 l
:= New(&buf
, "", 0)
222 for i
:= 0; i
< b
.N
; i
++ {
224 l
.Println(testString
)