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 TestFlagAndPrefixSetting(t
*testing
.T
) {
93 l
:= New(&b
, "Test:", LstdFlags
)
96 t
.Errorf("Flags 1: expected %x got %x", LstdFlags
, f
)
98 l
.SetFlags(f | Lmicroseconds
)
100 if f
!= LstdFlags|Lmicroseconds
{
101 t
.Errorf("Flags 2: expected %x got %x", LstdFlags|Lmicroseconds
, f
)
105 t
.Errorf(`Prefix: expected "Test:" got %q`, p
)
107 l
.SetPrefix("Reality:")
110 t
.Errorf(`Prefix: expected "Reality:" got %q`, p
)
112 // Verify a log message looks right, with our prefix and microseconds present.
114 pattern
:= "^Reality:" + Rdate
+ " " + Rtime
+ Rmicroseconds
+ " hello\n"
115 matched
, err
:= regexp
.Match(pattern
, b
.Bytes())
117 t
.Fatalf("pattern %q did not compile: %s", pattern
, err
)
120 t
.Error("message did not match pattern")
124 func TestUTCFlag(t
*testing
.T
) {
126 l
:= New(&b
, "Test:", LstdFlags
)
127 l
.SetFlags(Ldate | Ltime | LUTC
)
128 // Verify a log message looks right in the right time zone. Quantize to the second only.
129 now
:= time
.Now().UTC()
131 want
:= fmt
.Sprintf("Test:%d/%.2d/%.2d %.2d:%.2d:%.2d hello\n",
132 now
.Year(), now
.Month(), now
.Day(), now
.Hour(), now
.Minute(), now
.Second())
137 // It's possible we crossed a second boundary between getting now and logging,
138 // so add a second and try again. This should very nearly always work.
139 now
= now
.Add(time
.Second
)
140 want
= fmt
.Sprintf("Test:%d/%.2d/%.2d %.2d:%.2d:%.2d hello\n",
141 now
.Year(), now
.Month(), now
.Day(), now
.Hour(), now
.Minute(), now
.Second())
145 t
.Errorf("got %q; want %q", got
, want
)
148 func TestEmptyPrintCreatesLine(t
*testing
.T
) {
150 l
:= New(&b
, "Header:", LstdFlags
)
152 l
.Println("non-empty")
154 if n
:= strings
.Count(output
, "Header"); n
!= 2 {
155 t
.Errorf("expected 2 headers, got %d", n
)
157 if n
:= strings
.Count(output
, "\n"); n
!= 2 {
158 t
.Errorf("expected 2 lines, got %d", n
)
162 func BenchmarkItoa(b
*testing
.B
) {
163 dst
:= make([]byte, 0, 64)
164 for i
:= 0; i
< b
.N
; i
++ {
166 itoa(&dst
, 2015, 4) // year
167 itoa(&dst
, 1, 2) // month
168 itoa(&dst
, 30, 2) // day
169 itoa(&dst
, 12, 2) // hour
170 itoa(&dst
, 56, 2) // minute
171 itoa(&dst
, 0, 2) // second
172 itoa(&dst
, 987654, 6) // microsecond
176 func BenchmarkPrintln(b
*testing
.B
) {
177 const testString
= "test"
179 l
:= New(&buf
, "", LstdFlags
)
180 for i
:= 0; i
< b
.N
; i
++ {
182 l
.Println(testString
)