OLD | NEW |
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 1130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1141 t.Errorf("Decoded time = %v, want %v", gobtt, tt) | 1141 t.Errorf("Decoded time = %v, want %v", gobtt, tt) |
1142 } | 1142 } |
1143 b.Reset() | 1143 b.Reset() |
1144 } | 1144 } |
1145 } | 1145 } |
1146 | 1146 |
1147 var invalidEncodingTests = []struct { | 1147 var invalidEncodingTests = []struct { |
1148 bytes []byte | 1148 bytes []byte |
1149 want string | 1149 want string |
1150 }{ | 1150 }{ |
1151 » {[]byte{}, "Time.GobDecode: no data"}, | 1151 » {[]byte{}, "Time.UnmarshalBinary: no data"}, |
1152 » {[]byte{0, 2, 3}, "Time.GobDecode: unsupported version"}, | 1152 » {[]byte{0, 2, 3}, "Time.UnmarshalBinary: unsupported version"}, |
1153 » {[]byte{1, 2, 3}, "Time.GobDecode: invalid length"}, | 1153 » {[]byte{1, 2, 3}, "Time.UnmarshalBinary: invalid length"}, |
1154 } | 1154 } |
1155 | 1155 |
1156 func TestInvalidTimeGob(t *testing.T) { | 1156 func TestInvalidTimeGob(t *testing.T) { |
1157 for _, tt := range invalidEncodingTests { | 1157 for _, tt := range invalidEncodingTests { |
1158 var ignored Time | 1158 var ignored Time |
1159 err := ignored.GobDecode(tt.bytes) | 1159 err := ignored.GobDecode(tt.bytes) |
1160 if err == nil || err.Error() != tt.want { | 1160 if err == nil || err.Error() != tt.want { |
1161 t.Errorf("time.GobDecode(%#v) error = %v, want %v", tt.b
ytes, err, tt.want) | 1161 t.Errorf("time.GobDecode(%#v) error = %v, want %v", tt.b
ytes, err, tt.want) |
1162 } | 1162 } |
| 1163 err = ignored.UnmarshalBinary(tt.bytes) |
| 1164 if err == nil || err.Error() != tt.want { |
| 1165 t.Errorf("time.UnmarshalBinary(%#v) error = %v, want %v"
, tt.bytes, err, tt.want) |
| 1166 } |
1163 } | 1167 } |
1164 } | 1168 } |
1165 | 1169 |
1166 var notEncodableTimes = []struct { | 1170 var notEncodableTimes = []struct { |
1167 time Time | 1171 time Time |
1168 want string | 1172 want string |
1169 }{ | 1173 }{ |
1170 » {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", 1)), "Time.GobEncode: zone offs
et has fractional minute"}, | 1174 » {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", 1)), "Time.MarshalBinary: zone
offset has fractional minute"}, |
1171 » {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", -1*60)), "Time.GobEncode: unexp
ected zone offset"}, | 1175 » {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", -1*60)), "Time.MarshalBinary: u
nexpected zone offset"}, |
1172 » {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", -32769*60)), "Time.GobEncode: u
nexpected zone offset"}, | 1176 » {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", -32769*60)), "Time.MarshalBinar
y: unexpected zone offset"}, |
1173 » {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", 32768*60)), "Time.GobEncode: un
expected zone offset"}, | 1177 » {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", 32768*60)), "Time.MarshalBinary
: unexpected zone offset"}, |
1174 } | 1178 } |
1175 | 1179 |
1176 func TestNotGobEncodableTime(t *testing.T) { | 1180 func TestNotGobEncodableTime(t *testing.T) { |
1177 for _, tt := range notEncodableTimes { | 1181 for _, tt := range notEncodableTimes { |
1178 _, err := tt.time.GobEncode() | 1182 _, err := tt.time.GobEncode() |
1179 if err == nil || err.Error() != tt.want { | 1183 if err == nil || err.Error() != tt.want { |
1180 t.Errorf("%v GobEncode error = %v, want %v", tt.time, er
r, tt.want) | 1184 t.Errorf("%v GobEncode error = %v, want %v", tt.time, er
r, tt.want) |
1181 } | 1185 } |
| 1186 _, err = tt.time.MarshalBinary() |
| 1187 if err == nil || err.Error() != tt.want { |
| 1188 t.Errorf("%v MarshalBinary error = %v, want %v", tt.time
, err, tt.want) |
| 1189 } |
1182 } | 1190 } |
1183 } | 1191 } |
1184 | 1192 |
1185 var jsonTests = []struct { | 1193 var jsonTests = []struct { |
1186 time Time | 1194 time Time |
1187 json string | 1195 json string |
1188 }{ | 1196 }{ |
1189 {Date(9999, 4, 12, 23, 20, 50, 520*1e6, UTC), `"9999-04-12T23:20:50.52Z"
`}, | 1197 {Date(9999, 4, 12, 23, 20, 50, 520*1e6, UTC), `"9999-04-12T23:20:50.52Z"
`}, |
1190 {Date(1996, 12, 19, 16, 39, 57, 0, Local), `"1996-12-19T16:39:57-08:00"`
}, | 1198 {Date(1996, 12, 19, 16, 39, 57, 0, Local), `"1996-12-19T16:39:57-08:00"`
}, |
1191 {Date(0, 1, 1, 0, 0, 0, 1, FixedZone("", 1*60)), `"0000-01-01T00:00:00.0
00000001+00:01"`}, | 1199 {Date(0, 1, 1, 0, 0, 0, 1, FixedZone("", 1*60)), `"0000-01-01T00:00:00.0
00000001+00:01"`}, |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1457 _ = t.Year() | 1465 _ = t.Year() |
1458 } | 1466 } |
1459 } | 1467 } |
1460 | 1468 |
1461 func BenchmarkDay(b *testing.B) { | 1469 func BenchmarkDay(b *testing.B) { |
1462 t := Now() | 1470 t := Now() |
1463 for i := 0; i < b.N; i++ { | 1471 for i := 0; i < b.N; i++ { |
1464 _ = t.Day() | 1472 _ = t.Day() |
1465 } | 1473 } |
1466 } | 1474 } |
OLD | NEW |