@@ -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 )
0 commit comments