/
plot_test.go
130 lines (111 loc) · 2.79 KB
/
plot_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
// Copyright ©2015 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package plot_test
import (
"bytes"
"fmt"
"image/color"
"io/ioutil"
"math"
"testing"
"time"
"gonum.org/v1/plot"
"gonum.org/v1/plot/cmpimg"
"gonum.org/v1/plot/font"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/text"
"gonum.org/v1/plot/vg"
"gonum.org/v1/plot/vg/draw"
"gonum.org/v1/plot/vg/recorder"
"gonum.org/v1/plot/vg/vgimg"
)
func TestLegendAlignment(t *testing.T) {
l := plot.Legend{
ThumbnailWidth: vg.Points(20),
TextStyle: text.Style{
Font: font.From(plot.DefaultFont, 10),
Handler: plot.DefaultTextHandler,
},
}
for i, n := range []string{"A", "B", "C", "D"} {
b, err := plotter.NewBarChart(plotter.Values{0}, 1)
if err != nil {
t.Fatalf("failed to create bar chart %q: %v", n, err)
}
b.Color = color.Gray{byte(i+1)*64 - 1}
l.Add(n, b)
}
c := vgimg.PngCanvas{Canvas: vgimg.New(5*vg.Centimeter, 5*vg.Centimeter)}
l.Draw(draw.New(c))
var buf bytes.Buffer
if _, err := c.WriteTo(&buf); err != nil {
t.Fatal(err)
}
if *cmpimg.GenerateTestData {
// Recreate Golden images and exit.
err := ioutil.WriteFile("testdata/legendAlignment_golden.png", buf.Bytes(), 0o644)
if err != nil {
t.Fatal(err)
}
return
}
err := ioutil.WriteFile("testdata/legendAlignment.png", buf.Bytes(), 0o644)
if err != nil {
t.Fatal(err)
}
want, err := ioutil.ReadFile("testdata/legendAlignment_golden.png")
if err != nil {
t.Fatal(err)
}
ok, err := cmpimg.Equal("png", buf.Bytes(), want)
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatalf("images differ")
}
}
func TestIssue514(t *testing.T) {
for _, ulp := range []int{
0,
+1, +2, +3, +4, +5, +6, +7, +8, +9, +10, +11, +12, +13, +14, +15, +16, +17, +18, +19, +20, +21, +22,
-1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20, -21, -22,
} {
t.Run(fmt.Sprintf("ulps%+02d", ulp), func(t *testing.T) {
done := make(chan int)
go func() {
defer close(done)
p := plot.New()
var (
y1 = 100.0
y2 = y1
)
switch {
case ulp < 0:
y2 = math.Float64frombits(math.Float64bits(y1) - uint64(-ulp))
case ulp > 0:
y2 = math.Float64frombits(math.Float64bits(y1) + uint64(ulp))
}
pts, err := plotter.NewScatter(plotter.XYs{
{X: 1, Y: y1},
{X: 1, Y: y2},
})
if err != nil {
t.Errorf("could not create scatter: %v", err)
return
}
p.Add(pts)
c := draw.NewCanvas(&recorder.Canvas{}, 100, 100)
p.Draw(c)
}()
timeout := time.NewTimer(100 * time.Millisecond)
defer timeout.Stop()
select {
case <-done:
case <-timeout.C:
t.Fatalf("could not create plot with small axis range within allotted time")
}
})
}
}