1 // Copyright 2017 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.
13 func TestHasMonotonicClock(t
*testing
.T
) {
14 yes
:= func(expr
string, tt Time
) {
15 if GetMono(&tt
) == 0 {
16 t
.Errorf("%s: missing monotonic clock reading", expr
)
19 no
:= func(expr
string, tt Time
) {
20 if GetMono(&tt
) != 0 {
21 t
.Errorf("%s: unexpected monotonic clock reading", expr
)
25 yes("<-After(1)", <-After(1))
26 ticker
:= NewTicker(1)
27 yes("<-Tick(1)", <-ticker
.C
)
29 no("Date(2009, 11, 23, 0, 0, 0, 0, UTC)", Date(2009, 11, 23, 0, 0, 0, 0, UTC
))
30 tp
, _
:= Parse(UnixDate
, "Sat Mar 7 11:06:39 PST 2015")
31 no(`Parse(UnixDate, "Sat Mar 7 11:06:39 PST 2015")`, tp
)
32 no("Unix(1486057371, 0)", Unix(1486057371, 0))
36 tu
:= Unix(1486057371, 0)
42 no("tu.Add(1)", tu
.Add(1))
43 no("tu.In(UTC)", tu
.In(UTC
))
44 no("tu.AddDate(1, 1, 1)", tu
.AddDate(1, 1, 1))
45 no("tu.AddDate(0, 0, 0)", tu
.AddDate(0, 0, 0))
46 no("tu.Local()", tu
.Local())
47 no("tu.UTC()", tu
.UTC())
48 no("tu.Round(2)", tu
.Round(2))
49 no("tu.Truncate(2)", tu
.Truncate(2))
51 yes("tm.Add(1)", tm
.Add(1))
52 no("tm.AddDate(1, 1, 1)", tm
.AddDate(1, 1, 1))
53 no("tm.AddDate(0, 0, 0)", tm
.AddDate(0, 0, 0))
54 no("tm.In(UTC)", tm
.In(UTC
))
55 no("tm.Local()", tm
.Local())
56 no("tm.UTC()", tm
.UTC())
57 no("tm.Round(2)", tm
.Round(2))
58 no("tm.Truncate(2)", tm
.Truncate(2))
61 func TestMonotonicAdd(t
*testing
.T
) {
62 tm
:= Unix(1486057371, 123456)
63 SetMono(&tm
, 123456789012345)
66 if t2
.Nanosecond() != 100123456 {
67 t
.Errorf("t2.Nanosecond() = %d, want 100123456", t2
.Nanosecond())
69 if GetMono(&t2
) != 123456889012345 {
70 t
.Errorf("t2.mono = %d, want 123456889012345", GetMono(&t2
))
73 t3
:= tm
.Add(-9e18
) // wall now out of range
74 if t3
.Nanosecond() != 123456 {
75 t
.Errorf("t3.Nanosecond() = %d, want 123456", t3
.Nanosecond())
77 if GetMono(&t3
) != 0 {
78 t
.Errorf("t3.mono = %d, want 0 (wall time out of range for monotonic reading)", GetMono(&t3
))
81 t4
:= tm
.Add(+9e18
) // wall now out of range
82 if t4
.Nanosecond() != 123456 {
83 t
.Errorf("t4.Nanosecond() = %d, want 123456", t4
.Nanosecond())
85 if GetMono(&t4
) != 0 {
86 t
.Errorf("t4.mono = %d, want 0 (wall time out of range for monotonic reading)", GetMono(&t4
))
90 tn1
:= tn
.Add(1 * Hour
)
91 Sleep(100 * Millisecond
)
94 t
.Errorf("Until(Now().Add(1*Hour)) = %v, wanted at least 59m", d
)
98 t
.Errorf("Now().After(Now().Add(1*Hour)) = true, want false")
101 t
.Errorf("Now().Add(1*Hour).After(now) = false, want true")
104 t
.Errorf("Now().Add(1*Hour).Before(Now()) = true, want false")
106 if !now
.Before(tn1
) {
107 t
.Errorf("Now().Before(Now().Add(1*Hour)) = false, want true")
111 func TestMonotonicSub(t
*testing
.T
) {
112 t1
:= Unix(1483228799, 995e6
)
113 SetMono(&t1
, 123456789012345)
115 t2
:= Unix(1483228799, 5e6
)
116 SetMono(&t2
, 123456789012345+10e6
)
118 t3
:= Unix(1483228799, 995e6
)
119 SetMono(&t3
, 123456789012345+1e9
)
121 t1w
:= t1
.AddDate(0, 0, 0)
122 if GetMono(&t1w
) != 0 {
123 t
.Fatalf("AddDate didn't strip monotonic clock reading")
125 t2w
:= t2
.AddDate(0, 0, 0)
126 if GetMono(&t2w
) != 0 {
127 t
.Fatalf("AddDate didn't strip monotonic clock reading")
129 t3w
:= t3
.AddDate(0, 0, 0)
130 if GetMono(&t3w
) != 0 {
131 t
.Fatalf("AddDate didn't strip monotonic clock reading")
134 sub
:= func(txs
, tys
string, tx
, txw
, ty
, tyw Time
, d
, dw Duration
) {
135 check
:= func(expr
string, d
, want Duration
) {
137 t
.Errorf("%s = %v, want %v", expr
, d
, want
)
140 check(txs
+".Sub("+tys
+")", tx
.Sub(ty
), d
)
141 check(txs
+"w.Sub("+tys
+")", txw
.Sub(ty
), dw
)
142 check(txs
+".Sub("+tys
+"w)", tx
.Sub(tyw
), dw
)
143 check(txs
+"w.Sub("+tys
+"w)", txw
.Sub(tyw
), dw
)
145 sub("t1", "t1", t1
, t1w
, t1
, t1w
, 0, 0)
146 sub("t1", "t2", t1
, t1w
, t2
, t2w
, -10*Millisecond
, 990*Millisecond
)
147 sub("t1", "t3", t1
, t1w
, t3
, t3w
, -1000*Millisecond
, 0)
149 sub("t2", "t1", t2
, t2w
, t1
, t1w
, 10*Millisecond
, -990*Millisecond
)
150 sub("t2", "t2", t2
, t2w
, t2
, t2w
, 0, 0)
151 sub("t2", "t3", t2
, t2w
, t3
, t3w
, -990*Millisecond
, -990*Millisecond
)
153 sub("t3", "t1", t3
, t3w
, t1
, t1w
, 1000*Millisecond
, 0)
154 sub("t3", "t2", t3
, t3w
, t2
, t2w
, 990*Millisecond
, 990*Millisecond
)
155 sub("t3", "t3", t3
, t3w
, t3
, t3w
, 0, 0)
157 cmp
:= func(txs
, tys
string, tx
, txw
, ty
, tyw Time
, c
, cw
int) {
158 check
:= func(expr
string, b
, want
bool) {
160 t
.Errorf("%s = %v, want %v", expr
, b
, want
)
163 check(txs
+".After("+tys
+")", tx
.After(ty
), c
> 0)
164 check(txs
+"w.After("+tys
+")", txw
.After(ty
), cw
> 0)
165 check(txs
+".After("+tys
+"w)", tx
.After(tyw
), cw
> 0)
166 check(txs
+"w.After("+tys
+"w)", txw
.After(tyw
), cw
> 0)
168 check(txs
+".Before("+tys
+")", tx
.Before(ty
), c
< 0)
169 check(txs
+"w.Before("+tys
+")", txw
.Before(ty
), cw
< 0)
170 check(txs
+".Before("+tys
+"w)", tx
.Before(tyw
), cw
< 0)
171 check(txs
+"w.Before("+tys
+"w)", txw
.Before(tyw
), cw
< 0)
173 check(txs
+".Equal("+tys
+")", tx
.Equal(ty
), c
== 0)
174 check(txs
+"w.Equal("+tys
+")", txw
.Equal(ty
), cw
== 0)
175 check(txs
+".Equal("+tys
+"w)", tx
.Equal(tyw
), cw
== 0)
176 check(txs
+"w.Equal("+tys
+"w)", txw
.Equal(tyw
), cw
== 0)
179 cmp("t1", "t1", t1
, t1w
, t1
, t1w
, 0, 0)
180 cmp("t1", "t2", t1
, t1w
, t2
, t2w
, -1, +1)
181 cmp("t1", "t3", t1
, t1w
, t3
, t3w
, -1, 0)
183 cmp("t2", "t1", t2
, t2w
, t1
, t1w
, +1, -1)
184 cmp("t2", "t2", t2
, t2w
, t2
, t2w
, 0, 0)
185 cmp("t2", "t3", t2
, t2w
, t3
, t3w
, -1, -1)
187 cmp("t3", "t1", t3
, t3w
, t1
, t1w
, +1, 0)
188 cmp("t3", "t2", t3
, t3w
, t2
, t2w
, +1, +1)
189 cmp("t3", "t3", t3
, t3w
, t3
, t3w
, 0, 0)
192 func TestMonotonicOverflow(t
*testing
.T
) {
193 t1
:= Now().Add(-30 * Second
)
195 if d
< -35*Second ||
-30*Second
< d
{
196 t
.Errorf("Until(Now().Add(-30s)) = %v, want roughly -30s (-35s to -30s)", d
)
199 t1
= Now().Add(30 * Second
)
201 if d
< 25*Second ||
30*Second
< d
{
202 t
.Errorf("Until(Now().Add(-30s)) = %v, want roughly 30s (25s to 30s)", d
)
206 t1
= t0
.Add(Duration(1<<63 - 1))
207 if GetMono(&t1
) != 0 {
208 t
.Errorf("Now().Add(maxDuration) has monotonic clock reading (%v => %v %d %d)", t0
.String(), t1
.String(), t0
.Unix(), t1
.Unix())
210 t2
:= t1
.Add(-Duration(1<<63 - 1))
212 if d
< -10*Second ||
10*Second
< d
{
213 t
.Errorf("Since(Now().Add(max).Add(-max)) = %v, want [-10s, 10s]", d
)
217 t1
= t0
.Add(1 * Hour
)
218 Sleep(100 * Millisecond
)
219 t2
= Now().Add(-5 * Second
)
221 t
.Errorf("Now().Add(1*Hour).After(Now().Add(-5*Second)) = false, want true\nt1=%v\nt2=%v", t1
, t2
)
224 t
.Errorf("Now().Add(-5*Second).After(Now().Add(1*Hour)) = true, want false\nt1=%v\nt2=%v", t1
, t2
)
227 t
.Errorf("Now().Add(1*Hour).Before(Now().Add(-5*Second)) = true, want false\nt1=%v\nt2=%v", t1
, t2
)
230 t
.Errorf("Now().Add(-5*Second).Before(Now().Add(1*Hour)) = false, want true\nt1=%v\nt2=%v", t1
, t2
)
234 var monotonicStringTests
= []struct {
238 {0, "m=+0.000000000"},
239 {123456789, "m=+0.123456789"},
240 {-123456789, "m=-0.123456789"},
241 {123456789000, "m=+123.456789000"},
242 {-123456789000, "m=-123.456789000"},
243 {9e18
, "m=+9000000000.000000000"},
244 {-9e18
, "m=-9000000000.000000000"},
245 {-1 << 63, "m=-9223372036.854775808"},
248 func TestMonotonicString(t
*testing
.T
) {
250 t
.Logf("Now() = %v", t1
)
252 for _
, tt
:= range monotonicStringTests
{
254 SetMono(&t1
, tt
.mono
)
256 got
:= s
[strings
.LastIndex(s
, " ")+1:]
258 t
.Errorf("with mono=%d: got %q; want %q", tt
.mono
, got
, tt
.want
)