...

Source file src/github.com/google/pprof/internal/measurement/measurement_test.go

Documentation: github.com/google/pprof/internal/measurement

     1  // Copyright 2017 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 measurement
    16  
    17  import (
    18  	"math"
    19  	"testing"
    20  )
    21  
    22  func TestScale(t *testing.T) {
    23  	for _, tc := range []struct {
    24  		value            int64
    25  		fromUnit, toUnit string
    26  		wantValue        float64
    27  		wantUnit         string
    28  	}{
    29  		{1, "s", "ms", 1000, "ms"},
    30  		{1, "kb", "b", 1024, "B"},
    31  		{1, "kbyte", "b", 1024, "B"},
    32  		{1, "kilobyte", "b", 1024, "B"},
    33  		{1, "mb", "kb", 1024, "kB"},
    34  		{1, "gb", "mb", 1024, "MB"},
    35  		{1024, "gb", "tb", 1, "TB"},
    36  		{1024, "tb", "pb", 1, "PB"},
    37  		{2048, "mb", "auto", 2, "GB"},
    38  		{3.1536e7, "s", "auto", 8760, "hrs"},
    39  		{-1, "s", "ms", -1000, "ms"},
    40  		{1, "foo", "count", 1, ""},
    41  		{1, "foo", "bar", 1, "bar"},
    42  		{2000, "count", "count", 2000, ""},
    43  		{2000, "count", "auto", 2000, ""},
    44  		{2000, "count", "minimum", 2000, ""},
    45  		{8e10, "nanogcu", "petagcus", 8e-14, "P*GCU"},
    46  		{1.5e10, "microGCU", "teraGCU", 1.5e-8, "T*GCU"},
    47  		{3e6, "milliGCU", "gigagcu", 3e-6, "G*GCU"},
    48  		{1000, "kilogcu", "megagcu", 1, "M*GCU"},
    49  		{2000, "GCU", "kiloGCU", 2, "k*GCU"},
    50  		{7, "megaGCU", "gcu", 7e6, "GCU"},
    51  		{5, "gigagcus", "milligcu", 5e12, "m*GCU"},
    52  		{7, "teragcus", "microGCU", 7e18, "u*GCU"},
    53  		{1, "petaGCU", "nanogcus", 1e24, "n*GCU"},
    54  		{100, "NanoGCU", "auto", 100, "n*GCU"},
    55  		{5000, "nanogcu", "auto", 5, "u*GCU"},
    56  		{3000, "MicroGCU", "auto", 3, "m*GCU"},
    57  		{4000, "MilliGCU", "auto", 4, "GCU"},
    58  		{4000, "GCU", "auto", 4, "k*GCU"},
    59  		{5000, "KiloGCU", "auto", 5, "M*GCU"},
    60  		{6000, "MegaGCU", "auto", 6, "G*GCU"},
    61  		{7000, "GigaGCU", "auto", 7, "T*GCU"},
    62  		{8000, "TeraGCU", "auto", 8, "P*GCU"},
    63  		{9000, "PetaGCU", "auto", 9000, "P*GCU"},
    64  	} {
    65  		if gotValue, gotUnit := Scale(tc.value, tc.fromUnit, tc.toUnit); !floatEqual(gotValue, tc.wantValue) || gotUnit != tc.wantUnit {
    66  			t.Errorf("Scale(%d, %q, %q) = (%g, %q), want (%g, %q)",
    67  				tc.value, tc.fromUnit, tc.toUnit, gotValue, gotUnit, tc.wantValue, tc.wantUnit)
    68  		}
    69  	}
    70  }
    71  
    72  func floatEqual(a, b float64) bool {
    73  	diff := math.Abs(a - b)
    74  	avg := (math.Abs(a) + math.Abs(b)) / 2
    75  	return diff/avg < 0.0001
    76  }
    77  

View as plain text