Skip to content

Commit c8327eb

Browse files
committed
update Java version
1 parent b68bfab commit c8327eb

File tree

5 files changed

+244
-26
lines changed

5 files changed

+244
-26
lines changed

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -517,26 +517,10 @@ public static float Int32BitsToSingle(int bits) {
517517
return Float.intBitsToFloat(bits);
518518
}
519519

520-
/**
521-
* @deprecated
522-
*/
523-
@Deprecated
524-
public static String DoubleToString(double dbl) {
525-
return EFloat.FromDouble(dbl).ToShortestString(EContext.Binary64);
526-
}
527-
528520
public static String DoubleBitsToString(long dblbits) {
529521
return EFloat.FromDoubleBits(dblbits).ToShortestString(EContext.Binary64);
530522
}
531523

532-
/**
533-
* @deprecated
534-
*/
535-
@Deprecated
536-
public static String SingleToString(float sing) {
537-
return EFloat.FromSingle(sing).ToShortestString(EContext.Binary32);
538-
}
539-
540524
public static String LongToString(long longValue) {
541525
if (longValue == Long.MIN_VALUE) {
542526
return "-9223372036854775808";
@@ -1407,14 +1391,6 @@ public static long GetIntegerValue(long bits) {
14071391
return (mant >> shift) * sgn;
14081392
}
14091393

1410-
/**
1411-
* @deprecated
1412-
*/
1413-
@Deprecated
1414-
public static EInteger EIntegerFromDouble(double dbl) {
1415-
return EIntegerFromDoubleBits(Double.doubleToRawLongBits(dbl));
1416-
}
1417-
14181394
public static EInteger EIntegerFromDoubleBits(long lvalue) {
14191395
int value0 = ((int)(lvalue & 0xffffffffL));
14201396
int value1 = ((int)((lvalue >> 32) & 0xffffffffL));

src/test/java/com/upokecenter/test/CBORObjectTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4040,6 +4040,15 @@ public void TestIsInfinity() {
40404040
}).AsNumber().IsInfinity()))Assert.fail();
40414041
}
40424042

