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