@@ -112,6 +112,18 @@ var correctnessSamples = []struct {
112112 {"-1234567891234567890.0987654321987654321" , "c7150113012345678912345678900987654321987654321d" , false },
113113}
114114
115+ var correctnessDecodeSamples = []struct {
116+ numString string
117+ mpBuf string
118+ fixExt bool
119+ }{
120+ {"1e2" , "d501fe1c" , true },
121+ {"1e33" , "c70301d0df1c" , false },
122+ {"1.1e31" , "c70301e2011c" , false },
123+ {"13e-2" , "c7030102013c" , false },
124+ {"-1e3" , "d501fd1d" , true },
125+ }
126+
115127// There is a difference between encoding result from a raw string and from
116128// decimal.Decimal. It's expected because decimal.Decimal simplifies decimals:
117129// 0.00010000 -> 0.0001
@@ -384,17 +396,21 @@ func TestEncodeStringToBCD(t *testing.T) {
384396
385397func TestDecodeStringFromBCD (t * testing.T ) {
386398 samples := append (correctnessSamples , rawSamples ... )
399+ samples = append (samples , correctnessDecodeSamples ... )
387400 samples = append (samples , benchmarkSamples ... )
388401 for _ , testcase := range samples {
389402 t .Run (testcase .numString , func (t * testing.T ) {
390403 b , _ := hex .DecodeString (testcase .mpBuf )
391404 bcdBuf := trimMPHeader (b , testcase .fixExt )
392- s , err := DecodeStringFromBCD (bcdBuf )
405+ s , exp , err := DecodeStringFromBCD (bcdBuf )
393406 if err != nil {
394407 t .Fatalf ("Failed to decode BCD '%x' to decimal: %s" , bcdBuf , err )
395408 }
396409
397410 decActual , err := decimal .NewFromString (s )
411+ if exp != 0 {
412+ decActual = decActual .Shift (int32 (exp ))
413+ }
398414 if err != nil {
399415 t .Fatalf ("Failed to encode string ('%s') to decimal" , s )
400416 }
@@ -525,6 +541,37 @@ func TestSelect(t *testing.T) {
525541 tupleValueIsDecimal (t , resp .Data , number )
526542}
527543
544+ func TestUnmarshal_from_decimal_new (t * testing.T ) {
545+ skipIfDecimalUnsupported (t )
546+
547+ conn := test_helpers .ConnectWithValidation (t , server , opts )
548+ defer conn .Close ()
549+
550+ samples := correctnessSamples
551+ samples = append (samples , correctnessDecodeSamples ... )
552+ samples = append (samples , benchmarkSamples ... )
553+ for _ , testcase := range samples {
554+ str := testcase .numString
555+ t .Run (str , func (t * testing.T ) {
556+ number , err := decimal .NewFromString (str )
557+ if err != nil {
558+ t .Fatalf ("Failed to prepare test decimal: %s" , err )
559+ }
560+
561+ call := NewEvalRequest ("return require('decimal').new(...)" ).
562+ Args ([]interface {}{str })
563+ resp , err := conn .Do (call ).Get ()
564+ if err != nil {
565+ t .Fatalf ("Decimal create failed: %s" , err )
566+ }
567+ if resp == nil {
568+ t .Fatalf ("Response is nil after Call" )
569+ }
570+ tupleValueIsDecimal (t , []interface {}{resp .Data }, number )
571+ })
572+ }
573+ }
574+
528575func assertInsert (t * testing.T , conn * Connection , numString string ) {
529576 number , err := decimal .NewFromString (numString )
530577 if err != nil {
0 commit comments