@@ -121,6 +121,18 @@ var correctnessSamples = []struct {
121
121
"c7150113012345678912345678900987654321987654321d" , false },
122
122
}
123
123
124
+ var correctnessDecodeSamples = []struct {
125
+ numString string
126
+ mpBuf string
127
+ fixExt bool
128
+ }{
129
+ {"1e2" , "d501fe1c" , true },
130
+ {"1e33" , "c70301d0df1c" , false },
131
+ {"1.1e31" , "c70301e2011c" , false },
132
+ {"13e-2" , "c7030102013c" , false },
133
+ {"-1e3" , "d501fd1d" , true },
134
+ }
135
+
124
136
// There is a difference between encoding result from a raw string and from
125
137
// decimal.Decimal. It's expected because decimal.Decimal simplifies decimals:
126
138
// 0.00010000 -> 0.0001
@@ -397,18 +409,22 @@ func TestEncodeStringToBCD(t *testing.T) {
397
409
398
410
func TestDecodeStringFromBCD (t * testing.T ) {
399
411
samples := correctnessSamples
412
+ samples = append (samples , correctnessDecodeSamples ... )
400
413
samples = append (samples , rawSamples ... )
401
414
samples = append (samples , benchmarkSamples ... )
402
415
for _ , testcase := range samples {
403
416
t .Run (testcase .numString , func (t * testing.T ) {
404
417
b , _ := hex .DecodeString (testcase .mpBuf )
405
418
bcdBuf := trimMPHeader (b , testcase .fixExt )
406
- s , err := DecodeStringFromBCD (bcdBuf )
419
+ s , exp , err := DecodeStringFromBCD (bcdBuf )
407
420
if err != nil {
408
421
t .Fatalf ("Failed to decode BCD '%x' to decimal: %s" , bcdBuf , err )
409
422
}
410
423
411
424
decActual , err := decimal .NewFromString (s )
425
+ if exp != 0 {
426
+ decActual = decActual .Shift (int32 (exp ))
427
+ }
412
428
if err != nil {
413
429
t .Fatalf ("Failed to msgpack.Encoder string ('%s') to decimal" , s )
414
430
}
@@ -551,6 +567,37 @@ func TestSelect(t *testing.T) {
551
567
tupleValueIsDecimal (t , resp .Data , number )
552
568
}
553
569
570
+ func TestUnmarshal_from_decimal_new (t * testing.T ) {
571
+ skipIfDecimalUnsupported (t )
572
+
573
+ conn := test_helpers .ConnectWithValidation (t , server , opts )
574
+ defer conn .Close ()
575
+
576
+ samples := correctnessSamples
577
+ samples = append (samples , correctnessDecodeSamples ... )
578
+ samples = append (samples , benchmarkSamples ... )
579
+ for _ , testcase := range samples {
580
+ str := testcase .numString
581
+ t .Run (str , func (t * testing.T ) {
582
+ number , err := decimal .NewFromString (str )
583
+ if err != nil {
584
+ t .Fatalf ("Failed to prepare test decimal: %s" , err )
585
+ }
586
+
587
+ call := NewEvalRequest ("return require('decimal').new(...)" ).
588
+ Args ([]interface {}{str })
589
+ resp , err := conn .Do (call ).Get ()
590
+ if err != nil {
591
+ t .Fatalf ("Decimal create failed: %s" , err )
592
+ }
593
+ if resp == nil {
594
+ t .Fatalf ("Response is nil after Call" )
595
+ }
596
+ tupleValueIsDecimal (t , []interface {}{resp .Data }, number )
597
+ })
598
+ }
599
+ }
600
+
554
601
func assertInsert (t * testing.T , conn * Connection , numString string ) {
555
602
number , err := decimal .NewFromString (numString )
556
603
if err != nil {
0 commit comments