github.com/jgbaldwinbrown/perf@v0.1.1/benchproc/sort_test.go (about)

     1  // Copyright 2022 The Go 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 benchproc
     6  
     7  import (
     8  	"math/rand"
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  func TestSort(t *testing.T) {
    14  	check := func(keys []Key, want ...string) {
    15  		SortKeys(keys)
    16  		var got []string
    17  		for _, key := range keys {
    18  			got = append(got, key.String())
    19  		}
    20  		if !reflect.DeepEqual(got, want) {
    21  			t.Errorf("got %v, want %v", got, want)
    22  		}
    23  	}
    24  
    25  	// Observation order.
    26  	s, _ := mustParse(t, "a")
    27  	k := []Key{
    28  		p(t, s, "", "a", "1"),
    29  		p(t, s, "", "a", "3"),
    30  		p(t, s, "", "a", "2"),
    31  	}
    32  	check(k, "a:1", "a:3", "a:2")
    33  
    34  	// Tuple observation order.
    35  	s, _ = mustParse(t, "a,b")
    36  	// Prepare order.
    37  	p(t, s, "", "a", "1")
    38  	p(t, s, "", "a", "2")
    39  	p(t, s, "", "b", "1")
    40  	p(t, s, "", "b", "2")
    41  	k = []Key{
    42  		p(t, s, "", "a", "2", "b", "1"),
    43  		p(t, s, "", "a", "1", "b", "2"),
    44  	}
    45  	check(k, "a:1 b:2", "a:2 b:1")
    46  
    47  	// Alphabetic
    48  	s, _ = mustParse(t, "a@alpha")
    49  	k = []Key{
    50  		p(t, s, "", "a", "c"),
    51  		p(t, s, "", "a", "b"),
    52  		p(t, s, "", "a", "a"),
    53  	}
    54  	check(k, "a:a", "a:b", "a:c")
    55  
    56  	// Numeric.
    57  	s, _ = mustParse(t, "a@num")
    58  	k = []Key{
    59  		p(t, s, "", "a", "100"),
    60  		p(t, s, "", "a", "20"),
    61  		p(t, s, "", "a", "3"),
    62  	}
    63  	check(k, "a:3", "a:20", "a:100")
    64  
    65  	// Numeric with strings.
    66  	s, _ = mustParse(t, "a@num")
    67  	k = []Key{
    68  		p(t, s, "", "a", "b"),
    69  		p(t, s, "", "a", "a"),
    70  		p(t, s, "", "a", "100"),
    71  		p(t, s, "", "a", "20"),
    72  	}
    73  	check(k, "a:20", "a:100", "a:a", "a:b")
    74  
    75  	// Numeric with weird cases.
    76  	s, _ = mustParse(t, "a@num")
    77  	k = []Key{
    78  		p(t, s, "", "a", "1"),
    79  		p(t, s, "", "a", "-inf"),
    80  		p(t, s, "", "a", "-infinity"),
    81  		p(t, s, "", "a", "inf"),
    82  		p(t, s, "", "a", "infinity"),
    83  		p(t, s, "", "a", "1.0"),
    84  		p(t, s, "", "a", "NaN"),
    85  		p(t, s, "", "a", "nan"),
    86  	}
    87  	// Shuffle the slice to exercise any instabilities.
    88  	for try := 0; try < 10; try++ {
    89  		for i := 1; i < len(k); i++ {
    90  			p := rand.Intn(i)
    91  			k[p], k[i] = k[i], k[p]
    92  		}
    93  		check(k, "a:-inf", "a:-infinity", "a:1", "a:1.0", "a:inf", "a:infinity", "a:NaN", "a:nan")
    94  	}
    95  
    96  	// Fixed.
    97  	s, _ = mustParse(t, "a@(c b a)")
    98  	k = []Key{
    99  		p(t, s, "", "a", "a"),
   100  		p(t, s, "", "a", "b"),
   101  		p(t, s, "", "a", "c"),
   102  	}
   103  	check(k, "a:c", "a:b", "a:a")
   104  }
   105  
   106  func TestParseNum(t *testing.T) {
   107  	check := func(x string, want float64) {
   108  		t.Helper()
   109  		got, err := parseNum(x)
   110  		if err != nil {
   111  			t.Errorf("%s: want %v, got error %s", x, want, err)
   112  		} else if want != got {
   113  			t.Errorf("%s: want %v, got %v", x, want, got)
   114  		}
   115  	}
   116  
   117  	check("1", 1)
   118  	check("1B", 1)
   119  	check("1b", 1)
   120  	check("100.5", 100.5)
   121  	check("1k", 1000)
   122  	check("1K", 1000)
   123  	check("1ki", 1024)
   124  	check("1kiB", 1024)
   125  	check("1M", 1000000)
   126  	check("1Mi", 1<<20)
   127  	check("1G", 1000000000)
   128  	check("1T", 1000000000000)
   129  	check("1P", 1000000000000000)
   130  	check("1E", 1000000000000000000)
   131  	check("1Z", 1000000000000000000000)
   132  	check("1Y", 1000000000000000000000000)
   133  }