github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/base/bmath/abs_test.go (about)

     1  package bmath
     2  
     3  import (
     4  	"math"
     5  	"math/rand"
     6  	"strconv"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  func TestAbs(t *testing.T) {
    12  	type args struct {
    13  		a float64
    14  	}
    15  	type ts struct {
    16  		name string
    17  		args args
    18  		want float64
    19  	}
    20  	var tests []ts
    21  
    22  	rand.Seed(time.Now().Unix())
    23  	for i := 0; i < 100; i++ {
    24  		v := rand.Intn(100) - 50
    25  		tests = append(tests, ts{
    26  			name: strconv.Itoa(v),
    27  			args: args{
    28  				a: float64(v),
    29  			},
    30  			want: math.Abs(float64(v)),
    31  		})
    32  	}
    33  	for _, tt := range tests {
    34  		t.Run(tt.name, func(t *testing.T) {
    35  			if got := Abs(tt.args.a); got != tt.want {
    36  				t.Errorf("Abs() = %v, want %v", got, tt.want)
    37  			}
    38  		})
    39  	}
    40  }