Skip to content

Commit c2b8ca7

Browse files
committed
Fix ToObject for BigDecimal
1 parent d26af7b commit c2b8ca7

File tree

2 files changed

+68
-16
lines changed

2 files changed

+68
-16
lines changed

src/main/java/com/upokecenter/cbor/PropertyMap.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,9 @@ public static Object TypeToObject(CBORObject objThis, Type t,
617617

618618
if (t.equals(java.math.BigDecimal.class)) {
619619
EDecimal ei = objThis.AsEDecimal();
620+
if (!ei.isFinite()) {
621+
throw new CBORException("Can't convert to BigDecimal");
622+
}
620623
try {
621624
return new BigDecimal(
622625
new BigInteger(ei.getMantissa().ToBytes(false)),

src/test/java/com/upokecenter/test/JavaTests.java

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,17 @@ public void TestAsBigInteger() {
6767
CBORObject numberinfo = numbers.get(i);
6868
String numberString = (String)numberinfo.get("number")
6969
.ToObject(String.class);
70-
CBORObject cbornumber =
71-
ToObjectTest.TestToFromObjectRoundTrip(
70+
CBORObject cbornumber = null;
71+
try {
72+
cbornumber = ToObjectTest.TestToFromObjectRoundTrip(
7273
new BigDecimal(numberString));
74+
} catch(NumberFormatException nfe) {
75+
EDecimal ed=EDecimal.FromString(numberString);
76+
if(ed.isFinite()) {
77+
Assert.fail();
78+
}
79+
continue;
80+
}
7381
if (!numberinfo.get("integer").equals(CBORObject.Null)) {
7482
Assert.assertEquals(
7583
numberinfo.get("integer").ToObject(String.class),
@@ -252,15 +260,58 @@ public void TestAsBigInteger() {
252260
}
253261
}
254262

263+
public static BigDecimal RandomBigDecimal(IRandomGenExtended r) {
264+
return RandomBigDecimal(r, null);
265+
}
266+
267+
public static BigDecimal RandomBigDecimal(IRandomGenExtended r, String[]
268+
decimalString) {
269+
if (r == null) {
270+
throw new NullPointerException("r");
271+
}
272+
if (r.GetInt32(100) < 30) {
273+
String str = RandomObjects.RandomDecimalString(r);
274+
if (str.length() < 500) {
275+
if (decimalString != null) {
276+
decimalString[0] = str;
277+
}
278+
return new BigDecimal(str);
279+
}
280+
}
281+
EInteger emant = RandomObjects.RandomEInteger(r);
282+
int exp = (r.GetInt32(100) < 80) ? (r.GetInt32(50) - 25) :
283+
(r.GetInt32(5000) - 2500);
284+
BigDecimal ed = new BigDecimal(new BigInteger(emant.ToBytes(false)), -exp);
285+
if (decimalString != null) {
286+
decimalString[0] = emant.toString() + "E" + EInteger.FromInt32(-exp).toString();
287+
}
288+
return ed;
289+
}
290+
255291
@Test
256292
public void TestAsBigDecimal() {
293+
BigDecimal bd=new BigDecimal("334.337");
294+
CBORObject cborObject=CBORObject.FromObject(bd);
295+
EDecimal ed=cborObject.ToObject(EDecimal.class);
296+
Assert.assertEquals("334.337",ed.toString());
297+
bd=cborObject.ToObject(BigDecimal.class);
298+
Assert.assertEquals("334.337",bd.toString());
299+
RandomGenerator rg=new RandomGenerator();
300+
for(int i=0;i<500;i++){
301+
bd=RandomBigDecimal(rg,null);
302+
String str=bd.toString();
303+
cborObject=CBORObject.FromObject(bd);
304+
ed=cborObject.ToObject(EDecimal.class);
305+
Assert.assertEquals(str,ed.toString());
306+
bd=cborObject.ToObject(BigDecimal.class);
307+
}
257308
try {
258309
Object objectTemp = CBORTestCommon.DecPosInf;
259310
Object objectTemp2 =
260311
ToObjectTest.TestToFromObjectRoundTrip(Float.POSITIVE_INFINITY)
261312
.ToObject(BigDecimal.class);
262-
Assert.assertEquals(objectTemp, objectTemp2);
263-
} catch (ArithmeticException ex) {
313+
Assert.fail("Should have failed");
314+
} catch (CBORException ex) {
264315
// NOTE: Intentionally empty
265316
} catch (Exception ex) {
266317
Assert.fail(ex.toString());
@@ -271,8 +322,8 @@ public void TestAsBigDecimal() {
271322
Object objectTemp2 =
272323
ToObjectTest.TestToFromObjectRoundTrip(Float.NEGATIVE_INFINITY)
273324
.ToObject(BigDecimal.class);
274-
Assert.assertEquals(objectTemp, objectTemp2);
275-
} catch (ArithmeticException ex) {
325+
Assert.fail("Should have failed");
326+
} catch (CBORException ex) {
276327
// NOTE: Intentionally empty
277328
} catch (Exception ex) {
278329
Assert.fail(ex.toString());
@@ -281,10 +332,8 @@ public void TestAsBigDecimal() {
281332
try {
282333
String stringTemp = ToObjectTest.TestToFromObjectRoundTrip(Float.NaN)
283334
.ToObject(BigDecimal.class).toString();
284-
Assert.assertEquals(
285-
"NaN",
286-
stringTemp);
287-
} catch (ArithmeticException ex) {
335+
Assert.fail("Should have failed");
336+
} catch (CBORException ex) {
288337
// NOTE: Intentionally empty
289338
} catch (Exception ex) {
290339
Assert.fail(ex.toString());
@@ -295,8 +344,8 @@ public void TestAsBigDecimal() {
295344
Object objectTemp2 =
296345
ToObjectTest.TestToFromObjectRoundTrip(Double.POSITIVE_INFINITY)
297346
.ToObject(BigDecimal.class);
298-
Assert.assertEquals(objectTemp, objectTemp2);
299-
} catch (ArithmeticException ex) {
347+
Assert.fail("Should have failed");
348+
} catch (CBORException ex) {
300349
// NOTE: Intentionally empty
301350
} catch (Exception ex) {
302351
Assert.fail(ex.toString());
@@ -307,8 +356,8 @@ public void TestAsBigDecimal() {
307356
Object objectTemp2 =
308357
ToObjectTest.TestToFromObjectRoundTrip(Double.NEGATIVE_INFINITY)
309358
.ToObject(BigDecimal.class);
310-
Assert.assertEquals(objectTemp, objectTemp2);
311-
} catch (ArithmeticException ex) {
359+
Assert.fail("Should have failed");
360+
} catch (CBORException ex) {
312361
// NOTE: Intentionally empty
313362
} catch (Exception ex) {
314363
Assert.fail(ex.toString());
@@ -319,8 +368,8 @@ public void TestAsBigDecimal() {
319368
Object objectTemp2 =
320369
ToObjectTest.TestToFromObjectRoundTrip(Double.NaN)
321370
.ToObject(BigDecimal.class).toString();
322-
Assert.assertEquals(objectTemp, objectTemp2);
323-
} catch (ArithmeticException ex) {
371+
Assert.fail("Should have failed");
372+
} catch (CBORException ex) {
324373
// NOTE: Intentionally empty
325374
} catch (Exception ex) {
326375
Assert.fail(ex.toString());

0 commit comments

Comments
 (0)