@@ -112,6 +112,18 @@ var correctnessSamples = []struct {
112
112
{"-1234567891234567890.0987654321987654321" , "c7150113012345678912345678900987654321987654321d" , false },
113
113
}
114
114
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
+
115
127
// There is a difference between encoding result from a raw string and from
116
128
// decimal.Decimal. It's expected because decimal.Decimal simplifies decimals:
117
129
// 0.00010000 -> 0.0001
@@ -384,17 +396,21 @@ func TestEncodeStringToBCD(t *testing.T) {
384
396
385
397
func TestDecodeStringFromBCD (t * testing.T ) {
386
398
samples := append (correctnessSamples , rawSamples ... )
399
+ samples = append (samples , correctnessDecodeSamples ... )
387
400
samples = append (samples , benchmarkSamples ... )
388
401
for _ , testcase := range samples {
389
402
t .Run (testcase .numString , func (t * testing.T ) {
390
403
b , _ := hex .DecodeString (testcase .mpBuf )
391
404
bcdBuf := trimMPHeader (b , testcase .fixExt )
392
- s , err := DecodeStringFromBCD (bcdBuf )
405
+ s , exp , err := DecodeStringFromBCD (bcdBuf )
393
406
if err != nil {
394
407
t .Fatalf ("Failed to decode BCD '%x' to decimal: %s" , bcdBuf , err )
395
408
}
396
409
397
410
decActual , err := decimal .NewFromString (s )
411
+ if exp != 0 {
412
+ decActual = decActual .Shift (int32 (exp ))
413
+ }
398
414
if err != nil {
399
415
t .Fatalf ("Failed to encode string ('%s') to decimal" , s )
400
416
}
@@ -525,6 +541,37 @@ func TestSelect(t *testing.T) {
525
541
tupleValueIsDecimal (t , resp .Data , number )
526
542
}
527
543
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
+
528
575
func assertInsert (t * testing.T , conn * Connection , numString string ) {
529
576
number , err := decimal .NewFromString (numString )
530
577
if err != nil {
0 commit comments