github.com/jgbaldwinbrown/perf@v0.1.1/benchmath/sample_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 benchmath 6 7 import ( 8 "math" 9 "testing" 10 ) 11 12 func TestSummaryFormat(t *testing.T) { 13 check := func(center, lo, hi float64, want string) { 14 t.Helper() 15 s := Summary{Center: center, Lo: lo, Hi: hi} 16 got := s.PctRangeString() 17 if got != want { 18 t.Errorf("for %v CI [%v, %v], got %s, want %s", center, lo, hi, got, want) 19 } 20 } 21 inf := math.Inf(1) 22 23 check(1, 0.5, 1.1, "50%") 24 check(1, 0.9, 1.5, "50%") 25 check(1, 1, 1, "0%") 26 27 check(-1, -0.5, -1.1, "50%") 28 check(-1, -0.9, -1.5, "50%") 29 check(-1, -1, -1, "0%") 30 31 check(1, -inf, 1, "∞") 32 check(1, 1, inf, "∞") 33 34 check(1, -1, 1, "?") 35 check(1, -1, -1, "?") 36 check(-1, -1, 1, "?") 37 check(-1, 1, -1, "?") 38 check(0, -1, 1, "?") 39 40 check(0, 0, 0, "0%") 41 } 42 43 func TestComparisonFormat(t *testing.T) { 44 check := func(p float64, n1, n2 int, want string) { 45 t.Helper() 46 got := Comparison{P: p, N1: n1, N2: n2}.String() 47 if got != want { 48 t.Errorf("for %v,%v,%v, got %s, want %s", p, n1, n2, got, want) 49 } 50 } 51 check(0.5, 1, 2, "p=0.500 n=1+2") 52 check(0.5, 2, 2, "p=0.500 n=2") 53 check(0, 1, 2, "n=1+2") 54 check(0, 2, 2, "n=2") 55 56 checkD := func(p, old, new, alpha float64, want string) { 57 got := Comparison{P: p, Alpha: alpha}.FormatDelta(old, new) 58 if got != want { 59 t.Errorf("for p=%v %v=>%v @%v, got %s, want %s", p, old, new, alpha, got, want) 60 } 61 } 62 checkD(0.5, 0, 0, 0.05, "~") 63 checkD(0.01, 0, 0, 0.05, "0.00%") 64 checkD(0.01, 1, 1, 0.05, "0.00%") 65 checkD(0.01, 0, 1, 0.05, "?") 66 checkD(0.01, 1, 1.5, 0.05, "+50.00%") 67 checkD(0.01, 1, 0.5, 0.05, "-50.00%") 68 }