-
Notifications
You must be signed in to change notification settings - Fork 204
/
axis_test.go
174 lines (163 loc) · 4.48 KB
/
axis_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
// 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
import (
"fmt"
"math"
"reflect"
"testing"
"gonum.org/v1/plot/cmpimg"
"gonum.org/v1/plot/vg"
)
var axisSmallTickTests = []struct {
min, max float64
wantValues []float64
wantLabels []string
}{
{
min: -1.9846500878911073,
max: 0.4370974820125605,
wantValues: []float64{-1.75, -0.75, 0.25},
wantLabels: []string{"-1.75", "-0.75", "0.25"},
},
{
min: -1.985e15,
max: 0.4371e15,
wantValues: []float64{-1.75e15, -7.5e14, 2.5e14},
wantLabels: []string{"-1.75e+15", "-7.5e+14", "2.5e+14"},
},
{
min: -1.985e-15,
max: 0.4371e-15,
wantValues: []float64{-1.985e-15, -7.739500000000001e-16, 4.3709999999999994e-16},
wantLabels: []string{"-1.985e-15", "-7.7395e-16", "4.371e-16"},
},
{
min: math.MaxFloat64 / 4,
max: math.MaxFloat64 / 3,
wantValues: []float64{4.4942328371557893e+307, 5.243271643348421e+307, 5.992310449541053e+307},
wantLabels: []string{"4e+307", "5e+307", "6e+307"},
},
{
min: 0.00010,
max: 0.00015,
wantValues: []float64{0.0001, 0.00012, 0.00014000000000000001},
wantLabels: []string{"0.0001", "0.00012", "0.00014"},
},
{
min: 555.6545,
max: 21800.9875,
wantValues: []float64{4000, 12000, 20000},
wantLabels: []string{"4000", "12000", "20000"},
},
{
min: 555.6545,
max: 27800.9875,
wantValues: []float64{5000, 15000, 25000},
wantLabels: []string{"5000", "15000", "25000"},
},
{
min: 55.6545,
max: 1555.9875,
wantValues: []float64{300, 900, 1500},
wantLabels: []string{"300", "900", "1500"},
},
{
min: 3.096916 - 0.125,
max: 3.096916 + 0.125,
wantValues: []float64{3, 3.1, 3.2},
wantLabels: []string{"3.0", "3.1", "3.2"},
},
}
func TestAxisSmallTick(t *testing.T) {
d := DefaultTicks{}
for i, test := range axisSmallTickTests {
ticks := d.Ticks(test.min, test.max)
gotLabels := labelsOf(ticks)
gotValues := valuesOf(ticks)
if !reflect.DeepEqual(gotValues, test.wantValues) {
t.Errorf("tick values mismatch %d:\ngot: %v\nwant:%v", i, gotValues, test.wantValues)
}
if !reflect.DeepEqual(gotLabels, test.wantLabels) {
t.Errorf("tick labels mismatch %d:\ngot: %q\nwant:%q", i, gotLabels, test.wantLabels)
}
}
}
func valuesOf(ticks []Tick) []float64 {
var values []float64
for _, t := range ticks {
if t.Label != "" {
values = append(values, t.Value)
}
}
return values
}
func labelsOf(ticks []Tick) []string {
var labels []string
for _, t := range ticks {
if t.Label != "" {
labels = append(labels, t.Label)
}
}
return labels
}
func TestTickerFunc_Ticks(t *testing.T) {
type args struct {
min float64
max float64
}
tests := []struct {
name string
args args
want []Tick
f TickerFunc
}{
{
name: "return exactly the same ticks as the function passed to TickerFunc",
args: args{0, 3},
want: []Tick{{1, "a"}, {2, "b"}},
f: func(min, max float64) []Tick {
return []Tick{{1, "a"}, {2, "b"}}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.f.Ticks(tt.args.min, tt.args.max); !reflect.DeepEqual(got, tt.want) {
t.Errorf("TickerFunc.Ticks() = %v, want %v", got, tt.want)
}
})
}
}
func TestInvertedScale_Normalize(t *testing.T) {
inverter := InvertedScale{Normalizer: LinearScale{}}
if got := inverter.Normalize(0, 1, 1); got != 0.0 {
t.Errorf("Expected a normalization inversion %f->%f not %f", 1.0, 0.0, got)
}
if got := inverter.Normalize(0, 1, .5); got != 0.5 {
t.Errorf("Expected a normalization inversion %f->%f not %f", 0.5, 0.5, got)
}
if got := inverter.Normalize(0, 1, 0); got != 1.0 {
t.Errorf("Expected a normalization inversion %f->%f not %f", 0.0, 1.0, got)
}
}
func TestAxisPadding(t *testing.T) {
for _, padding := range []int{0, 5, 10} {
t.Run(fmt.Sprintf("padding-%d", padding), func(t *testing.T) {
cmpimg.CheckPlot(func() {
p := New()
p.Title.Text = fmt.Sprintf("padding=%d", padding)
p.X.Label.Text = "X-Axis"
p.X.Label.Padding = vg.Points(float64(padding))
p.Y.Label.Text = "Y-Axis"
p.Y.Label.Padding = vg.Points(float64(padding))
const size = 5 * vg.Centimeter
err := p.Save(size, size, fmt.Sprintf("testdata/axis_padding_%02d.png", padding))
if err != nil {
t.Fatalf("error: %+v", err)
}
}, t, fmt.Sprintf("axis_padding_%02d.png", padding))
})
}
}