4043+
@Test(timeout = 30000)
4044+
public void TestValidJsonOverflowedExponent() {
4045+
CBORObject cbo = CBORObject.FromJSONString("5137.46038043E+8235685134",
4046+
new JSONOptions("numberconversion=intorfloat"));
4047+
if (!(cbo.AsNumber().IsInfinity())) {
4048+
Assert.fail();
4049+
}
4050+
}
4051+
40434052
@Test(timeout = 10001)
40444053
public void TestIsIntegral() {
40454054
CBORObject cbor;
@@ -4868,6 +4877,18 @@ public void TestNonUtf8FromJSONBytes() {
48684877
Assert.assertEquals(CBORObject.FromObject(11), cbor);
48694878
}
48704879

4880+
@Test(timeout = 30000)
4881+
public void TestNonUtf8FromJSONBytes2() {
4882+
byte[] bytes;
4883+
CBORObject cbor;
4884+
bytes = new byte[] { 0x39, 0 };
4885+
cbor = CBORObject.FromJSONBytes(bytes);
4886+
Assert.assertEquals(CBORObject.FromObject(9), cbor);
4887+
bytes = new byte[] { 0, 0x39 };
4888+
cbor = CBORObject.FromJSONBytes(bytes);
4889+
Assert.assertEquals(CBORObject.FromObject(9), cbor);
4890+
}
4891+
48714892
@Test(timeout = 10001)
48724893
public void TestReadJSON() {
48734894
try {

src/test/java/com/upokecenter/test/JSONGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ private static void GenerateJsonString(
274274
int len = ra.GetInt32(1000) * ra.GetInt32(1000);
275275
len /= 1000;
276276
if (ra.GetInt32(50) == 0 && depth < 2) {
277-
// Exponential curve that strongly favors small numbers
277+
// Curve that strongly favors small numbers
278278
long v = (long)ra.GetInt32(1000000) * ra.GetInt32(1000000);
279279
len = (int)(v / 1000000);
280280
}

src/test/java/com/upokecenter/test/JavaSpecificTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ public static BigDecimal RandomBigDecimal(IRandomGenExtended r, String[]
322322
return new BigDecimal(str);
323323
}
324324
}
325-
EInteger emant = RandomObjects.RandomEInteger(r);
325+
EInteger emant = RandomNumerics.RandomEInteger(r);
326326
int exp = (r.GetInt32(100) < 80) ? (r.GetInt32(50) - 25) :
327327
(r.GetInt32(5000) - 2500);
328328
BigDecimal ed = new BigDecimal(new BigInteger(emant.ToBytes(false)), -exp);
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
package com.upokecenter.test;
2+
/*
3+
Written by Peter O.
4+
Any copyright to this work is released to the Public Domain.
5+
In case this is not possible, this work is also
6+
licensed under the Unlicense: https://unlicense.org/
7+
8+
*/
9+
10+
import com.upokecenter.util.*;
11+
import com.upokecenter.numbers.*;
12+
13+
/**
14+
* Generates random objects of various kinds for purposes of testing code that
15+
* uses them. The methods will not necessarily sample uniformly from all
16+
* objects of a particular kind.
17+
*/
18+
public final class RandomNumerics {
19+
private RandomNumerics() {
20+
}
21+
private static final int MaxExclusiveStringLength = 0x2000;
22+
private static final int MaxExclusiveShortStringLength = 50;
23+
private static final int MaxNumberLength = 50000;
24+
private static final int MaxShortNumberLength = 40;
25+
26+
public static ERational RandomERational(IRandomGenExtended rand) {
27+
EInteger bigintA = RandomEInteger(rand);
28+
EInteger bigintB = RandomEInteger(rand);
29+
if (bigintB.isZero()) {
30+
bigintB = EInteger.FromInt32(1);
31+
}
32+
return ERational.Create(bigintA, bigintB);
33+
}
34+
35+
public static EDecimal GenerateEDecimalSmall(IRandomGenExtended wrapper) {
36+
if (wrapper == null) {
37+
throw new NullPointerException("wrapper");
38+
}
39+
if (wrapper.GetInt32(2) == 0) {
40+
EInteger eix = EInteger.FromBytes(
41+
RandomObjects.RandomByteString(wrapper, 1 + wrapper.GetInt32(36)),
42+
true);
43+
int exp = wrapper.GetInt32(25) - 12;
44+
return EDecimal.Create(eix, exp);
45+
}
46+
return
47+
EDecimal.FromString(RandomObjects.RandomDecimalStringShort(wrapper, false));
48+
}
49+
50+
public static EDecimal RandomEDecimal(IRandomGenExtended r) {
51+
return RandomEDecimal(r, null);
52+
}
53+
54+
public static EDecimal RandomEDecimal(IRandomGenExtended r, String[]
55+
decimalString) {
56+
if (r == null) {
57+
throw new NullPointerException("r");
58+
}
59+
if (r.GetInt32(100) == 0) {
60+
int x = r.GetInt32(3);
61+
if (x == 0) {
62+
if (decimalString != null) {
63+
decimalString[0] = "Infinity";
64+
}
65+
return EDecimal.PositiveInfinity;
66+
}
67+
if (x == 1) {
68+
if (decimalString != null) {
69+
decimalString[0] = "-Infinity";
70+
}
71+
return EDecimal.NegativeInfinity;
72+
}
73+
if (x == 2) {
74+
if (decimalString != null) {
75+
decimalString[0] = "NaN";
76+
}
77+
return EDecimal.NaN;
78+
}
79+
// Signaling NaN currently not generated because
80+
// it doesn't round-trip as well
81+
}
82+
if (r.GetInt32(100) < 30) {
83+
String str = RandomObjects.RandomDecimalString(r);
84+
if (str.length() < 500) {
85+
if (decimalString != null) {
86+
decimalString[0] = str;
87+
}
88+
return EDecimal.FromString(str);
89+
}
90+
}
91+
EInteger emant = RandomEInteger(r);
92+
EInteger eexp;
93+
if (r.GetInt32(100) < 95) {
94+
int exp = (r.GetInt32(100) < 80) ? (r.GetInt32(50) - 25) :
95+
(r.GetInt32(5000) - 2500);
96+
eexp = EInteger.FromInt32(exp);
97+
} else {
98+
eexp = RandomEInteger(r);
99+
}
100+
EDecimal ed = EDecimal.Create(emant, eexp);
101+
if (decimalString != null) {
102+
decimalString[0] = emant.toString() + "E" + eexp.toString();
103+
}
104+
return ed;
105+
}
106+
107+
private static EInteger BitHeavyEInteger(IRandomGenExtended rg, int count) {
108+
StringBuilder sb = new StringBuilder();
109+
int[] oneChances = {
110+
999, 1, 980, 20, 750, 250, 980,
111+
20, 980, 20, 980, 20, 750, 250,
112+
};
113+
int oneChance = oneChances[rg.GetInt32(oneChances.length)];
114+
for (int i = 0; i < count; ++i) {
115+
sb.append((rg.GetInt32(1000) >= oneChance) ? '0' : '1');
116+
}
117+
return EInteger.FromRadixString(sb.toString(), 2);
118+
}
119+
120+
private static EInteger DigitHeavyEInteger(IRandomGenExtended rg, int
121+
count) {
122+
StringBuilder sb = new StringBuilder();
123+
int[] oneChances = {
124+
999, 1, 980, 20, 750, 250, 980,
125+
20, 980, 20, 980, 20, 750, 250,
126+
};
127+
int oneChance = oneChances[rg.GetInt32(oneChances.length)];
128+
for (int i = 0; i < count; ++i) {
129+
sb.append((rg.GetInt32(1000) >= oneChance) ? '0' : '9');
130+
}
131+
return EInteger.FromRadixString(sb.toString(), 10);
132+
}
133+
134+
public static EInteger RandomEInteger(IRandomGenExtended r) {
135+
if (r == null) {
136+
throw new NullPointerException("r");
137+
}
138+
int selection = r.GetInt32(100);
139+
if (selection < 10) {
140+
int count = r.GetInt32(MaxNumberLength);
141+
count = (int)((long)count * r.GetInt32(MaxNumberLength) /
142+
MaxNumberLength);
143+
count = (int)((long)count * r.GetInt32(MaxNumberLength) /
144+
MaxNumberLength);
145+
count = Math.max(count, 1);
146+
if (selection == 0 || selection == 1) {
147+
return BitHeavyEInteger(r, count);
148+
} else if ((selection == 2 || selection == 3) && count < 500) {
149+
return DigitHeavyEInteger(r, count);
150+
}
151+
byte[] bytes = RandomObjects.RandomByteString(r, count);
152+
return EInteger.FromBytes(bytes, true);
153+
} else {
154+
byte[] bytes = RandomObjects.RandomByteString(
155+
r,
156+
r.GetInt32(MaxShortNumberLength) + 1);
157+
return EInteger.FromBytes(bytes, true);
158+
}
159+
}
160+
161+
public static EInteger RandomEIntegerSmall(IRandomGenExtended r) {
162+
if (r == null) {
163+
throw new NullPointerException("r");
164+
}
165+
byte[] bytes = RandomObjects.RandomByteString(
166+
r,
167+
r.GetInt32(MaxShortNumberLength) + 1);
168+
return EInteger.FromBytes(bytes, true);
169+
}
170+
171+
private static int IntInRange(IRandomGenExtended rg, int minInc, int
172+
maxExc) {
173+
return minInc + rg.GetInt32(maxExc - minInc);
174+
}
175+
176+
public static EFloat CloseToPowerOfTwo(IRandomGenExtended rg) {
177+
if (rg == null) {
178+
throw new NullPointerException("rg");
179+
}
180+
int pwr = (rg.GetInt32(100) < 80) ? IntInRange(rg, -20, 20) :
181+
IntInRange(rg, -300, 300);
182+
int pwr2 = pwr - (rg.GetInt32(100) < 80 ? IntInRange(rg, 51, 61) :
183+
IntInRange(rg, 2, 300));
184+
EFloat ef = rg.GetInt32(2) == 0 ? EFloat.Create(1,
185+
pwr).Add(EFloat.Create(1, pwr2)) : EFloat.Create(1,
186+
pwr).Subtract(EFloat.Create(1, pwr2));
187+
if (rg.GetInt32(10) == 0) {
188+
pwr2 = pwr - (rg.GetInt32(100) < 80 ? IntInRange(rg, 51, 61) :
189+
IntInRange(rg, 2, 300));
190+
ef = (rg.GetInt32(2) == 0) ? ef.Add(EFloat.Create(1, pwr2)) :
191+
ef.Subtract(EFloat.Create(1, pwr2));
192+
}
193+
return ef;
194+
}
195+
196+
public static EFloat RandomEFloat(IRandomGenExtended r) {
197+
if (r == null) {
198+
throw new NullPointerException("r");
199+
}
200+
if (r.GetInt32(100) == 0) {
201+
int x = r.GetInt32(3);
202+
if (x == 0) {
203+
return EFloat.PositiveInfinity;
204+
}
205+
if (x == 1) {
206+
return EFloat.NegativeInfinity;
207+
}
208+
if (x == 2) {
209+
return EFloat.NaN;
210+
}
211+
}
212+
return r.GetInt32(100) == 3 ?
213+
CloseToPowerOfTwo(r) : EFloat.Create(
214+
RandomEInteger(r),
215+
EInteger.FromInt64(r.GetInt32(400) - 200));
216+
}
217+
218+
public static EInteger RandomSmallIntegral(IRandomGenExtended r) {
219+
return EInteger.FromString(RandomObjects.RandomSmallIntegralString(r));
220+
}
221+
}

0 commit comments

Comments
 (0)