github.com/avicd/go-utilx@v0.1.0/conv/number_test.go (about)

     1  package conv
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"testing"
     6  )
     7  
     8  func TestParseNum(t *testing.T) {
     9  	var info *numInfo
    10  	info = parseNum("fsdfjj0x32332.3232weqw")
    11  	assert.Equal(t, "32332", info.integer)
    12  	assert.Equal(t, "3232", info.decimal)
    13  	assert.Equal(t, 16, info.base)
    14  	assert.Equal(t, false, info.negative)
    15  
    16  	info = parseNum("-1.223")
    17  	assert.Equal(t, "1", info.integer)
    18  	assert.Equal(t, "223", info.decimal)
    19  	assert.Equal(t, 10, info.base)
    20  	assert.Equal(t, true, info.negative)
    21  
    22  	info = parseNum("2.0323")
    23  	assert.Equal(t, "2", info.integer)
    24  	assert.Equal(t, "0323", info.decimal)
    25  	assert.Equal(t, 10, info.base)
    26  	assert.Equal(t, false, info.negative)
    27  
    28  	info = parseNum("0x2.0323fgdf")
    29  	assert.Equal(t, "2", info.integer)
    30  	assert.Equal(t, "0323f", info.decimal)
    31  	assert.Equal(t, 16, info.base)
    32  	assert.Equal(t, false, info.negative)
    33  
    34  	info = parseNum("0o78721")
    35  	assert.Equal(t, "7", info.integer)
    36  	assert.Equal(t, "", info.decimal)
    37  	assert.Equal(t, 8, info.base)
    38  	assert.Equal(t, false, info.negative)
    39  
    40  	info = parseNum("-0b10010.11001")
    41  	assert.Equal(t, "10010", info.integer)
    42  	assert.Equal(t, "11001", info.decimal)
    43  	assert.Equal(t, 2, info.base)
    44  	assert.Equal(t, true, info.negative)
    45  }
    46  
    47  func TestParseInt(t *testing.T) {
    48  	assert.Equal(t, int64(255), ParseInt("0xff"))
    49  	assert.Equal(t, int64(57), ParseInt("0o71"))
    50  	assert.Equal(t, int64(2), ParseInt("0b10"))
    51  	assert.Equal(t, int64(4454), ParseInt("4454"))
    52  	assert.Equal(t, int64(18), ParseInt("0x12.212"))
    53  }
    54  
    55  func TestParseFloat(t *testing.T) {
    56  	assert.Equal(t, 1.9375, ParseFloat("0x1.f"))
    57  	assert.Equal(t, 1.875, ParseFloat("0o1.7"))
    58  	assert.Equal(t, 1.8125, ParseFloat("0b1.1101"))
    59  	assert.Equal(t, 2.1121, ParseFloat("2.1121"))
    60  	assert.Equal(t, 2.0, ParseFloat("2"))
    61  }