-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_test.go
More file actions
474 lines (453 loc) · 17.5 KB
/
Copy pathfunction_test.go
File metadata and controls
474 lines (453 loc) · 17.5 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
package render
import (
"math"
"testing"
"github.com/go-pdfkit/reader"
)
// fnDoc builds a document holding one function so that it can be read the way
// a page would read it, and gives back the function.
func fnDoc(t *testing.T, build func(w *reader.Writer) reader.Object) function {
t.Helper()
r, obj := fnRenderer(t, build)
return r.readFunction(obj, 0)
}
// fnRenderer builds the document and a renderer over it, so that a test can
// also read something that is not a function.
func fnRenderer(t *testing.T, build func(w *reader.Writer) reader.Object) (*renderer, reader.Object) {
t.Helper()
w := reader.NewWriter("1.7")
pagesRef := w.Reserve()
obj := build(w)
page := w.Add(reader.Dict{"Type": reader.Name("Page"), "Parent": pagesRef,
"MediaBox": reader.Array{reader.Integer(0), reader.Integer(0), reader.Integer(10), reader.Integer(10)},
"Held": obj})
w.Put(pagesRef, reader.Dict{"Type": reader.Name("Pages"),
"Kids": reader.Array{page}, "Count": reader.Integer(1)})
root := w.Add(reader.Dict{"Type": reader.Name("Catalog"), "Pages": pagesRef})
out, err := w.Finish(reader.Dict{"Root": root})
if err != nil {
t.Fatal(err)
}
d, err := reader.Open(out)
if err != nil {
t.Fatal(err)
}
p, err := d.Page(1)
if err != nil {
t.Fatal(err)
}
return &renderer{doc: d, fonts: map[int]*pdfFont{}}, p.Get("Held")
}
// nums is an array of numbers, which is what most of a function dictionary is.
func nums(vs ...float64) reader.Array {
out := make(reader.Array, 0, len(vs))
for _, v := range vs {
out = append(out, reader.Real(v))
}
return out
}
// close reports whether two lists of numbers agree to within a whisker.
func closeEnough(got, want []float64) bool {
if len(got) != len(want) {
return false
}
for i := range got {
if math.Abs(got[i]-want[i]) > 1e-6 {
return false
}
}
return true
}
func TestAnExponentialFunction(t *testing.T) {
// The second kind: a curve from one colour to another.
f := fnDoc(t, func(w *reader.Writer) reader.Object {
return w.Add(reader.Dict{
"FunctionType": reader.Integer(2), "Domain": nums(0, 1),
"C0": nums(1, 0, 0), "C1": nums(0, 0, 1), "N": reader.Integer(1),
})
})
if f == nil {
t.Fatal("an exponential function was not read")
}
if f.outputs() != 3 {
t.Errorf("outputs() = %d", f.outputs())
}
for _, c := range []struct {
in float64
want []float64
}{
{0, []float64{1, 0, 0}},
{1, []float64{0, 0, 1}},
{0.5, []float64{0.5, 0, 0.5}},
{-3, []float64{1, 0, 0}}, // clipped to the domain
{9, []float64{0, 0, 1}},
} {
if got := f.eval([]float64{c.in}); !closeEnough(got, c.want) {
t.Errorf("at %v: %v, want %v", c.in, got, c.want)
}
}
// An exponent other than one bends the curve, and a negative input to a
// fractional power is taken as nothing rather than as no number at all.
sq := fnDoc(t, func(w *reader.Writer) reader.Object {
return w.Add(reader.Dict{
"FunctionType": reader.Integer(2), "Domain": nums(-1, 1),
"N": reader.Real(0.5),
})
})
if got := sq.eval([]float64{0.25}); !closeEnough(got, []float64{0.5}) {
t.Errorf("the square root of a quarter came out as %v", got)
}
if got := sq.eval([]float64{-1}); !closeEnough(got, []float64{0}) {
t.Errorf("a negative input gave %v", got)
}
// A function with no operands at all is evaluated at nothing.
if got := sq.eval(nil); !closeEnough(got, []float64{0}) {
t.Errorf("with no input it gave %v", got)
}
// The range clips the result.
clipped := fnDoc(t, func(w *reader.Writer) reader.Object {
return w.Add(reader.Dict{
"FunctionType": reader.Integer(2), "Domain": nums(0, 1),
"Range": nums(0, 0.25), "C0": nums(0), "C1": nums(1),
})
})
if got := clipped.eval([]float64{1}); !closeEnough(got, []float64{0.25}) {
t.Errorf("the range did not clip: %v", got)
}
}
func TestAStitchingFunction(t *testing.T) {
// The third kind: several functions laid end to end.
f := fnDoc(t, func(w *reader.Writer) reader.Object {
first := w.Add(reader.Dict{"FunctionType": reader.Integer(2),
"Domain": nums(0, 1), "C0": nums(0), "C1": nums(1)})
second := w.Add(reader.Dict{"FunctionType": reader.Integer(2),
"Domain": nums(0, 1), "C0": nums(1), "C1": nums(0)})
return w.Add(reader.Dict{
"FunctionType": reader.Integer(3), "Domain": nums(0, 1),
"Functions": reader.Array{first, second},
"Bounds": nums(0.5), "Encode": nums(0, 1, 0, 1),
})
})
if f == nil {
t.Fatal("a stitching function was not read")
}
for _, c := range []struct{ in, want float64 }{
{0, 0}, {0.25, 0.5}, {0.5, 1}, {0.75, 0.5}, {1, 0},
} {
if got := f.eval([]float64{c.in}); !closeEnough(got, []float64{c.want}) {
t.Errorf("at %v: %v, want %v", c.in, got, c.want)
}
}
if f.outputs() != 1 {
t.Errorf("outputs() = %d", f.outputs())
}
}
func TestASampledFunction(t *testing.T) {
// The first kind: a grid of numbers read off with straight lines between.
// Four samples of one byte each, from black to white and back.
f := fnDoc(t, func(w *reader.Writer) reader.Object {
return w.Add(&reader.Stream{
Dict: reader.Dict{
"FunctionType": reader.Integer(0), "Domain": nums(0, 1),
"Range": nums(0, 1), "Size": reader.Array{reader.Integer(3)},
"BitsPerSample": reader.Integer(8),
},
Raw: []byte{0, 255, 0},
})
})
if f == nil {
t.Fatal("a sampled function was not read")
}
for _, c := range []struct{ in, want float64 }{
{0, 0}, {0.5, 1}, {1, 0}, {0.25, 0.5}, {0.75, 0.5},
} {
if got := f.eval([]float64{c.in}); !closeEnough(got, []float64{c.want}) {
t.Errorf("at %v: %v, want %v", c.in, got, c.want)
}
}
// Two inputs read a grid rather than a row, and every sample width the
// format allows is read the same way.
for _, bps := range []int{1, 2, 4, 8, 16} {
max := float64(uint64(1)<<uint(bps) - 1)
grid := fnDoc(t, func(w *reader.Writer) reader.Object {
var raw []byte
switch bps {
case 16:
raw = []byte{0, 0, 0xff, 0xff}
case 8:
raw = []byte{0, 255}
default:
// Two samples packed into the top of one byte.
raw = []byte{byte(uint64(max) << uint(8-bps))}
}
return w.Add(&reader.Stream{
Dict: reader.Dict{
"FunctionType": reader.Integer(0), "Domain": nums(0, 1),
"Range": nums(0, 1), "Size": reader.Array{reader.Integer(2)},
"BitsPerSample": reader.Integer(int64(bps)),
},
Raw: raw,
})
})
if grid == nil {
t.Fatalf("%d bits a sample: not read", bps)
}
want := []float64{1}
if bps < 8 {
want = []float64{0} // the packed pair is high then low
}
got := grid.eval([]float64{1})
if bps >= 8 && !closeEnough(got, want) {
t.Errorf("%d bits a sample: at one it gave %v", bps, got)
}
}
}
func TestFunctionsThatAreNotOnes(t *testing.T) {
cases := []struct {
name string
build func(w *reader.Writer) reader.Object
}{
{"not a dictionary at all", func(w *reader.Writer) reader.Object {
return reader.Integer(7)
}},
{"no function type", func(w *reader.Writer) reader.Object {
return w.Add(reader.Dict{"Domain": nums(0, 1)})
}},
{"a type nobody has defined", func(w *reader.Writer) reader.Object {
return w.Add(reader.Dict{"FunctionType": reader.Integer(9), "Domain": nums(0, 1)})
}},
{"no domain", func(w *reader.Writer) reader.Object {
return w.Add(reader.Dict{"FunctionType": reader.Integer(2)})
}},
{"a domain that is not numbers", func(w *reader.Writer) reader.Object {
return w.Add(reader.Dict{"FunctionType": reader.Integer(2),
"Domain": reader.Array{reader.Name("x"), reader.Integer(1)}})
}},
{"an exponential whose two colours differ in length", func(w *reader.Writer) reader.Object {
return w.Add(reader.Dict{"FunctionType": reader.Integer(2), "Domain": nums(0, 1),
"C0": nums(0, 0), "C1": nums(1)})
}},
{"a sampled function that is not a stream", func(w *reader.Writer) reader.Object {
return w.Add(reader.Dict{"FunctionType": reader.Integer(0), "Domain": nums(0, 1),
"Range": nums(0, 1), "Size": reader.Array{reader.Integer(2)},
"BitsPerSample": reader.Integer(8)})
}},
{"a sampled function with no range", func(w *reader.Writer) reader.Object {
return w.Add(&reader.Stream{Dict: reader.Dict{
"FunctionType": reader.Integer(0), "Domain": nums(0, 1),
"Size": reader.Array{reader.Integer(2)}, "BitsPerSample": reader.Integer(8)},
Raw: []byte{0, 255}})
}},
{"a sampled function with no size", func(w *reader.Writer) reader.Object {
return w.Add(&reader.Stream{Dict: reader.Dict{
"FunctionType": reader.Integer(0), "Domain": nums(0, 1),
"Range": nums(0, 1), "BitsPerSample": reader.Integer(8)},
Raw: []byte{0, 255}})
}},
{"a sampled function with a size of nothing", func(w *reader.Writer) reader.Object {
return w.Add(&reader.Stream{Dict: reader.Dict{
"FunctionType": reader.Integer(0), "Domain": nums(0, 1),
"Range": nums(0, 1), "Size": reader.Array{reader.Integer(0)},
"BitsPerSample": reader.Integer(8)}, Raw: []byte{0}})
}},
{"a sampled function bigger than any memory", func(w *reader.Writer) reader.Object {
return w.Add(&reader.Stream{Dict: reader.Dict{
"FunctionType": reader.Integer(0), "Domain": nums(0, 1, 0, 1, 0, 1),
"Range": nums(0, 1),
"Size": reader.Array{reader.Integer(1000), reader.Integer(1000), reader.Integer(1000)},
"BitsPerSample": reader.Integer(8)}, Raw: []byte{0}})
}},
{"a sample width nothing uses", func(w *reader.Writer) reader.Object {
return w.Add(&reader.Stream{Dict: reader.Dict{
"FunctionType": reader.Integer(0), "Domain": nums(0, 1),
"Range": nums(0, 1), "Size": reader.Array{reader.Integer(2)},
"BitsPerSample": reader.Integer(7)}, Raw: []byte{0, 255}})
}},
{"a sampled function with no sample width at all", func(w *reader.Writer) reader.Object {
return w.Add(&reader.Stream{Dict: reader.Dict{
"FunctionType": reader.Integer(0), "Domain": nums(0, 1),
"Range": nums(0, 1), "Size": reader.Array{reader.Integer(2)}},
Raw: []byte{0, 255}})
}},
{"a stitching function with no parts", func(w *reader.Writer) reader.Object {
return w.Add(reader.Dict{"FunctionType": reader.Integer(3), "Domain": nums(0, 1),
"Functions": reader.Array{}, "Bounds": nums(), "Encode": nums()})
}},
{"a stitching function whose parts are not functions", func(w *reader.Writer) reader.Object {
return w.Add(reader.Dict{"FunctionType": reader.Integer(3), "Domain": nums(0, 1),
"Functions": reader.Array{reader.Integer(3)}, "Bounds": nums(), "Encode": nums(0, 1)})
}},
{"a stitching function with the wrong number of bounds", func(w *reader.Writer) reader.Object {
part := w.Add(reader.Dict{"FunctionType": reader.Integer(2), "Domain": nums(0, 1)})
return w.Add(reader.Dict{"FunctionType": reader.Integer(3), "Domain": nums(0, 1),
"Functions": reader.Array{part}, "Bounds": nums(0.5), "Encode": nums(0, 1)})
}},
{"a stitching function with the wrong encoding", func(w *reader.Writer) reader.Object {
part := w.Add(reader.Dict{"FunctionType": reader.Integer(2), "Domain": nums(0, 1)})
return w.Add(reader.Dict{"FunctionType": reader.Integer(3), "Domain": nums(0, 1),
"Functions": reader.Array{part}, "Bounds": nums(), "Encode": nums(0)})
}},
{"a stitching function with no domain to stitch over", func(w *reader.Writer) reader.Object {
part := w.Add(reader.Dict{"FunctionType": reader.Integer(2), "Domain": nums(0, 1)})
return w.Add(reader.Dict{"FunctionType": reader.Integer(3), "Domain": nums(0),
"Functions": reader.Array{part}, "Bounds": nums(), "Encode": nums(0, 1)})
}},
{"a calculator that is not a stream", func(w *reader.Writer) reader.Object {
return w.Add(reader.Dict{"FunctionType": reader.Integer(4), "Domain": nums(0, 1),
"Range": nums(0, 1)})
}},
{"a calculator with no range", func(w *reader.Writer) reader.Object {
return w.Add(&reader.Stream{Dict: reader.Dict{"FunctionType": reader.Integer(4),
"Domain": nums(0, 1)}, Raw: []byte("{}")})
}},
{"a calculator that is not a program", func(w *reader.Writer) reader.Object {
return w.Add(&reader.Stream{Dict: reader.Dict{"FunctionType": reader.Integer(4),
"Domain": nums(0, 1), "Range": nums(0, 1)}, Raw: []byte("2 3 add")})
}},
{"a calculator whose braces do not match", func(w *reader.Writer) reader.Object {
return w.Add(&reader.Stream{Dict: reader.Dict{"FunctionType": reader.Integer(4),
"Domain": nums(0, 1), "Range": nums(0, 1)}, Raw: []byte("{ 2 3 add")})
}},
{"a calculator with something after its program", func(w *reader.Writer) reader.Object {
return w.Add(&reader.Stream{Dict: reader.Dict{"FunctionType": reader.Integer(4),
"Domain": nums(0, 1), "Range": nums(0, 1)}, Raw: []byte("{ 1 } 2")})
}},
{"a calculator with nothing in it at all", func(w *reader.Writer) reader.Object {
return w.Add(&reader.Stream{Dict: reader.Dict{"FunctionType": reader.Integer(4),
"Domain": nums(0, 1), "Range": nums(0, 1)}, Raw: []byte("")})
}},
{"an array holding something that is not a function", func(w *reader.Writer) reader.Object {
return reader.Array{reader.Integer(1)}
}},
{"an empty array", func(w *reader.Writer) reader.Object {
return reader.Array{}
}},
}
for _, c := range cases {
if f := fnDoc(t, c.build); f != nil {
t.Errorf("%s was read as a function", c.name)
}
}
}
func TestFunctionsSideBySide(t *testing.T) {
// A shading may name one function per colour component rather than one
// giving them all.
f := fnDoc(t, func(w *reader.Writer) reader.Object {
one := w.Add(reader.Dict{"FunctionType": reader.Integer(2),
"Domain": nums(0, 1), "C0": nums(0), "C1": nums(1)})
two := w.Add(reader.Dict{"FunctionType": reader.Integer(2),
"Domain": nums(0, 1), "C0": nums(1), "C1": nums(0)})
return reader.Array{one, two}
})
if f == nil {
t.Fatal("an array of functions was not read")
}
if f.outputs() != 2 {
t.Errorf("outputs() = %d", f.outputs())
}
if got := f.eval([]float64{0.25}); !closeEnough(got, []float64{0.25, 0.75}) {
t.Errorf("at a quarter: %v", got)
}
}
func TestAFunctionThatNamesItselfForEver(t *testing.T) {
// A stitching function whose part is itself would go round for ever;
// the depth it is read at is what stops it.
r, _ := fnRenderer(t, func(w *reader.Writer) reader.Object { return reader.Null{} })
if f := r.readOneFunction(reader.Null{}, maxFunctionDepth+1); f != nil {
t.Error("a function nested past the limit was read")
}
}
func TestFunctionsWhoseStreamCannotBeRead(t *testing.T) {
// A function whose data is filtered as an image is not one, however well
// formed the rest of its dictionary is.
for _, kind := range []int64{0, 4} {
f := fnDoc(t, func(w *reader.Writer) reader.Object {
return w.Add(&reader.Stream{Dict: reader.Dict{
"FunctionType": reader.Integer(kind),
"Domain": nums(0, 1),
"Range": nums(0, 1),
"Size": reader.Array{reader.Integer(2)},
"BitsPerSample": reader.Integer(8),
"Filter": reader.Name("DCTDecode"),
}, Raw: []byte("not a jpeg")})
})
if f != nil {
t.Errorf("a type %d function filtered as an image was read", kind)
}
}
}
func TestASampledFunctionShorterThanItSaysItIs(t *testing.T) {
// A grid the stream does not hold all of: the samples that are there are
// read and the rest come out as nothing, rather than reaching past the
// end of the data.
f := fnDoc(t, func(w *reader.Writer) reader.Object {
return w.Add(&reader.Stream{Dict: reader.Dict{
"FunctionType": reader.Integer(0), "Domain": nums(0, 1),
"Range": nums(0, 1), "Size": reader.Array{reader.Integer(4)},
"BitsPerSample": reader.Integer(12),
}, Raw: []byte{0xff, 0xf0}})
})
if f == nil {
t.Fatal("the function was not read")
}
if f.outputs() != 1 {
t.Errorf("outputs() = %d", f.outputs())
}
if got := f.eval([]float64{1}); len(got) != 1 {
t.Errorf("gave %v", got)
}
}
func TestAStitchingFunctionThatSaysWhatItsRangeIs(t *testing.T) {
f := fnDoc(t, func(w *reader.Writer) reader.Object {
part := w.Add(reader.Dict{"FunctionType": reader.Integer(2), "Domain": nums(0, 1),
"C0": nums(0, 0), "C1": nums(1, 1)})
return w.Add(reader.Dict{"FunctionType": reader.Integer(3), "Domain": nums(0, 1),
"Range": nums(0, 1, 0, 1), "Functions": reader.Array{part},
"Bounds": nums(), "Encode": nums(0, 1)})
})
if f == nil {
t.Fatal("the function was not read")
}
if f.outputs() != 2 {
t.Errorf("outputs() = %d, want 2", f.outputs())
}
}
func TestADomainWrittenBackToFront(t *testing.T) {
// A domain whose ends are the wrong way round still holds the input
// between them, and one of no width at all gives the same answer
// everywhere.
f := fnDoc(t, func(w *reader.Writer) reader.Object {
return w.Add(reader.Dict{"FunctionType": reader.Integer(2), "Domain": nums(1, 0),
"C0": nums(0), "C1": nums(1)})
})
if got := f.eval([]float64{9}); !closeEnough(got, []float64{1}) {
t.Errorf("past the end of a backwards domain: %v", got)
}
flat := fnDoc(t, func(w *reader.Writer) reader.Object {
return w.Add(&reader.Stream{Dict: reader.Dict{
"FunctionType": reader.Integer(0), "Domain": nums(0.5, 0.5),
"Range": nums(0, 1), "Size": reader.Array{reader.Integer(2)},
"BitsPerSample": reader.Integer(8)}, Raw: []byte{0, 255}})
})
if flat == nil {
t.Fatal("a function over a domain of no width was not read")
}
if got := flat.eval([]float64{0.5}); len(got) != 1 {
t.Errorf("gave %v", got)
}
}
func TestACalculatorWhoseBracesNestWrong(t *testing.T) {
if f := calc(t, "{ {", 1); f != nil {
t.Error("a program whose inner block never closes was read")
}
if f := calc(t, "{ pop dup }", 2); f != nil {
// two outputs from one value is fine; this is only here to run the
// dup with nothing under it, which is the next case.
_ = f
}
if f := calc(t, "{ pop dup }", 1); f != nil {
if got := f.eval([]float64{3}); len(got) != 1 {
t.Errorf("gave %v", got)
}
}
}