@@ -67,35 +67,35 @@ public void bigDecimalConv() {
6767
6868 @ Test
6969 public void contract () {
70- DecimalType type = DecimalType .of (13 , 2 );
70+ DecimalType type = DecimalType .of (20 , 2 );
7171 DecimalValue value = DecimalValue .fromBits (type , 0x0001 , 0x0002 );
7272
73- Assert .assertEquals (DecimalType .of (13 , 2 ), value .getType ());
73+ Assert .assertEquals (DecimalType .of (20 , 2 ), value .getType ());
7474 Assert .assertEquals (0x0001 , value .getHigh ());
7575 Assert .assertEquals (0x0002 , value .getLow ());
7676
7777 // equals
7878 Assert .assertEquals (value , DecimalValue .fromBits (type , 0x0001 , 0x0002 ));
79- Assert .assertEquals (value , DecimalValue .fromBits (DecimalType .of (13 , 2 ), 0x0001 , 0x0002 ));
79+ Assert .assertEquals (value , DecimalValue .fromBits (DecimalType .of (20 , 2 ), 0x0001 , 0x0002 ));
8080
8181 Assert .assertNotEquals (value , DecimalValue .fromBits (type , 0x0001 , 0x0003 ));
8282 Assert .assertNotEquals (value , DecimalValue .fromBits (type , 0x0002 , 0x0002 ));
83- Assert .assertNotEquals (value , DecimalValue .fromBits (DecimalType .of (12 , 2 ), 0x0001 , 0x0002 ));
84- Assert .assertNotEquals (value , DecimalValue .fromBits (DecimalType .of (13 , 1 ), 0x0001 , 0x0002 ));
83+ Assert .assertNotEquals (value , DecimalValue .fromBits (DecimalType .of (21 , 2 ), 0x0001 , 0x0002 ));
84+ Assert .assertNotEquals (value , DecimalValue .fromBits (DecimalType .of (20 , 1 ), 0x0001 , 0x0002 ));
8585
8686 // hashCode
8787 Assert .assertEquals (value .hashCode (), DecimalValue .fromBits (type , 0x0001 , 0x0002 ).hashCode ());
88- Assert .assertEquals (value .hashCode (), DecimalValue .fromBits (DecimalType .of (13 , 2 ), 0x0001 , 0x0002 ).hashCode ());
88+ Assert .assertEquals (value .hashCode (), DecimalValue .fromBits (DecimalType .of (20 , 2 ), 0x0001 , 0x0002 ).hashCode ());
8989
9090 Assert .assertNotEquals (value .hashCode (), DecimalValue .fromBits (type , 0x0001 , 0x0003 ).hashCode ());
9191 Assert .assertNotEquals (value .hashCode (), DecimalValue .fromBits (type , 0x0002 , 0x0002 ).hashCode ());
92- Assert .assertNotEquals (value .hashCode (), DecimalValue .fromBits (DecimalType .of (12 , 2 ), 0x0001 , 0x0002 ).hashCode ());
93- Assert .assertNotEquals (value .hashCode (), DecimalValue .fromBits (DecimalType .of (13 , 1 ), 0x0001 , 0x0002 ).hashCode ());
92+ Assert .assertNotEquals (value .hashCode (), DecimalValue .fromBits (DecimalType .of (21 , 2 ), 0x0001 , 0x0002 ).hashCode ());
93+ Assert .assertNotEquals (value .hashCode (), DecimalValue .fromBits (DecimalType .of (20 , 1 ), 0x0001 , 0x0002 ).hashCode ());
9494 }
9595
9696 @ Test
9797 public void protobuf () {
98- DecimalType type = DecimalType .of (13 , 2 );
98+ DecimalType type = DecimalType .of (20 , 2 );
9999 DecimalValue value = DecimalValue .fromBits (type , 0x0001 , 0x0002 );
100100
101101 ValueProtos .Value valuePb = value .toPb ();
@@ -128,11 +128,83 @@ public void inf() {
128128 DecimalValue value = type .newValue (inf );
129129 Assert .assertTrue (value .isInf ());
130130 Assert .assertFalse (value .isNegative ());
131- Assert .assertEquals (DecimalValue . INF , value );
131+ Assert .assertEquals (type . getInf () , value );
132132 inf = inf .add (k );
133133 }
134134 }
135135
136+ private void assertIsValid (DecimalValue v ) {
137+ Assert .assertFalse ("Non expected Nan for " + v , v .isNan ());
138+ Assert .assertFalse ("Non expected Inf for " + v , v .isInf ());
139+ Assert .assertFalse ("Non expected -Inf for " + v , v .isNegativeInf ());
140+ }
141+
142+ private void assertIsNan (DecimalValue v ) {
143+ Assert .assertTrue ("Expected Nan for " + v , v .isNan ());
144+ Assert .assertFalse ("Non expected Inf for " + v , v .isInf ());
145+ Assert .assertFalse ("Non expected -Inf for " + v , v .isNegativeInf ());
146+
147+ Assert .assertEquals (DecimalValue .NAN_LOW , v .getLow ());
148+ Assert .assertEquals (DecimalValue .NAN_HIGH , v .getHigh ());
149+ }
150+
151+ private void assertIsInf (DecimalValue v ) {
152+ Assert .assertFalse ("Non expected Nan for " + v , v .isNan ());
153+ Assert .assertTrue ("Expected Inf for " + v , v .isInf ());
154+ Assert .assertFalse ("Non expected -Inf for " + v , v .isNegativeInf ());
155+
156+ Assert .assertEquals (DecimalValue .INF_LOW , v .getLow ());
157+ Assert .assertEquals (DecimalValue .INF_HIGH , v .getHigh ());
158+ }
159+
160+ private void assertIsNegInf (DecimalValue v ) {
161+ Assert .assertFalse ("Non expected Nan for " + v , v .isNan ());
162+ Assert .assertFalse ("Non expected Inf for " + v , v .isInf ());
163+ Assert .assertTrue ("Expected -Inf for " + v , v .isNegativeInf ());
164+
165+ Assert .assertEquals (DecimalValue .NEG_INF_LOW , v .getLow ());
166+ Assert .assertEquals (DecimalValue .NEG_INF_HIGH , v .getHigh ());
167+ }
168+
169+ @ Test
170+ public void allTypeInfiniteAndNan () {
171+ BigInteger inf = BigInteger .ONE ;
172+ BigInteger nan = new BigInteger ("100000000000000000000000000000000001" );
173+ int [] scales = new int [] { 1 , 9 , 35 };
174+ for (int precision = 1 ; precision <= DecimalType .MAX_PRECISION ; precision ++) {
175+ inf = inf .multiply (BigInteger .TEN );
176+
177+ DecimalType type = DecimalType .of (precision );
178+
179+ assertIsInf (type .newValue (inf ));
180+ assertIsNegInf (type .newValue (inf .negate ()));
181+ assertIsNan (type .newValue (nan ));
182+
183+ assertIsValid (type .newValue (inf .subtract (BigInteger .ONE )));
184+ assertIsValid (type .newValue (inf .negate ().add (BigInteger .ONE )));
185+
186+ for (int scale : scales ) {
187+ if (scale > precision ) {
188+ continue ;
189+ }
190+
191+ DecimalType scaled = DecimalType .of (precision , scale );
192+ BigDecimal scaledInf = new BigDecimal (inf , scale );
193+ BigDecimal scaledNan = new BigDecimal (nan , scale );
194+
195+ System .out .println ("Nan for " + scaled + " -> " + scaledNan );
196+
197+ assertIsInf (scaled .newValue (scaledInf ));
198+ assertIsNegInf (scaled .newValue (scaledInf .negate ()));
199+
200+ assertIsValid (scaled .newValue (scaledInf .subtract (BigDecimal .valueOf (1 , scale ))));
201+ assertIsValid (scaled .newValue (scaledInf .negate ().add (BigDecimal .valueOf (1 , scale ))));
202+
203+ assertIsNan (scaled .newValue (scaledNan ));
204+ }
205+ }
206+ }
207+
136208 @ Test
137209 public void infDefaulttype () {
138210 DecimalType type = DecimalType .getDefault ();
@@ -143,7 +215,7 @@ public void infDefaulttype() {
143215 DecimalValue value = type .newValue (inf );
144216 Assert .assertTrue (value .isInf ());
145217 Assert .assertFalse (value .isNegative ());
146- Assert .assertNotEquals ( DecimalValue . INF , value );
218+ Assert .assertEquals ( type . getInf () , value );
147219 inf = inf .add (k );
148220 }
149221 }
@@ -158,7 +230,7 @@ public void negativeInf() {
158230 DecimalValue value = type .newValue (inf );
159231 Assert .assertTrue (value .isNegativeInf ());
160232 Assert .assertTrue (value .isNegative ());
161- Assert .assertEquals (DecimalValue . NEG_INF , value );
233+ Assert .assertEquals (type . getNegInf () , value );
162234 inf = inf .subtract (k );
163235 }
164236 }
@@ -173,7 +245,7 @@ public void negativeInfDefaultType() {
173245 DecimalValue value = type .newValue (inf );
174246 Assert .assertTrue (value .isNegativeInf ());
175247 Assert .assertTrue (value .isNegative ());
176- Assert .assertNotEquals ( DecimalValue . NEG_INF , value );
248+ Assert .assertEquals ( type . getNegInf () , value );
177249 inf = inf .subtract (k );
178250 }
179251 }
@@ -258,7 +330,7 @@ public void ofString() {
258330
259331 @ Test
260332 public void ofUnsigned () {
261- DecimalType t = DecimalType .getDefault ( );
333+ DecimalType t = DecimalType .of ( 31 , 9 );
262334 String zeros = "." + String .join ("" , Collections .nCopies (t .getScale (), "0" ));
263335
264336 Assert .assertTrue (t .newValueUnsigned (0 ).isZero ());
@@ -278,7 +350,7 @@ public void ofUnsigned() {
278350
279351 @ Test
280352 public void ofLong () {
281- DecimalType t = DecimalType .getDefault ( );
353+ DecimalType t = DecimalType .of ( 31 , 9 );
282354 String zeros = "." + String .join ("" , Collections .nCopies (t .getScale (), "0" ));
283355
284356 Assert .assertTrue (t .newValue (0 ).isZero ());
@@ -575,25 +647,25 @@ public void toUnscaledBigInteger() {
575647
576648 // (2) positive numbers: 1, 12, 123, ...
577649 String s = "" ;
578- for (int i = 1 ; i < DecimalType . MAX_PRECISION ; i ++) {
650+ for (int i = 1 ; i < t . getPrecision () ; i ++) {
579651 s += Integer .toString (i % 10 );
580652 BigInteger value = new BigInteger (s );
581653 Assert .assertEquals (value , t .newValueUnscaled (value ).toUnscaledBigInteger ());
582654 }
583655
584656 // (3) negative numbers: -1, -12, -123, ...
585657 s = "-" ;
586- for (int i = 1 ; i < DecimalType . MAX_PRECISION ; i ++) {
658+ for (int i = 1 ; i < t . getPrecision () ; i ++) {
587659 s += Integer .toString (i % 10 );
588660 BigInteger value = new BigInteger (s );
589661 Assert .assertEquals (value , t .newValueUnscaled (value ).toUnscaledBigInteger ());
590662 }
591663
592664 // (4) -inf, +inf, nan
593665 BigInteger inf = BigInteger .TEN .pow (DecimalType .MAX_PRECISION );
594- Assert .assertEquals (DecimalValue . INF .toUnscaledBigInteger (), inf );
595- Assert .assertEquals (DecimalValue . NEG_INF .toUnscaledBigInteger (), inf .negate ());
596- Assert .assertEquals (DecimalValue . NAN .toUnscaledBigInteger (), inf .add (BigInteger .ONE ));
666+ Assert .assertEquals (t . getInf () .toUnscaledBigInteger (), inf );
667+ Assert .assertEquals (t . getNegInf () .toUnscaledBigInteger (), inf .negate ());
668+ Assert .assertEquals (t . getNaN () .toUnscaledBigInteger (), inf .add (BigInteger .ONE ));
597669 }
598670
599671 @ Test
@@ -627,18 +699,19 @@ public void toBigInteger() {
627699
628700 // (4) -inf, +inf, nan
629701 BigInteger inf = BigInteger .TEN .pow (DecimalType .MAX_PRECISION );
630- Assert .assertEquals (DecimalValue . INF .toBigInteger (), inf );
631- Assert .assertEquals (DecimalValue . NEG_INF .toBigInteger (), inf .negate ());
632- Assert .assertEquals (DecimalValue . NAN .toBigInteger (), inf .add (BigInteger .ONE ));
702+ Assert .assertEquals (t . getInf () .toBigInteger (), inf );
703+ Assert .assertEquals (t . getNegInf () .toBigInteger (), inf .negate ());
704+ Assert .assertEquals (t . getNaN () .toBigInteger (), inf .add (BigInteger .ONE ));
633705 }
634706
635707 @ Test
636708 public void toBigDecimal () {
637709 // (1) special values
638- BigDecimal inf = BigDecimal .TEN .pow (DecimalType .MAX_PRECISION );
639- Assert .assertEquals (DecimalValue .INF .toBigDecimal (), inf );
640- Assert .assertEquals (DecimalValue .NEG_INF .toBigDecimal (), inf .negate ());
641- Assert .assertEquals (DecimalValue .NAN .toBigDecimal (), inf .add (BigDecimal .ONE ));
710+ BigDecimal inf = BigDecimal .ONE .scaleByPowerOfTen (DecimalType .MAX_PRECISION );
711+ DecimalType type = DecimalType .getDefault ();
712+ Assert .assertEquals (type .getInf ().toBigDecimal (), inf .setScale (type .getScale ()));
713+ Assert .assertEquals (type .getNegInf ().toBigDecimal (), inf .negate ().setScale (type .getScale ()));
714+ Assert .assertEquals (type .getNaN ().toBigDecimal (), inf .add (BigDecimal .ONE ).setScale (type .getScale ()));
642715
643716 // (2) positive numbers
644717 Assert .assertEquals (newDecimal (1234567890L , 0 ).toBigDecimal (), BigDecimal .valueOf (1234567890L , 0 ));
@@ -682,30 +755,30 @@ public void toUnscaledString() {
682755
683756 // (2) positive numbers: 1, 12, 123, ...
684757 String s = "" ;
685- for (int i = 1 ; i < DecimalType . MAX_PRECISION ; i ++) {
758+ for (int i = 1 ; i < t . getPrecision () ; i ++) {
686759 s += Integer .toString (i % 10 );
687760 Assert .assertEquals (s , t .newValueUnscaled (new BigInteger (s )).toUnscaledString ());
688761 }
689762
690763 // (3) negative numbers: -1, -12, -123, ...
691764 s = "-" ;
692- for (int i = 1 ; i < DecimalType . MAX_PRECISION ; i ++) {
765+ for (int i = 1 ; i < t . getPrecision () ; i ++) {
693766 s += Integer .toString (i % 10 );
694767 Assert .assertEquals (s , t .newValueUnscaled (new BigInteger (s )).toUnscaledString ());
695768 }
696769
697770 // (4) -inf, +inf, nan
698- Assert .assertEquals ("100000000000000000000000000000000000" , DecimalValue . INF .toUnscaledString ()); // 10^35
699- Assert .assertEquals ("-100000000000000000000000000000000000" , DecimalValue . NEG_INF .toUnscaledString ()); // -10^35
700- Assert .assertEquals ("100000000000000000000000000000000001" , DecimalValue . NAN .toUnscaledString ()); // 10^35 + 1
771+ Assert .assertEquals ("100000000000000000000000000000000000" , t . getInf () .toUnscaledString ()); // 10^35
772+ Assert .assertEquals ("-100000000000000000000000000000000000" , t . getNegInf () .toUnscaledString ()); // -10^35
773+ Assert .assertEquals ("100000000000000000000000000000000001" , t . getNaN () .toUnscaledString ()); // 10^35 + 1
701774 }
702775
703776 @ Test
704777 public void toStringTest () {
705778 // (1) special values
706- Assert .assertEquals ("inf" , DecimalValue . INF .toString ());
707- Assert .assertEquals ("-inf" , DecimalValue . NEG_INF .toString ());
708- Assert .assertEquals ("nan" , DecimalValue . NAN .toString ());
779+ Assert .assertEquals ("inf" , DecimalType . getDefault (). getInf () .toString ());
780+ Assert .assertEquals ("-inf" , DecimalType . getDefault (). getNegInf () .toString ());
781+ Assert .assertEquals ("nan" , DecimalType . getDefault (). getNaN () .toString ());
709782
710783 // (2) positive numbers
711784 Assert .assertEquals ("1234567890" , newDecimal (1234567890L , 0 ).toString ());
0 commit comments