Skip to content

Commit 9eea9fa

Browse files
committed
update Java version
1 parent 14b6e4b commit 9eea9fa

File tree

8 files changed

+141
-69
lines changed

8 files changed

+141
-69
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ static CBORObject ParseJSONNumber(
195195
v = -v;
196196
}
197197
if (kind == JSONOptions.ConversionMode.Double) {
198-
return CBORObject.FromObject(EFloat.FromInt64(v).ToDoubleBits());
198+
return CBORObject.FromFloatingPointBits(EFloat.FromInt64(v).ToDoubleBits(), 8);
199199
} else if (kind == JSONOptions.ConversionMode.Decimal128) {
200200
return CBORObject.FromObject(EDecimal.FromInt64(v));
201201
} else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ static CBORObject ParseJSONNumber(
195195
v = -v;
196196
}
197197
if (kind == JSONOptions.ConversionMode.Double) {
198-
return CBORObject.FromObject(EFloat.FromInt64(v).ToDoubleBits());
198+
return CBORObject.FromFloatingPointBits(EFloat.FromInt64(v).ToDoubleBits(), 8);
199199
} else if (kind == JSONOptions.ConversionMode.Decimal128) {
200200
return CBORObject.FromObject(EDecimal.FromInt64(v));
201201
} else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ static CBORObject ParseJSONNumber(
195195
v = -v;
196196
}
197197
if (kind == JSONOptions.ConversionMode.Double) {
198-
return CBORObject.FromObject(EFloat.FromInt64(v).ToDoubleBits());
198+
return CBORObject.FromFloatingPointBits(EFloat.FromInt64(v).ToDoubleBits(), 8);
199199
} else if (kind == JSONOptions.ConversionMode.Decimal128) {
200200
return CBORObject.FromObject(EDecimal.FromInt64(v));
201201
} else {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ private CBORObject NextJSONNegativeNumber(
222222
str = this.sb.toString();
223223
// System.out.println("negb=" + sw.getElapsedMilliseconds() + " ms");
224224
obj = CBORDataUtilities.ParseJSONNumber(str, this.options);
225+
// System.out.println("str=" + str + " obj=" + (obj));
225226
// System.out.println("negc=" + sw.getElapsedMilliseconds() + " ms");
226227
if (obj == null) {
227228
String errstr = (str.length() <= 100) ? str : (str.substring(0,100) + "...");

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,12 @@ private CBORObject NextJSONNonnegativeNumber(int c, int[] nextChar) {
425425
numberStartIndex,
426426
numberEndIndex - numberStartIndex,
427427
this.options);
428-
428+
/*
429+
System.out.println("ParseJSONNumber
430+
{0}->{1}",EDecimal.FromString(this.bytes,
431+
numberStartIndex,
432+
numberEndIndex - numberStartIndex), obj);
433+
*/
429434
if (obj == null) {
430435
String errstr = "";
431436
// errstr = (str.length() <= 100) ? str : (str.substring(0, // 100) + "...");

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,9 @@ public static long IntegerToDoubleBits(int i) {
11421142
if (i < 0) {
11431143
longmant |= ((long)(1L << 63));
11441144
}
1145-
return longmant;
1145+
/*
1146+
System.out.println("" + i + "->" + (longmant==DoubleToInt64Bits(i)));
1147+
*/ return longmant;
11461148
}
11471149

11481150
public static boolean IsBeyondSafeRange(long bits) {

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

Lines changed: 113 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9337,45 +9337,64 @@ public static void AssertJSONDouble(
93379337
String json,
93389338
String numconv,
93399339
double dbl) {
9340-
CBORObject cbor = FromJSON(json, numconv);
9341-
Assert.assertEquals(json + " " + numconv + " " + dbl,CBORType.FloatingPoint,cbor.getType());
9342-
double cbordbl = cbor.AsDoubleValue();
9343-
if (dbl != cbordbl) {
9344-
Assert.fail("dbl = " + dbl + ", cbordbl = " + cbordbl);
9340+
JSONOptions opt=new JSONOptions("numberconversion=" + numconv);
9341+
CBORObject[] cbors = {
9342+
FromJSON(json, numconv),
9343+
CBORDataUtilities.ParseJSONNumber(json, opt)
9344+
};
9345+
for (CBORObject cbor : cbors) {
9346+
if (cbor.getType() != CBORType.FloatingPoint) {
9347+
Assert.assertEquals(json + " " + numconv + " " + dbl,CBORType.FloatingPoint,cbor.getType());
9348+
}
9349+
double cbordbl = cbor.AsDoubleValue();
9350+
if (dbl != cbordbl) {
9351+
Assert.fail("dbl = " + dbl + ", cbordbl = " + cbordbl);
9352+
}
93459353
}
93469354
}
93479355

93489356
public static void AssertJSONInteger(
93499357
String json,
93509358
String numconv,
93519359
long longval) {
9352-
CBORObject cbor = FromJSON(json, numconv);
9353-
if (cbor.getType() != CBORType.Integer) {
9360+
JSONOptions opt=new JSONOptions("numberconversion=" + numconv);
9361+
CBORObject[] cbors = {
9362+
FromJSON(json, numconv),
9363+
CBORDataUtilities.ParseJSONNumber(json, opt)
9364+
};
9365+
for (CBORObject cbor : cbors) {
9366+
if (cbor.getType() != CBORType.Integer) {
93549367
String msg = json + " " + numconv + " " + longval;
93559368
msg = msg.substring(0, Math.min(100, msg.length()));
93569369
if (msg.length() > 100) {
93579370
msg += "...";
93589371
}
93599372
Assert.assertEquals(msg, CBORType.Integer, cbor.getType());
9373+
}
9374+
Assert.assertEquals(longval, cbor.AsInt64Value());
93609375
}
9361-
Assert.assertEquals(longval, cbor.AsInt64Value());
93629376
}
93639377

93649378
public static void AssertJSONInteger(
93659379
String json,
93669380
String numconv,
93679381
int intval) {
9368-
CBORObject cbor = FromJSON(json, numconv);
9369-
if (cbor.getType() != CBORType.Integer) {
9382+
JSONOptions opt=new JSONOptions("numberconversion=" + numconv);
9383+
CBORObject[] cbors = {
9384+
FromJSON(json, numconv),
9385+
CBORDataUtilities.ParseJSONNumber(json, opt)
9386+
};
9387+
for (CBORObject cbor : cbors) {
9388+
if (cbor.getType() != CBORType.Integer) {
93709389
String msg = json + " " + numconv + " " + intval;
93719390
msg = msg.substring(0, Math.min(100, msg.length()));
93729391
if (msg.length() > 100) {
9373-
{ msg += "...";
9374-
}
9392+
msg += "...";
93759393
}
93769394
Assert.assertEquals(msg, CBORType.Integer, cbor.getType());
9395+
}
9396+
Assert.assertEquals(intval, cbor.AsInt32Value());
93779397
}
9378-
Assert.assertEquals(intval, cbor.AsInt32Value());
93799398
}
93809399

93819400
@Test(timeout = 10000)
@@ -9487,38 +9506,107 @@ public void TestFromJsonStringFastCases() {
94879506
0);
94889507
}
94899508

9509+
@Test
9510+
public void TestFromJsonStringFiniteDoubleSpec() {
9511+
RandomGenerator rg = new RandomGenerator();
9512+
for (int i = 0; i < 10000; ++i) {
9513+
double dbl = RandomObjects.RandomFiniteDouble(rg);
9514+
EFloat efd = EFloat.FromDouble(dbl);
9515+
AssertJSONDouble(
9516+
efd.ToShortestString(EContext.Binary64),
9517+
"double",
9518+
dbl);
9519+
AssertJSONDouble(
9520+
efd.toString(),
9521+
"double",
9522+
dbl);
9523+
}
9524+
}
9525+
9526+
@Test
9527+
public void TestFromJsonStringEDecimalSpec() {
9528+
RandomGenerator rg = new RandomGenerator();
9529+
for (int i = 0; i < 1000; ++i) {
9530+
String[] decstring = new String[1];
9531+
EDecimal ed = RandomObjects.RandomEDecimal(rg, decstring);
9532+
if ((decstring[0]) == null) {
9533+
Assert.fail();
9534+
}
9535+
double dbl = ed.ToDouble();
9536+
if (((dbl) == Double.POSITIVE_INFINITY) ||
9537+
((dbl) == Double.NEGATIVE_INFINITY) ||
9538+
Double.isNaN(dbl)) {
9539+
continue;
9540+
}
9541+
AssertJSONDouble(
9542+
decstring[0],
9543+
"double",
9544+
dbl);
9545+
}
9546+
}
9547+
9548+
@Test
9549+
public void TestFromJsonStringSmallDoubleSpec() {
9550+
RandomGenerator rg = new RandomGenerator();
9551+
for (int i = 0; i < 10000; ++i) {
9552+
int rv = rg.GetInt32(Integer.MAX_VALUE) * (rg.GetInt32(2)*2-1);
9553+
String rvstring = TestCommon.IntToString(rv);
9554+
AssertJSONDouble(
9555+
rvstring,
9556+
"double",
9557+
(double)rv);
9558+
AssertJSONInteger(
9559+
rvstring,
9560+
"intorfloat",
9561+
rv);
9562+
}
9563+
AssertJSONDouble("511","double",511);
9564+
AssertJSONDouble("-511","double",-511);
9565+
AssertJSONDouble(
9566+
TestCommon.IntToString(Integer.MAX_VALUE),
9567+
"double",
9568+
(double)Integer.MAX_VALUE);
9569+
AssertJSONDouble(
9570+
TestCommon.IntToString(Integer.MAX_VALUE),
9571+
"double",
9572+
(double)Integer.MAX_VALUE);
9573+
AssertJSONDouble(
9574+
TestCommon.IntToString(Integer.MIN_VALUE),
9575+
"double",
9576+
(double)Integer.MIN_VALUE);
9577+
}
9578+
94909579
@Test(timeout = 10000)
94919580
public void TestFromJsonStringSmallDouble() {
94929581
CBORObject cbor;
9493-
AssertJSONDouble("0", "double", 0.0);
9494-
cbor = FromJSON("[0, 1, 2, 3]", "double");
9582+
AssertJSONDouble("0","double",0.0);
9583+
cbor=FromJSON("[0, 1, 2, 3]", "double");
94959584
Assert.assertEquals(4, cbor.size());
94969585
Assert.assertEquals(0.0, cbor.get(0).AsDouble());
94979586
Assert.assertEquals(1.0, cbor.get(1).AsDouble());
94989587
Assert.assertEquals(2.0, cbor.get(2).AsDouble());
94999588
Assert.assertEquals(3.0, cbor.get(3).AsDouble());
9500-
cbor = FromJSON("[0]", "double");
9589+
cbor=FromJSON("[0]", "double");
95019590
Assert.assertEquals(1, cbor.size());
95029591
Assert.assertEquals(0.0, cbor.get(0).AsDouble());
9503-
cbor = FromJSON("[-0]", "double");
9592+
cbor=FromJSON("[-0]", "double");
95049593
Assert.assertEquals(1, cbor.size());
9505-
cbor = FromJSON("[1]", "double");
9594+
cbor=FromJSON("[1]", "double");
95069595
Assert.assertEquals(1, cbor.size());
95079596
Assert.assertEquals(1.0, cbor.get(0).AsDouble());
9508-
cbor = FromJSON("[-1]", "double");
9597+
cbor=FromJSON("[-1]", "double");
95099598
Assert.assertEquals(1, cbor.size());
95109599
Assert.assertEquals(-1.0, cbor.get(0).AsDouble());
9511-
cbor = FromJSON("[-1022,-1023,-1024,-1025,1022,1023,1024,1025]",
9512-
"double");
9600+
cbor=FromJSON("[-1022,-1023,-1024,-1025,1022,1023,1024,1025]", "double");
95139601
Assert.assertEquals(8, cbor.size());
95149602
Assert.assertEquals(-1022.0, cbor.get(0).AsDouble());
95159603
Assert.assertEquals(-1023.0, cbor.get(1).AsDouble());
95169604
Assert.assertEquals(-1024.0, cbor.get(2).AsDouble());
95179605
Assert.assertEquals(-1025.0, cbor.get(3).AsDouble());
9518-
Assert.assertEquals(1022.0, cbor.get(0).AsDouble());
9519-
Assert.assertEquals(1023.0, cbor.get(1).AsDouble());
9520-
Assert.assertEquals(1024.0, cbor.get(2).AsDouble());
9521-
Assert.assertEquals(1025.0, cbor.get(3).AsDouble());
9606+
Assert.assertEquals(1022.0, cbor.get(4).AsDouble());
9607+
Assert.assertEquals(1023.0, cbor.get(5).AsDouble());
9608+
Assert.assertEquals(1024.0, cbor.get(6).AsDouble());
9609+
Assert.assertEquals(1025.0, cbor.get(7).AsDouble());
95229610
}
95239611

95249612
@Test(timeout = 10000)

src/test/java/com/upokecenter/test/CBORTest.java

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5202,55 +5202,31 @@ public static boolean CheckUtf16(String str) {
52025202
@Test
52035203
public void TestJSONOptions() {
52045204
JSONOptions jsonop1 = new JSONOptions("numberconversion=intorfloat");
5205-
{
5206-
Object objectTemp = jsonop1.toString();
5207-
Object objectTemp2 = new
5208-
JSONOptions(jsonop1.toString()).toString();
5209-
Assert.assertEquals(objectTemp, objectTemp2);
5210-
}
5205+
Assert.assertEquals(jsonop1.toString(), new
5206+
JSONOptions(jsonop1.toString()).toString());
52115207
JSONOptions jsonop2 = new JSONOptions("numberconversion=decimal128");
5212-
{
5213-
Object objectTemp = jsonop2.toString();
5214-
Object objectTemp2 = new
5215-
JSONOptions(jsonop2.toString()).toString();
5216-
Assert.assertEquals(objectTemp, objectTemp2);
5217-
}
5208+
Assert.assertEquals(jsonop2.toString(), new
5209+
JSONOptions(jsonop2.toString()).toString());
52185210
JSONOptions jsonop3 = new JSONOptions("numberconversion=intorfloatfromdouble");
5219-
{
5220-
Object objectTemp = jsonop3.toString();
5221-
Object objectTemp2 = new
5222-
JSONOptions(jsonop3.toString()).toString();
5223-
Assert.assertEquals(objectTemp, objectTemp2);
5224-
}
5211+
Assert.assertEquals(jsonop3.toString(), new
5212+
JSONOptions(jsonop3.toString()).toString());
52255213
JSONOptions jsonop4 = new JSONOptions("numberconversion=double");
5226-
{
5227-
Object objectTemp = jsonop4.toString();
5228-
Object objectTemp2 = new
5229-
JSONOptions(jsonop4.toString()).toString();
5230-
Assert.assertEquals(objectTemp, objectTemp2);
5231-
}
5214+
Assert.assertEquals(jsonop4.toString(), new
5215+
JSONOptions(jsonop4.toString()).toString());
52325216
}
52335217

52345218
@Test
52355219
public void TestPODOptions() {
52365220
PODOptions podop = PODOptions.Default;
5237-
{
5238-
Object objectTemp = podop.toString();
5239-
Object objectTemp2 = new
5240-
PODOptions(podop.toString()).toString();
5241-
Assert.assertEquals(objectTemp, objectTemp2);
5242-
}
5221+
Assert.assertEquals(podop.toString(), new
5222+
PODOptions(podop.toString()).toString());
52435223
}
52445224

52455225
@Test
52465226
public void TestCBOREncodeOptions() {
52475227
CBOREncodeOptions encodeop = CBOREncodeOptions.Default;
5248-
{
5249-
Object objectTemp = encodeop.toString();
5250-
Object objectTemp2 = new
5251-
CBOREncodeOptions(encodeop.toString()).toString();
5252-
Assert.assertEquals(objectTemp, objectTemp2);
5253-
}
5228+
Assert.assertEquals(encodeop.toString(), new
5229+
CBOREncodeOptions(encodeop.toString()).toString());
52545230
}
52555231

52565232
@Test
@@ -5261,9 +5237,9 @@ public void TestRandomJSON() {
52615237
JSONOptions jsonop2 = new JSONOptions("numberconversion=decimal128");
52625238
JSONOptions jsonop3 = new JSONOptions("numberconversion=intorfloatfromdouble");
52635239
JSONOptions jsonop4 = new JSONOptions("numberconversion=double");
5264-
for (int i = 0; i < 1000; ++i) {
5240+
for (int i = 0; i < 200; ++i) {
52655241
byte[] json = jsongen.Generate(rg);
5266-
System.out.println("" + i + " len=" + json.length);
5242+
System.out.println("" + i + " len=" + (json.length));
52675243
JSONOptions currop = null;
52685244
try {
52695245
currop = jsonop1;
@@ -5276,7 +5252,7 @@ public void TestRandomJSON() {
52765252
CBORObject.FromJSONBytes(json, jsonop4);
52775253
} catch (CBORException ex) {
52785254
String msg = ex.getMessage() + "\n" +
5279-
DataUtilities.GetUtf8String(json, true) + "\n" + currop;
5255+
DataUtilities.GetUtf8String(json,true) + "\n" + currop;
52805256
throw new IllegalStateException(msg, ex);
52815257
}
52825258
}

0 commit comments

Comments
 (0)