github.com/jgbaldwinbrown/perf@v0.1.1/benchfmt/result_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 benchfmt
     6  
     7  import (
     8  	"fmt"
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  func TestResultSetConfig(t *testing.T) {
    14  	r := &Result{}
    15  	check := func(want ...string) {
    16  		t.Helper()
    17  		var kv []string
    18  		for _, cfg := range r.Config {
    19  			kv = append(kv, fmt.Sprintf("%s: %s", cfg.Key, cfg.Value))
    20  		}
    21  		if !reflect.DeepEqual(want, kv) {
    22  			t.Errorf("want %q, got %q", want, kv)
    23  		}
    24  
    25  		// Check the index.
    26  		for i, cfg := range r.Config {
    27  			gotI, ok := r.ConfigIndex(cfg.Key)
    28  			if !ok {
    29  				t.Errorf("key %s missing from index", cfg.Key)
    30  			} else if i != gotI {
    31  				t.Errorf("key %s index: want %d, got %d", cfg.Key, i, gotI)
    32  			}
    33  		}
    34  		if len(r.Config) != len(r.configPos) {
    35  			t.Errorf("index size mismatch: %d file configs, %d index length", len(r.Config), len(r.configPos))
    36  		}
    37  	}
    38  
    39  	// Basic key additions.
    40  	check()
    41  	r.SetConfig("a", "b")
    42  	check("a: b")
    43  	r.SetConfig("x", "y")
    44  	check("a: b", "x: y")
    45  	r.SetConfig("z", "w")
    46  	check("a: b", "x: y", "z: w")
    47  
    48  	// Update value.
    49  	r.SetConfig("x", "z")
    50  	check("a: b", "x: z", "z: w")
    51  
    52  	// Delete key.
    53  	r.SetConfig("a", "") // Check swapping
    54  	check("z: w", "x: z")
    55  	r.SetConfig("x", "") // Last key
    56  	check("z: w")
    57  	r.SetConfig("c", "") // Non-existent
    58  	check("z: w")
    59  
    60  	// Add key after deletion.
    61  	r.SetConfig("c", "d")
    62  	check("z: w", "c: d")
    63  }
    64  
    65  func TestResultGetConfig(t *testing.T) {
    66  	r := &Result{}
    67  	check := func(key, want string) {
    68  		t.Helper()
    69  		got := r.GetConfig(key)
    70  		if want != got {
    71  			t.Errorf("for key %s: want %s, got %s", key, want, got)
    72  		}
    73  	}
    74  	check("x", "")
    75  	r.SetConfig("x", "y")
    76  	check("x", "y")
    77  	r.SetConfig("a", "b")
    78  	check("a", "b")
    79  	check("x", "y")
    80  	r.SetConfig("a", "")
    81  	check("a", "")
    82  	check("x", "y")
    83  
    84  	// Test a literal.
    85  	r = &Result{
    86  		Config: []Config{{Key: "a", Value: []byte("b")}},
    87  	}
    88  	check("a", "b")
    89  	check("x", "")
    90  }
    91  
    92  func TestResultValue(t *testing.T) {
    93  	r := &Result{
    94  		Values: []Value{{42, "ns/op", 42e-9, "sec/op"}, {24, "B/op", 0, ""}},
    95  	}
    96  	check := func(unit string, want float64) {
    97  		t.Helper()
    98  		got, ok := r.Value(unit)
    99  		if !ok {
   100  			t.Errorf("missing unit %s", unit)
   101  		} else if want != got {
   102  			t.Errorf("for unit %s: want %v, got %v", unit, want, got)
   103  		}
   104  	}
   105  	check("ns/op", 42)
   106  	check("B/op", 24)
   107  	_, ok := r.Value("B/sec")
   108  	if ok {
   109  		t.Errorf("unexpectedly found unit %s", "B/sec")
   110  	}
   111  }
   112  
   113  func TestBaseName(t *testing.T) {
   114  	check := func(fullName string, want string) {
   115  		t.Helper()
   116  		got := string(Name(fullName).Base())
   117  		if got != want {
   118  			t.Errorf("BaseName(%q) = %q, want %q", fullName, got, want)
   119  		}
   120  	}
   121  	check("Test", "Test")
   122  	check("Test-42", "Test")
   123  	check("Test/foo", "Test")
   124  	check("Test/foo-42", "Test")
   125  }
   126  
   127  func TestNameParts(t *testing.T) {
   128  	check := func(fullName string, base string, parts ...string) {
   129  		t.Helper()
   130  		got, gotParts := Name(fullName).Parts()
   131  		fail := string(got) != base
   132  		if len(gotParts) != len(parts) {
   133  			fail = true
   134  		} else {
   135  			for i := range parts {
   136  				if parts[i] != string(gotParts[i]) {
   137  					fail = true
   138  				}
   139  			}
   140  		}
   141  		if fail {
   142  			t.Errorf("FullName(%q) = %q, %q, want %q, %q", fullName, got, gotParts, base, parts)
   143  		}
   144  	}
   145  	check("Test", "Test")
   146  	// Gomaxprocs
   147  	check("Test-42", "Test", "-42")
   148  	// Subtests
   149  	check("Test/foo", "Test", "/foo")
   150  	check("Test/foo=42/bar=24", "Test", "/foo=42", "/bar=24")
   151  	// Both
   152  	check("Test/foo=123-42", "Test", "/foo=123", "-42")
   153  	// Looks like gomaxprocs, but isn't
   154  	check("Test/foo-bar", "Test", "/foo-bar")
   155  	check("Test/foo-1/bar", "Test", "/foo-1", "/bar")
   156  	// Trailing slash
   157  	check("Test/foo/", "Test", "/foo", "/")
   158  	// Empty name
   159  	check("", "")
   160  	check("/a/b", "", "/a", "/b")
   161  }