LEFT | RIGHT |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package time_test | 5 package time_test |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "encoding/gob" | 9 "encoding/gob" |
10 "encoding/json" | 10 "encoding/json" |
(...skipping 1208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1219 // imprecise round-trips. | 1219 // imprecise round-trips. |
1220 d0 := Duration(rand.Int31()) * Millisecond | 1220 d0 := Duration(rand.Int31()) * Millisecond |
1221 s := d0.String() | 1221 s := d0.String() |
1222 d1, err := ParseDuration(s) | 1222 d1, err := ParseDuration(s) |
1223 if err != nil || d0 != d1 { | 1223 if err != nil || d0 != d1 { |
1224 t.Errorf("round-trip failed: %d => %q => %d, %v", d0, s,
d1, err) | 1224 t.Errorf("round-trip failed: %d => %q => %d, %v", d0, s,
d1, err) |
1225 } | 1225 } |
1226 } | 1226 } |
1227 } | 1227 } |
1228 | 1228 |
| 1229 // golang.org/issue/4622 |
| 1230 func TestLocationRace(t *testing.T) { |
| 1231 ResetLocalOnceForTest() // reset the Once to trigger the race |
| 1232 |
| 1233 c := make(chan string, 1) |
| 1234 go func() { |
| 1235 c <- Now().String() |
| 1236 }() |
| 1237 Now().String() |
| 1238 <-c |
| 1239 Sleep(100 * Millisecond) |
| 1240 |
| 1241 // Back to Los Angeles for subsequent tests: |
| 1242 ForceUSPacificForTesting() |
| 1243 } |
| 1244 |
1229 var ( | 1245 var ( |
1230 t Time | 1246 t Time |
1231 u int64 | 1247 u int64 |
1232 ) | 1248 ) |
1233 | 1249 |
1234 var mallocTest = []struct { | 1250 var mallocTest = []struct { |
1235 count int | 1251 count int |
1236 desc string | 1252 desc string |
1237 fn func() | 1253 fn func() |
1238 }{ | 1254 }{ |
1239 {0, `time.Now()`, func() { t = Now() }}, | 1255 {0, `time.Now()`, func() { t = Now() }}, |
1240 {0, `time.Now().UnixNano()`, func() { u = Now().UnixNano() }}, | 1256 {0, `time.Now().UnixNano()`, func() { u = Now().UnixNano() }}, |
1241 } | 1257 } |
1242 | 1258 |
1243 func TestCountMallocs(t *testing.T) { | 1259 func TestCountMallocs(t *testing.T) { |
1244 const N = 100 | |
1245 | |
1246 for _, mt := range mallocTest { | 1260 for _, mt := range mallocTest { |
1247 » » allocs := testing.AllocsPerRun(N, mt.fn) | 1261 » » allocs := int(testing.AllocsPerRun(100, mt.fn)) |
1248 » » if max := float64(mt.count); allocs > max { | 1262 » » if allocs > mt.count { |
1249 » » » t.Errorf("%s: %v allocs, want <=%v", mt.desc, allocs, ma
x) | 1263 » » » t.Errorf("%s: %d allocs, want %d", mt.desc, allocs, mt.c
ount) |
1250 } | 1264 } |
1251 } | 1265 } |
1252 } | 1266 } |
1253 | 1267 |
1254 func BenchmarkNow(b *testing.B) { | 1268 func BenchmarkNow(b *testing.B) { |
1255 for i := 0; i < b.N; i++ { | 1269 for i := 0; i < b.N; i++ { |
1256 t = Now() | 1270 t = Now() |
1257 } | 1271 } |
1258 } | 1272 } |
1259 | 1273 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1305 _ = t.Year() | 1319 _ = t.Year() |
1306 } | 1320 } |
1307 } | 1321 } |
1308 | 1322 |
1309 func BenchmarkDay(b *testing.B) { | 1323 func BenchmarkDay(b *testing.B) { |
1310 t := Now() | 1324 t := Now() |
1311 for i := 0; i < b.N; i++ { | 1325 for i := 0; i < b.N; i++ { |
1312 _ = t.Day() | 1326 _ = t.Day() |
1313 } | 1327 } |
1314 } | 1328 } |
LEFT | RIGHT |