...

Source file src/gonum.org/v1/plot/plotter/field_test.go

Documentation: gonum.org/v1/plot/plotter

     1  // Copyright ©2019 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 plotter_test
     6  
     7  import (
     8  	"testing"
     9  
    10  	"gonum.org/v1/plot"
    11  	"gonum.org/v1/plot/cmpimg"
    12  	"gonum.org/v1/plot/plotter"
    13  	"gonum.org/v1/plot/vg/draw"
    14  	"gonum.org/v1/plot/vg/vgimg"
    15  )
    16  
    17  type field struct {
    18  	c, r int
    19  	fn   func(x, y float64) plotter.XY
    20  }
    21  
    22  func (f field) Dims() (c, r int)           { return f.c, f.r }
    23  func (f field) Vector(c, r int) plotter.XY { return f.fn(f.X(c), f.Y(r)) }
    24  func (f field) X(c int) float64 {
    25  	if c < 0 || c >= f.c {
    26  		panic("column index out of range")
    27  	}
    28  	return float64(c - f.c/2)
    29  }
    30  func (f field) Y(r int) float64 {
    31  	if r < 0 || r >= f.r {
    32  		panic("row index out of range")
    33  	}
    34  	return float64(r - f.r/2)
    35  }
    36  
    37  func TestField(t *testing.T) {
    38  	cmpimg.CheckPlot(ExampleField, t, "field.png")
    39  }
    40  
    41  func TestFieldColors(t *testing.T) {
    42  	cmpimg.CheckPlot(ExampleField_colors, t, "color_field.png")
    43  }
    44  
    45  func TestFieldGophers(t *testing.T) {
    46  	cmpimg.CheckPlot(ExampleField_gophers, t, "gopher_field.png")
    47  }
    48  
    49  func TestFieldDims(t *testing.T) {
    50  	for _, test := range []struct {
    51  		rows int
    52  		cols int
    53  	}{
    54  		{rows: 1, cols: 2},
    55  		{rows: 2, cols: 1},
    56  		{rows: 2, cols: 2},
    57  	} {
    58  		func() {
    59  			defer func() {
    60  				r := recover()
    61  				if r != nil {
    62  					t.Errorf("unexpected panic for rows=%d cols=%d: %v", test.rows, test.cols, r)
    63  				}
    64  			}()
    65  
    66  			f := plotter.NewField(field{
    67  				r: test.rows, c: test.cols,
    68  				fn: func(x, y float64) plotter.XY {
    69  					return plotter.XY{
    70  						X: y,
    71  						Y: -x,
    72  					}
    73  				},
    74  			})
    75  
    76  			p := plot.New()
    77  			p.Add(f)
    78  
    79  			img := vgimg.New(250, 175)
    80  			dc := draw.New(img)
    81  
    82  			p.Draw(dc)
    83  		}()
    84  	}
    85  }
    86  

View as plain text