github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/ztype/decima_test.go (about)

     1  package ztype_test
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/sohaha/zlsgo"
     9  	"github.com/sohaha/zlsgo/ztype"
    10  )
    11  
    12  func TestDecimal(tt *testing.T) {
    13  	t := zlsgo.NewTest(tt)
    14  
    15  	t.Equal("10100", strconv.FormatInt(20, 2))
    16  	t.Equal("10100", fmt.Sprintf("%b", 20))
    17  	t.Equal("10100", ztype.DecimalToAny(20, 2))
    18  
    19  	t.Equal(strconv.FormatInt(20, 2), ztype.DecimalToAny(20, 2))
    20  	t.Equal(strconv.FormatInt(20, 8), ztype.DecimalToAny(20, 8))
    21  	t.Equal(strconv.FormatInt(20, 16), ztype.DecimalToAny(20, 16))
    22  	t.Equal(strconv.FormatInt(20, 32), ztype.DecimalToAny(20, 32))
    23  	t.Equal(strconv.FormatInt(20, 32), ztype.DecimalToAny(20, 31))
    24  
    25  	t.Equal("10100", ztype.DecimalToAny(20, 2))
    26  	t.Equal("2Bi", ztype.DecimalToAny(10000, 62))
    27  	t.Equal(10000, ztype.AnyToDecimal("2Bi", 62))
    28  	t.Equal(0, ztype.AnyToDecimal("20", 0))
    29  	t.Equal(2281, ztype.AnyToDecimal("2Bi", 31))
    30  
    31  	tt.Log(ztype.DecimalToAny(65, 66))
    32  	tt.Log(strconv.FormatInt(65, 9), ztype.DecimalToAny(65, 9))
    33  	t.Equal("10011100010000", ztype.DecimalToAny(10000, 2))
    34  }
    35  
    36  func BenchmarkDecimalStrconv2(b *testing.B) {
    37  	for i := 0; i < b.N; i++ {
    38  		v := strconv.FormatInt(20, 2)
    39  		_ = v
    40  	}
    41  }
    42  
    43  func BenchmarkDecimalZtype2(b *testing.B) {
    44  	for i := 0; i < b.N; i++ {
    45  		v := ztype.DecimalToAny(20, 2)
    46  		_ = v
    47  	}
    48  }
    49  
    50  func BenchmarkDecimalStrconv8(b *testing.B) {
    51  	for i := 0; i < b.N; i++ {
    52  		v := strconv.FormatInt(20, 8)
    53  		_, _ = strconv.ParseInt(v, 8, 64)
    54  	}
    55  }
    56  
    57  func BenchmarkDecimalZtype8(b *testing.B) {
    58  	for i := 0; i < b.N; i++ {
    59  		v := ztype.DecimalToAny(20, 8)
    60  		_ = ztype.AnyToDecimal(v, 8)
    61  	}
    62  }
    63  
    64  func BenchmarkDecimalStrconv16(b *testing.B) {
    65  	for i := 0; i < b.N; i++ {
    66  		v := strconv.FormatInt(20, 16)
    67  		_, _ = strconv.ParseInt(v, 16, 64)
    68  	}
    69  }
    70  
    71  func BenchmarkDecimalZtype16(b *testing.B) {
    72  	for i := 0; i < b.N; i++ {
    73  		v := ztype.DecimalToAny(20, 16)
    74  		_ = ztype.AnyToDecimal(v, 16)
    75  	}
    76  }
    77  
    78  func BenchmarkDecimalStrconv32(b *testing.B) {
    79  	for i := 0; i < b.N; i++ {
    80  		v := strconv.FormatInt(20, 32)
    81  		_, _ = strconv.ParseInt(v, 32, 64)
    82  	}
    83  }
    84  
    85  func BenchmarkDecimalZtype32(b *testing.B) {
    86  	for i := 0; i < b.N; i++ {
    87  		v := ztype.DecimalToAny(20, 32)
    88  		_ = ztype.AnyToDecimal(v, 32)
    89  	}
    90  }