...

Source file src/github.com/google/pprof/profile/index_test.go

Documentation: github.com/google/pprof/profile

     1  // Copyright 2016 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package profile
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestSampleIndexByName(t *testing.T) {
    22  	for _, c := range []struct {
    23  		desc              string
    24  		sampleTypes       []string
    25  		defaultSampleType string
    26  		index             string
    27  		want              int
    28  		wantError         bool
    29  	}{
    30  		{
    31  			desc:        "use last by default",
    32  			index:       "",
    33  			want:        1,
    34  			sampleTypes: []string{"zero", "default"},
    35  		},
    36  		{
    37  			desc:              "honour specified default",
    38  			index:             "",
    39  			want:              1,
    40  			defaultSampleType: "default",
    41  			sampleTypes:       []string{"zero", "default", "two"},
    42  		},
    43  		{
    44  			desc:              "invalid default is ignored",
    45  			index:             "",
    46  			want:              2,
    47  			defaultSampleType: "non-existent",
    48  			sampleTypes:       []string{"zero", "one", "default"},
    49  		},
    50  		{
    51  			desc:        "index by int",
    52  			index:       "0",
    53  			want:        0,
    54  			sampleTypes: []string{"zero", "one", "two"},
    55  		},
    56  		{
    57  			desc:              "index by int ignores default",
    58  			index:             "0",
    59  			want:              0,
    60  			defaultSampleType: "default",
    61  			sampleTypes:       []string{"zero", "default", "two"},
    62  		},
    63  		{
    64  			desc:        "index by name",
    65  			index:       "two",
    66  			want:        2,
    67  			sampleTypes: []string{"zero", "one", "two", "three"},
    68  		},
    69  		{
    70  			desc:              "index by name ignores default",
    71  			index:             "zero",
    72  			want:              0,
    73  			defaultSampleType: "default",
    74  			sampleTypes:       []string{"zero", "default", "two"},
    75  		},
    76  		{
    77  			desc:        "out of bound int causes error",
    78  			index:       "100",
    79  			wantError:   true,
    80  			sampleTypes: []string{"zero", "default"},
    81  		},
    82  		{
    83  			desc:        "unknown name causes error",
    84  			index:       "does not exist",
    85  			wantError:   true,
    86  			sampleTypes: []string{"zero", "default"},
    87  		},
    88  		{
    89  			desc:        "'inused_{x}' recognized for legacy '{x}'",
    90  			index:       "inuse_zero",
    91  			want:        0,
    92  			sampleTypes: []string{"zero", "default"},
    93  		},
    94  	} {
    95  		p := &Profile{
    96  			DefaultSampleType: c.defaultSampleType,
    97  			SampleType:        []*ValueType{},
    98  		}
    99  		for _, st := range c.sampleTypes {
   100  			p.SampleType = append(p.SampleType, &ValueType{Type: st, Unit: "milliseconds"})
   101  		}
   102  
   103  		got, err := p.SampleIndexByName(c.index)
   104  
   105  		switch {
   106  		case c.wantError && err == nil:
   107  			t.Errorf("%s: error should have been returned not index=%d, err=%v", c.desc, got, err)
   108  		case !c.wantError && err != nil:
   109  			t.Errorf("%s: unexpected got index=%d, err=%v; wanted index=%d, err=nil", c.desc, got, err, c.want)
   110  		case !c.wantError && got != c.want:
   111  			t.Errorf("%s: got index=%d, want index=%d", c.desc, got, c.want)
   112  		}
   113  	}
   114  }
   115  

View as plain text