vitess.io/vitess@v0.16.2/go/mathstats/util_test.go (about)

     1  // Copyright 2015 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 mathstats
     6  
     7  import (
     8  	"fmt"
     9  	"math"
    10  	"sort"
    11  	"strings"
    12  	"testing"
    13  )
    14  
    15  // aeq returns true if expect and got are equal to 8 significant
    16  // figures (1 part in 100 million).
    17  func aeq(expect, got float64) bool {
    18  	if expect < 0 && got < 0 {
    19  		expect, got = -expect, -got
    20  	}
    21  	return expect*0.99999999 <= got && got*0.99999999 <= expect
    22  }
    23  
    24  func testFunc(t *testing.T, name string, f func(float64) float64, vals map[float64]float64) {
    25  	xs := make([]float64, 0, len(vals))
    26  	for x := range vals {
    27  		xs = append(xs, x)
    28  	}
    29  	sort.Float64s(xs)
    30  
    31  	for _, x := range xs {
    32  		want, got := vals[x], f(x)
    33  		if math.IsNaN(want) && math.IsNaN(got) || aeq(want, got) {
    34  			continue
    35  		}
    36  		var label string
    37  		if strings.Contains(name, "%v") {
    38  			label = fmt.Sprintf(name, x)
    39  		} else {
    40  			label = fmt.Sprintf("%s(%v)", name, x)
    41  		}
    42  		t.Errorf("want %s=%v, got %v", label, want, got)
    43  	}
    44  }