go-hep.org/x/hep@v0.38.1/hplot/s2d_test.go (about)

     1  // Copyright ©2016 The go-hep 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 hplot_test
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  
    11  	"go-hep.org/x/hep/hbook"
    12  	"go-hep.org/x/hep/hplot"
    13  	"gonum.org/v1/plot/cmpimg"
    14  )
    15  
    16  func TestS2D(t *testing.T) {
    17  	checkPlot(cmpimg.CheckPlot)(ExampleS2D, t, "s2d.png")
    18  }
    19  
    20  func TestScatter2DWithErrorBars(t *testing.T) {
    21  	checkPlot(cmpimg.CheckPlot)(ExampleS2D_withErrorBars, t, "s2d_errbars.png")
    22  }
    23  
    24  func TestScatter2DWithBand(t *testing.T) {
    25  	checkPlot(cmpimg.CheckPlot)(ExampleS2D_withBand, t, "s2d_band.png")
    26  }
    27  
    28  func TestScatter2DWithStepsKind(t *testing.T) {
    29  	checkPlot(cmpimg.CheckPlot)(ExampleS2D_withStepsKind, t, "s2d_steps.png")
    30  }
    31  
    32  func TestScatter2DWithPreMidPostSteps(t *testing.T) {
    33  	checkPlot(cmpimg.CheckPlot)(ExampleS2D_withPreMidPostSteps, t, "s2d_premidpost_steps.png")
    34  }
    35  
    36  func TestScatter2DWithStepsKindWithBand(t *testing.T) {
    37  	checkPlot(cmpimg.CheckPlot)(ExampleS2D_withStepsKind_withBand, t, "s2d_steps_band.png")
    38  }
    39  
    40  func TestScatter2DSteps(t *testing.T) {
    41  	for _, tc := range []struct {
    42  		name string
    43  		pts  []hbook.Point2D
    44  		opts []hplot.Options
    45  		want error
    46  	}{
    47  		{
    48  			name: "histeps_no_xerr",
    49  			pts: []hbook.Point2D{
    50  				{X: 1, Y: 1, ErrY: hbook.Range{Min: 2, Max: 3}},
    51  				{X: 2, Y: 2, ErrY: hbook.Range{Min: 5, Max: 2}},
    52  				{X: 3, Y: 3, ErrY: hbook.Range{Min: 2, Max: 2}},
    53  				{X: 4, Y: 4, ErrY: hbook.Range{Min: 1.2, Max: 2}},
    54  			},
    55  			opts: []hplot.Options{hplot.WithStepsKind(hplot.HiSteps)},
    56  			want: fmt.Errorf("s2d with HiSteps needs XErr informations for all points"),
    57  		},
    58  		{
    59  			name: "histeps_missing_some_xerr",
    60  			pts: []hbook.Point2D{
    61  				{X: 1, Y: 1, ErrY: hbook.Range{Min: 2, Max: 3}, ErrX: hbook.Range{Min: 1, Max: 2}},
    62  				{X: 2, Y: 2, ErrY: hbook.Range{Min: 5, Max: 2}},
    63  				{X: 3, Y: 3, ErrY: hbook.Range{Min: 2, Max: 2}, ErrX: hbook.Range{Min: 1, Max: 2}},
    64  				{X: 4, Y: 4, ErrY: hbook.Range{Min: 1.2, Max: 2}},
    65  			},
    66  			opts: []hplot.Options{hplot.WithStepsKind(hplot.HiSteps)},
    67  			want: fmt.Errorf("s2d with HiSteps needs XErr informations for all points"),
    68  		},
    69  		{
    70  			name: "presteps_with_band", // TODO(sbinet)
    71  			pts: []hbook.Point2D{
    72  				{X: 1, Y: 1},
    73  				{X: 2, Y: 2},
    74  				{X: 3, Y: 3},
    75  				{X: 4, Y: 4},
    76  			},
    77  			opts: []hplot.Options{hplot.WithStepsKind(hplot.PreSteps), hplot.WithBand(true)},
    78  			want: fmt.Errorf("presteps not implemented"),
    79  		},
    80  		{
    81  			name: "midsteps_with_band", // TODO(sbinet)
    82  			pts: []hbook.Point2D{
    83  				{X: 1, Y: 1},
    84  				{X: 2, Y: 2},
    85  				{X: 3, Y: 3},
    86  				{X: 4, Y: 4},
    87  			},
    88  			opts: []hplot.Options{hplot.WithStepsKind(hplot.MidSteps), hplot.WithBand(true)},
    89  			want: fmt.Errorf("midsteps not implemented"),
    90  		},
    91  		{
    92  			name: "poststeps_with_band", // TODO(sbinet)
    93  			pts: []hbook.Point2D{
    94  				{X: 1, Y: 1},
    95  				{X: 2, Y: 2},
    96  				{X: 3, Y: 3},
    97  				{X: 4, Y: 4},
    98  			},
    99  			opts: []hplot.Options{hplot.WithStepsKind(hplot.PostSteps), hplot.WithBand(true)},
   100  			want: fmt.Errorf("poststeps not implemented"),
   101  		},
   102  	} {
   103  		t.Run(tc.name, func(t *testing.T) {
   104  			defer func() {
   105  				err := recover()
   106  				switch {
   107  				case err == nil && tc.want != nil:
   108  					t.Fatalf("expected a panic")
   109  				case err == nil && tc.want == nil:
   110  					// ok.
   111  				case err != nil && tc.want == nil:
   112  					panic(err) // bubble up
   113  				case err != nil && tc.want != nil:
   114  					var got string
   115  					switch err := err.(type) {
   116  					case error:
   117  						got = err.Error()
   118  					case string:
   119  						got = err
   120  					default:
   121  						panic(fmt.Errorf("invalid recover type %T", err))
   122  					}
   123  					if got, want := got, tc.want.Error(); got != want {
   124  						t.Fatalf("invalid error:\ngot= %q\nwant=%q", got, want)
   125  					}
   126  				}
   127  			}()
   128  
   129  			s2d := hbook.NewS2D(tc.pts...)
   130  
   131  			_ = hplot.NewS2D(s2d, tc.opts...)
   132  		})
   133  	}
   134  }