-
Notifications
You must be signed in to change notification settings - Fork 63
/
pytables_test.go
346 lines (315 loc) · 6.76 KB
/
pytables_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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package decimal
import (
"bufio"
"compress/gzip"
"fmt"
"math/big"
"os"
"path/filepath"
"reflect"
"strconv"
"testing"
"github.com/ericlagergren/decimal/suite"
)
func TestPytables(t *testing.T) {
for _, s := range []string{
testAbs,
testAdd,
testCTR,
testCTS,
testCTS,
testClass,
testCmp,
testExp,
testFMA,
testLog,
testLog10,
testMul,
testNeg,
testNextMinus,
testNextPlus,
testPow,
testQuant,
testQuo,
testQuoInt,
testReduce,
testRem,
testRoundToInt,
testSign,
testSignbit,
testSqrt,
testSub,
} {
t.Run(s, func(t *testing.T) {
pytables(t, s)
})
}
}
const (
testAbs = "absolute-value"
testAdd = "addition"
testClass = "class"
testCmp = "comparison"
testCTR = "convert-to-rat"
testCFS = "convert-from-string"
testCTS = "convert-to-string"
testExp = "exponential-function"
testFMA = "fused-multiply-add"
testLog10 = "common-logarithm"
testLogb = "base-b-logarithm"
testLog = "natural-logarithm"
testMul = "multiplication"
testNeg = "negation"
testNextMinus = "next-minus"
testNextPlus = "next-plus"
testPow = "power"
testQuant = "quantization"
testQuo = "division"
testQuoInt = "integer-division"
testReduce = "reduction"
testRem = "remainder"
testRoundToInt = "round-to-integral-exact"
testShift = "shift"
testSign = "sign"
testSignbit = "signbit"
testSub = "subtraction"
testSqrt = "square-root"
)
func pytables(t *testing.T, name string) {
s := open(t, name)
for s.Next() {
c := s.Case(t)
c.execute(name)
}
}
var nilaryTests = map[string]func(z *Big) *Big{
testReduce: (*Big).Reduce,
testRoundToInt: (*Big).RoundToInt,
}
var unaryTests = map[string]func(ctx Context, z, x *Big) *Big{
testAbs: Context.Abs,
testNeg: Context.Neg,
testExp: Context.Exp,
testLog: Context.Log,
testLog10: Context.Log10,
testNextMinus: Context.NextMinus,
testNextPlus: Context.NextPlus,
testSqrt: Context.Sqrt,
}
var binaryTests = map[string]func(ctx Context, z, x, y *Big) *Big{
testAdd: Context.Add,
testMul: Context.Mul,
testQuo: Context.Quo,
testQuoInt: Context.QuoInt,
testRem: Context.Rem,
testSub: Context.Sub,
// The Python version we test against has rounding errors of
// 1 ULP. So test to see if we're within 1 ULP.
// Pow: math.Pow,
}
var ternaryTests = map[string]func(ctx Context, z, x, y, u *Big) *Big{
testFMA: Context.FMA,
}
func (c *scase) execute(name string) {
ctx := c.ctx
if nfn, ok := nilaryTests[name]; ok {
c.Check(nfn(c.x))
} else if ufn, ok := unaryTests[name]; ok {
c.Check(ufn(ctx, c.z, c.x))
} else if bfn, ok := binaryTests[name]; ok {
c.Check(bfn(ctx, c.z, c.x, c.y))
} else if tfn, ok := ternaryTests[name]; ok {
c.Check(tfn(ctx, c.z, c.x, c.y, c.u))
} else {
switch name {
case testClass:
c.Assert(c.x.Class(), c.r)
case testCmp:
rv := c.x.Cmp(c.y)
r, _, snan := c.Cmp()
c.Assert(rv, r)
c.Assert(snan, c.x.Context.Conditions&InvalidOperation != 0)
case testShift:
c.t.Skip("TODO")
case testQuant:
v, _ := c.y.Int64()
c.Check(c.x.Quantize(int(v)))
case testCTR:
r := new(big.Rat).SetFrac(c.x.Int(nil), c.y.Int(nil))
c.Check(ctx.SetRat(c.z, r))
case testSign:
c.Assert(c.x.Sign(), c.Sign())
case testCTS, testCFS:
xs := c.x.String()
if !Regexp.MatchString(xs) {
c.t.Fatalf("should match regexp: %q", xs)
}
c.Assert(xs, c.r)
case testPow:
ctx.Pow(c.z, c.x, c.y)
r := c.R()
if !pytablesEqual(c.z, r) {
diff := new(Big)
eps := New(1, c.c.Prec)
ctx := Context{Precision: -c.c.Prec}
if ctx.Sub(diff, r, c.z).CmpAbs(eps) > 0 {
c.t.Logf(`#%d: %s
wanted: %q (%s:%d)
got : %q (%s:%d)
`,
c.i, c.c.ShortString(22),
r, c.flags, -r.Scale(),
c.z, c.z.Context.Conditions, -c.z.Scale(),
)
}
}
case testSignbit:
c.Assert(c.x.Signbit(), c.Signbit())
default:
panic("unknown test: " + name)
}
}
}
func open(t *testing.T, name string) (c *scanner) {
fpath := filepath.Join("testdata", "pytables",
fmt.Sprintf("%s-tables.gz", name))
f, err := os.Open(fpath)
if err != nil {
t.Fatal(err)
}
gzr, err := gzip.NewReader(f)
if err != nil {
panic(err)
}
return &scanner{
s: bufio.NewScanner(gzr),
close: func() {
gzr.Close()
f.Close()
},
}
}
type scanner struct {
i int
s *bufio.Scanner
close func()
}
func (c *scanner) Next() bool {
if !c.s.Scan() {
c.close()
return false
}
c.i++
return true
}
func (c *scanner) Case(t *testing.T) *scase {
cs, err := suite.ParseCase(c.s.Bytes())
if err != nil {
panic(err)
}
return parse(t, cs, c.i)
}
func ctx(c suite.Case) Context {
return Context{
Precision: c.Prec,
OperatingMode: GDA,
RoundingMode: RoundingMode(c.Mode),
Traps: Condition(c.Trap),
}
}
func parse(t *testing.T, c suite.Case, i int) *scase {
ctx := ctx(c)
s := scase{
t: t,
ctx: ctx,
i: i,
c: c,
z: new(Big),
r: string(c.Output),
flags: Condition(c.Excep),
}
switch len(c.Inputs) {
case 3:
s.u, _ = WithContext(ctx).SetString(string(c.Inputs[2]))
fallthrough
case 2:
s.y, _ = WithContext(ctx).SetString(string(c.Inputs[1]))
fallthrough
case 1:
s.x, _ = WithContext(ctx).SetString(string(c.Inputs[0]))
default:
panic(fmt.Sprintf("%s\n%d inputs", s.c, len(c.Inputs)))
}
return &s
}
func (c *scase) Assert(got, want interface{}) {
c.t.Helper()
if !reflect.DeepEqual(got, want) {
c.t.Errorf(`#%d: %s
wanted: %v
got : %v
`, c.i, c.c.ShortString(22), want, got)
}
}
func (c *scase) Check(z *Big) {
c.t.Helper()
r := c.R()
if !pytablesEqual(z, r) {
c.t.Errorf(`#%d: %s
wanted: %q (%s:%d)
got : %q (%s:%d)
`,
c.i, c.c.ShortString(10000),
r, c.flags, -r.Scale(),
z, z.Context.Conditions, -z.Scale(),
)
}
}
type scase struct {
z, x, y, u *Big
c suite.Case
i int
r string
t *testing.T
flags Condition
ctx Context
}
func (s *scase) R() *Big {
r, _ := WithContext(s.ctx).SetString(s.r)
r.Context.Conditions = s.flags
return r
}
func (s *scase) Str() string { return s.r }
func (s *scase) Sign() int {
s.t.Helper()
r, err := strconv.Atoi(s.r)
if err != nil {
s.t.Fatal(err)
}
return r
}
func (s *scase) Cmp() (int, bool, bool) {
s.t.Helper()
qnan, snan := suite.Data(s.r).IsNaN()
if qnan || snan {
return 0, qnan, snan
}
r, err := strconv.Atoi(s.r)
if err != nil {
s.t.Fatal(err)
}
return r, false, false
}
func (s *scase) Signbit() bool {
s.t.Helper()
r, err := strconv.ParseBool(s.r)
if err != nil {
s.t.Fatal(err)
}
return r
}
func pytablesEqual(x, y *Big) bool {
// Python doesn't have DivisionUndefined.
x.Context.Conditions &^= DivisionUndefined
return equal(x, y)
}