...

Source file src/gonum.org/v1/plot/axis_test.go

Documentation: gonum.org/v1/plot

     1  // Copyright ©2015 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package plot
     6  
     7  import (
     8  	"fmt"
     9  	"math"
    10  	"reflect"
    11  	"testing"
    12  
    13  	"gonum.org/v1/plot/cmpimg"
    14  	"gonum.org/v1/plot/vg"
    15  )
    16  
    17  var axisSmallTickTests = []struct {
    18  	min, max   float64
    19  	wantValues []float64
    20  	wantLabels []string
    21  }{
    22  	{
    23  		min:        -1.9846500878911073,
    24  		max:        0.4370974820125605,
    25  		wantValues: []float64{-1.75, -0.75, 0.25},
    26  		wantLabels: []string{"-1.75", "-0.75", "0.25"},
    27  	},
    28  	{
    29  		min:        -1.985e15,
    30  		max:        0.4371e15,
    31  		wantValues: []float64{-1.75e15, -7.5e14, 2.5e14},
    32  		wantLabels: []string{"-1.75e+15", "-7.5e+14", "2.5e+14"},
    33  	},
    34  	{
    35  		min:        -1.985e-15,
    36  		max:        0.4371e-15,
    37  		wantValues: []float64{-1.985e-15, -7.739500000000001e-16, 4.3709999999999994e-16},
    38  		wantLabels: []string{"-1.985e-15", "-7.7395e-16", "4.371e-16"},
    39  	},
    40  	{
    41  		min:        math.MaxFloat64 / 4,
    42  		max:        math.MaxFloat64 / 3,
    43  		wantValues: []float64{4.4942328371557893e+307, 5.243271643348421e+307, 5.992310449541053e+307},
    44  		wantLabels: []string{"4e+307", "5e+307", "6e+307"},
    45  	},
    46  	{
    47  		min:        0.00010,
    48  		max:        0.00015,
    49  		wantValues: []float64{0.0001, 0.00012, 0.00014000000000000001},
    50  		wantLabels: []string{"0.0001", "0.00012", "0.00014"},
    51  	},
    52  	{
    53  		min:        555.6545,
    54  		max:        21800.9875,
    55  		wantValues: []float64{4000, 12000, 20000},
    56  		wantLabels: []string{"4000", "12000", "20000"},
    57  	},
    58  	{
    59  		min:        555.6545,
    60  		max:        27800.9875,
    61  		wantValues: []float64{5000, 15000, 25000},
    62  		wantLabels: []string{"5000", "15000", "25000"},
    63  	},
    64  	{
    65  		min:        55.6545,
    66  		max:        1555.9875,
    67  		wantValues: []float64{300, 900, 1500},
    68  		wantLabels: []string{"300", "900", "1500"},
    69  	},
    70  	{
    71  		min:        3.096916 - 0.125,
    72  		max:        3.096916 + 0.125,
    73  		wantValues: []float64{3, 3.1, 3.2},
    74  		wantLabels: []string{"3.0", "3.1", "3.2"},
    75  	},
    76  }
    77  
    78  func TestAxisSmallTick(t *testing.T) {
    79  	d := DefaultTicks{}
    80  	for i, test := range axisSmallTickTests {
    81  		ticks := d.Ticks(test.min, test.max)
    82  		gotLabels := labelsOf(ticks)
    83  		gotValues := valuesOf(ticks)
    84  		if !reflect.DeepEqual(gotValues, test.wantValues) {
    85  			t.Errorf("tick values mismatch %d:\ngot: %v\nwant:%v", i, gotValues, test.wantValues)
    86  		}
    87  		if !reflect.DeepEqual(gotLabels, test.wantLabels) {
    88  			t.Errorf("tick labels mismatch %d:\ngot: %q\nwant:%q", i, gotLabels, test.wantLabels)
    89  		}
    90  	}
    91  }
    92  
    93  func valuesOf(ticks []Tick) []float64 {
    94  	var values []float64
    95  	for _, t := range ticks {
    96  		if t.Label != "" {
    97  			values = append(values, t.Value)
    98  		}
    99  	}
   100  	return values
   101  }
   102  
   103  func labelsOf(ticks []Tick) []string {
   104  	var labels []string
   105  	for _, t := range ticks {
   106  		if t.Label != "" {
   107  			labels = append(labels, t.Label)
   108  		}
   109  	}
   110  	return labels
   111  }
   112  
   113  func TestTickerFunc_Ticks(t *testing.T) {
   114  	type args struct {
   115  		min float64
   116  		max float64
   117  	}
   118  	tests := []struct {
   119  		name string
   120  		args args
   121  		want []Tick
   122  		f    TickerFunc
   123  	}{
   124  		{
   125  			name: "return exactly the same ticks as the function passed to TickerFunc",
   126  			args: args{0, 3},
   127  			want: []Tick{{1, "a"}, {2, "b"}},
   128  			f: func(min, max float64) []Tick {
   129  				return []Tick{{1, "a"}, {2, "b"}}
   130  			},
   131  		},
   132  	}
   133  	for _, tt := range tests {
   134  		t.Run(tt.name, func(t *testing.T) {
   135  			if got := tt.f.Ticks(tt.args.min, tt.args.max); !reflect.DeepEqual(got, tt.want) {
   136  				t.Errorf("TickerFunc.Ticks() = %v, want %v", got, tt.want)
   137  			}
   138  		})
   139  	}
   140  }
   141  
   142  func TestInvertedScale_Normalize(t *testing.T) {
   143  	inverter := InvertedScale{Normalizer: LinearScale{}}
   144  	if got := inverter.Normalize(0, 1, 1); got != 0.0 {
   145  		t.Errorf("Expected a normalization inversion %f->%f not %f", 1.0, 0.0, got)
   146  	}
   147  	if got := inverter.Normalize(0, 1, .5); got != 0.5 {
   148  		t.Errorf("Expected a normalization inversion %f->%f not %f", 0.5, 0.5, got)
   149  	}
   150  	if got := inverter.Normalize(0, 1, 0); got != 1.0 {
   151  		t.Errorf("Expected a normalization inversion %f->%f not %f", 0.0, 1.0, got)
   152  	}
   153  }
   154  
   155  func TestAxisPadding(t *testing.T) {
   156  	for _, padding := range []int{0, 5, 10} {
   157  		t.Run(fmt.Sprintf("padding-%d", padding), func(t *testing.T) {
   158  			cmpimg.CheckPlot(func() {
   159  				p := New()
   160  				p.Title.Text = fmt.Sprintf("padding=%d", padding)
   161  				p.X.Label.Text = "X-Axis"
   162  				p.X.Label.Padding = vg.Points(float64(padding))
   163  				p.Y.Label.Text = "Y-Axis"
   164  				p.Y.Label.Padding = vg.Points(float64(padding))
   165  
   166  				const size = 5 * vg.Centimeter
   167  				err := p.Save(size, size, fmt.Sprintf("testdata/axis_padding_%02d.png", padding))
   168  				if err != nil {
   169  					t.Fatalf("error: %+v", err)
   170  				}
   171  			}, t, fmt.Sprintf("axis_padding_%02d.png", padding))
   172  		})
   173  	}
   174  }
   175  

View as plain text