-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtime_test.go
203 lines (188 loc) · 5.41 KB
/
time_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package emacs
import (
"fmt"
"log"
"math/rand"
"reflect"
"testing/quick"
"time"
)
func init() {
ERTTest(goTimeRoundtrip)
ERTTest(goDurationRoundtrip)
ERTTest(emacsTimeRoundtrip)
ERTTest(emacsDurationRoundtrip)
}
func goTimeRoundtrip(e Env) error {
f := func(a Time) bool {
v, err := a.Emacs(e)
if err != nil {
log.Printf("couldn’t convert time %s to Emacs: %v", a, e.Message(err))
return false
}
b, err := e.Time(v)
if err != nil {
log.Printf("couldn’t convert time from Emacs: %v", e.Message(err))
return false
}
return time.Time(a).Equal(b)
}
return quick.Check(f, nil)
}
func (Time) Generate(rand *rand.Rand, size int) reflect.Value {
return reflect.ValueOf(Time(time.Unix(rand.Int63(), rand.Int63())))
}
func goDurationRoundtrip(e Env) error {
f := func(a Duration) bool {
v, err := a.Emacs(e)
if err != nil {
log.Printf("couldn’t convert duration %s to Emacs: %v", a, e.Message(err))
return false
}
b, err := e.Duration(v)
if err != nil {
log.Printf("couldn’t convert duration from Emacs: %v", e.Message(err))
return false
}
return time.Duration(a) == b
}
return quick.Check(f, nil)
}
func emacsTimeRoundtrip(e Env) error {
f := func(q quad) bool {
a, trunc, ok := q.lists(e)
if !ok {
return false
}
t, err := e.Time(a)
if e.IsOverflowError(err) {
// Treat overflows as success.
return true
}
if err != nil {
log.Printf("couldn’t convert time from Emacs: %v", e.Message(err))
return false
}
b, err := Time(t).Emacs(e)
if err != nil {
log.Printf("couldn’t convert time %s to Emacs: %v", t, e.Message(err))
return false
}
// We need to compare the truncated value.
return equalTime(e, trunc, b)
}
return quick.Check(f, &quick.Config{MaxCountScale: 10})
}
func emacsDurationRoundtrip(e Env) error {
f := func(q quad) bool {
a, trunc, ok := q.lists(e)
if !ok {
return false
}
d, err := e.Duration(a)
if e.IsOverflowError(err) {
// Treat overflows as success.
return true
}
if err != nil {
log.Printf("couldn’t convert duration from Emacs: %v", e.Message(err))
return false
}
b, err := Duration(d).Emacs(e)
if err != nil {
log.Printf("couldn’t convert duration %s to Emacs: %v", d, e.Message(err))
return false
}
// We need to compare the truncated value.
return equalTime(e, trunc, b)
}
return quick.Check(f, &quick.Config{MaxCountScale: 10})
}
type quad struct {
hi int64
lo uint16
μs, ps uint32
}
func (quad) Generate(rand *rand.Rand, size int) reflect.Value {
return reflect.ValueOf(quad{
// We intentionally leave the upper 16 bit out. On typical
// systems, time_t is a 64-bit signed integer, so filling all
// the bits would cause almost 100% overflow errors and no real
// tests. We cast to signed first to have some negative
// numbers.
int64(rand.Uint64()) >> 16,
// The other parts have technically a smaller domain than
// uint16/uint32, but the functions should accept denormalized
// values as well.
uint16(rand.Uint32()), rand.Uint32(), rand.Uint32(),
})
}
func (q quad) String() string {
return fmt.Sprintf("(%d %d %d %d)", q.hi, q.lo, q.μs, q.ps)
}
func (q quad) lists(e Env) (precise, truncated Value, ok bool) {
precise, ok = q.list(e)
if !ok {
return
}
q.ps -= q.ps % 1000
truncated, ok = q.list(e)
return
}
func (q quad) list(e Env) (v Value, ok bool) {
v, err := e.List(Int(q.hi), Int(q.lo), Int(q.μs), Int(q.ps))
if err != nil {
log.Printf("couldn’t convert time quadruple %s to list: %v", q, e.Message(err))
}
return v, err == nil
}
// equalTime returns whether the two Emacs time values in a and b are equal.
func equalTime(e Env, a, b Value) bool {
// Rewrite: a = b ⟺ a − b = 0 ⟺ ¬(a − b < 0 ∨ 0 < a − b)
d, err := e.Call("time-subtract", a, b)
if err != nil {
log.Printf("couldn’t subtract time values: %s", e.Message(err))
return false
}
// Emacs doesn’t have a time-equal-p function or similar.
pos, err := timeLess(e, Int(0), d)
if err != nil {
// Don’t bother returning the error since the test functions
// above treat different times as an error.
log.Printf("couldn’t compare times: %s", e.Message(err))
return false
}
neg, err := timeLess(e, d, Int(0))
if err != nil {
// Don’t bother returning the error since the test functions
// above treat different times as an error.
log.Printf("couldn’t compare times: %s", e.Message(err))
return false
}
equal := !(pos || neg)
if !equal {
log.Print(e.FormatMessage("time values %s and %s aren’t equal, difference is %s", a, b, d))
// Don’t bother with returning the error since the test
// functions above treat different times as an error.
}
return equal
}
// timeLess returns whether a < b for two Emacs time values.
func timeLess(e Env, a, b In) (bool, error) {
var less Bool
err := e.CallOut("time-less-p", &less, a, b)
return bool(less), err
}