github.com/PandaGoAdmin/utils@v0.0.0-20211208134815-d5461603a00f/number_test.go (about)

     1  package kgo
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"math"
     6  	"testing"
     7  )
     8  
     9  func TestNumber_NumberFormat(t *testing.T) {
    10  	var res string
    11  
    12  	res = KNum.NumberFormat(floNum1, 10, ".", "")
    13  	assert.Equal(t, "12345.1234567890", res)
    14  
    15  	res = KNum.NumberFormat(floNum2, 6, ".", ",")
    16  	assert.Equal(t, "12,345,678.123457", res)
    17  
    18  	res = KNum.NumberFormat(floNum3, 0, ".", "")
    19  	assert.Equal(t, "-123", res)
    20  
    21  	res = KNum.NumberFormat(math.Pi, 15, ".", "")
    22  	assert.Equal(t, "3.141592653589793", res)
    23  }
    24  
    25  func BenchmarkNumber_NumberFormat(b *testing.B) {
    26  	b.ResetTimer()
    27  	for i := 0; i < b.N; i++ {
    28  		KNum.NumberFormat(floNum1, 3, ".", "")
    29  	}
    30  }
    31  
    32  func TestNumber_Range(t *testing.T) {
    33  	var res []int
    34  	var start, end int
    35  
    36  	//升序
    37  	start, end = 1, 5
    38  	res = KNum.Range(start, end)
    39  	assert.Equal(t, 5, len(res))
    40  	assert.Equal(t, start, res[0])
    41  
    42  	//降序
    43  	start, end = 5, 1
    44  	res = KNum.Range(start, end)
    45  	assert.Equal(t, 5, len(res))
    46  	assert.Equal(t, start, res[0])
    47  
    48  	//起始和结尾相同
    49  	start, end = 3, 3
    50  	res = KNum.Range(start, end)
    51  	assert.Equal(t, 1, len(res))
    52  	assert.Equal(t, start, res[0])
    53  }
    54  
    55  func BenchmarkNumber_Range(b *testing.B) {
    56  	b.ResetTimer()
    57  	for i := 0; i < b.N; i++ {
    58  		KNum.Range(0, 9)
    59  	}
    60  }
    61  
    62  func TestNumber_AbsFloat(t *testing.T) {
    63  	var res float64
    64  
    65  	res = KNum.AbsFloat(floNum3)
    66  	assert.Greater(t, res, 0.0)
    67  }
    68  
    69  func BenchmarkNumber_AbsFloat(b *testing.B) {
    70  	b.ResetTimer()
    71  	for i := 0; i < b.N; i++ {
    72  		KNum.AbsFloat(floNum3)
    73  	}
    74  }
    75  
    76  func TestNumber_AbsInt(t *testing.T) {
    77  	var res int64
    78  
    79  	res = KNum.AbsInt(-123)
    80  	assert.Greater(t, res, int64(0))
    81  }
    82  
    83  func BenchmarkNumber_AbsInt(b *testing.B) {
    84  	b.ResetTimer()
    85  	for i := 0; i < b.N; i++ {
    86  		KNum.AbsInt(-123)
    87  	}
    88  }
    89  
    90  func TestNumber_FloatEqual(t *testing.T) {
    91  	var res bool
    92  
    93  	//默认小数位
    94  	res = KNum.FloatEqual(floNum1, floNum4)
    95  	assert.True(t, res)
    96  
    97  	res = KNum.FloatEqual(floNum1, floNum4, 0)
    98  	assert.True(t, res)
    99  
   100  	res = KNum.FloatEqual(floNum1, floNum4, 11)
   101  	assert.True(t, res)
   102  
   103  	res = KNum.FloatEqual(floNum1, floNum4, 12)
   104  	assert.False(t, res)
   105  }
   106  
   107  func BenchmarkNumber_FloatEqual(b *testing.B) {
   108  	b.ResetTimer()
   109  	for i := 0; i < b.N; i++ {
   110  		KNum.FloatEqual(floNum1, floNum4)
   111  	}
   112  }
   113  
   114  func TestNumber_RandInt64(t *testing.T) {
   115  	var min, max, res int64
   116  
   117  	min, max = -9, 9
   118  	res = KNum.RandInt64(min, max)
   119  	assert.GreaterOrEqual(t, res, min)
   120  	assert.LessOrEqual(t, res, max)
   121  
   122  	//最小最大值调换
   123  	min, max = 9, -9
   124  	res = KNum.RandInt64(min, max)
   125  	assert.GreaterOrEqual(t, res, max)
   126  	assert.LessOrEqual(t, res, min)
   127  
   128  	res = KNum.RandInt64(max, max)
   129  	assert.Equal(t, res, max)
   130  
   131  	KNum.RandInt64(INT64_MIN, INT64_MAX)
   132  }
   133  
   134  func BenchmarkNumber_RandInt64(b *testing.B) {
   135  	b.ResetTimer()
   136  	var min, max int64
   137  	min, max = 9, -9
   138  	for i := 0; i < b.N; i++ {
   139  		KNum.RandInt64(min, max)
   140  	}
   141  }
   142  
   143  func TestNumber_RandInt(t *testing.T) {
   144  	var res int
   145  	min, max := -9, 9
   146  	res = KNum.RandInt(min, max)
   147  	assert.GreaterOrEqual(t, res, min)
   148  	assert.LessOrEqual(t, res, max)
   149  
   150  	//最小最大值调换
   151  	min, max = 9, -9
   152  	res = KNum.RandInt(min, max)
   153  	assert.GreaterOrEqual(t, res, max)
   154  	assert.LessOrEqual(t, res, min)
   155  
   156  	res = KNum.RandInt(max, max)
   157  	assert.Equal(t, res, max)
   158  
   159  	KNum.RandInt(INT_MIN, INT_MAX)
   160  }
   161  
   162  func BenchmarkNumber_RandInt(b *testing.B) {
   163  	b.ResetTimer()
   164  	for i := 0; i < b.N; i++ {
   165  		KNum.RandInt(-9, 9)
   166  	}
   167  }
   168  
   169  func TestNumber_Rand(t *testing.T) {
   170  	var res int
   171  	min, max := -9, 9
   172  	res = KNum.Rand(min, max)
   173  	assert.GreaterOrEqual(t, res, min)
   174  	assert.LessOrEqual(t, res, max)
   175  
   176  	res = KNum.Rand(max, max)
   177  	assert.Equal(t, res, max)
   178  }
   179  
   180  func BenchmarkNumber_Rand(b *testing.B) {
   181  	b.ResetTimer()
   182  	for i := 0; i < b.N; i++ {
   183  		KNum.Rand(-9, 9)
   184  	}
   185  }
   186  
   187  func TestNumber_RandFloat64(t *testing.T) {
   188  	var res float64
   189  
   190  	min, max := floNum3, floNum1
   191  	res = KNum.RandFloat64(min, max)
   192  	assert.GreaterOrEqual(t, res, min)
   193  	assert.LessOrEqual(t, res, max)
   194  
   195  	//最小最大值调换
   196  	min, max = floNum1, floNum3
   197  	res = KNum.RandFloat64(min, max)
   198  	assert.GreaterOrEqual(t, res, max)
   199  	assert.LessOrEqual(t, res, min)
   200  
   201  	KNum.RandFloat64(max, max)
   202  	KNum.RandFloat64(-math.MaxFloat64, math.MaxFloat64)
   203  }
   204  
   205  func BenchmarkNumber_RandFloat64(b *testing.B) {
   206  	b.ResetTimer()
   207  	min, max := floNum3, floNum1
   208  	for i := 0; i < b.N; i++ {
   209  		KNum.RandFloat64(min, max)
   210  	}
   211  }
   212  
   213  func TestNumber_Round(t *testing.T) {
   214  	var tests = []struct {
   215  		num      float64
   216  		expected int
   217  	}{
   218  		{0.3, 0},
   219  		{0.6, 1},
   220  		{1.55, 2},
   221  		{-2.4, -2},
   222  		{-3.6, -4},
   223  	}
   224  	for _, test := range tests {
   225  		actual := KNum.Round(test.num)
   226  		assert.Equal(t, test.expected, int(actual))
   227  	}
   228  }
   229  
   230  func BenchmarkNumber_Round(b *testing.B) {
   231  	b.ResetTimer()
   232  	for i := 0; i < b.N; i++ {
   233  		KNum.Round(floNum3)
   234  	}
   235  }
   236  
   237  func TestNumber_RoundPlus(t *testing.T) {
   238  	var res float64
   239  
   240  	res = KNum.RoundPlus(floNum1, 2)
   241  	assert.True(t, KNum.FloatEqual(res, 12345.12, 2))
   242  
   243  	res = KNum.RoundPlus(floNum1, 4)
   244  	assert.True(t, KNum.FloatEqual(res, 12345.1235, 4))
   245  
   246  	res = KNum.RoundPlus(floNum3, 4)
   247  	assert.True(t, KNum.FloatEqual(res, -123.4568, 4))
   248  }
   249  
   250  func BenchmarkNumber_RoundPlus(b *testing.B) {
   251  	b.ResetTimer()
   252  	for i := 0; i < b.N; i++ {
   253  		KNum.RoundPlus(floNum1, 2)
   254  	}
   255  }
   256  
   257  func TestNumber_Floor(t *testing.T) {
   258  	var res float64
   259  
   260  	res = KNum.Floor(flPi2)
   261  	assert.Equal(t, int(res), 3)
   262  
   263  	res = KNum.Floor(float64(floSpeedLight))
   264  	assert.Equal(t, int(res), 2)
   265  
   266  	res = KNum.Floor(floNum3)
   267  	assert.Equal(t, int(res), -124)
   268  }
   269  
   270  func BenchmarkNumber_Floor(b *testing.B) {
   271  	b.ResetTimer()
   272  	for i := 0; i < b.N; i++ {
   273  		KNum.Floor(flPi2)
   274  	}
   275  }
   276  
   277  func TestNumber_Ceil(t *testing.T) {
   278  	var res float64
   279  
   280  	res = KNum.Ceil(flPi2)
   281  	assert.Equal(t, int(res), 4)
   282  
   283  	res = KNum.Ceil(float64(floSpeedLight))
   284  	assert.Equal(t, int(res), 3)
   285  
   286  	res = KNum.Ceil(floNum3)
   287  	assert.Equal(t, int(res), -123)
   288  }
   289  
   290  func BenchmarkNumber_Ceil(b *testing.B) {
   291  	b.ResetTimer()
   292  	for i := 0; i < b.N; i++ {
   293  		KNum.Ceil(flPi2)
   294  	}
   295  }
   296  
   297  func TestNumber_MaxInt(t *testing.T) {
   298  	defer func() {
   299  		r := recover()
   300  		assert.NotEmpty(t, r)
   301  	}()
   302  
   303  	var res int
   304  
   305  	res = KNum.MaxInt(intSlc...)
   306  	assert.Equal(t, res, 15)
   307  
   308  	//无输入
   309  	KNum.MaxInt()
   310  }
   311  
   312  func BenchmarkNumber_MaxInt(b *testing.B) {
   313  	b.ResetTimer()
   314  	for i := 0; i < b.N; i++ {
   315  		KNum.MaxInt(intSlc...)
   316  	}
   317  }
   318  
   319  func TestNumber_MaxFloat64(t *testing.T) {
   320  	defer func() {
   321  		r := recover()
   322  		assert.NotEmpty(t, r)
   323  	}()
   324  
   325  	var res float64
   326  
   327  	res = KNum.MaxFloat64(flo64Slc...)
   328  	assert.Equal(t, res, floAvogadro)
   329  
   330  	//无输入
   331  	KNum.MaxFloat64()
   332  }
   333  
   334  func BenchmarkNumber_MaxFloat64(b *testing.B) {
   335  	b.ResetTimer()
   336  	for i := 0; i < b.N; i++ {
   337  		KNum.MaxFloat64(flo64Slc...)
   338  	}
   339  }
   340  
   341  func TestNumber_Max(t *testing.T) {
   342  	defer func() {
   343  		r := recover()
   344  		assert.NotEmpty(t, r)
   345  	}()
   346  
   347  	var res float64
   348  
   349  	res = KNum.Max(slItf...)
   350  	assert.Equal(t, res, floAvogadro)
   351  
   352  	//非数值输入
   353  	res = KNum.Max(strHello, admTesDir)
   354  	assert.Equal(t, res, 0.0)
   355  
   356  	//无输入
   357  	KNum.Max()
   358  }
   359  
   360  func BenchmarkNumber_Max(b *testing.B) {
   361  	b.ResetTimer()
   362  	for i := 0; i < b.N; i++ {
   363  		KNum.Max(slItf...)
   364  	}
   365  }
   366  
   367  func TestNumber_MinInt(t *testing.T) {
   368  	defer func() {
   369  		r := recover()
   370  		assert.NotEmpty(t, r)
   371  	}()
   372  
   373  	var res int
   374  
   375  	res = KNum.MinInt(intSlc...)
   376  	assert.Equal(t, res, 0)
   377  
   378  	//无输入
   379  	KNum.MinInt()
   380  }
   381  
   382  func BenchmarkNumber_MinInt(b *testing.B) {
   383  	b.ResetTimer()
   384  	for i := 0; i < b.N; i++ {
   385  		KNum.MinInt(intSlc...)
   386  	}
   387  }
   388  
   389  func TestNumber_MinFloat64(t *testing.T) {
   390  	defer func() {
   391  		r := recover()
   392  		assert.NotEmpty(t, r)
   393  	}()
   394  
   395  	var res float64
   396  
   397  	res = KNum.MinFloat64(flo64Slc...)
   398  	assert.Equal(t, res, floPlanck)
   399  
   400  	//无输入
   401  	KNum.MinFloat64()
   402  }
   403  
   404  func BenchmarkNumber_MinFloat64(b *testing.B) {
   405  	b.ResetTimer()
   406  	for i := 0; i < b.N; i++ {
   407  		KNum.MinFloat64(flo64Slc...)
   408  	}
   409  }
   410  
   411  func TestNumber_Min(t *testing.T) {
   412  	defer func() {
   413  		r := recover()
   414  		assert.NotEmpty(t, r)
   415  	}()
   416  
   417  	var res float64
   418  
   419  	res = KNum.Min(slItf...)
   420  	assert.Equal(t, res, floNum3)
   421  
   422  	//非数值输入
   423  	res = KNum.Min(strHello, admTesDir)
   424  	assert.Equal(t, res, 0.0)
   425  
   426  	//无输入
   427  	KNum.Min()
   428  }
   429  
   430  func BenchmarkNumber_Min(b *testing.B) {
   431  	b.ResetTimer()
   432  	for i := 0; i < b.N; i++ {
   433  		KNum.Min(slItf...)
   434  	}
   435  }
   436  
   437  func TestNumber_Exp(t *testing.T) {
   438  	var res float64
   439  
   440  	res = KNum.Exp(1.1)
   441  	assert.Greater(t, res, 1.0)
   442  }
   443  
   444  func BenchmarkNumber_Exp(b *testing.B) {
   445  	b.ResetTimer()
   446  	for i := 0; i < b.N; i++ {
   447  		KNum.Exp(1.1)
   448  	}
   449  }
   450  
   451  func TestNumber_Expm1(t *testing.T) {
   452  	var res float64
   453  
   454  	res = KNum.Expm1(1.1)
   455  	assert.Greater(t, res, 0.0)
   456  }
   457  
   458  func BenchmarkNumber_Expm1(b *testing.B) {
   459  	b.ResetTimer()
   460  	for i := 0; i < b.N; i++ {
   461  		KNum.Expm1(1.1)
   462  	}
   463  }
   464  
   465  func TestNumber_Pow(t *testing.T) {
   466  	var res float64
   467  
   468  	res = KNum.Pow(10, 2)
   469  	assert.Equal(t, res, 100.0)
   470  }
   471  
   472  func BenchmarkNumber_Equal(b *testing.B) {
   473  	b.ResetTimer()
   474  	for i := 0; i < b.N; i++ {
   475  		KNum.Pow(10, 2)
   476  	}
   477  }
   478  
   479  func TestNumber_Log(t *testing.T) {
   480  	var res float64
   481  
   482  	res = KNum.Log(100, 10)
   483  	assert.Equal(t, res, 2.0)
   484  }
   485  
   486  func BenchmarkNumber_Log(b *testing.B) {
   487  	b.ResetTimer()
   488  	for i := 0; i < b.N; i++ {
   489  		KNum.Log(100, 10)
   490  	}
   491  }
   492  
   493  func TestNumber_ByteFormat(t *testing.T) {
   494  	var res string
   495  
   496  	res = KNum.ByteFormat(0, 0, "")
   497  	assert.Equal(t, res, "0B")
   498  
   499  	res = KNum.ByteFormat(floNum5, 4, " ")
   500  	assert.NotEmpty(t, res)
   501  
   502  	res = KNum.ByteFormat(floNum6, 4, "")
   503  	assert.Contains(t, res, Unknown)
   504  }
   505  
   506  func BenchmarkNumber_ByteFormat(b *testing.B) {
   507  	b.ResetTimer()
   508  	for i := 0; i < b.N; i++ {
   509  		KNum.ByteFormat(floNum6, 4, "")
   510  	}
   511  }
   512  
   513  func TestNumber_IsOdd(t *testing.T) {
   514  	var tests = []struct {
   515  		num      int
   516  		expected bool
   517  	}{
   518  		{-4, false},
   519  		{-1, true},
   520  		{0, false},
   521  		{3, true},
   522  	}
   523  
   524  	var actual bool
   525  	for _, test := range tests {
   526  		actual = KNum.IsOdd(test.num)
   527  		assert.Equal(t, actual, test.expected)
   528  	}
   529  }
   530  
   531  func BenchmarkNumber_IsOdd(b *testing.B) {
   532  	b.ResetTimer()
   533  	for i := 0; i < b.N; i++ {
   534  		KNum.IsOdd(1)
   535  	}
   536  }
   537  
   538  func TestNumber_IsEven(t *testing.T) {
   539  	var tests = []struct {
   540  		num      int
   541  		expected bool
   542  	}{
   543  		{-4, true},
   544  		{-1, false},
   545  		{0, true},
   546  		{3, false},
   547  	}
   548  
   549  	var actual bool
   550  	for _, test := range tests {
   551  		actual = KNum.IsEven(test.num)
   552  		assert.Equal(t, actual, test.expected)
   553  	}
   554  }
   555  
   556  func BenchmarkNumber_IsEven(b *testing.B) {
   557  	b.ResetTimer()
   558  	for i := 0; i < b.N; i++ {
   559  		KNum.IsEven(2)
   560  	}
   561  }
   562  
   563  func TestNumber_NumSign(t *testing.T) {
   564  	var tests = []struct {
   565  		num      float64
   566  		expected int8
   567  	}{
   568  		{0, 0},
   569  		{floNum1, 1},
   570  		{math.Pi, 1},
   571  		{floNum3, -1},
   572  		{floNum7, -1},
   573  	}
   574  	var actual int8
   575  	for _, test := range tests {
   576  		actual = KNum.NumSign(test.num)
   577  		assert.Equal(t, actual, test.expected)
   578  	}
   579  }
   580  
   581  func TestNumber_IsNegative(t *testing.T) {
   582  	var tests = []struct {
   583  		num      float64
   584  		expected bool
   585  	}{
   586  		{0, false},
   587  		{floNum1, false},
   588  		{math.Pi, false},
   589  		{floNum3, true},
   590  		{floNum7, true},
   591  	}
   592  	var actual bool
   593  	for _, test := range tests {
   594  		actual = KNum.IsNegative(test.num)
   595  		assert.Equal(t, actual, test.expected)
   596  	}
   597  }
   598  
   599  func BenchmarkNumber_IsNegative(b *testing.B) {
   600  	b.ResetTimer()
   601  	for i := 0; i < b.N; i++ {
   602  		KNum.IsNegative(floNum1)
   603  	}
   604  }
   605  
   606  func TestNumber_IsPositive(t *testing.T) {
   607  	var tests = []struct {
   608  		num      float64
   609  		expected bool
   610  	}{
   611  		{0, false},
   612  		{floNum1, true},
   613  		{math.Pi, true},
   614  		{floNum3, false},
   615  		{floNum7, false},
   616  	}
   617  	var actual bool
   618  	for _, test := range tests {
   619  		actual = KNum.IsPositive(test.num)
   620  		assert.Equal(t, actual, test.expected)
   621  	}
   622  }
   623  
   624  func BenchmarkNumber_IsPositive(b *testing.B) {
   625  	b.ResetTimer()
   626  	for i := 0; i < b.N; i++ {
   627  		KNum.IsPositive(floNum1)
   628  	}
   629  }
   630  
   631  func TestNumber_IsNonNegative(t *testing.T) {
   632  	var tests = []struct {
   633  		num      float64
   634  		expected bool
   635  	}{
   636  		{0, true},
   637  		{floNum1, true},
   638  		{math.Pi, true},
   639  		{floNum3, false},
   640  		{floNum7, false},
   641  	}
   642  	var actual bool
   643  	for _, test := range tests {
   644  		actual = KNum.IsNonNegative(test.num)
   645  		assert.Equal(t, actual, test.expected)
   646  	}
   647  }
   648  
   649  func BenchmarkNumber_IsNonNegative(b *testing.B) {
   650  	b.ResetTimer()
   651  	for i := 0; i < b.N; i++ {
   652  		KNum.IsNonNegative(floNum1)
   653  	}
   654  }
   655  
   656  func TestNumber_IsNonPositive(t *testing.T) {
   657  	var tests = []struct {
   658  		num      float64
   659  		expected bool
   660  	}{
   661  		{0, true},
   662  		{floNum1, false},
   663  		{math.Pi, false},
   664  		{floNum3, true},
   665  		{floNum7, true},
   666  	}
   667  	var actual bool
   668  	for _, test := range tests {
   669  		actual = KNum.IsNonPositive(test.num)
   670  		assert.Equal(t, actual, test.expected)
   671  	}
   672  }
   673  
   674  func BenchmarkNumber_IsNonPositive(b *testing.B) {
   675  	b.ResetTimer()
   676  	for i := 0; i < b.N; i++ {
   677  		KNum.IsNonPositive(floNum1)
   678  	}
   679  }
   680  
   681  func TestNumber_IsWhole(t *testing.T) {
   682  	var tests = []struct {
   683  		num      float64
   684  		expected bool
   685  	}{
   686  		{0, true},
   687  		{10, true},
   688  		{math.Pi, false},
   689  		{floNum3, false},
   690  		{floNum7, false},
   691  	}
   692  	var actual bool
   693  	for _, test := range tests {
   694  		actual = KNum.IsWhole(test.num)
   695  		assert.Equal(t, actual, test.expected)
   696  	}
   697  }
   698  
   699  func BenchmarkNumber_IsWhole(b *testing.B) {
   700  	b.ResetTimer()
   701  	for i := 0; i < b.N; i++ {
   702  		KNum.IsWhole(floNum5)
   703  	}
   704  }
   705  
   706  func TestNumber_IsNatural(t *testing.T) {
   707  	var tests = []struct {
   708  		num      float64
   709  		expected bool
   710  	}{
   711  		{0, true},
   712  		{10, true},
   713  		{-1, false},
   714  		{math.Pi, false},
   715  		{floNum3, false},
   716  		{floNum7, false},
   717  	}
   718  	var actual bool
   719  	for _, test := range tests {
   720  		actual = KNum.IsNatural(test.num)
   721  		assert.Equal(t, actual, test.expected)
   722  	}
   723  }
   724  
   725  func BenchmarkNumber_IsNatural(b *testing.B) {
   726  	b.ResetTimer()
   727  	for i := 0; i < b.N; i++ {
   728  		KNum.IsNatural(9)
   729  	}
   730  }
   731  
   732  func TestNumber_InRangeInt(t *testing.T) {
   733  	var testAsInts = []struct {
   734  		num      int
   735  		left     int
   736  		right    int
   737  		expected bool
   738  	}{
   739  		{0, 0, 0, true},
   740  		{1, 0, 0, false},
   741  		{-1, 0, 0, false},
   742  		{0, -1, 1, true},
   743  		{0, 0, 1, true},
   744  		{0, -1, 0, true},
   745  		{0, 0, -1, true},
   746  		{0, 10, 5, false},
   747  		{1, 0, 5, true},
   748  	}
   749  	var actual bool
   750  	for _, test := range testAsInts {
   751  		actual = KNum.InRangeInt(test.num, test.left, test.right)
   752  		assert.Equal(t, actual, test.expected)
   753  	}
   754  }
   755  
   756  func BenchmarkNumber_InRangeInt(b *testing.B) {
   757  	b.ResetTimer()
   758  	for i := 0; i < b.N; i++ {
   759  		KNum.InRangeInt(5, 1, 9)
   760  	}
   761  }
   762  
   763  func TestNumber_InRangeFloat64(t *testing.T) {
   764  	var testAsInts = []struct {
   765  		num      float64
   766  		left     float64
   767  		right    float64
   768  		expected bool
   769  	}{
   770  		{0, 0, 0, true},
   771  		{1, 0, 0, false},
   772  		{-1, 0, 0, false},
   773  		{0, -1, 1, true},
   774  		{0, 0, 1, true},
   775  		{0, -1, 0, true},
   776  		{0, 0, -1, true},
   777  		{0, 10, 5, false},
   778  		{1, 0, 5, true},
   779  	}
   780  	var actual bool
   781  	for _, test := range testAsInts {
   782  		actual = KNum.InRangeFloat64(test.num, test.left, test.right)
   783  		assert.Equal(t, actual, test.expected)
   784  	}
   785  }
   786  
   787  func BenchmarkNumber_InRangeFloat64(b *testing.B) {
   788  	b.ResetTimer()
   789  	for i := 0; i < b.N; i++ {
   790  		KNum.InRangeFloat64(5, 1, 9)
   791  	}
   792  }
   793  
   794  func TestNumber_InRangeFloat32(t *testing.T) {
   795  	var testAsInts = []struct {
   796  		num      float32
   797  		left     float32
   798  		right    float32
   799  		expected bool
   800  	}{
   801  		{0, 0, 0, true},
   802  		{1, 0, 0, false},
   803  		{-1, 0, 0, false},
   804  		{0, -1, 1, true},
   805  		{0, 0, 1, true},
   806  		{0, -1, 0, true},
   807  		{0, 0, -1, true},
   808  		{0, 10, 5, false},
   809  		{1, 0, 5, true},
   810  	}
   811  	var actual bool
   812  	for _, test := range testAsInts {
   813  		actual = KNum.InRangeFloat32(test.num, test.left, test.right)
   814  		assert.Equal(t, actual, test.expected)
   815  	}
   816  }
   817  
   818  func BenchmarkNumber_InRangeFloat32(b *testing.B) {
   819  	b.ResetTimer()
   820  	for i := 0; i < b.N; i++ {
   821  		KNum.InRangeFloat32(5, 1, 9)
   822  	}
   823  }
   824  
   825  func TestNumber_InRange(t *testing.T) {
   826  	var actual bool
   827  
   828  	//int
   829  	var testsInt = []struct {
   830  		num      int
   831  		left     int
   832  		right    int
   833  		expected bool
   834  	}{
   835  		{0, 0, 0, true},
   836  		{1, 0, 0, false},
   837  		{-1, 0, 0, false},
   838  		{0, -1, 1, true},
   839  		{0, 0, 1, true},
   840  		{0, -1, 0, true},
   841  		{0, 0, -1, true},
   842  		{0, 10, 5, false},
   843  	}
   844  	for _, test := range testsInt {
   845  		actual = KNum.InRange(test.num, test.left, test.right)
   846  		assert.Equal(t, actual, test.expected)
   847  	}
   848  
   849  	//float32
   850  	var testsFloat32 = []struct {
   851  		num      float32
   852  		left     float32
   853  		right    float32
   854  		expected bool
   855  	}{
   856  		{0, 0, 0, true},
   857  		{1, 0, 0, false},
   858  		{-1, 0, 0, false},
   859  		{0, -1, 1, true},
   860  		{0, 0, 1, true},
   861  		{0, -1, 0, true},
   862  		{0, 0, -1, true},
   863  		{0, 10, 5, false},
   864  	}
   865  	for _, test := range testsFloat32 {
   866  		actual = KNum.InRange(test.num, test.left, test.right)
   867  		assert.Equal(t, actual, test.expected)
   868  	}
   869  
   870  	//float64
   871  	var testsFloat64 = []struct {
   872  		num      float64
   873  		left     float64
   874  		right    float64
   875  		expected bool
   876  	}{
   877  		{0, 0, 0, true},
   878  		{1, 0, 0, false},
   879  		{-1, 0, 0, false},
   880  		{0, -1, 1, true},
   881  		{0, 0, 1, true},
   882  		{0, -1, 0, true},
   883  		{0, 0, -1, true},
   884  		{0, 10, 5, false},
   885  	}
   886  	for _, test := range testsFloat64 {
   887  		actual = KNum.InRange(test.num, test.left, test.right)
   888  		assert.Equal(t, actual, test.expected)
   889  	}
   890  
   891  	//mix
   892  	var testsTypeMix = []struct {
   893  		num      int
   894  		left     float64
   895  		right    float64
   896  		expected bool
   897  	}{
   898  		{0, 0, 0, true},
   899  		{1, 0, 0, false},
   900  		{-1, 0, 0, false},
   901  		{0, -1, 1, true},
   902  		{0, 0, 1, true},
   903  		{0, -1, 0, true},
   904  		{0, 0, -1, true},
   905  		{0, 10, 5, false},
   906  	}
   907  	for _, test := range testsTypeMix {
   908  		actual = KNum.InRange(test.num, test.left, test.right)
   909  		assert.Equal(t, actual, test.expected)
   910  	}
   911  
   912  	//other
   913  	KNum.InRange("1", 0, 3)
   914  	KNum.InRange("hello", []byte{}, 3)
   915  }
   916  
   917  func BenchmarkNumber_InRange(b *testing.B) {
   918  	b.ResetTimer()
   919  	for i := 0; i < b.N; i++ {
   920  		KNum.InRange(89, -1.2, 999.123)
   921  	}
   922  }
   923  
   924  func TestNumber_SumInt(t *testing.T) {
   925  	var res int
   926  
   927  	res = KNum.SumInt(naturalArr[:]...)
   928  	assert.Equal(t, res, 55)
   929  }
   930  
   931  func BenchmarkNumber_SumInt(b *testing.B) {
   932  	b.ResetTimer()
   933  	for i := 0; i < b.N; i++ {
   934  		KNum.SumInt(intSlc...)
   935  	}
   936  }
   937  
   938  func TestNumber_SumFloat64(t *testing.T) {
   939  	var res float64
   940  
   941  	res = KNum.SumFloat64(flo64Slc2...)
   942  	assert.Equal(t, "12370248.06", KNum.NumberFormat(res, 2, ".", ""))
   943  }
   944  
   945  func BenchmarkNumber_SumFloat64(b *testing.B) {
   946  	b.ResetTimer()
   947  	for i := 0; i < b.N; i++ {
   948  		KNum.SumFloat64(flo64Slc2...)
   949  	}
   950  }
   951  
   952  func TestNumber_Sum(t *testing.T) {
   953  	var res float64
   954  	var nums []interface{}
   955  
   956  	nums = KArr.ArrayShuffle(naturalArr)
   957  	res = KNum.Sum(nums...)
   958  	assert.Equal(t, res, 55.0)
   959  
   960  	nums = KArr.ArrayShuffle(flo64Slc2)
   961  	res = KNum.Sum(nums...)
   962  	assert.Equal(t, "12370248.06", KNum.NumberFormat(res, 2, ".", ""))
   963  
   964  	//any
   965  	res = KNum.Sum(slItf2...)
   966  	assert.Equal(t, "3.20", KNum.NumberFormat(res, 2, ".", ""))
   967  }
   968  
   969  func BenchmarkNumber_Sum(b *testing.B) {
   970  	b.ResetTimer()
   971  	nums := KArr.ArrayShuffle(naturalArr)
   972  	for i := 0; i < b.N; i++ {
   973  		KNum.Sum(nums...)
   974  	}
   975  }
   976  
   977  func TestNumber_AverageInt(t *testing.T) {
   978  	var res float64
   979  
   980  	res = KNum.AverageInt()
   981  	assert.Equal(t, 0.0, res)
   982  
   983  	res = KNum.AverageInt(intTen)
   984  	assert.Equal(t, float64(intTen), res)
   985  
   986  	res = KNum.AverageInt(naturalArr[:]...)
   987  	assert.Equal(t, 5.0, res)
   988  }
   989  
   990  func BenchmarkNumber_AverageInt(b *testing.B) {
   991  	b.ResetTimer()
   992  	for i := 0; i < b.N; i++ {
   993  		KNum.AverageInt(intSlc...)
   994  	}
   995  }
   996  
   997  func TestNumber_AverageFloat64(t *testing.T) {
   998  	var res float64
   999  
  1000  	res = KNum.AverageFloat64()
  1001  	assert.Equal(t, 0.0, res)
  1002  
  1003  	res = KNum.AverageFloat64(floTen)
  1004  	assert.Equal(t, floTen, res)
  1005  
  1006  	res = KNum.AverageFloat64(flo64Slc2...)
  1007  	assert.Equal(t, "2474049.61", KNum.NumberFormat(res, 2, ".", ""))
  1008  }
  1009  
  1010  func BenchmarkNumber_AverageFloat64(b *testing.B) {
  1011  	b.ResetTimer()
  1012  	for i := 0; i < b.N; i++ {
  1013  		KNum.AverageFloat64(flo64Slc2...)
  1014  	}
  1015  }
  1016  
  1017  func TestNumber_Average(t *testing.T) {
  1018  	var res float64
  1019  	var nums []interface{}
  1020  
  1021  	res = KNum.Average()
  1022  	assert.Equal(t, 0.0, res)
  1023  
  1024  	res = KNum.Average(floTen)
  1025  	assert.Equal(t, floTen, res)
  1026  
  1027  	nums = KArr.ArrayShuffle(naturalArr)
  1028  	res = KNum.Average(nums...)
  1029  	assert.Equal(t, 5.0, res)
  1030  
  1031  	nums = KArr.ArrayShuffle(flo64Slc2)
  1032  	res = KNum.Average(nums...)
  1033  	assert.Equal(t, "2474049.61", KNum.NumberFormat(res, 2, ".", ""))
  1034  
  1035  	//any
  1036  	res = KNum.Average(slItf2...)
  1037  	assert.Equal(t, "0.46", KNum.NumberFormat(res, 2, ".", ""))
  1038  }
  1039  
  1040  func BenchmarkNumber_Average(b *testing.B) {
  1041  	b.ResetTimer()
  1042  	for i := 0; i < b.N; i++ {
  1043  		KNum.Average(slItf2...)
  1044  	}
  1045  }
  1046  
  1047  func TestNumber_Percent(t *testing.T) {
  1048  	var actual float64
  1049  	tests := []struct {
  1050  		val      interface{}
  1051  		total    interface{}
  1052  		expected float64
  1053  	}{
  1054  		{0, "", 0.0},
  1055  		{0, 0, 0.0},
  1056  		{"1", "20", 5.0},
  1057  		{2.5, 10, 25.0},
  1058  		{3.46, 12.24, 28.2679},
  1059  	}
  1060  
  1061  	for _, test := range tests {
  1062  		actual = KNum.Percent(test.val, test.total)
  1063  		assert.True(t, KNum.FloatEqual(actual, test.expected, 4))
  1064  	}
  1065  }
  1066  
  1067  func BenchmarkNumber_Percent(b *testing.B) {
  1068  	b.ResetTimer()
  1069  	for i := 0; i < b.N; i++ {
  1070  		KNum.Percent(1, 2.0)
  1071  	}
  1072  }
  1073  
  1074  func TestNumber_IsNan(t *testing.T) {
  1075  	var actual bool
  1076  
  1077  	var tests = []struct {
  1078  		val      interface{}
  1079  		expected bool
  1080  	}{
  1081  		{math.Acos(1.01), true},
  1082  		{floNum1, false},
  1083  		{0, false},
  1084  		{strPi6, false},
  1085  		{strHello, true},
  1086  		{nil, true},
  1087  		{cmplNum2, true},
  1088  	}
  1089  	for _, test := range tests {
  1090  		actual = KNum.IsNan(test.val)
  1091  		assert.Equal(t, actual, test.expected)
  1092  	}
  1093  }
  1094  
  1095  func BenchmarkNumber_IsNan(b *testing.B) {
  1096  	b.ResetTimer()
  1097  	for i := 0; i < b.N; i++ {
  1098  		KNum.IsNan(cmplNum2)
  1099  	}
  1100  }
  1101  
  1102  func TestNumber_IsNaturalRange(t *testing.T) {
  1103  	var res bool
  1104  	var nums []int
  1105  
  1106  	res = KNum.IsNaturalRange(nums, false)
  1107  	assert.False(t, res)
  1108  
  1109  	nums = []int{1, 2, 3}
  1110  	res = KNum.IsNaturalRange(nums, false)
  1111  	assert.False(t, res)
  1112  
  1113  	nums = []int{0, 1, 2, 3}
  1114  	res = KNum.IsNaturalRange(nums, false)
  1115  	assert.True(t, res)
  1116  	res = KNum.IsNaturalRange(nums, true)
  1117  	assert.True(t, res)
  1118  
  1119  	nums = []int{0, 3, 1, 2}
  1120  	res = KNum.IsNaturalRange(nums, false)
  1121  	assert.True(t, res)
  1122  	res = KNum.IsNaturalRange(nums, true)
  1123  	assert.False(t, res)
  1124  
  1125  	nums = []int{0, 1, 3, 4}
  1126  	res = KNum.IsNaturalRange(nums, false)
  1127  	assert.False(t, res)
  1128  	res = KNum.IsNaturalRange(nums, true)
  1129  	assert.False(t, res)
  1130  }
  1131  
  1132  func BenchmarkNumber_IsNaturalRange(b *testing.B) {
  1133  	b.ResetTimer()
  1134  	for i := 0; i < b.N; i++ {
  1135  		KNum.IsNaturalRange(intSlc, false)
  1136  	}
  1137  }
  1138  
  1139  func TestNumber_GeoDistance(t *testing.T) {
  1140  	var res1, res2 float64
  1141  	var lat1, lng1, lat2, lng2 float64
  1142  
  1143  	lat1, lng1 = 30.0, 45.0
  1144  	lat2, lng2 = 40.0, 90.0
  1145  	res1 = KNum.GeoDistance(lng1, lat1, lng2, lat2)
  1146  	assert.Greater(t, res1, 0.0)
  1147  
  1148  	lat1, lng1 = 390.0, 405.0
  1149  	lat2, lng2 = -320.0, 90.0
  1150  	res2 = KNum.GeoDistance(lng1, lat1, lng2, lat2)
  1151  	assert.Greater(t, res2, 0.0)
  1152  
  1153  	assert.Equal(t, res1, res2)
  1154  }
  1155  
  1156  func BenchmarkNumber_GeoDistance(b *testing.B) {
  1157  	b.ResetTimer()
  1158  	lat1, lng1 := 30.0, 45.0
  1159  	lat2, lng2 := 40.0, 90.0
  1160  	for i := 0; i < b.N; i++ {
  1161  		KNum.GeoDistance(lng1, lat1, lng2, lat2)
  1162  	}
  1163  }
  1164  
  1165  func TestNearLogarithm(t *testing.T) {
  1166  	var actual int
  1167  	var tests = []struct {
  1168  		num      int
  1169  		base     int
  1170  		left     bool
  1171  		expected int
  1172  	}{
  1173  		{10, 2, false, 4},
  1174  		{10, 2, true, 3},
  1175  		{16, 2, true, 4},
  1176  		{16, 2, true, 4},
  1177  	}
  1178  	for _, test := range tests {
  1179  		actual = KNum.NearLogarithm(test.num, test.base, test.left)
  1180  		assert.Equal(t, actual, test.expected)
  1181  	}
  1182  }
  1183  
  1184  func TestNearLogarithm_Panic_Num(t *testing.T) {
  1185  	defer func() {
  1186  		r := recover()
  1187  		assert.Contains(t, r, "num must be non-negative")
  1188  	}()
  1189  	_ = KNum.NearLogarithm(-9, 2, false)
  1190  }
  1191  
  1192  func TestNearLogarithm_Panic_Base(t *testing.T) {
  1193  	defer func() {
  1194  		r := recover()
  1195  		assert.Contains(t, r, "base must be a positive integer")
  1196  	}()
  1197  	_ = KNum.NearLogarithm(9, -2, false)
  1198  }
  1199  
  1200  func BenchmarkNearLogarithm(b *testing.B) {
  1201  	b.ResetTimer()
  1202  	for i := 0; i < b.N; i++ {
  1203  		KNum.NearLogarithm(99, 4, true)
  1204  	}
  1205  }
  1206  
  1207  func TestSplitNaturalNum(t *testing.T) {
  1208  	var actual []int
  1209  	var tests = []struct {
  1210  		num      int
  1211  		base     int
  1212  		expected int
  1213  	}{
  1214  		{10, 2, 2},  //[8 2]
  1215  		{16, 2, 1},  //[16]
  1216  		{56, 3, 3},  //[27 27 2]
  1217  		{199, 6, 9}, //[36 36 36 36 36 6 6 6 1]
  1218  	}
  1219  	for _, test := range tests {
  1220  		actual = KNum.SplitNaturalNum(test.num, test.base)
  1221  		assert.Equal(t, len(actual), test.expected)
  1222  	}
  1223  }
  1224  
  1225  func BenchmarkSplitNaturalNum(b *testing.B) {
  1226  	b.ResetTimer()
  1227  	for i := 0; i < b.N; i++ {
  1228  		KNum.SplitNaturalNum(199, 3)
  1229  	}
  1230  }