github.com/go-playground/pkg/v5@v5.29.1/math/max_test.go (about)

     1  //go:build go1.18
     2  // +build go1.18
     3  
     4  package mathext
     5  
     6  import (
     7  	"math"
     8  	"testing"
     9  
    10  	. "github.com/go-playground/assert/v2"
    11  )
    12  
    13  func TestMax(t *testing.T) {
    14  	Equal(t, true, math.IsNaN(Max(math.NaN(), 1)))
    15  	Equal(t, true, math.IsNaN(Max(1, math.NaN())))
    16  	Equal(t, math.Inf(0), Max(math.Inf(0), math.Inf(-1)))
    17  	Equal(t, math.Inf(0), Max(math.Inf(-1), math.Inf(0)))
    18  	Equal(t, 1.333, Max(1.333, 1.0))
    19  	Equal(t, 1.333, Max(1.0, 1.333))
    20  	Equal(t, 3, Max(3, 1))
    21  	Equal(t, 3, Max(1, 3))
    22  	Equal(t, 0, Max(0, -0))
    23  	Equal(t, 0, Max(-0, 0))
    24  }
    25  
    26  func BenchmarkMaxInf(b *testing.B) {
    27  	n1 := math.Inf(0)
    28  	n2 := math.Inf(-1)
    29  
    30  	for i := 0; i < b.N; i++ {
    31  		_ = Max(n1, n2)
    32  	}
    33  }
    34  
    35  func BenchmarkMaxNaN(b *testing.B) {
    36  	n1 := math.Inf(0)
    37  	n2 := math.NaN()
    38  
    39  	for i := 0; i < b.N; i++ {
    40  		_ = Max(n1, n2)
    41  	}
    42  }
    43  
    44  func BenchmarkMaxNumber(b *testing.B) {
    45  	n1 := 1
    46  	n2 := 3
    47  
    48  	for i := 0; i < b.N; i++ {
    49  		_ = Max(n1, n2)
    50  	}
    51  }