github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/decimal/decimal_test.go (about)

     1  package decimal
     2  
     3  import (
     4  	"encoding/binary"
     5  	"testing"
     6  )
     7  
     8  func TestFromBytes(t *testing.T) {
     9  	for _, test := range []struct {
    10  		name      string
    11  		bts       []byte
    12  		precision uint32
    13  		scale     uint32
    14  	}{
    15  		{
    16  			bts:       uint128(0xffffffffffffffff, 0xffffffffffffffff),
    17  			precision: 22,
    18  			scale:     9,
    19  		},
    20  		{
    21  			bts:       uint128(0xffffffffffffffff, 0),
    22  			precision: 22,
    23  			scale:     9,
    24  		},
    25  		{
    26  			bts:       uint128(0x4000000000000000, 0),
    27  			precision: 22,
    28  			scale:     9,
    29  		},
    30  		{
    31  			bts:       uint128(0x8000000000000000, 0),
    32  			precision: 22,
    33  			scale:     9,
    34  		},
    35  		{
    36  			bts:       uint128s(1000000000),
    37  			precision: 22,
    38  			scale:     9,
    39  		},
    40  	} {
    41  		t.Run(test.name, func(t *testing.T) {
    42  			x := FromBytes(test.bts, test.precision, test.scale)
    43  			p := Append(nil, x)
    44  			y := FromBytes(p, test.precision, test.scale)
    45  			if x.Cmp(y) != 0 {
    46  				t.Errorf(
    47  					"parsed bytes serialized to different value: %v; want %v",
    48  					x, y,
    49  				)
    50  			}
    51  			t.Logf(
    52  				"%s %s",
    53  				Format(x, test.precision, test.scale),
    54  				Format(y, test.precision, test.scale),
    55  			)
    56  		})
    57  	}
    58  }
    59  
    60  func uint128(hi, lo uint64) []byte {
    61  	p := make([]byte, 16)
    62  	binary.BigEndian.PutUint64(p[:8], hi)
    63  	binary.BigEndian.PutUint64(p[8:], lo)
    64  
    65  	return p
    66  }
    67  
    68  func uint128s(lo uint64) []byte {
    69  	return uint128(0, lo)
    70  }