1717package org .apache .commons .numbers .core ;
1818
1919import java .math .BigDecimal ;
20+ import java .math .BigInteger ;
2021import java .math .MathContext ;
2122import java .math .RoundingMode ;
2223import java .util .ArrayList ;
2627import java .util .function .Supplier ;
2728import java .util .function .UnaryOperator ;
2829import java .util .stream .IntStream ;
30+ import java .util .stream .LongStream ;
2931import java .util .stream .Stream ;
3032import org .apache .commons .rng .UniformRandomProvider ;
3133import org .apache .commons .rng .simple .RandomSource ;
@@ -131,8 +133,9 @@ void testOfInt(int x) {
131133 * to rounding to 2^53 so we have extra cases for this.
132134 */
133135 @ ParameterizedTest
134- @ ValueSource (longs = {0 , 1 , 42 , 89545664 , 8263492364L , Long .MIN_VALUE ,
135- Long .MAX_VALUE - (1L << 10 ), Long .MAX_VALUE - 42 , Long .MAX_VALUE - 1 , Long .MAX_VALUE })
136+ @ ValueSource (longs = {0 , 1 , 42 , 89545664 , 8263492364L ,
137+ Long .MAX_VALUE - (1L << 10 ), Long .MAX_VALUE - 42 , Long .MAX_VALUE - 1 , Long .MAX_VALUE ,
138+ Long .MIN_VALUE })
136139 void testOfLong (long x ) {
137140 DD dd = DD .of (x );
138141 Assertions .assertEquals (x , dd .hi (), "x hi should be (double) x" );
@@ -142,6 +145,38 @@ void testOfLong(long x) {
142145 Assertions .assertEquals (BigDecimal .valueOf (-x ).subtract (bd (-x )).doubleValue (), dd .lo (), "-x lo should be remaining bits" );
143146 }
144147
148+ /**
149+ * Test conversion of an unsigned {@code long}.
150+ */
151+ @ ParameterizedTest
152+ @ ValueSource (longs = {0 , 1 , 42 , 89545664 , 8263492364L ,
153+ -1 , -42 , -89545664 , -8263492364L ,
154+ Long .MAX_VALUE - (1L << 10 ), Long .MAX_VALUE - 42 , Long .MAX_VALUE - 1 , Long .MAX_VALUE ,
155+ Long .MIN_VALUE + (1L << 10 ), Long .MIN_VALUE + 42 , Long .MIN_VALUE + 1 , Long .MIN_VALUE })
156+ @ MethodSource
157+ void testOfUnsignedLong (long x ) {
158+ final DD dd = DD .ofUnsigned (x );
159+ Assertions .assertTrue (dd .hi () >= 0 , "x hi should be positive" );
160+ // Create the exact unsigned value
161+ final BigInteger xx ;
162+ if (x < 0 ) {
163+ // 63-bits plus 2^63
164+ xx = BigInteger .valueOf (x & Long .MAX_VALUE ).or (BigInteger .ONE .shiftLeft (63 ));
165+ } else {
166+ xx = BigInteger .valueOf (x );
167+ }
168+ final BigDecimal expected = new BigDecimal (xx );
169+ final double hi = expected .doubleValue ();
170+ final double lo = expected .subtract (bd (hi )).doubleValue ();
171+ Assertions .assertEquals (hi , dd .hi (), "x hi" );
172+ Assertions .assertEquals (lo , dd .lo (), "x lo" );
173+ }
174+
175+ static LongStream testOfUnsignedLong () {
176+ // Random
177+ return createRNG ().longs (10 );
178+ }
179+
145180 @ ParameterizedTest
146181 @ MethodSource (value = {"twoSumAddends" })
147182 void testFromBigDecimal (double x , double y ) {
0 commit comments