github.com/go-eden/common@v0.1.15-0.20210617133546-059099253264/emath/math_minmax_test.go (about)

     1  package emath
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"math"
     6  	"math/rand"
     7  	"testing"
     8  )
     9  
    10  func TestMinInt(t *testing.T) {
    11  	assert.True(t, MinInt(math.MaxInt64) == math.MaxInt64)
    12  	assert.True(t, MinInt(math.MaxInt64, 0) == 0)
    13  	assert.True(t, MinInt(1, 2, 3, 10, -1, -100, -200, 300) == -200)
    14  
    15  	arr := make([]int, 1000)
    16  	for i := 0; i < len(arr); i++ {
    17  		arr[i] = rand.Int()
    18  	}
    19  
    20  	min := MinInt(arr...)
    21  
    22  	for _, v := range arr {
    23  		assert.True(t, v >= min)
    24  	}
    25  }
    26  
    27  func TestMaxInt(t *testing.T) {
    28  	assert.True(t, MaxInt(math.MinInt64) == math.MinInt64)
    29  	assert.True(t, MaxInt(math.MinInt64, 0) == 0)
    30  
    31  	arr := make([]int, 1000)
    32  	for i := 0; i < len(arr); i++ {
    33  		arr[i] = rand.Int()
    34  	}
    35  
    36  	max := MaxInt(arr...)
    37  
    38  	for _, v := range arr {
    39  		assert.True(t, v <= max)
    40  	}
    41  }
    42  
    43  func TestByteMinMax(t *testing.T) {
    44  	arr := make([]byte, 1000)
    45  	for i := 0; i < len(arr); i++ {
    46  		arr[i] = byte(rand.Int())
    47  	}
    48  	max := MaxByte(arr...)
    49  	min := MinByte(arr...)
    50  	for _, v := range arr {
    51  		assert.True(t, v <= max)
    52  		assert.True(t, v >= min)
    53  	}
    54  }
    55  
    56  func TestInt8MinMax(t *testing.T) {
    57  	arr := make([]int8, 1000)
    58  	for i := 0; i < len(arr); i++ {
    59  		arr[i] = int8(rand.Int())
    60  	}
    61  	max := MaxInt8(arr...)
    62  	min := MinInt8(arr...)
    63  	for _, v := range arr {
    64  		assert.True(t, v <= max)
    65  		assert.True(t, v >= min)
    66  	}
    67  }
    68  
    69  func TestUint8MinMax(t *testing.T) {
    70  	arr := make([]uint8, 1000)
    71  	for i := 0; i < len(arr); i++ {
    72  		arr[i] = uint8(rand.Int())
    73  	}
    74  	max := MaxUint8(arr...)
    75  	min := MinUint8(arr...)
    76  	for _, v := range arr {
    77  		assert.True(t, v <= max)
    78  		assert.True(t, v >= min)
    79  	}
    80  }
    81  
    82  func TestInt16MinMax(t *testing.T) {
    83  	arr := make([]int16, 1000)
    84  	for i := 0; i < len(arr); i++ {
    85  		arr[i] = int16(rand.Int())
    86  	}
    87  	max := MaxInt16(arr...)
    88  	min := MinInt16(arr...)
    89  	for _, v := range arr {
    90  		assert.True(t, v <= max)
    91  		assert.True(t, v >= min)
    92  	}
    93  }
    94  
    95  func TestUint16MinMax(t *testing.T) {
    96  	arr := make([]uint16, 1000)
    97  	for i := 0; i < len(arr); i++ {
    98  		arr[i] = uint16(rand.Int())
    99  	}
   100  	max := MaxUint16(arr...)
   101  	min := MinUint16(arr...)
   102  	for _, v := range arr {
   103  		assert.True(t, v <= max)
   104  		assert.True(t, v >= min)
   105  	}
   106  }
   107  
   108  func TestInt32MinMax(t *testing.T) {
   109  	arr := make([]int32, 1000)
   110  	for i := 0; i < len(arr); i++ {
   111  		arr[i] = int32(rand.Int())
   112  	}
   113  	max := MaxInt32(arr...)
   114  	min := MinInt32(arr...)
   115  	for _, v := range arr {
   116  		assert.True(t, v <= max)
   117  		assert.True(t, v >= min)
   118  	}
   119  }
   120  
   121  func TestUint32MinMax(t *testing.T) {
   122  	arr := make([]uint32, 1000)
   123  	for i := 0; i < len(arr); i++ {
   124  		arr[i] = uint32(rand.Int())
   125  	}
   126  	max := MaxUint32(arr...)
   127  	min := MinUint32(arr...)
   128  	for _, v := range arr {
   129  		assert.True(t, v <= max)
   130  		assert.True(t, v >= min)
   131  	}
   132  }
   133  
   134  func TestInt64MinMax(t *testing.T) {
   135  	arr := make([]int64, 1000)
   136  	for i := 0; i < len(arr); i++ {
   137  		arr[i] = int64(rand.Int())
   138  	}
   139  	max := MaxInt64(arr...)
   140  	min := MinInt64(arr...)
   141  	for _, v := range arr {
   142  		assert.True(t, v <= max)
   143  		assert.True(t, v >= min)
   144  	}
   145  }
   146  
   147  func TestUint64MinMax(t *testing.T) {
   148  	arr := make([]uint64, 1000)
   149  	for i := 0; i < len(arr); i++ {
   150  		arr[i] = uint64(rand.Int())
   151  	}
   152  	max := MaxUint64(arr...)
   153  	min := MinUint64(arr...)
   154  	for _, v := range arr {
   155  		assert.True(t, v <= max)
   156  		assert.True(t, v >= min)
   157  	}
   158  }
   159  
   160  func TestFloat32MinMax(t *testing.T) {
   161  	arr := make([]float32, 1000)
   162  	for i := 0; i < len(arr); i++ {
   163  		arr[i] = float32(rand.Int())
   164  	}
   165  	max := MaxFloat32(arr...)
   166  	min := MinFloat32(arr...)
   167  	for _, v := range arr {
   168  		assert.True(t, v <= max)
   169  		assert.True(t, v >= min)
   170  	}
   171  }
   172  
   173  func TestFloat64MinMax(t *testing.T) {
   174  	arr := make([]float64, 1000)
   175  	for i := 0; i < len(arr); i++ {
   176  		arr[i] = float64(rand.Int())
   177  	}
   178  	max := MaxFloat64(arr...)
   179  	min := MinFloat64(arr...)
   180  	for _, v := range arr {
   181  		assert.True(t, v <= max)
   182  		assert.True(t, v >= min)
   183  	}
   184  }
   185  
   186  func BenchmarkMinMax(b *testing.B) {
   187  	var i1 = rand.Float64()
   188  	var i2 = rand.Float64()
   189  
   190  	b.ResetTimer()
   191  	b.ReportAllocs()
   192  	for i := 0; i < b.N; i++ {
   193  		MinFloat64(i1, i2)
   194  		MaxFloat64(i1, i2)
   195  	}
   196  }
   197  
   198  func BenchmarkMathMinMax(b *testing.B) {
   199  	var i1 = rand.Float64()
   200  	var i2 = rand.Float64()
   201  
   202  	b.ResetTimer()
   203  	b.ReportAllocs()
   204  	for i := 0; i < b.N; i++ {
   205  		math.Min(i1, i2)
   206  		math.Max(i1, i2)
   207  	}
   208  }
   209  
   210  // BenchmarkMinMax-12        	218639050	         5.09 ns/op	       0 B/op	       0 allocs/op
   211  // BenchmarkMathMinMax-12    	267942950	         4.54 ns/op	       0 B/op	       0 allocs/op