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.
20 Rdate
= `[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]`
21 Rtime
= `[0-9][0-9]:[0-9][0-9]:[0-9][0-9]`
22 Rmicroseconds
= `\.[0-9][0-9][0-9][0-9][0-9][0-9]`
23 Rline
= `(57|59):` // must update if the calls to l.Printf / l.Print below move
24 Rlongfile
= `.*/[A-Za-z0-9_\-]+\.go:` + Rline
25 Rshortfile
= `[A-Za-z0-9_\-]+\.go:` + Rline
31 pattern
string // regexp that log output must match; we add ^ and expected_text$ always
38 {Ldate
, "", Rdate
+ " "},
39 {Ltime
, "", Rtime
+ " "},
40 {Ltime | Lmicroseconds
, "", Rtime
+ Rmicroseconds
+ " "},
41 {Lmicroseconds
, "", Rtime
+ Rmicroseconds
+ " "}, // microsec implies time
42 {Llongfile
, "", Rlongfile
+ " "},
43 {Lshortfile
, "", Rshortfile
+ " "},
44 {Llongfile | Lshortfile
, "", Rshortfile
+ " "}, // shortfile overrides longfile
45 // everything at once:
46 {Ldate | Ltime | Lmicroseconds | Llongfile
, "XXX", "XXX" + Rdate
+ " " + Rtime
+ Rmicroseconds
+ " " + Rlongfile
+ " "},
47 {Ldate | Ltime | Lmicroseconds | Lshortfile
, "XXX", "XXX" + Rdate
+ " " + Rtime
+ Rmicroseconds
+ " " + Rshortfile
+ " "},
50 // Test using Println("hello", 23, "world") or using Printf("hello %d world", 23)
51 func testPrint(t
*testing
.T
, flag
int, prefix
string, pattern
string, useFormat
bool) {
52 buf
:= new(bytes
.Buffer
)
57 Printf("hello %d world", 23)
59 Println("hello", 23, "world")
62 line
= line
[0 : len(line
)-1]
63 pattern
= "^" + pattern
+ "hello 23 world$"
64 matched
, err4
:= regexp
.MatchString(pattern
, line
)
66 t
.Fatal("pattern did not compile:", err4
)
69 t
.Errorf("log output should match %q is %q", pattern
, line
)
74 func TestAll(t
*testing
.T
) {
75 for _
, testcase
:= range tests
{
76 testPrint(t
, testcase
.flag
, testcase
.prefix
, testcase
.pattern
, false)
77 testPrint(t
, testcase
.flag
, testcase
.prefix
, testcase
.pattern
, true)
81 func TestOutput(t
*testing
.T
) {
82 const testString
= "test"
86 if expect
:= testString
+ "\n"; b
.String() != expect
{
87 t
.Errorf("log output should match %q is %q", expect
, b
.String())
91 func TestOutputRace(t
*testing
.T
) {
94 for i
:= 0; i
< 100; i
++ {
102 func TestFlagAndPrefixSetting(t
*testing
.T
) {
104 l
:= New(&b
, "Test:", LstdFlags
)
107 t
.Errorf("Flags 1: expected %x got %x", LstdFlags
, f
)
109 l
.SetFlags(f | Lmicroseconds
)
111 if f
!= LstdFlags|Lmicroseconds
{
112 t
.Errorf("Flags 2: expected %x got %x", LstdFlags|Lmicroseconds
, f
)
116 t
.Errorf(`Prefix: expected "Test:" got %q`, p
)
118 l
.SetPrefix("Reality:")
121 t
.Errorf(`Prefix: expected "Reality:" got %q`, p
)
123 // Verify a log message looks right, with our prefix and microseconds present.
125 pattern
:= "^Reality:" + Rdate
+ " " + Rtime
+ Rmicroseconds
+ " hello\n"
126 matched
, err
:= regexp
.Match(pattern
, b
.Bytes())
128 t
.Fatalf("pattern %q did not compile: %s", pattern
, err
)
131 t
.Error("message did not match pattern")
135 func TestUTCFlag(t
*testing
.T
) {
137 l
:= New(&b
, "Test:", LstdFlags
)
138 l
.SetFlags(Ldate | Ltime | LUTC
)
139 // Verify a log message looks right in the right time zone. Quantize to the second only.
140 now
:= time
.Now().UTC()
142 want
:= fmt
.Sprintf("Test:%d/%.2d/%.2d %.2d:%.2d:%.2d hello\n",
143 now
.Year(), now
.Month(), now
.Day(), now
.Hour(), now
.Minute(), now
.Second())
148 // It's possible we crossed a second boundary between getting now and logging,
149 // so add a second and try again. This should very nearly always work.
150 now
= now
.Add(time
.Second
)
151 want
= fmt
.Sprintf("Test:%d/%.2d/%.2d %.2d:%.2d:%.2d hello\n",
152 now
.Year(), now
.Month(), now
.Day(), now
.Hour(), now
.Minute(), now
.Second())
156 t
.Errorf("got %q; want %q", got
, want
)
159 func TestEmptyPrintCreatesLine(t
*testing
.T
) {
161 l
:= New(&b
, "Header:", LstdFlags
)
163 l
.Println("non-empty")
165 if n
:= strings
.Count(output
, "Header"); n
!= 2 {
166 t
.Errorf("expected 2 headers, got %d", n
)
168 if n
:= strings
.Count(output
, "\n"); n
!= 2 {
169 t
.Errorf("expected 2 lines, got %d", n
)
173 func BenchmarkItoa(b
*testing
.B
) {
174 dst
:= make([]byte, 0, 64)
175 for i
:= 0; i
< b
.N
; i
++ {
177 itoa(&dst
, 2015, 4) // year
178 itoa(&dst
, 1, 2) // month
179 itoa(&dst
, 30, 2) // day
180 itoa(&dst
, 12, 2) // hour
181 itoa(&dst
, 56, 2) // minute
182 itoa(&dst
, 0, 2) // second
183 itoa(&dst
, 987654, 6) // microsecond
187 func BenchmarkPrintln(b
*testing
.B
) {
188 const testString
= "test"
190 l
:= New(&buf
, "", LstdFlags
)
191 for i
:= 0; i
< b
.N
; i
++ {
193 l
.Println(testString
)
197 func BenchmarkPrintlnNoFlags(b
*testing
.B
) {
198 const testString
= "test"
200 l
:= New(&buf
, "", 0)
201 for i
:= 0; i
< b
.N
; i
++ {
203 l
.Println(testString
)