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

     1  package kgo
     2  
     3  import (
     4  	"github.com/brianvoe/gofakeit/v6"
     5  	"github.com/stretchr/testify/assert"
     6  	"math"
     7  	"reflect"
     8  	"testing"
     9  )
    10  
    11  func TestConvert_Struct2Map(t *testing.T) {
    12  	//结构体
    13  	var p1 sPerson
    14  	gofakeit.Struct(&p1)
    15  	mp1, _ := KConv.Struct2Map(p1, "json")
    16  	mp2, _ := KConv.Struct2Map(p1, "")
    17  
    18  	var ok bool
    19  
    20  	_, ok = mp1["name"]
    21  	assert.True(t, ok)
    22  
    23  	_, ok = mp1["none"]
    24  	assert.False(t, ok)
    25  
    26  	_, ok = mp2["Age"]
    27  	assert.True(t, ok)
    28  
    29  	_, ok = mp2["none"]
    30  	assert.True(t, ok)
    31  }
    32  
    33  func BenchmarkConvert_Struct2Map_UseTag(b *testing.B) {
    34  	b.ResetTimer()
    35  	var p1 sPerson
    36  	gofakeit.Struct(&p1)
    37  	for i := 0; i < b.N; i++ {
    38  		_, _ = KConv.Struct2Map(p1, "json")
    39  	}
    40  }
    41  
    42  func BenchmarkConvert_Struct2Map_NoTag(b *testing.B) {
    43  	b.ResetTimer()
    44  	var p1 sPerson
    45  	gofakeit.Struct(&p1)
    46  	for i := 0; i < b.N; i++ {
    47  		_, _ = KConv.Struct2Map(p1, "")
    48  	}
    49  }
    50  
    51  func TestConvert_Int2Str(t *testing.T) {
    52  	var res string
    53  
    54  	res = KConv.Int2Str(0)
    55  	assert.NotEmpty(t, res)
    56  
    57  	res = KConv.Int2Str(31.4)
    58  	assert.Empty(t, res)
    59  
    60  	res = KConv.Int2Str(PKCS_SEVEN)
    61  	assert.Equal(t, "7", res)
    62  }
    63  
    64  func BenchmarkConvert_Int2Str(b *testing.B) {
    65  	b.ResetTimer()
    66  	for i := 0; i < b.N; i++ {
    67  		KConv.Int2Str(123456789)
    68  	}
    69  }
    70  
    71  func TestConvert_Float2Str(t *testing.T) {
    72  	var res string
    73  
    74  	//小数位为负数
    75  	res = KConv.Float2Str(flPi1, -2)
    76  	assert.Equal(t, 4, len(res))
    77  
    78  	res = KConv.Float2Str(flPi2, 3)
    79  	assert.Equal(t, 5, len(res))
    80  
    81  	res = KConv.Float2Str(flPi3, 3)
    82  	assert.Equal(t, 5, len(res))
    83  
    84  	res = KConv.Float2Str(flPi4, 9)
    85  	assert.Equal(t, 11, len(res))
    86  
    87  	res = KConv.Float2Str(true, 9)
    88  	assert.Empty(t, res)
    89  }
    90  
    91  func BenchmarkConvert_Float2Str(b *testing.B) {
    92  	b.ResetTimer()
    93  	for i := 0; i < b.N; i++ {
    94  		KConv.Float2Str(flPi2, 3)
    95  	}
    96  }
    97  
    98  func TestConvert_Bool2Str(t *testing.T) {
    99  	var res string
   100  
   101  	res = KConv.Bool2Str(true)
   102  	assert.Equal(t, "true", res)
   103  
   104  	res = KConv.Bool2Str(false)
   105  	assert.Equal(t, "false", res)
   106  }
   107  
   108  func BenchmarkConvert_Bool2Str(b *testing.B) {
   109  	b.ResetTimer()
   110  	for i := 0; i < b.N; i++ {
   111  		KConv.Bool2Str(true)
   112  	}
   113  }
   114  
   115  func TestConvert_Bool2Int(t *testing.T) {
   116  	var res int
   117  
   118  	res = KConv.Bool2Int(true)
   119  	assert.Equal(t, 1, res)
   120  
   121  	res = KConv.Bool2Int(false)
   122  	assert.Equal(t, 0, res)
   123  }
   124  
   125  func BenchmarkConvert_Bool2Int(b *testing.B) {
   126  	b.ResetTimer()
   127  	for i := 0; i < b.N; i++ {
   128  		KConv.Bool2Int(true)
   129  	}
   130  }
   131  
   132  func TestConvert_Str2Int(t *testing.T) {
   133  	var res int
   134  
   135  	res = KConv.Str2Int("123")
   136  	assert.Equal(t, 123, res)
   137  
   138  	res = KConv.Str2Int("TRUE")
   139  	assert.Equal(t, 1, res)
   140  
   141  	res = KConv.Str2Int("")
   142  	assert.Equal(t, 0, res)
   143  
   144  	res = KConv.Str2Int(strHello)
   145  	assert.Equal(t, 0, res)
   146  
   147  	res = KConv.Str2Int("123.456")
   148  	assert.Equal(t, 123, res)
   149  
   150  	res = KConv.Str2Int("123.678")
   151  	assert.Equal(t, 123, res)
   152  }
   153  
   154  func BenchmarkConvert_Str2Int_Bool(b *testing.B) {
   155  	b.ResetTimer()
   156  	for i := 0; i < b.N; i++ {
   157  		KConv.Str2Int("TRUE")
   158  	}
   159  }
   160  
   161  func BenchmarkConvert_Str2Int_Float(b *testing.B) {
   162  	b.ResetTimer()
   163  	for i := 0; i < b.N; i++ {
   164  		KConv.Str2Int("1234.567")
   165  	}
   166  }
   167  
   168  func BenchmarkConvert_Str2Int_Int(b *testing.B) {
   169  	b.ResetTimer()
   170  	for i := 0; i < b.N; i++ {
   171  		KConv.Str2Int("1234567")
   172  	}
   173  }
   174  
   175  func TestConvert_Str2Int8(t *testing.T) {
   176  	var res int8
   177  
   178  	res = KConv.Str2Int8("99")
   179  	assert.Equal(t, int8(99), res)
   180  
   181  	res = KConv.Str2Int8(nowNanoStr)
   182  	assert.Equal(t, int8(127), res)
   183  }
   184  
   185  func BenchmarkConvert_Str2Int8(b *testing.B) {
   186  	b.ResetTimer()
   187  	for i := 0; i < b.N; i++ {
   188  		KConv.Str2Int8("99")
   189  	}
   190  }
   191  
   192  func TestConvert_Str2Int16(t *testing.T) {
   193  	var res int16
   194  
   195  	res = KConv.Str2Int16("99")
   196  	assert.Equal(t, int16(99), res)
   197  
   198  	res = KConv.Str2Int16(nowNanoStr)
   199  	assert.Equal(t, int16(32767), res)
   200  }
   201  
   202  func BenchmarkConvert_Str2Int16(b *testing.B) {
   203  	b.ResetTimer()
   204  	for i := 0; i < b.N; i++ {
   205  		KConv.Str2Int16("99")
   206  	}
   207  }
   208  
   209  func TestConvert_Str2Int32(t *testing.T) {
   210  	var res int32
   211  
   212  	res = KConv.Str2Int32("99")
   213  	assert.Equal(t, int32(99), res)
   214  
   215  	res = KConv.Str2Int32(nowNanoStr)
   216  	assert.Equal(t, int32(2147483647), res)
   217  }
   218  
   219  func BenchmarkConvert_Str2Int32(b *testing.B) {
   220  	b.ResetTimer()
   221  	for i := 0; i < b.N; i++ {
   222  		KConv.Str2Int32("99")
   223  	}
   224  }
   225  
   226  func TestConvert_Str2Int64(t *testing.T) {
   227  	var res int64
   228  
   229  	res = KConv.Str2Int64("99")
   230  	assert.Equal(t, int64(99), res)
   231  
   232  	res = KConv.Str2Int64(nowNanoStr)
   233  	assert.Greater(t, res, int64(2147483648))
   234  }
   235  
   236  func BenchmarkConvert_Str2Int64(b *testing.B) {
   237  	b.ResetTimer()
   238  	for i := 0; i < b.N; i++ {
   239  		KConv.Str2Int64("99")
   240  	}
   241  }
   242  
   243  func TestConvert_Str2Uint(t *testing.T) {
   244  	var res uint
   245  
   246  	res = KConv.Str2Uint("TRUE")
   247  	assert.Equal(t, uint(1), res)
   248  
   249  	res = KConv.Str2Uint("")
   250  	assert.Equal(t, uint(0), res)
   251  
   252  	res = KConv.Str2Uint(strHello)
   253  	assert.Equal(t, uint(0), res)
   254  
   255  	res = KConv.Str2Uint("123.456")
   256  	assert.Equal(t, uint(123), res)
   257  
   258  	//不合法的
   259  	res = KConv.Str2Uint(" 123.456")
   260  	assert.Equal(t, uint(0), res)
   261  
   262  	res = KConv.Str2Uint("123.678")
   263  	assert.Equal(t, uint(123), res)
   264  
   265  	res = KConv.Str2Uint("125")
   266  	assert.Equal(t, uint(125), res)
   267  
   268  	res = KConv.Str2Uint("-125")
   269  	assert.Equal(t, uint(0), res)
   270  }
   271  
   272  func BenchmarkConvert_Str2Uint_Bool(b *testing.B) {
   273  	b.ResetTimer()
   274  	for i := 0; i < b.N; i++ {
   275  		KConv.Str2Uint("TRUE")
   276  	}
   277  }
   278  
   279  func BenchmarkConvert_Str2Uint_Float(b *testing.B) {
   280  	b.ResetTimer()
   281  	for i := 0; i < b.N; i++ {
   282  		KConv.Str2Uint("1234.567")
   283  	}
   284  }
   285  
   286  func BenchmarkConvert_Str2Uint_Int(b *testing.B) {
   287  	b.ResetTimer()
   288  	for i := 0; i < b.N; i++ {
   289  		KConv.Str2Uint("1234567")
   290  	}
   291  }
   292  
   293  func TestConvert_Str2Uint8(t *testing.T) {
   294  	var res uint8
   295  
   296  	res = KConv.Str2Uint8("99")
   297  	assert.Equal(t, uint8(99), res)
   298  
   299  	res = KConv.Str2Uint8(nowNanoStr)
   300  	assert.Equal(t, uint8(255), res)
   301  }
   302  
   303  func BenchmarkConvert_Str2Uint8(b *testing.B) {
   304  	b.ResetTimer()
   305  	for i := 0; i < b.N; i++ {
   306  		KConv.Str2Uint8("99")
   307  	}
   308  }
   309  
   310  func TestConvert_Str2Uint16(t *testing.T) {
   311  	var res uint16
   312  
   313  	res = KConv.Str2Uint16("99")
   314  	assert.Equal(t, uint16(99), res)
   315  
   316  	res = KConv.Str2Uint16(nowNanoStr)
   317  	assert.Equal(t, uint16(65535), res)
   318  }
   319  
   320  func BenchmarkConvert_Str2Uint16(b *testing.B) {
   321  	b.ResetTimer()
   322  	for i := 0; i < b.N; i++ {
   323  		KConv.Str2Uint16("99")
   324  	}
   325  }
   326  
   327  func TestConvert_Str2Uint32(t *testing.T) {
   328  	var res uint32
   329  
   330  	res = KConv.Str2Uint32("99")
   331  	assert.Equal(t, uint32(99), res)
   332  
   333  	res = KConv.Str2Uint32(nowNanoStr)
   334  	assert.Equal(t, uint32(4294967295), res)
   335  }
   336  
   337  func BenchmarkConvert_Str2Uint32(b *testing.B) {
   338  	b.ResetTimer()
   339  	for i := 0; i < b.N; i++ {
   340  		KConv.Str2Uint32("99")
   341  	}
   342  }
   343  
   344  func TestConvert_Str2Uint64(t *testing.T) {
   345  	var res uint64
   346  
   347  	res = KConv.Str2Uint64("99")
   348  	assert.Equal(t, uint64(99), res)
   349  
   350  	res = KConv.Str2Uint64(nowNanoStr)
   351  	assert.Greater(t, res, uint64(4294967295))
   352  }
   353  
   354  func BenchmarkConvert_Str2Uint64(b *testing.B) {
   355  	b.ResetTimer()
   356  	for i := 0; i < b.N; i++ {
   357  		KConv.Str2Uint64("99")
   358  	}
   359  }
   360  
   361  func TestConvert_Str2Float32(t *testing.T) {
   362  	var res float32
   363  
   364  	res = KConv.Str2Float32("true")
   365  	assert.Equal(t, float32(1), res)
   366  
   367  	res = KConv.Str2Float32("")
   368  	assert.Equal(t, float32(0), res)
   369  
   370  	res = KConv.Str2Float32("123.556")
   371  	assert.Equal(t, float32(123.556), res)
   372  }
   373  
   374  func BenchmarkConvert_Str2Float32(b *testing.B) {
   375  	b.ResetTimer()
   376  	for i := 0; i < b.N; i++ {
   377  		KConv.Str2Float32("123.556")
   378  	}
   379  }
   380  
   381  func TestConvert_Str2Float64(t *testing.T) {
   382  	var res float64
   383  
   384  	res = KConv.Str2Float64("true")
   385  	assert.Equal(t, float64(1), res)
   386  
   387  	res = KConv.Str2Float64("")
   388  	assert.Equal(t, float64(0), res)
   389  
   390  	res = KConv.Str2Float64("123.556")
   391  	assert.Equal(t, float64(123.556), res)
   392  }
   393  
   394  func BenchmarkConvert_Str2Float64(b *testing.B) {
   395  	b.ResetTimer()
   396  	for i := 0; i < b.N; i++ {
   397  		KConv.Str2Float64("123.556")
   398  	}
   399  }
   400  
   401  func TestConvert_Str2Bool(t *testing.T) {
   402  	var res bool
   403  
   404  	//true
   405  	res = KConv.Str2Bool("1")
   406  	assert.True(t, res)
   407  
   408  	res = KConv.Str2Bool("t")
   409  	assert.True(t, res)
   410  
   411  	res = KConv.Str2Bool("T")
   412  	assert.True(t, res)
   413  
   414  	res = KConv.Str2Bool("TRUE")
   415  	assert.True(t, res)
   416  
   417  	res = KConv.Str2Bool("true")
   418  	assert.True(t, res)
   419  
   420  	res = KConv.Str2Bool("True")
   421  	assert.True(t, res)
   422  
   423  	//false
   424  	res = KConv.Str2Bool("0")
   425  	assert.False(t, res)
   426  
   427  	res = KConv.Str2Bool("f")
   428  	assert.False(t, res)
   429  
   430  	res = KConv.Str2Bool("F")
   431  	assert.False(t, res)
   432  
   433  	res = KConv.Str2Bool("FALSE")
   434  	assert.False(t, res)
   435  
   436  	res = KConv.Str2Bool("false")
   437  	assert.False(t, res)
   438  
   439  	res = KConv.Str2Bool("False")
   440  	assert.False(t, res)
   441  
   442  	//other
   443  	res = KConv.Str2Bool("2")
   444  	assert.False(t, res)
   445  
   446  	res = KConv.Str2Bool(strHello)
   447  	assert.False(t, res)
   448  }
   449  
   450  func BenchmarkConvert_Str2Bool(b *testing.B) {
   451  	b.ResetTimer()
   452  	for i := 0; i < b.N; i++ {
   453  		KConv.Str2Bool(strHello)
   454  	}
   455  }
   456  
   457  func TestConvert_Str2Bytes(t *testing.T) {
   458  	var res []byte
   459  
   460  	res = KConv.Str2Bytes("")
   461  	assert.Empty(t, res)
   462  
   463  	res = KConv.Str2Bytes(strHello)
   464  	assert.Equal(t, len(strHello), len(res))
   465  }
   466  
   467  func BenchmarkConvert_Str2Bytes(b *testing.B) {
   468  	b.ResetTimer()
   469  	for i := 0; i < b.N; i++ {
   470  		KConv.Str2Bytes(strHello)
   471  	}
   472  }
   473  
   474  func TestConvert_Bytes2Str(t *testing.T) {
   475  	var res string
   476  
   477  	res = KConv.Bytes2Str([]byte{})
   478  	assert.Equal(t, "", res)
   479  
   480  	res = KConv.Bytes2Str(bytsHello)
   481  	assert.NotEmpty(t, res)
   482  }
   483  
   484  func BenchmarkConvert_Bytes2Str(b *testing.B) {
   485  	b.ResetTimer()
   486  	for i := 0; i < b.N; i++ {
   487  		KConv.Bytes2Str(bytsHello)
   488  	}
   489  }
   490  
   491  func TestConvert_Str2BytesUnsafe(t *testing.T) {
   492  	var res []byte
   493  
   494  	res = KConv.Str2BytesUnsafe("")
   495  	assert.Empty(t, res)
   496  
   497  	res = KConv.Str2BytesUnsafe(strHello)
   498  	assert.Equal(t, len(strHello), len(res))
   499  }
   500  
   501  func BenchmarkConvert_Str2BytesUnsafe(b *testing.B) {
   502  	b.ResetTimer()
   503  	for i := 0; i < b.N; i++ {
   504  		KConv.Str2BytesUnsafe(strHello)
   505  	}
   506  }
   507  
   508  func TestConvert_Bytes2StrUnsafe(t *testing.T) {
   509  	var res string
   510  
   511  	res = KConv.Bytes2StrUnsafe([]byte{})
   512  	assert.Equal(t, "", res)
   513  
   514  	res = KConv.Bytes2StrUnsafe(bytsHello)
   515  	assert.NotEmpty(t, res)
   516  }
   517  
   518  func BenchmarkConvert_Bytes2StrUnsafe(b *testing.B) {
   519  	b.ResetTimer()
   520  	for i := 0; i < b.N; i++ {
   521  		KConv.Bytes2StrUnsafe(bytsHello)
   522  	}
   523  }
   524  
   525  func TestConvert_Dec2Bin(t *testing.T) {
   526  	var res string
   527  
   528  	res = KConv.Dec2Bin(8)
   529  	assert.Equal(t, "1000", res)
   530  
   531  	res = KConv.Dec2Bin(16)
   532  	assert.Equal(t, "10000", res)
   533  
   534  	res = KConv.Dec2Bin(16)
   535  }
   536  
   537  func BenchmarkConvert_Dec2Bin(b *testing.B) {
   538  	b.ResetTimer()
   539  	for i := 0; i < b.N; i++ {
   540  		KConv.Dec2Bin(16)
   541  	}
   542  }
   543  
   544  func TestConvert_Bin2Dec(t *testing.T) {
   545  	var res int64
   546  	var err error
   547  
   548  	res, _ = KConv.Bin2Dec("1000")
   549  	assert.Equal(t, int64(8), res)
   550  
   551  	res, _ = KConv.Bin2Dec("10000")
   552  	assert.Equal(t, int64(16), res)
   553  
   554  	//不合法
   555  	_, err = KConv.Bin2Dec(strHello)
   556  	assert.NotNil(t, err)
   557  }
   558  
   559  func BenchmarkConvert_Bin2Dec(b *testing.B) {
   560  	b.ResetTimer()
   561  	for i := 0; i < b.N; i++ {
   562  		_, _ = KConv.Bin2Dec("10000")
   563  	}
   564  }
   565  
   566  func TestConvert_Hex2Bin(t *testing.T) {
   567  	var res string
   568  	var dec int64
   569  	var err error
   570  
   571  	res, err = KConv.Hex2Bin("123abff")
   572  	assert.Nil(t, err)
   573  
   574  	res, err = KConv.Hex2Bin(hexAstronomicalUnit)
   575  	dec, _ = KConv.Bin2Dec(res)
   576  	assert.Equal(t, intAstronomicalUnit, dec)
   577  
   578  	//不合法
   579  	_, err = KConv.Hex2Bin(strHello)
   580  	assert.NotNil(t, err)
   581  }
   582  
   583  func BenchmarkConvert_Hex2Bin(b *testing.B) {
   584  	b.ResetTimer()
   585  	for i := 0; i < b.N; i++ {
   586  		_, _ = KConv.Hex2Bin(hexAstronomicalUnit)
   587  	}
   588  }
   589  
   590  func TestConvert_Bin2Hex(t *testing.T) {
   591  	var res string
   592  	var err error
   593  
   594  	res, err = KConv.Bin2Hex(binAstronomicalUnit)
   595  	assert.Equal(t, hexAstronomicalUnit, res)
   596  
   597  	//不合法
   598  	_, err = KConv.Bin2Hex(strHello)
   599  	assert.NotNil(t, err)
   600  }
   601  
   602  func BenchmarkConvert_Bin2Hex(b *testing.B) {
   603  	b.ResetTimer()
   604  	for i := 0; i < b.N; i++ {
   605  		_, _ = KConv.Bin2Hex(binAstronomicalUnit)
   606  	}
   607  }
   608  
   609  func TestConvert_Dec2Hex(t *testing.T) {
   610  	var res string
   611  
   612  	res = KConv.Dec2Hex(intAstronomicalUnit)
   613  	assert.Equal(t, hexAstronomicalUnit, res)
   614  
   615  	res = KConv.Dec2Hex(0)
   616  	assert.Equal(t, "0", res)
   617  }
   618  
   619  func BenchmarkConvert_Dec2Hex(b *testing.B) {
   620  	b.ResetTimer()
   621  	for i := 0; i < b.N; i++ {
   622  		KConv.Dec2Hex(intAstronomicalUnit)
   623  	}
   624  }
   625  
   626  func TestConvert_Hex2Dec(t *testing.T) {
   627  	var res int64
   628  	var err error
   629  
   630  	res, err = KConv.Hex2Dec(hexAstronomicalUnit)
   631  	assert.Equal(t, intAstronomicalUnit, res)
   632  
   633  	//不合法
   634  	_, err = KConv.Hex2Dec(strHello)
   635  	assert.NotNil(t, err)
   636  }
   637  
   638  func BenchmarkConvert_Hex2Dec(b *testing.B) {
   639  	b.ResetTimer()
   640  	for i := 0; i < b.N; i++ {
   641  		_, _ = KConv.Hex2Dec(hexAstronomicalUnit)
   642  	}
   643  }
   644  
   645  func TestConvert_Dec2Oct(t *testing.T) {
   646  	var res string
   647  
   648  	res = KConv.Dec2Oct(intAstronomicalUnit)
   649  	assert.Equal(t, otcAstronomicalUnit, res)
   650  
   651  	res = KConv.Dec2Oct(0)
   652  	assert.Equal(t, "0", res)
   653  }
   654  
   655  func BenchmarkConvert_Dec2Oct(b *testing.B) {
   656  	b.ResetTimer()
   657  	for i := 0; i < b.N; i++ {
   658  		KConv.Dec2Oct(intAstronomicalUnit)
   659  	}
   660  }
   661  
   662  func TestConvert_Oct2Dec(t *testing.T) {
   663  	var res int64
   664  	var err error
   665  
   666  	res, err = KConv.Oct2Dec(otcAstronomicalUnit)
   667  	assert.Equal(t, intAstronomicalUnit, res)
   668  
   669  	//不合法
   670  	_, err = KConv.Oct2Dec(strHello)
   671  	assert.NotNil(t, err)
   672  }
   673  
   674  func BenchmarkConvert_Oct2Dec(b *testing.B) {
   675  	b.ResetTimer()
   676  	for i := 0; i < b.N; i++ {
   677  		_, _ = KConv.Oct2Dec(otcAstronomicalUnit)
   678  	}
   679  }
   680  
   681  func TestConvert_BaseConvert(t *testing.T) {
   682  	var res string
   683  	var err error
   684  
   685  	res, err = KConv.BaseConvert(toStr(intAstronomicalUnit), 10, 16)
   686  	assert.Equal(t, hexAstronomicalUnit, res)
   687  
   688  	//不合法
   689  	_, err = KConv.BaseConvert(strHello, 10, 8)
   690  	assert.NotNil(t, err)
   691  }
   692  
   693  func BenchmarkConvert_BaseConvert(b *testing.B) {
   694  	b.ResetTimer()
   695  	s := toStr(intAstronomicalUnit)
   696  	for i := 0; i < b.N; i++ {
   697  		_, _ = KConv.BaseConvert(s, 10, 16)
   698  	}
   699  }
   700  
   701  func TestConvert_Ip2Long(t *testing.T) {
   702  	var res uint32
   703  
   704  	res = KConv.Ip2Long(localIp)
   705  	assert.Equal(t, localIpInt, res)
   706  
   707  	res = KConv.Ip2Long(lanIp)
   708  	assert.Equal(t, lanIpInt, res)
   709  
   710  	res = KConv.Ip2Long("")
   711  	assert.Equal(t, uint32(0), res)
   712  }
   713  
   714  func BenchmarkConvert_Ip2Long(b *testing.B) {
   715  	b.ResetTimer()
   716  	for i := 0; i < b.N; i++ {
   717  		KConv.Ip2Long(localIp)
   718  	}
   719  }
   720  
   721  func TestConvert_Long2Ip(t *testing.T) {
   722  	var res string
   723  
   724  	res = KConv.Long2Ip(localIpInt)
   725  	assert.Equal(t, localIp, res)
   726  
   727  	res = KConv.Long2Ip(lanIpInt)
   728  	assert.Equal(t, lanIp, res)
   729  
   730  	res = KConv.Long2Ip(0)
   731  	assert.Equal(t, noneIp, res)
   732  }
   733  
   734  func BenchmarkConvert_Long2Ip(b *testing.B) {
   735  	b.ResetTimer()
   736  	for i := 0; i < b.N; i++ {
   737  		KConv.Long2Ip(localIpInt)
   738  	}
   739  }
   740  
   741  func TestConvert_ToStr(t *testing.T) {
   742  	var tests = []struct {
   743  		input    interface{}
   744  		expected string
   745  	}{
   746  		{nil, ""},
   747  		{true, "true"},
   748  		{strHello, strHello},
   749  		{intSpeedLight, strSpeedLight},
   750  		{localIpInt, "2130706433"},
   751  		{floSpeedLight, "2.99792458"},
   752  		{flPi2, "3.141592456"},
   753  		{fnCb1, "<nil>"},
   754  		{fnPtr1, ""},
   755  		{bytsHello, strHello},
   756  		{int64(INT64_MAX), "9223372036854775807"},
   757  		{uint64(UINT64_MAX), "18446744073709551615"},
   758  		{float32(math.Pi), "3.1415927"},
   759  		{float64(math.Pi), "3.141592653589793"},
   760  		{strMpEmp, "{}"},
   761  		{strMp2, `{"2":"cc","a":"0","b":"2","c":"4","g":"4","h":""}`},
   762  	}
   763  
   764  	for _, test := range tests {
   765  		actual := KConv.ToStr(test.input)
   766  
   767  		if reflect.DeepEqual(test.input, floSpeedLight) { //32位浮点会损失精度
   768  			str := longestSameString("2.99792458", actual)
   769  			assert.Less(t, 5, len(str))
   770  		} else {
   771  			assert.Equal(t, test.expected, actual)
   772  		}
   773  	}
   774  }
   775  
   776  func TestConvert_ToBool(t *testing.T) {
   777  	//并行测试
   778  	t.Parallel()
   779  
   780  	var tests = []struct {
   781  		input    interface{}
   782  		expected bool
   783  	}{
   784  		{int(-1), false},
   785  		{int8(0), false},
   786  		{int16(1), true},
   787  		{int32(2), true},
   788  		{int64(3), true},
   789  		{uint(0), false},
   790  		{uint8(0), false},
   791  		{uint16(0), false},
   792  		{uint32(0), false},
   793  		{uint64(0), false},
   794  		{float32(0), false},
   795  		{float64(0), false},
   796  		{[]byte{}, false},
   797  		{bytsHello, true},
   798  		{"1", true},
   799  		{"2.1", false},
   800  		{"TRUE", true},
   801  		{false, false},
   802  		{fnCb1, false},
   803  		{nil, false},
   804  		{personS1, true},
   805  	}
   806  
   807  	for _, test := range tests {
   808  		actual := KConv.ToBool(test.input)
   809  		assert.Equal(t, actual, test.expected)
   810  	}
   811  }
   812  
   813  func BenchmarkConvert_ToBool(b *testing.B) {
   814  	b.ResetTimer()
   815  	for i := 0; i < b.N; i++ {
   816  		KConv.ToBool(intSpeedLight)
   817  	}
   818  }
   819  
   820  func TestConvert_ToInt(t *testing.T) {
   821  	//并行测试
   822  	t.Parallel()
   823  
   824  	var tests = []struct {
   825  		input    interface{}
   826  		expected int
   827  	}{
   828  		{int(-1), -1},
   829  		{int8(0), 0},
   830  		{int16(1), 1},
   831  		{int32(2), 2},
   832  		{int64(3), 3},
   833  		{uint(0), 0},
   834  		{uint8(0), 0},
   835  		{uint16(0), 0},
   836  		{uint32(0), 0},
   837  		{uint64(0), 0},
   838  		{float32(1.234), 1},
   839  		{float64(4.5678), 4},
   840  		{[]byte{}, 0},
   841  		{"1", 1},
   842  		{"2.1", 2},
   843  		{"TRUE", 1},
   844  		{true, 1},
   845  		{false, 0},
   846  		{fnCb1, 0},
   847  		{nil, 0},
   848  		{personS1, 1},
   849  		{crowd, 5},
   850  	}
   851  	for _, test := range tests {
   852  		actual := KConv.ToInt(test.input)
   853  		assert.Equal(t, actual, test.expected)
   854  	}
   855  }
   856  
   857  func BenchmarkConvert_ToInt(b *testing.B) {
   858  	b.ResetTimer()
   859  	for i := 0; i < b.N; i++ {
   860  		KConv.ToInt(intSpeedLight)
   861  	}
   862  }
   863  
   864  func TestConvert_ToFloat(t *testing.T) {
   865  	var tests = []struct {
   866  		input    interface{}
   867  		expected float64
   868  	}{
   869  		{int(-1), -1.0},
   870  		{int8(0), 0.0},
   871  		{int16(1), 1.0},
   872  		{int32(2), 2.0},
   873  		{int64(3), 3.0},
   874  		{uint(0), 0.0},
   875  		{uint8(0), 0.0},
   876  		{uint16(0), 0.0},
   877  		{uint32(0), 0.0},
   878  		{uint64(0), 0.0},
   879  		{float32(0), 0.0},
   880  		{float64(0), 0.0},
   881  		{[]byte{}, 0.0},
   882  		{"1", 1.0},
   883  		{"2.1", 2.1},
   884  		{"TRUE", 1.0},
   885  		{true, 1.0},
   886  		{false, 0},
   887  		{fnCb1, 0},
   888  		{nil, 0},
   889  		{personS1, 1.0},
   890  		{crowd, 5.0},
   891  	}
   892  	for _, test := range tests {
   893  		actual := KConv.ToFloat(test.input)
   894  		assert.Equal(t, actual, test.expected)
   895  	}
   896  }
   897  
   898  func BenchmarkConvert_ToFloat(b *testing.B) {
   899  	b.ResetTimer()
   900  	for i := 0; i < b.N; i++ {
   901  		KConv.ToFloat(intSpeedLight)
   902  	}
   903  }
   904  
   905  func TestConvert_Float64ToByte(t *testing.T) {
   906  	var res []byte
   907  	res = KConv.Float64ToByte(flPi2)
   908  	assert.NotEmpty(t, res)
   909  }
   910  
   911  func BenchmarkConvert_Float64ToByte(b *testing.B) {
   912  	b.ResetTimer()
   913  	for i := 0; i < b.N; i++ {
   914  		KConv.Float64ToByte(flPi2)
   915  	}
   916  }
   917  
   918  func TestConvert_Byte2Float64(t *testing.T) {
   919  	defer func() {
   920  		r := recover()
   921  		assert.NotEmpty(t, r)
   922  	}()
   923  
   924  	var res float64
   925  
   926  	res = KConv.Byte2Float64(bytPi5)
   927  	assert.Equal(t, flPi2, res)
   928  
   929  	//不合法
   930  	KConv.Byte2Float64([]byte{0, 1, 2, 3})
   931  }
   932  
   933  func BenchmarkConvert_Byte2Float64(b *testing.B) {
   934  	b.ResetTimer()
   935  	for i := 0; i < b.N; i++ {
   936  		KConv.Byte2Float64(bytPi5)
   937  	}
   938  }
   939  
   940  func TestConvert_Int64ToByte(t *testing.T) {
   941  	var res []byte
   942  	res = KConv.Int64ToByte(intAstronomicalUnit)
   943  	assert.NotEmpty(t, res)
   944  }
   945  
   946  func BenchmarkConvert_Int64ToByte(b *testing.B) {
   947  	b.ResetTimer()
   948  	for i := 0; i < b.N; i++ {
   949  		KConv.Int64ToByte(intAstronomicalUnit)
   950  	}
   951  }
   952  
   953  func TestConvert_Byte2Int64(t *testing.T) {
   954  	defer func() {
   955  		r := recover()
   956  		assert.NotEmpty(t, r)
   957  	}()
   958  
   959  	var res int64
   960  	res = KConv.Byte2Int64(bytAstronomicalUnit)
   961  	assert.Equal(t, intAstronomicalUnit, res)
   962  
   963  	//不合法
   964  	KConv.Byte2Int64([]byte{0, 1, 2, 3})
   965  }
   966  
   967  func BenchmarkConvert_Byte2Int64(b *testing.B) {
   968  	b.ResetTimer()
   969  	for i := 0; i < b.N; i++ {
   970  		KConv.Byte2Int64(bytAstronomicalUnit)
   971  	}
   972  }
   973  
   974  func TestConvert_Byte2Hex(t *testing.T) {
   975  	var res string
   976  
   977  	res = KConv.Byte2Hex(bytsHello)
   978  	assert.Equal(t, strHelloHex, res)
   979  
   980  	res = KConv.Byte2Hex(bytEmp)
   981  	assert.Empty(t, res)
   982  }
   983  
   984  func BenchmarkConvert_Byte2Hex(b *testing.B) {
   985  	b.ResetTimer()
   986  	for i := 0; i < b.N; i++ {
   987  		KConv.Byte2Hex(bytsHello)
   988  	}
   989  }
   990  
   991  func TestConvert_Byte2Hexs(t *testing.T) {
   992  	var res []byte
   993  
   994  	res = KConv.Byte2Hexs(bytsHello)
   995  	assert.Equal(t, strHelloHex, string(res))
   996  
   997  	res = KConv.Byte2Hexs(bytEmp)
   998  	assert.Empty(t, res)
   999  }
  1000  
  1001  func BenchmarkConvert_Byte2Hexs(b *testing.B) {
  1002  	b.ResetTimer()
  1003  	for i := 0; i < b.N; i++ {
  1004  		KConv.Byte2Hexs(bytsHello)
  1005  	}
  1006  }
  1007  
  1008  func TestConvert_Hex2Byte(t *testing.T) {
  1009  	var res []byte
  1010  
  1011  	res = KConv.Hex2Byte(strHelloHex)
  1012  	assert.Equal(t, strHello, string(res))
  1013  
  1014  	res = KConv.Hex2Byte("")
  1015  	assert.Empty(t, res)
  1016  }
  1017  
  1018  func BenchmarkConvert_Hex2Byte(b *testing.B) {
  1019  	b.ResetTimer()
  1020  	for i := 0; i < b.N; i++ {
  1021  		KConv.Hex2Byte(strHelloHex)
  1022  	}
  1023  }
  1024  
  1025  func TestConvert_Hexs2Byte(t *testing.T) {
  1026  	var res []byte
  1027  
  1028  	bs := KConv.Byte2Hexs(bytsHello)
  1029  	res = KConv.Hexs2Byte(bs)
  1030  	assert.Equal(t, strHello, string(res))
  1031  
  1032  	res = KConv.Byte2Hexs([]byte{})
  1033  	assert.Empty(t, res)
  1034  }
  1035  
  1036  func BenchmarkConvert_Hexs2Byte(b *testing.B) {
  1037  	b.ResetTimer()
  1038  	bs := KConv.Byte2Hexs(bytsHello)
  1039  	for i := 0; i < b.N; i++ {
  1040  		KConv.Hexs2Byte(bs)
  1041  	}
  1042  }
  1043  
  1044  func TestConvert_Runes2Bytes(t *testing.T) {
  1045  	var res []byte
  1046  
  1047  	res = KConv.Runes2Bytes(runesHello)
  1048  	assert.Equal(t, bytsHello, res)
  1049  }
  1050  
  1051  func BenchmarkConvert_Runes2Bytes(b *testing.B) {
  1052  	b.ResetTimer()
  1053  	for i := 0; i < b.N; i++ {
  1054  		KConv.Runes2Bytes(runesHello)
  1055  	}
  1056  }
  1057  
  1058  func TestConvert_IsString(t *testing.T) {
  1059  	var res bool
  1060  
  1061  	res = KConv.IsString(intSpeedLight)
  1062  	assert.False(t, res)
  1063  
  1064  	res = KConv.IsString(strHello)
  1065  	assert.True(t, res)
  1066  }
  1067  
  1068  func BenchmarkConvert_IsString(b *testing.B) {
  1069  	b.ResetTimer()
  1070  	for i := 0; i < b.N; i++ {
  1071  		KConv.IsString(strHello)
  1072  	}
  1073  }
  1074  
  1075  func TestConvert_IsBinary(t *testing.T) {
  1076  	var res bool
  1077  	var cont []byte
  1078  
  1079  	cont, _ = KFile.ReadFile(imgPng)
  1080  	res = KConv.IsBinary(string(cont))
  1081  	assert.True(t, res)
  1082  
  1083  	cont, _ = KFile.ReadFile(fileDante)
  1084  	res = KConv.IsBinary(string(cont))
  1085  	assert.False(t, res)
  1086  }
  1087  
  1088  func BenchmarkConvert_IsBinary(b *testing.B) {
  1089  	b.ResetTimer()
  1090  	cont, _ := KFile.ReadFile(imgPng)
  1091  	for i := 0; i < b.N; i++ {
  1092  		KConv.IsBinary(string(cont))
  1093  	}
  1094  }
  1095  
  1096  func TestConvert_IsNumeric(t *testing.T) {
  1097  	var tests = []struct {
  1098  		input    interface{}
  1099  		expected bool
  1100  	}{
  1101  		{intSpeedLight, true},
  1102  		{flPi1, true},
  1103  		{strSpeedLight, true},
  1104  		{strHello, false},
  1105  		{crowd, false},
  1106  		{"", false},
  1107  	}
  1108  	for _, test := range tests {
  1109  		actual := KConv.IsNumeric(test.input)
  1110  		assert.Equal(t, actual, test.expected)
  1111  	}
  1112  }
  1113  
  1114  func BenchmarkConvert_IsNumeric(b *testing.B) {
  1115  	b.ResetTimer()
  1116  	for i := 0; i < b.N; i++ {
  1117  		KConv.IsNumeric(intSpeedLight)
  1118  	}
  1119  }
  1120  
  1121  func TestConvert_IsInt(t *testing.T) {
  1122  	var tests = []struct {
  1123  		input    interface{}
  1124  		expected bool
  1125  	}{
  1126  		{intSpeedLight, true},
  1127  		{flPi1, false},
  1128  		{strSpeedLight, true},
  1129  		{strPi6, false},
  1130  		{strHello, false},
  1131  		{crowd, false},
  1132  		{"", false},
  1133  	}
  1134  	for _, test := range tests {
  1135  		actual := KConv.IsInt(test.input)
  1136  		assert.Equal(t, actual, test.expected)
  1137  	}
  1138  }
  1139  
  1140  func BenchmarkConvert_IsInt(b *testing.B) {
  1141  	b.ResetTimer()
  1142  	for i := 0; i < b.N; i++ {
  1143  		KConv.IsInt(intSpeedLight)
  1144  	}
  1145  }
  1146  
  1147  func TestConvert_IsFloat(t *testing.T) {
  1148  	var tests = []struct {
  1149  		input    interface{}
  1150  		expected bool
  1151  	}{
  1152  		{intSpeedLight, false},
  1153  		{flPi1, true},
  1154  		{strSpeedLight, false},
  1155  		{strPi6, true},
  1156  		{strHello, false},
  1157  		{crowd, false},
  1158  		{"", false},
  1159  	}
  1160  	for _, test := range tests {
  1161  		actual := KConv.IsFloat(test.input)
  1162  		assert.Equal(t, actual, test.expected)
  1163  	}
  1164  }
  1165  
  1166  func BenchmarkConvert_IsFloat(b *testing.B) {
  1167  	b.ResetTimer()
  1168  	for i := 0; i < b.N; i++ {
  1169  		KConv.IsFloat(flPi1)
  1170  	}
  1171  }
  1172  
  1173  func TestConvert_IsEmpty(t *testing.T) {
  1174  	var org sOrganization
  1175  	var itf interface{} = &strSlEmp
  1176  	var tests = []struct {
  1177  		input    interface{}
  1178  		expected bool
  1179  	}{
  1180  		{nil, true},
  1181  		{"", true},
  1182  		{strMpEmp, true},
  1183  		{false, true},
  1184  		{0, true},
  1185  		{uint(0), true},
  1186  		{0.0, true},
  1187  		{org, true},
  1188  		{itf, false},
  1189  	}
  1190  	for _, test := range tests {
  1191  		actual := KConv.IsEmpty(test.input)
  1192  		assert.Equal(t, actual, test.expected)
  1193  	}
  1194  }
  1195  
  1196  func BenchmarkConvert_IsEmpty(b *testing.B) {
  1197  	b.ResetTimer()
  1198  	for i := 0; i < b.N; i++ {
  1199  		KConv.IsEmpty("")
  1200  	}
  1201  }
  1202  
  1203  func TestConvert_IsNil(t *testing.T) {
  1204  	var org sOrganization
  1205  	var itf interface{} = &strSlEmp
  1206  	var s []int
  1207  	var tests = []struct {
  1208  		input    interface{}
  1209  		expected bool
  1210  	}{
  1211  		{nil, true},
  1212  		{"", false},
  1213  		{strMpEmp, false},
  1214  		{false, false},
  1215  		{0, false},
  1216  		{uint(0), false},
  1217  		{0.0, false},
  1218  		{org, false},
  1219  		{itf, false},
  1220  		{s, true},
  1221  	}
  1222  	for _, test := range tests {
  1223  		actual := KConv.IsNil(test.input)
  1224  		assert.Equal(t, actual, test.expected)
  1225  	}
  1226  }
  1227  
  1228  func BenchmarkConvert_IsNil(b *testing.B) {
  1229  	b.ResetTimer()
  1230  	for i := 0; i < b.N; i++ {
  1231  		KConv.IsNil(nil)
  1232  	}
  1233  }
  1234  
  1235  func TestConvert_IsBool(t *testing.T) {
  1236  	var res bool
  1237  
  1238  	res = KConv.IsBool(false)
  1239  	assert.True(t, res)
  1240  
  1241  	res = KConv.IsBool("true")
  1242  	assert.False(t, res)
  1243  }
  1244  
  1245  func BenchmarkConvert_IsBool(b *testing.B) {
  1246  	b.ResetTimer()
  1247  	for i := 0; i < b.N; i++ {
  1248  		KConv.IsBool(false)
  1249  	}
  1250  }
  1251  
  1252  func TestConvert_IsHex(t *testing.T) {
  1253  	var str1 = KConv.Dec2Hex(intAstronomicalUnit)
  1254  	var str2 = "0x" + str1
  1255  
  1256  	var tests = []struct {
  1257  		input    string
  1258  		expected bool
  1259  	}{
  1260  		{"", false},
  1261  		{str1, true},
  1262  		{str2, true},
  1263  		{strHelloHex, true},
  1264  		{strHello, false},
  1265  	}
  1266  	for _, test := range tests {
  1267  		actual := KConv.IsHex(test.input)
  1268  		assert.Equal(t, actual, test.expected)
  1269  	}
  1270  }
  1271  
  1272  func BenchmarkConvert_IsHex(b *testing.B) {
  1273  	b.ResetTimer()
  1274  	for i := 0; i < b.N; i++ {
  1275  		KConv.IsHex(strHelloHex)
  1276  	}
  1277  }
  1278  
  1279  func TestConvert_IsByte(t *testing.T) {
  1280  	var tests = []struct {
  1281  		input    interface{}
  1282  		expected bool
  1283  	}{
  1284  		{"", false},
  1285  		{runesHello, false},
  1286  		{bytsHello, true},
  1287  	}
  1288  	for _, test := range tests {
  1289  		actual := KConv.IsByte(test.input)
  1290  		assert.Equal(t, actual, test.expected)
  1291  	}
  1292  }
  1293  
  1294  func BenchmarkConvert_IsByte(b *testing.B) {
  1295  	b.ResetTimer()
  1296  	for i := 0; i < b.N; i++ {
  1297  		KConv.IsByte(bytsHello)
  1298  	}
  1299  }
  1300  
  1301  func TestConvert_IsStruct(t *testing.T) {
  1302  	var tests = []struct {
  1303  		input    interface{}
  1304  		expected bool
  1305  	}{
  1306  		{strHello, false},
  1307  		{runesHello, false},
  1308  		{cmplNum1, false},
  1309  		{colorMp, false},
  1310  		{personS1, true},
  1311  		{&personS1, true},
  1312  		{orgS1, true},
  1313  		{&orgS1, false},
  1314  	}
  1315  	for _, test := range tests {
  1316  		actual := KConv.IsStruct(test.input)
  1317  		assert.Equal(t, actual, test.expected)
  1318  	}
  1319  }
  1320  
  1321  func BenchmarkConvert_IsStruct(b *testing.B) {
  1322  	b.ResetTimer()
  1323  	for i := 0; i < b.N; i++ {
  1324  		KConv.IsStruct(personS1)
  1325  	}
  1326  }
  1327  
  1328  func TestConvert_IsInterface(t *testing.T) {
  1329  	var tests = []struct {
  1330  		input    interface{}
  1331  		expected bool
  1332  	}{
  1333  		{strHello, false},
  1334  		{personS1, false},
  1335  		{itfObj, true},
  1336  	}
  1337  	for _, test := range tests {
  1338  		actual := KConv.IsInterface(test.input)
  1339  		assert.Equal(t, actual, test.expected)
  1340  	}
  1341  }
  1342  
  1343  func BenchmarkConvert_IsInterface(b *testing.B) {
  1344  	b.ResetTimer()
  1345  	for i := 0; i < b.N; i++ {
  1346  		KConv.IsInterface(itfObj)
  1347  	}
  1348  }
  1349  
  1350  func TestConvert_IsPort(t *testing.T) {
  1351  	var tests = []struct {
  1352  		param    interface{}
  1353  		expected bool
  1354  	}{
  1355  		{"hello", false},
  1356  		{"1", true},
  1357  		{0, false},
  1358  		{100, true},
  1359  		{"65535", true},
  1360  		{"0", false},
  1361  		{"65536", false},
  1362  		{"65538.9", false},
  1363  	}
  1364  
  1365  	for _, test := range tests {
  1366  		actual := KConv.IsPort(test.param)
  1367  		assert.Equal(t, actual, test.expected)
  1368  	}
  1369  }
  1370  
  1371  func BenchmarkConvert_IsPort(b *testing.B) {
  1372  	b.ResetTimer()
  1373  	for i := 0; i < b.N; i++ {
  1374  		KConv.IsPort(80)
  1375  	}
  1376  }